Skip to content

The agent that grows with you

fix(plugins): a constructor registry naming a global as a key is not an alias

Teknium317f201ea872
diff --git a/hermes_cli/plugin_validate_desktop.py b/hermes_cli/plugin_validate_desktop.py
index dba85b9bed8..789e526e970 100644
--- a/hermes_cli/plugin_validate_desktop.py
+++ b/hermes_cli/plugin_validate_desktop.py
@@ -98,14 +98,19 @@ _RENDERER_GLOBAL = re.compile(rf"^(?:{_RENDERER_GLOBAL_NAMES})$")
# class named ``S`` declared in some other scope of the file would otherwise excuse ``S.prototype``.
_GLOBAL_ALIAS = re.compile(
rf"(?<![\w$.])([A-Za-z_$][\w$]*)\s*=\s*(?:(?:window|globalThis|self)\s*\.\s*)?(?:{_RENDERER_GLOBAL_NAMES})"
- rf"(?![\w$(])|\b(?:{_RENDERER_GLOBAL_NAMES})\s*:\s*([A-Za-z_$][\w$]*)")
+ r"(?![\w$(])")
+# Destructuring counts only when the source is the global object itself: an ordinary constructor
+# registry (``{ Array: Track }``) names a global as a KEY and must not turn ``Track`` into an alias.
+_GLOBAL_DESTRUCTURE = re.compile(r"\{([^{}]*)\}\s*=\s*(?:window|globalThis|self)(?![\w$.])")
+_DESTRUCTURED_GLOBAL = re.compile(rf"\b(?:{_RENDERER_GLOBAL_NAMES})\s*:\s*([A-Za-z_$][\w$]*)")
def _foreign_prototype_writes(source: str) -> list[int]:
"""Offsets of ``X.prototype.y =`` where ``X`` is a renderer global, an alias of one, or not
declared in the file."""
declared = {name for names in _DECLARED_NAME.findall(source) for name in names if name}
- aliases = {name for names in _GLOBAL_ALIAS.findall(source) for name in names if name}
+ aliases = set(_GLOBAL_ALIAS.findall(source)) | {
+ name for body in _GLOBAL_DESTRUCTURE.findall(source) for name in _DESTRUCTURED_GLOBAL.findall(body)}
return [m.start() for m in _PROTOTYPE_WRITE.finditer(source)
if m.group(1) not in declared or m.group(1) in aliases or _RENDERER_GLOBAL.match(m.group(1))]
diff --git a/tests/hermes_cli/test_plugin_validate.py b/tests/hermes_cli/test_plugin_validate.py
index cd7ba3ba86a..24dff0302e1 100644
--- a/tests/hermes_cli/test_plugin_validate.py
+++ b/tests/hermes_cli/test_plugin_validate.py
@@ -349,7 +349,8 @@ class TestDesktopSurface:
_Vector2 { constructor() { _Vector2.prototype.isVector2 = true } }``, ``function Track(){}``)
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 a
- global whose name some unrelated scope also declares as a class."""
+ global (assigned or destructured from ``window``) whose name some unrelated scope also
+ declares as a class. A constructor registry naming a global as a key is not an alias."""
d = self._desktop_plugin(tmp_path, (
"var Vector2 = class _Vector2 { constructor() { _Vector2.prototype.isVector2 = true } }\n"
"function Track() {}\nTrack.prototype.ValueTypeName = 'vector'\n"
@@ -357,12 +358,15 @@ class TestDesktopSurface:
"class Storage {}\nStorage.prototype.setItem = noop\n"
"HTMLElement.prototype.click = spy\n"
"function unused() { class H {} }\nconst H = window.Storage\nH.prototype.setItem = noop\n"
+ "function K() {}\nK.prototype.ValueTypeName = 'v'\nconst types = { Array: K }\n"
+ "function unused2() { class W {} }\nconst { Storage: W } = window\nW.prototype.setItem = noop\n"
))
assert desktop_surface_hits(d) == [
"prototype patching (desktop/plugin.js:5)",
"prototype patching (desktop/plugin.js:7)",
"prototype patching (desktop/plugin.js:8)",
"prototype patching (desktop/plugin.js:11)",
+ "prototype patching (desktop/plugin.js:17)",
]
def test_node_sidecar_and_test_mjs_outside_desktop_are_not_the_surface(self, tmp_path):