Skip to content

The agent that grows with you

fix(plugins): an alias of a renderer global cannot borrow an unrelated class name

Teknium3586e58d62d5
diff --git a/hermes_cli/plugin_validate_desktop.py b/hermes_cli/plugin_validate_desktop.py
index ee217f198b3..dba85b9bed8 100644
--- a/hermes_cli/plugin_validate_desktop.py
+++ b/hermes_cli/plugin_validate_desktop.py
@@ -86,20 +86,28 @@ _PROTOTYPE_WRITE = re.compile(r"\b([A-Za-z_$][\w$]*)\.prototype\.[\w$]+\s*=[^=]"
_DECLARED_NAME = re.compile(
r"(?<![\w$.])([A-Za-z_$][\w$]*)\s*=\s*(?:class|function)\b(?:\s+([A-Za-z_$][\w$]*))?"
r"|\b(?:class|function)\s+([A-Za-z_$][\w$]*)")
-# A regex cannot see scope, so a same-named local declaration elsewhere in the file must not excuse
-# a write to a renderer global: these receivers always fail, declared or not.
-_RENDERER_GLOBAL = re.compile(
- r"^(?:Object|Function|Array|String|Number|Boolean|Symbol|Promise|RegExp|Date|Error|JSON|Map|Set"
+_RENDERER_GLOBAL_NAMES = (
+ r"Object|Function|Array|String|Number|Boolean|Symbol|Promise|RegExp|Date|Error|JSON|Map|Set"
r"|WeakMap|WeakSet|Proxy|Reflect|Storage|Window|Document|Node|EventTarget|Event|Location|History"
r"|Navigator|WebSocket|XMLHttpRequest|Request|Response|Headers|URL|Blob|File|FileReader"
- r"|MutationObserver|Worker|BroadcastChannel|MessagePort|IDB\w*|\w*Element|\w*Event|CSS\w*)$")
+ r"|MutationObserver|Worker|BroadcastChannel|MessagePort|IDB\w*|\w*Element|\w*Event|CSS\w*")
+# A regex cannot see scope, so a same-named local declaration elsewhere in the file must not excuse
+# a write to a renderer global: these receivers always fail, declared or not.
+_RENDERER_GLOBAL = re.compile(rf"^(?:{_RENDERER_GLOBAL_NAMES})$")
+# ...and neither may an alias of one (``const S = window.Storage``, ``{ Storage: S } = window``): a
+# 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$]*)")
def _foreign_prototype_writes(source: str) -> list[int]:
- """Offsets of ``X.prototype.y =`` where ``X`` is a renderer global or not declared in the file."""
+ """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}
return [m.start() for m in _PROTOTYPE_WRITE.finditer(source)
- if m.group(1) not in declared or _RENDERER_GLOBAL.match(m.group(1))]
+ if m.group(1) not in declared or m.group(1) in aliases or _RENDERER_GLOBAL.match(m.group(1))]
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 44e2be0a230..cd7ba3ba86a 100644
--- a/tests/hermes_cli/test_plugin_validate.py
+++ b/tests/hermes_cli/test_plugin_validate.py
@@ -348,18 +348,21 @@ class TestDesktopSurface:
"""A bundled library setting fields on classes it declares (three.js: ``var Vector2 = class
_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."""
+ 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."""
d = self._desktop_plugin(tmp_path, (
"var Vector2 = class _Vector2 { constructor() { _Vector2.prototype.isVector2 = true } }\n"
"function Track() {}\nTrack.prototype.ValueTypeName = 'vector'\n"
"const S = window.Storage\nS.prototype.setItem = noop\n"
"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"
))
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)",
]
def test_node_sidecar_and_test_mjs_outside_desktop_are_not_the_surface(self, tmp_path):