Skip to content

JS: Recognize replaceAll with a string pattern as an XSS sanitizer - #23194

Open
mohitmishra786 wants to merge 2 commits into
github:mainfrom
mohitmishra786:23179-xss-replaceall-string
Open

mohitmishra786 wants to merge 2 commits into
github:mainfrom
mohitmishra786:23179-xss-replaceall-string

Conversation

@mohitmishra786

@mohitmishra786 mohitmishra786 commented Nov 21, 2026 •

Copy link
Copy Markdown

Fixes #23179.

MetacharEscapeSanitizer only recognized replacements whose pattern is a regular expression, so an escape chain such as s.replaceAll("&", "&amp;").replaceAll("<", "&lt;") was not treated as a sanitizer, while the same chain written with replace(/</g, "&lt;") or replaceAll(/</g, "&lt;") was. A replaceAll call with a string pattern replaces every occurrence, so it escapes the input the same way.

The sanitizer now also accepts a replaceAll call whose pattern is a string, when every known value of that string is <, ' or ". replace("<", "&lt;"), which only replaces the first occurrence, is still reported. Since the change is in the shared XSS library, it applies to js/xss, js/reflected-xss, js/stored-xss and the other queries that use Xss::Shared::Sanitizer.

The new test replaceAll.js covers the string form, a constant holding "<", quote escaping, and five cases that must still be reported: non-global replace("<", ...), replaceAll("&", ...) alone, a multi-character pattern, a conditional pattern that may be "x", and replace with 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. The CWE-079, CWE-116 and CWE-918 query tests pass with CodeQL CLI 2.27.5.

@mohitmishra786
mohitmishra786 requested a review from a team as a code owner November 21, 2026 15:59
Copilot AI adaptive review requested due to automatic review settings November 21, 2026 15:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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(["<", "'", "\""])

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, replaceAll(flag ? "<" : "x", "&lt;") 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(["<", "'", "\""])

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed, replace(flag ? /x/g : "<", "&lt;") was not reported. a803069 limits the string-pattern case to replaceAll, and the test now covers this call.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JS: HTML escaping with replaceAll("<", "&lt;") is not recognized as a sanitizer by js/xss and js/reflected-xss

2 participants