Repository navigation
JS: Recognize replaceAll with a string pattern as an XSS sanitizer - #23194
mohitmishra786 wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Critical sanitizer false negatives remain because mixed string/regexp inputs and alternative non-metacharacter patterns can incorrectly suppress real XSS alerts.
2 open issues
What changed in this PR
This PR addresses #23179 by extending the shared JavaScript XSS sanitizer model to recognize string-pattern HTML escaping with replaceAll, reducing false positives across the XSS queries.
Changes:
- Adds recognition of
<,', and"string patterns through local data flow. - Adds reflected-XSS regression cases and updates generated expectations.
- Documents the analysis improvement in a change note.
| File | Description |
|---|---|
javascript/ql/test/query-tests/Security/CWE-079/ReflectedXss/replaceAll.js |
Tests escaping chains, constant patterns, quotes, and unsafe replacements. |
javascript/ql/test/query-tests/Security/CWE-079/ReflectedXss/ReflectedXssWithCustomSanitizer.expected |
Records the three expected alerts for the custom-sanitizer query. |
javascript/ql/test/query-tests/Security/CWE-079/ReflectedXss/ReflectedXss.expected |
Records expected alerts and flow paths for the new cases. |
javascript/ql/lib/semmle/javascript/security/dataflow/Xss.qll |
Extends metacharacter sanitizer recognition to string patterns. |
javascript/ql/lib/change-notes/2026-11-21-xss-replaceall-string-sanitizer.md |
Announces the shared XSS analysis change. |
Review effort: Max (adaptive)
| RegExp::isWildcardLike(this.getRegExp().getRoot()) | ||
| or | ||
| // or it's a `replaceAll` call whose string pattern is a meta-character. | ||
| this.getArgument(0).mayHaveStringValue(["<", "'", "\""]) |
There was a problem hiding this comment.
Right, replaceAll(flag ? "<" : "x", "<") was treated as a sanitizer. Fixed in a803069: every known string value of the pattern must now be <, ' or ". Added this case to replaceAll.js; it is reported again.
| RegExp::isWildcardLike(this.getRegExp().getRoot()) | ||
| or | ||
| // or it's a `replaceAll` call whose string pattern is a meta-character. | ||
| this.getArgument(0).mayHaveStringValue(["<", "'", "\""]) |
There was a problem hiding this comment.
Confirmed, replace(flag ? /x/g : "<", "<") was not reported. a803069 limits the string-pattern case to replaceAll, and the test now covers this call.
…s as XSS sanitizers

Fixes #23179.
MetacharEscapeSanitizeronly recognized replacements whose pattern is a regular expression, so an escape chain such ass.replaceAll("&", "&").replaceAll("<", "<")was not treated as a sanitizer, while the same chain written withreplace(/</g, "<")orreplaceAll(/</g, "<")was. AreplaceAllcall with a string pattern replaces every occurrence, so it escapes the input the same way.The sanitizer now also accepts a
replaceAllcall whose pattern is a string, when every known value of that string is<,'or".replace("<", "<"), which only replaces the first occurrence, is still reported. Since the change is in the shared XSS library, it applies tojs/xss,js/reflected-xss,js/stored-xssand the other queries that useXss::Shared::Sanitizer.The new test
replaceAll.jscovers the string form, a constant holding"<", quote escaping, and five cases that must still be reported: non-globalreplace("<", ...),replaceAll("&", ...)alone, a multi-character pattern, a conditional pattern that may be"x", andreplacewith a pattern that is either a non-matching regular expression or a string. Without the change, the three string-pattern cases (the escape chain, the constant and the quote escaping) are reported. TheCWE-079,CWE-116andCWE-918query tests pass with CodeQL CLI 2.27.5.