The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(plugins): passing a class around or naming it in a string is not a rebinding
Teknium88e17eecf773
diff --git a/hermes_cli/plugin_validate_desktop.py b/hermes_cli/plugin_validate_desktop.pyindex 8fe4a15586e..335f412e22c 100644--- a/hermes_cli/plugin_validate_desktop.py+++ b/hermes_cli/plugin_validate_desktop.py@@ -98,16 +98,30 @@ _RENDERER_GLOBAL = re.compile(rf"^(?:{_RENDERER_GLOBAL_NAMES})$")_OWN_DECLARATION = r"(?:\bclass\s+{0}\b[^{{]*|\bfunction\s*\*?\s*{0}\s*\([^)]*\)\s*)\{{"# ``var X = class {...}`` / ``X = function () {...}``: what bundlers emit; binds ``X`` in its block._ASSIGNED_DECLARATION = (r"(?<![\w$.]){0}\s*=\s*(?:class\b[^{{]*|function\s*\*?\s*[\w$]*\s*\([^)]*\)\s*)\{{")-_REBINDING = r"(?<![\w$.]){0}\s*=(?![=>])|(?:\b(?:let|const|var|class|function)\s+|[(,{{]\s*(?:\.\.\.)?){0}\s*[,)=}}:]"+# What can make ``X`` name something else: ``X = …``, ``let/const/var/class/function X``, a+# destructuring declaration naming ``X``, or a parameter called ``X`` (function, method, arrow,+# catch). A reference (``register(X)``, ``[X, Y]``) is not a binding. Scanned with strings and regex+# literals masked, so ``"X = vector"`` in a label is not one either.+_REBINDING = (+ r"(?<![\w$.]){0}\s*=(?![=>])"+ r"|\b(?:let|const|var|class|function\s*\*?)\s+{0}(?![\w$])"+ r"|\b(?:let|const|var)\s*[{{\[][^;=]*?(?<![\w$.]){0}(?![\w$])[^;=]*?[}}\]]\s*="+ 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*=>"+ r"|\bcatch\s*\(\s*{0}\s*\)")_STRING_OR_REGEX = re.compile(_JS_STRING + r"|`(?:[^`\\]|\\.)*`|" + _REGEX_LITERAL.pattern)-def _brace_pairs(source: str) -> dict[int, int]:- """``{`` offset -> matching ``}`` offset, ignoring braces inside strings and regex literals."""- masked = _STRING_OR_REGEX.sub(lambda m: " " * len(m.group(0)), source)+def _mask_literals(source: str) -> str:+ return _STRING_OR_REGEX.sub(lambda m: " " * len(m.group(0)), source)+++def _brace_pairs(code: str) -> dict[int, int]:+ """``{`` offset -> matching ``}`` offset in literal-masked *code*."""pairs: dict[int, int] = {}stack: list[int] = []- for i, ch in enumerate(masked):+ for i, ch in enumerate(code):if ch == "{":stack.append(i)elif ch == "}" and stack:@@ -149,10 +163,11 @@ def _own_class_write(source: str, name: str, at: int, pairs: dict[int, int]) ->def _foreign_prototype_writes(source: str) -> list[int]:"""Offsets of ``X.prototype.y =`` that are not provably on ``X``'s own class (see above)."""- writes = [(m.start(), m.group(1)) for m in _PROTOTYPE_WRITE.finditer(source)]- pairs = _brace_pairs(source) if writes else {}+ code = _mask_literals(source) # offsets unchanged; literal text never reads as code+ writes = [(m.start(), m.group(1)) for m in _PROTOTYPE_WRITE.finditer(code)]+ pairs = _brace_pairs(code) if writes else {}return [at for at, name in writes- if _RENDERER_GLOBAL.match(name) or not _own_class_write(source, name, at, pairs)]+ if _RENDERER_GLOBAL.match(name) or not _own_class_write(code, name, at, pairs)]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.pyindex 1fcf0049033..6b1484c7e1e 100644--- a/tests/hermes_cli/test_plugin_validate.py+++ b/tests/hermes_cli/test_plugin_validate.py@@ -350,8 +350,9 @@ class TestDesktopSurface:is not prototype patching. A write to a renderer global or an undeclared receiver still is,including a global the file shadows with a same-named class elsewhere, and an alias of aglobal (any spelling: assigned, bracketed, destructured) whose name some unrelated scope- also declares as a class, including a minifier-reused one-letter name. A constructor- registry naming a global as a key is not an alias."""+ also declares as a class, including a minifier-reused one-letter name, and a parameter+ shadowing the class. Passing the class around (``register(Q)``, ``{ Array: Q }``) or text+ in a string literal 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"@@ -363,6 +364,8 @@ class TestDesktopSurface:"function unused2() { class W {} }\nconst { Storage: W } = window\nW.prototype.setItem = noop\n""function unused3() { class B {} }\nconst B = window['Storage']\nB.prototype.setItem = noop\n""var Mv=class s{constructor(){s.prototype.isMv=!0}};var s=window.Storage;s.prototype.setItem=noop\n"+ "function Q() {}\nregister(Q); const label = \"Q = vector\"\nQ.prototype.ValueTypeName = 'v'\n"+ "function R() {}\nfunction patch(R) { R.prototype.setItem = noop }\n"))assert desktop_surface_hits(d) == ["prototype patching (desktop/plugin.js:5)",@@ -372,6 +375,7 @@ class TestDesktopSurface:"prototype patching (desktop/plugin.js:17)","prototype patching (desktop/plugin.js:20)","prototype patching (desktop/plugin.js:21)",+ "prototype patching (desktop/plugin.js:26)",]def test_node_sidecar_and_test_mjs_outside_desktop_are_not_the_surface(self, tmp_path):