Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Resolve the effective form action once and stop nested budget replenish (SKY-12875) (#7989)
Aaron Perez12572d0c47ba
diff --git a/skyvern/webeye/scraper/domUtils.js b/skyvern/webeye/scraper/domUtils.jsindex dbd8448b1..3e4378e4c 100644--- a/skyvern/webeye/scraper/domUtils.js+++ b/skyvern/webeye/scraper/domUtils.js@@ -1941,6 +1941,9 @@ function enrichValidationState(attrs, element, elementTagNameLower) {// single-element parses and tree-from-element never enable it.var __captureDestinationFacts = false;var __destinationFactBudget = 0;+// Whether a build already owns the capture flag and the budget. Guards against a nested,+// page-triggered buildTreeFromBody replenishing an active build's bound; see that function.+var __destinationBuildActive = false;// Serialized overhead of one attached fact ({"kind":...,"url":null,"method":"get"} plus its// element-id key). Charging only url.length let 15,000 url-less facts through for 615,000 bytes:@@ -2060,14 +2063,12 @@ function buildDestinationFacts(element, tagNameLower) {// <base>, which real Chromium contradicted across origins.const isBlankUrl = (value) =>value.replace(/^[\u0000-\u0020]+|[\u0000-\u0020]+$/g, "") === "";- const rawAction = form.getAttribute("action");- // Per spec a missing or empty action attribute submits to the document's own URL.- let url;- if (rawAction === null || isBlankUrl(rawAction)) {- url = documentUrl();- } else {- url = resolve(rawAction);- }+ // The EFFECTIVE action is settled BEFORE anything is resolved, so only the destination the+ // browser will actually use costs a resolution. Resolving the owner's action and then+ // discarding it for a submitter's override charged the work budget twice for every submitter:+ // a near-cap shared action plus many short overrides exhausted the budget in ~124 controls+ // and dropped later facts whose effective URLs were small.+ let rawTarget = form.getAttribute("action");let method = normalizeFormMethod(form.getAttribute("method"));// Enumerated-attribute normalization: an INVALID button type defaults to "submit" (only// "button" and "reset" opt out, and both returned opaque above), while an invalid input type@@ -2084,13 +2085,18 @@ function buildDestinationFacts(element, tagNameLower) {// invalid-button-type fail-open's sibling.const formAction = element.getAttribute("formaction");if (formAction !== null) {- url = isBlankUrl(formAction) ? documentUrl() : resolve(formAction);+ rawTarget = formAction;}const formMethod = element.getAttribute("formmethod");if (formMethod !== null) {method = normalizeFormMethod(formMethod);}}+ // Per spec a missing or blank action submits to the document's own URL.+ const url =+ rawTarget === null || isBlankUrl(rawTarget)+ ? documentUrl()+ : resolve(rawTarget);return { kind: "form", url: url, method: method };} catch (e) {return null;@@ -2300,8 +2306,20 @@ async function buildTreeFromBody(// redefining the global between injection and the call (the export itself is no longer// hijackable via a setter), so the per-build budget is the load-bearing bound rather than the// flag: it caps total serialized fact bytes AND stops resolution once exhausted.- __captureDestinationFacts = captureDestinationFacts === true;- __destinationFactBudget = 524288;+ //+ // Only the OUTERMOST build may arm that bound. This function is exported onto globalThis and+ // its reset runs synchronously, before the first await, so a page that calls it from inside a+ // trusted resolution — by replacing window.URL, say — could re-arm the budget mid-build and+ // resolve without limit. Probed: with the budget drained, the synchronous prefix of a nested+ // call made a 524288 spend succeed again. A nested call still builds its tree; it just cannot+ // replenish. The ownership flag is only ever read from this closure: the wrapper exports a+ // value COPY of it, so writing globalThis.__destinationBuildActive does not reach this binding.+ const ownsDestinationBudget = !__destinationBuildActive;+ if (ownsDestinationBudget) {+ __destinationBuildActive = true;+ __captureDestinationFacts = captureDestinationFacts === true;+ __destinationFactBudget = 524288;+ }try {const maxElementNumber = 15000;const elementsAndResultArray = await buildElementTree(@@ -2315,7 +2333,10 @@ async function buildTreeFromBody(DomUtils.elementListCache = elementsAndResultArray[0];return elementsAndResultArray;} finally {- __captureDestinationFacts = false;+ if (ownsDestinationBudget) {+ __captureDestinationFacts = false;+ __destinationBuildActive = false;+ }}}diff --git a/tests/unit/test_domutils_destination_facts.js b/tests/unit/test_domutils_destination_facts.jsindex 92f89b5f4..3abf04395 100644--- a/tests/unit/test_domutils_destination_facts.js+++ b/tests/unit/test_domutils_destination_facts.js@@ -646,6 +646,91 @@ check(},);+check("a nested build cannot replenish an active build's budget", () => {+ // buildTreeFromBody is exported onto globalThis and arms the budget in its SYNCHRONOUS prefix,+ // before its first await. A page that calls it from inside a trusted resolution (by replacing+ // window.URL, say) could therefore re-arm the bound mid-build. Probed in real Chromium: with+ // the budget drained from inside an active build, a nested call made a 524288 spend succeed+ // again. Only the outermost build may arm; a nested one still builds, it just cannot replenish.+ const harness = new Function(+ `let __captureDestinationFacts = false;+ let __destinationFactBudget = 0;+ let __destinationBuildActive = false;+ const window = {};+ const document = { documentElement: {} };+ const DomUtils = {};+ async function buildElementTree() { return [[], []]; }+ async ${extract("buildTreeFromBody")}+ return {+ start: (f) => buildTreeFromBody(f, undefined, [], true).catch(() => {}),+ budget: () => __destinationFactBudget,+ drain: () => { __destinationFactBudget = 0; },+ capturing: () => __captureDestinationFacts,+ };`,+ )();+ harness.start("outer");+ assert.strictEqual(+ harness.budget(),+ 524288,+ "the outermost build must arm the budget",+ );+ assert.strictEqual(harness.capturing(), true);+ harness.drain(); // the outer build spends its whole budget on hostile resolutions+ harness.start("evil"); // page-triggered re-entry, still inside the outer build+ assert.strictEqual(+ harness.budget(),+ 0,+ "a nested build replenished the active build's budget",+ );+});++check("a submitter override costs ONE resolution, not two", () => {+ // The owner's action was resolved and then discarded whenever a formaction override won, so+ // every submitter charged the work budget twice -- directly counter to the bound this suite+ // exists to defend. A near-cap shared action plus many short overrides exhausted the budget in+ // ~124 controls while every effective destination was small.+ let constructions = 0;+ class CountingURL extends URL {+ constructor(...args) {+ super(...args);+ constructions++;+ }+ }+ const START = 100000;+ const harness = new Function(+ "URL",+ `let __destinationFactBudget = ${START};+ ${extract("normalizeFormMethod")}+ ${extract("spendDestinationBudget")}+ ${extract("buildDestinationFacts")}+ return { build: buildDestinationFacts, remaining: () => __destinationFactBudget };`,+ )(CountingURL);+ const facts = harness.build(+ stubElement({+ tag: "input",+ attrs: { type: "submit", formaction: "/override" },+ form: stubForm({ action: "/a-much-longer-owner-action", method: "post" }),+ }),+ "input",+ );+ assert.deepStrictEqual(facts, {+ kind: "form",+ url: "https://site.example/override",+ method: "post",+ });+ assert.strictEqual(+ constructions,+ 1,+ `resolved ${constructions} times, want 1`,+ );+ // Exact spend pins the charge too: only "/override" (9) against the base (25) may be billed.+ assert.strictEqual(+ harness.remaining(),+ START - ("/override".length + DOC.baseURI.length),+ "the discarded owner action was still charged",+ );+});+check("a hostile cost cannot INFLATE the budget", () => {// _wrap_js_in_isolated_scope exports every top-level declaration onto globalThis, so a page can// call this while a build is awaiting. Probed in real Chromium: spend(-Infinity) returned true,