Skip to content

The agent that grows with you

fix(plugins): computed keys are not patterns; a regex may follow an if condition

Teknium0b07d25774ae
diff --git a/hermes_cli/plugin_validate_desktop.py b/hermes_cli/plugin_validate_desktop.py
index f3375aff760..4f39e86782a 100644
--- a/hermes_cli/plugin_validate_desktop.py
+++ b/hermes_cli/plugin_validate_desktop.py
@@ -106,7 +106,10 @@ _ASSIGNED_DECLARATION = (r"(?<![\w$.]){0}\s*=\s*(?:class\b[^{{]*|function\s*\*?\
_REBINDING = (
r"(?<![\w$.]){0}\s*(?:&&|\|\||\?\?|\*\*|<<|>>>?|[-+*/%&|^])?=(?![=>])"
r"|\b(?:let|const|var|class|function\s*\*?)\s+{0}(?![\w$])"
- r"|(?<![\w$.]){0}(?![\w$])[^;=()]*?[}}\]][\s}}\]]*(?:=(?![=>])|\b(?:of|in)\b)"
+ # a PATTERN opens where an expression cannot (not ``obj[`` / ``f()[``), names ``X`` itself (not
+ # ``X.name``), and is assigned or iterated
+ r"|(?:^|(?<=[;(,{{}}=\n])|\b(?:let|const|var))\s*[\[{{][^;=()]*?(?<![\w$.]){0}(?![\w$.])[^;=()]*?[}}\]][\s}}\]]*"
+ r"(?:=(?![=>])|\b(?:of|in)\b)"
r"|(?:\bfunction\s*\*?\s*[\w$]*|(?<![\w$.])(?!(?:if|for|while|switch|with|return|typeof|await)\b)[\w$]+)"
r"\s*\([^()]*?(?<![\w$.]){0}(?![\w$])[^()]*\)\s*\{{"
r"|\(([^()]*?(?<![\w$.]){0}(?![\w$])[^()]*)\)\s*=>|(?<![\w$.]){0}\s*=>"
@@ -117,12 +120,27 @@ _REBINDING = (
_REGEX_AFTER_WORD = re.compile(r"(?:^|[^\w$.])(?:return|typeof|case|do|else|in|of|void|delete|throw|new|yield|await)\s*$")
+def _after_control_condition(source: str, close: int) -> bool:
+ """The ``)`` at *close* ends an ``if``/``while``/``for``/``with`` condition (a statement may follow)."""
+ depth = 0
+ for k in range(close, -1, -1):
+ if source[k] == ")":
+ depth += 1
+ elif source[k] == "(":
+ depth -= 1
+ if depth == 0:
+ return bool(re.search(r"(?:^|[^\w$.])(?:if|while|for|with)\s*$", source[max(0, k - 12):k]))
+ return False
+
+
def _regex_may_start(source: str, at: int) -> bool:
j = at - 1
while j >= 0 and source[j] in " \t\r\n":
j -= 1
if j < 0:
return True
+ if source[j] == ")":
+ return _after_control_condition(source, j)
if source[j] in "(,=:[!&|?{};+-*%<>~^":
return not source.startswith("/", at + 1) and not (source[j] in "+-" and j > 0 and source[j - 1] == source[j])
return bool(_REGEX_AFTER_WORD.search(source, max(0, j - 12), j + 1))
@@ -150,7 +168,8 @@ def _code_view(source: str) -> str:
"""*source* with the text of quoted strings, template literals and regex literals blanked, same
length. Only text that can never run is blanked: a template's ``${…}`` stays code, and a ``/``
starts a regex only where an expression cannot end before it (after an operator, ``(``, ``,``,
- a keyword such as ``return``); after a name, number, ``)`` or ``]`` it is division."""
+ a keyword such as ``return``, or the ``)`` of an ``if``/``while``/``for`` condition); after a
+ name, number, other ``)`` or ``]`` it is division."""
out = list(source)
def blank(i: int) -> None:
diff --git a/tests/hermes_cli/test_plugin_validate.py b/tests/hermes_cli/test_plugin_validate.py
index 0d7ebd21bce..840af4afe9a 100644
--- a/tests/hermes_cli/test_plugin_validate.py
+++ b/tests/hermes_cli/test_plugin_validate.py
@@ -352,8 +352,9 @@ class TestDesktopSurface:
global (any spelling: assigned, bracketed, destructured) whose name some unrelated scope
also declares as a class, including a minifier-reused one-letter name, and a parameter
shadowing the class, a destructuring assignment, and writes hidden in a template ``${}`` or
- between divisions. Passing the class around (``register(Q)``, ``{ Array: Q }``) or text in
- a string, template or regex literal is not a rebinding."""
+ between divisions. Passing the class around (``register(Q)``, ``{ Array: Q }``,
+ ``registry[F.name] = F``) or text in a string, template or regex literal (including a regex
+ statement after ``if (…)``) is not a rebinding."""
d = self._desktop_plugin(tmp_path, (
"var Vector2 = class _Vector2 { constructor() { _Vector2.prototype.isVector2 = true } }\n"
"function Track() {}\nTrack.prototype.ValueTypeName = 'vector'\n"
@@ -371,6 +372,8 @@ class TestDesktopSurface:
"const label = `${Storage.prototype.setItem = spy}`\n"
"const n = 1 / (Storage.prototype.setItem = spy, 2) / 3\n"
"function E() {}\nconst re = /E = x/, t = `E = ${1}`; E.prototype.isE = 1\n"
+ "function F() {}\nregistry[F.name] = F; if (ready) /F = x/.test(label); F.prototype.isF = 1\n"
+ "function G() {}\nlet {a: G} = window; G.prototype.setItem = spy\n"
))
assert desktop_surface_hits(d) == [
"prototype patching (desktop/plugin.js:5)",
@@ -384,6 +387,7 @@ class TestDesktopSurface:
"prototype patching (desktop/plugin.js:28)",
"prototype patching (desktop/plugin.js:29)",
"prototype patching (desktop/plugin.js:30)",
+ "prototype patching (desktop/plugin.js:36)",
]
def test_node_sidecar_and_test_mjs_outside_desktop_are_not_the_surface(self, tmp_path):