Skip to content

Automate browser based workflows with AI

SKY-10691: harden removeBoundingBoxes against Prototype.js-style remove() polyfill (#6609)

LawyZhengba276b03c03d
diff --git a/skyvern/webeye/scraper/domUtils.js b/skyvern/webeye/scraper/domUtils.js
index dc6543846..fa7b127e7 100644
--- a/skyvern/webeye/scraper/domUtils.js
+++ b/skyvern/webeye/scraper/domUtils.js
@@ -2383,8 +2383,11 @@ function addHintMarkersToPage(hintMarkers) {
function removeBoundingBoxes() {
var hintMarkerContainer = document.querySelector("#boundingBoxContainer");
- if (hintMarkerContainer) {
- hintMarkerContainer.remove();
+ // Avoid Element.prototype.remove(): pages that polyfill it Prototype.js-style
+ // expect a static `remove(element)` signature and crash when called as
+ // `el.remove()` with no arg. removeChild is DOM 1 and never polyfilled.
+ if (hintMarkerContainer && hintMarkerContainer.parentNode) {
+ hintMarkerContainer.parentNode.removeChild(hintMarkerContainer);
}
}
diff --git a/tests/unit/test_remove_bounding_boxes_polyfill.js b/tests/unit/test_remove_bounding_boxes_polyfill.js
new file mode 100644
index 000000000..09bb37721
--- /dev/null
+++ b/tests/unit/test_remove_bounding_boxes_polyfill.js
@@ -0,0 +1,128 @@
+/**
+ * Behavioral test: removeBoundingBoxes() must survive a Prototype.js-style
+ * monkey-patch of Element.prototype.remove.
+ *
+ * Some pages ship a `remove(element)` polyfill (Prototype.js shape) that
+ * ignores `this` and reads arguments[0]. Calling it as `el.remove()` with no
+ * arg then throws `Cannot read properties of undefined (reading 'parentNode'
+ * / 'removeChild')`. removeBoundingBoxes() must use parentNode.removeChild
+ * directly so it never invokes the polyfill.
+ *
+ * Exit 0 = pass, exit 1 = failures on stderr.
+ */
+
+const fs = require("fs");
+const path = require("path");
+
+const src = fs.readFileSync(
+ path.join(__dirname, "../../skyvern/webeye/scraper/domUtils.js"),
+ "utf8",
+);
+const fnStart = src.indexOf("function removeBoundingBoxes() {");
+if (fnStart === -1) throw new Error("removeBoundingBoxes not found");
+const bodyStart = src.indexOf("{", fnStart);
+let depth = 0,
+ fnEnd = -1;
+for (let i = bodyStart; i < src.length; i++) {
+ if (src[i] === "{") depth++;
+ else if (src[i] === "}") {
+ depth--;
+ if (depth === 0) {
+ fnEnd = i + 1;
+ break;
+ }
+ }
+}
+const fnSource = src.substring(fnStart, fnEnd);
+
+function makeDocumentWithContainer({ attachToParent = true } = {}) {
+ const container = {
+ id: "boundingBoxContainer",
+ _parent: null,
+ // Prototype.js-style polyfill: takes an `element` argument and ignores
+ // `this`. Calling `container.remove()` invokes this with no arg, so
+ // `element` is undefined and `element.parentNode` throws TypeError.
+ remove: function (element) {
+ element.parentNode.removeChild(element);
+ },
+ };
+ const parent = {
+ _children: [],
+ removeChild(child) {
+ const idx = this._children.indexOf(child);
+ if (idx >= 0) {
+ this._children.splice(idx, 1);
+ child._parent = null;
+ return child;
+ }
+ throw new Error("not a child");
+ },
+ };
+ if (attachToParent) {
+ parent._children.push(container);
+ container._parent = parent;
+ }
+ Object.defineProperty(container, "parentNode", {
+ get() {
+ return this._parent;
+ },
+ });
+ const document = {
+ _container: container,
+ querySelector(sel) {
+ return sel === "#boundingBoxContainer" ? container : null;
+ },
+ };
+ return { document, parent, container };
+}
+
+function runRemoveBoundingBoxes(document) {
+ const fn = new Function(
+ "document",
+ `${fnSource}\nreturn removeBoundingBoxes();`,
+ );
+ fn(document);
+}
+
+let passed = 0,
+ failed = 0;
+function test(name, fn) {
+ try {
+ fn();
+ passed++;
+ process.stdout.write(` PASS: ${name}\n`);
+ } catch (e) {
+ failed++;
+ process.stderr.write(` FAIL: ${name}\n ${e.message}\n`);
+ }
+}
+function assert(c, m) {
+ if (!c) throw new Error(m);
+}
+
+test("Prototype.js-style monkey-patched remove() does not break removeBoundingBoxes", () => {
+ const { document, parent, container } = makeDocumentWithContainer();
+ runRemoveBoundingBoxes(document);
+ assert(
+ parent._children.indexOf(container) === -1,
+ "container should be detached from its parent",
+ );
+});
+
+test("orphan #boundingBoxContainer (no parent) is a no-op, not a throw", () => {
+ const { document } = makeDocumentWithContainer({ attachToParent: false });
+ // Should not throw even when parentNode is null.
+ runRemoveBoundingBoxes(document);
+});
+
+test("no #boundingBoxContainer in document is a no-op", () => {
+ const document = {
+ querySelector() {
+ return null;
+ },
+ };
+ runRemoveBoundingBoxes(document);
+});
+
+console.log(`\n${passed + failed} tests: ${passed} passed, ${failed} failed`);
+process.exit(failed > 0 ? 1 : 0);
diff --git a/tests/unit/test_remove_bounding_boxes_polyfill.py b/tests/unit/test_remove_bounding_boxes_polyfill.py
new file mode 100644
index 000000000..0db1082ad
--- /dev/null
+++ b/tests/unit/test_remove_bounding_boxes_polyfill.py
@@ -0,0 +1,36 @@
+"""
+removeBoundingBoxes() must survive a Prototype.js-style monkey-patch of
+Element.prototype.remove.
+"""
+
+import shutil
+import subprocess
+from pathlib import Path
+
+import pytest
+
+_REPO_ROOT = Path(__file__).parent.parent.parent
+_DOMUTILS = _REPO_ROOT / "skyvern" / "webeye" / "scraper" / "domUtils.js"
+_NODE = shutil.which("node")
+
+
[email protected](_NODE is None, reason="node not on PATH")
+class TestRemoveBoundingBoxesPolyfill:
+ def test_js_syntax(self):
+ result = subprocess.run(
+ [_NODE, "--check", str(_DOMUTILS)],
+ capture_output=True,
+ text=True,
+ )
+ assert result.returncode == 0, f"JS syntax error: {result.stderr}"
+
+ def test_behavioral(self):
+ script = Path(__file__).parent / "test_remove_bounding_boxes_polyfill.js"
+ assert script.exists(), f"Missing {script}"
+ result = subprocess.run(
+ [_NODE, str(script)],
+ capture_output=True,
+ text=True,
+ timeout=30,
+ )
+ assert result.returncode == 0, f"Failed:\n{result.stdout}\n{result.stderr}"