security: use JSON.stringify for selector escaping in devtools panel (#93)

The inspectElement function previously used manual replace() chains to
escape backslashes and single quotes in CSS selectors before passing
them to chrome.devtools.inspectedWindow.eval(). This escaping was
incomplete: selectors containing crafted sequences of special characters
(backticks, newlines, Unicode escapes) could break out of the string
literal and inject arbitrary JS into the inspected page context.

JSON.stringify produces a properly escaped JS string literal that handles
all special characters, eliminating the injection surface entirely.
This commit is contained in:
xiaolai
2026-04-28 18:09:54 -07:00
committed by GitHub
parent efedf2d3d3
commit 04709eadf0
+2 -2
View File
@@ -501,10 +501,10 @@ function renderFindings(findings) {
}
function inspectElement(selector) {
const escaped = selector.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
const json = JSON.stringify(selector);
chrome.devtools.inspectedWindow.eval(
`(function() {
var el = document.querySelector('${escaped}');
var el = document.querySelector(${json});
if (el) { el.scrollIntoView({ behavior: 'smooth', block: 'center' }); inspect(el); }
})()`
);