Repository navigation
Description
Description of the false positive
Escaping HTML with a chain of String.prototype.replaceAll calls that take string patterns, such as s.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">"), is not recognized as a sanitizer, so js/reflected-xss and js/xss report the escaped value. Unlike replace with a string pattern, replaceAll replaces every occurrence, so this escapes the input exactly like the same chain written with global regular expressions (replace(/</g, "<")), which is not reported. replaceAll(/</g, "<") is not reported either. The string form is a natural way to write this since replaceAll became available (ES2021, Node.js 15).
As far as I can tell, this is because Shared::MetacharEscapeSanitizer only inspects this.getRegExp().getRoot(), which has no result when the first argument is a string. StringReplaceCall already handles this case: it includes replaceAll, treats it as global in isGlobal() and maybeGlobal(), and getAReplacedString() returns a string first argument. The sanitizer is shared by the DOM-based, reflected and stored XSS queries (DomBasedXssCustomizations.qll#L322-L328).
Code samples or links to source code
server.js:
const express = require("express");
const app = express();
function escapeHtml(s) {
return s.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">")
.replaceAll('"', """).replaceAll("'", "'");
}
function escapeHtmlRegExp(s) {
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")
.replace(/"/g, """).replace(/'/g, "'");
}
app.get("/a", (req, res) => {
res.send("<p>" + escapeHtml(String(req.query.q)) + "</p>"); // line 15: reported
});
app.get("/b", (req, res) => {
res.send("<p>" + escapeHtmlRegExp(String(req.query.q)) + "</p>"); // line 19: not reported
});
app.get("/c", (req, res) => {
const q = String(req.query.q);
res.send("<p>" + q.replaceAll(/</g, "<") + "</p>"); // line 24: not reported
});
app.get("/d", (req, res) => {
res.send("<p>" + String(req.query.q) + "</p>"); // line 28: reported (control)
});
app.listen(process.env.PORT || 3000);page.js:
function show() {
const v = decodeURIComponent(location.hash.slice(1));
document.getElementById("a").innerHTML =
v.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">"); // line 4: reported
document.getElementById("b").innerHTML =
v.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); // line 6: not reported
}With CodeQL 2.27.5, js/reflected-xss reports server.js lines 15 and 28, and js/xss reports page.js line 4. Running the server with Node.js v24.18.0 and Express 4.22.3, /a and /b return byte-identical responses when q is <script>alert(1)</script>"'& (sent URL-encoded):
GET /a -> <p><script>alert(1)</script>"'&</p>
GET /b -> <p><script>alert(1)</script>"'&</p>
GET /d -> <p><script>alert(1)</script>"'&</p>
The pattern occurs in real code, for example (in each repository, scanned as a whole with the default javascript-code-scanning.qls suite on 2.27.5, this is the only XSS alert):
- voicetreelab/voicetree:
esc()escapes&,<,>,"and'withreplaceAll. Therunvalue fromlocation.hash(L17-L20) is rendered as<h2>${esc(ts)}</h2>(L80), andjs/xssreports theinnerHTMLassignment at L170. - lbwnb666-ai/DeepSeekHarnessRemoteGateway:
escapeHtml()escapes&,<,>and"withreplaceAll. Thenextquery parameter (L201) is rendered as<input type="hidden" name="next" value="${escapeHtml(next)}">(L133), andjs/reflected-xssreports the response at L18.
Would it be reasonable for MetacharEscapeSanitizer to also accept such replaceAll calls?
Possible change
One possible change is a third case in Shared::MetacharEscapeSanitizer for replaceAll calls whose first argument is the constant string <, ' or " (against current main):
--- a/javascript/ql/lib/semmle/javascript/security/dataflow/Xss.qll
+++ b/javascript/ql/lib/semmle/javascript/security/dataflow/Xss.qll
@@ -42,6 +42,10 @@ module Shared {
or
// or it's like a wild-card.
RegExp::isWildcardLike(this.getRegExp().getRoot())
+ or
+ // `replaceAll` with a string pattern replaces every occurrence of that string
+ this.getMethodName() = "replaceAll" and
+ this.getArgument(0).getStringValue() = ["<", "'", "\""]
)
}
}replace with a string pattern is left out on purpose, since it only replaces the first occurrence. With this change, server.js line 15 and page.js line 4 are no longer reported, line 28 still is, and the tests in javascript/ql/test/query-tests/Security/CWE-079/ still pass with unchanged .expected files.
This does not add a new kind of false negative. The string form is then treated exactly like the regexp form, so every call it starts accepting has a regexp equivalent (replace(/</g, …), replaceAll(/'/g, …)) that is already accepted today. What it stops reporting falls under the existing limitations of this sanitizer: it does not consider where the escaped value ends up (for example a javascript: URL in an href attribute, or an unquoted inline script), and a call that only escapes quotes counts as a sanitizer even in element content, as replace(/'/g, "''") already does. In a sample of 278 real files that call replaceAll("<", …) and use request data, the change removed 21 alerts and added none. In 19 of them the escaped value ends up in element content or a double-quoted attribute; the other 2 also have a path into an href attribute or a string in an inline script, where HTML escaping is not enough.
Environment: CodeQL CLI 2.27.5 (codeql/javascript-queries 2.5.1); links point to CodeQL main f7a381fd5a8eee235dda7f83dd1f81e0a109e3f0.
Activity
jketema commentedon Nov 20, 2026
Hi @wildoranges,
Thank you for this false positive report. Resolving this issue is not a current product priority, but we acknowledge the report and will track it internally for future consideration, or if we observe repeated instances of the same problem.