Skip to content

feat(java): detect Matcher.replaceAll/appendReplacement with tainted replacement strings (CWE-74) - #111

Open
ai-anant wants to merge 1 commit into
CodeVigilant:mainfrom
ai-anant:rule/matcher-replaceall-tainted-replacement
Open

feat(java): detect Matcher.replaceAll/appendReplacement with tainted replacement strings (CWE-74)#111
ai-anant wants to merge 1 commit into
CodeVigilant:mainfrom
ai-anant:rule/matcher-replaceall-tainted-replacement

Conversation

@ai-anant

Copy link
Copy Markdown

New taint rule: an attacker-influenced value (map lookup, environment/system property, or request parameter) flowing into the replacement argument of Matcher.replaceAll() or Matcher.appendReplacement().

Java replacement strings interpret '$' and '' as metacharacters: a '$' followed by a digit or '{' is resolved as a back-reference to a capturing group, so a tainted replacement (e.g. a build parameter or environment value such as '$1') throws an unchecked exception ('No group 1') or silently substitutes the wrong text, enabling denial of service or string-manipulation defects. Reviewers should wrap replacements with Matcher.quoteReplacement().

Positive and negative testcases included under testcases/java/. Validated with semgrep 1.172 (pos fires, neg clean).

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.

1 participant