Skip to content

fix: escape regex metacharacters in environment variable keys#1656

Open
Fikri-20 wants to merge 1 commit into
foss42:mainfrom
Fikri-20:fix/envvar-regex-escape
Open

fix: escape regex metacharacters in environment variable keys#1656
Fikri-20 wants to merge 1 commit into
foss42:mainfrom
Fikri-20:fix/envvar-regex-escape

Conversation

@Fikri-20
Copy link
Copy Markdown
Contributor

@Fikri-20 Fikri-20 commented Apr 13, 2026

Summary

Fixes #1645

  • Environment variable keys are now escaped with RegExp.escape() before being joined into the substitution regex
  • Prevents FormatException when any key contains unclosed metacharacters like ( or [
  • Also fixes silent wrong-substitution for keys with ., +, * (previously matched incorrectly due to unescaped regex semantics)

Root Cause

substituteVariables() built a single regex from all active env var keys without escaping:

// Before — crashes if any key has ( or [
final regex = RegExp("{{(${envVarMap.keys.join('|')})}}");

One malformed key corrupts the regex for every request in that environment, silently freezing the Send button with no error shown.

Fix

// After — keys are safely escaped
final escapedKeys = envVarMap.keys.map((key) => RegExp.escape(key)).join('|');
final regex = RegExp("{{($escapedKeys)}}");

Video for the app after resolve the bug

sol.mp4

now the current, and subsequent requests is sent appropriately!

Test plan

… substitution

Environment variable keys containing regex metacharacters (., *, +, ?, [], etc.)
caused FormatException crashes when used in request fields. This fix properly
escapes all special characters using RegExp.escape() before building the regex pattern.

- Fixed substituteVariables function in lib/utils/envvar_utils.dart
- Added comprehensive test cases for special characters, regex metacharacters,
  and mixed character scenarios
- Resolves crash when using environment variables like 'api.key', 'user+id', etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Environment variable keys with special characters crash app with FormatException

1 participant