Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
fix(browser): report SVG xlink:href links in dom_urls (#1602)
Nithilan Kumaran0f605bca095b
diff --git a/src/browser/CdpCaptureDocuments.baseUrl.test.ts b/src/browser/CdpCaptureDocuments.baseUrl.test.tsindex 2bd43fc08..e5ff1fd59 100644--- a/src/browser/CdpCaptureDocuments.baseUrl.test.ts+++ b/src/browser/CdpCaptureDocuments.baseUrl.test.ts@@ -277,3 +277,332 @@ it("preserves case-sensitive SVG attributes and uses the actual href", () => {`${origin}/actual`,);});++interface SvgSnapshotNode {+ readonly name: string;+ readonly parent: number;+ readonly attributes?: readonly (readonly [string, string])[];+}++const svgDocument = (+ elements: readonly SvgSnapshotNode[],+ baseUrl = `${origin}/screens/assets/`,+) => {+ const strings = [documentUrl, baseUrl];+ const intern = (value: string): number => {+ const existing = strings.indexOf(value);+ if (existing >= 0) return existing;+ strings.push(value);+ return strings.length - 1;+ };+ const nodeType = [9];+ const nodeName = [intern("#document")];+ const nodeValue = [intern("")];+ const parentIndex = [-1];+ const attributes: number[][] = [[]];+ for (const element of elements) {+ nodeType.push(1);+ nodeName.push(intern(element.name));+ nodeValue.push(intern(""));+ parentIndex.push(element.parent);+ const encoded: number[] = [];+ for (const [name, value] of element.attributes ?? [])+ encoded.push(intern(name), intern(value));+ attributes.push(encoded);+ }+ return {+ strings,+ documents: [+ {+ documentURL: 0,+ baseURL: 1,+ nodes: { nodeType, nodeName, nodeValue, parentIndex, attributes },+ },+ ],+ };+};++const captureSvg = (value: ReturnType<typeof svgDocument>) =>+ captureDom(value, new Set([origin]), input);++const urlFor = (+ result: ReturnType<typeof captureSvg>,+ nodeIndex: number,+): (typeof result.urls)[number] | undefined =>+ result.urls.find((url) => url.node_index === nodeIndex);++describe("SVG xlink:href destinations", () => {+ it("reports an SVG anchor that only has xlink:href, keeping the first value", () => {+ const result = captureSvg(+ svgDocument([+ { name: "svg", parent: 0 },+ { name: "g", parent: 1 },+ {+ name: "a",+ parent: 2,+ attributes: [+ ["xlink:href", "/xl-s2h"],+ ["xlink:href", "/later"],+ ],+ },+ ]),+ );+ expect(result.nodes[3]?.attribute_names).toEqual(["xlink:href"]);+ expect(result.urls).toEqual([+ {+ node_index: 3,+ attribute: "href",+ url: `${origin}/xl-s2h`,+ destination_scope: "approved",+ },+ ]);+ });++ it.each([+ ["xlink:href", "href"],+ ["href", "xlink:href"],+ ] as const)(+ "uses href when an SVG anchor also has xlink:href (%s then %s)",+ (first, second) => {+ const valueFor = (name: string): string =>+ name === "href" ? "/href-wins" : "/xlink-loses";+ const result = captureSvg(+ svgDocument([+ { name: "svg", parent: 0 },+ {+ name: "a",+ parent: 1,+ attributes: [+ [first, valueFor(first)],+ [second, valueFor(second)],+ ],+ },+ ]),+ );+ expect(result.urls).toEqual([+ {+ node_index: 2,+ attribute: "href",+ url: `${origin}/href-wins`,+ destination_scope: "approved",+ },+ ]);+ },+ );++ it("does not report xlink:href on an HTML anchor or a MathML element", () => {+ const result = captureSvg(+ svgDocument([+ {+ name: "A",+ parent: 0,+ attributes: [["xlink:href", "/xl-html"]],+ },+ { name: "math", parent: 0 },+ {+ name: "mrow",+ parent: 2,+ attributes: [["xlink:href", "/xl-math"]],+ },+ { name: "svg", parent: 0 },+ {+ name: "a",+ parent: 4,+ attributes: [["xlink:href", "/xl-s2h"]],+ },+ ]),+ );+ expect(result.nodes[1]?.attribute_names).toEqual(["xlink:href"]);+ expect(result.nodes[3]?.attribute_names).toEqual(["xlink:href"]);+ expect(urlFor(result, 1)).toBeUndefined();+ expect(urlFor(result, 3)).toBeUndefined();+ expect(urlFor(result, 5)?.url).toBe(`${origin}/xl-s2h`);+ });++ it("classifies an SVG xlink:href outside the allowed origins as outside_policy", () => {+ const result = captureSvg(+ svgDocument([+ { name: "svg", parent: 0 },+ {+ name: "a",+ parent: 1,+ attributes: [["xlink:href", "https://cdn.example.test/out"]],+ },+ ]),+ );+ expect(result.urls).toEqual([+ {+ node_index: 2,+ attribute: "href",+ url: null,+ destination_scope: "outside_policy",+ },+ ]);+ });++ it("resolves SVG image and use xlink:href against the document base", () => {+ const result = captureSvg(+ svgDocument([+ { name: "svg", parent: 0 },+ { name: "image", parent: 1, attributes: [["xlink:href", "guide"]] },+ { name: "use", parent: 1, attributes: [["xlink:href", "icon.svg"]] },+ ]),+ );+ expect(result.urls).toEqual([+ {+ node_index: 2,+ attribute: "href",+ url: `${origin}/screens/assets/guide`,+ destination_scope: "approved",+ },+ {+ node_index: 3,+ attribute: "href",+ url: `${origin}/screens/assets/icon.svg`,+ destination_scope: "approved",+ },+ ]);+ });+});++describe("SVG xlink:href context", () => {+ it("keeps forward ancestor context local to each document", () => {+ const svg = svgDocument([+ { name: "a", parent: 2, attributes: [["xlink:href", "/svg-link"]] },+ { name: "g", parent: 3 },+ { name: "svg", parent: 0 },+ ]);+ const html = svgDocument([+ { name: "A", parent: 2, attributes: [["xlink:href", "/html-link"]] },+ { name: "DIV", parent: 3 },+ { name: "BODY", parent: 0 },+ ]);+ const offset = svg.strings.length;+ const result = captureSvg({+ strings: [...svg.strings, ...html.strings],+ documents: [+ ...svg.documents,+ ...html.documents.map((document) => ({+ ...document,+ documentURL: document.documentURL + offset,+ baseURL: document.baseURL + offset,+ nodes: {+ ...document.nodes,+ nodeName: document.nodes.nodeName.map((index) => index + offset),+ nodeValue: document.nodes.nodeValue.map((index) => index + offset),+ attributes: document.nodes.attributes.map((attributes) =>+ attributes.map((index) => index + offset),+ ),+ },+ })),+ ],+ });+ expect(result.urls).toEqual([+ {+ node_index: 1,+ attribute: "href",+ url: `${origin}/svg-link`,+ destination_scope: "approved",+ },+ ]);+ });++ it("stops SVG context at foreignObject and resumes it for a nested svg", () => {+ const result = captureSvg(+ svgDocument([+ { name: "svg", parent: 0 },+ { name: "foreignObject", parent: 1 },+ {+ name: "A",+ parent: 2,+ attributes: [["xlink:href", "/xl-html-fo"]],+ },+ { name: "svg", parent: 2 },+ {+ name: "a",+ parent: 4,+ attributes: [["xlink:href", "/xl-nested"]],+ },+ ]),+ );+ expect(urlFor(result, 3)).toBeUndefined();+ expect(urlFor(result, 5)).toMatchObject({+ attribute: "href",+ url: `${origin}/xl-nested`,+ destination_scope: "approved",+ });+ });++ it("keeps an empty href ahead of xlink:href", () => {+ const baseUrl = `${origin}/screens/assets/`;+ const result = captureSvg(+ svgDocument(+ [+ { name: "svg", parent: 0 },+ {+ name: "a",+ parent: 1,+ attributes: [+ ["href", ""],+ ["xlink:href", "/xlink-should-lose"],+ ],+ },+ ],+ baseUrl,+ ),+ );+ expect(result.urls).toEqual([+ {+ node_index: 2,+ attribute: "href",+ url: baseUrl,+ destination_scope: "approved",+ },+ ]);+ });++ it("classifies a non-http SVG xlink:href as unsupported", () => {+ const result = captureSvg(+ svgDocument([+ { name: "svg", parent: 0 },+ {+ name: "a",+ parent: 1,+ attributes: [["xlink:href", "javascript:void(0)"]],+ },+ ]),+ );+ expect(result.urls).toEqual([+ {+ node_index: 2,+ attribute: "href",+ url: null,+ destination_scope: "unsupported",+ },+ ]);+ });++ it("does not treat a cyclic or dangling parent chain as SVG", () => {+ const cyclic = captureSvg(+ svgDocument([+ { name: "g", parent: 2 },+ {+ name: "a",+ parent: 1,+ attributes: [["xlink:href", "/cycle"]],+ },+ ]),+ );+ const dangling = captureSvg(+ svgDocument([+ {+ name: "a",+ parent: 99,+ attributes: [["xlink:href", "/missing-parent"]],+ },+ ]),+ );+ expect(cyclic.urls).toEqual([]);+ expect(dangling.urls).toEqual([]);+ });+});diff --git a/src/browser/CdpCaptureDocuments.ts b/src/browser/CdpCaptureDocuments.tsindex aef32f894..38e3475f7 100644--- a/src/browser/CdpCaptureDocuments.ts+++ b/src/browser/CdpCaptureDocuments.ts@@ -171,6 +171,7 @@ export const captureDom = (? documentNodes.attributes: [];const baseIndex = nodes.length;+ const svgContexts = new Map<number, boolean>();total += nodeTypes.length;for (let index = 0; index < nodeTypes.length; index += 1) {const attributeIndexes = numberArray(attributes[index]);@@ -205,6 +206,13 @@ export const captureDom = (nodeIndex,nodeName,allowedOrigins,+ svgElement: elementInSvgContext(+ index,+ nodeNames,+ parents,+ strings,+ svgContexts,+ ),});for (const url of metadata.urls) {urls.push(url);@@ -375,8 +383,45 @@ interface DomMetadataOptions {readonly nodeIndex: number;readonly nodeName: string;readonly allowedOrigins: ReadonlySet<string>;+ readonly svgElement: boolean;}+// DOMSnapshot carries no namespace URI. Chrome's HTML parser records SVG+// local names (`svg`, `foreignObject`). Descendants of foreignObject are HTML+// until a nested `svg`.+const elementInSvgContext = (+ index: number,+ nodeNames: readonly number[],+ parents: readonly number[],+ strings: readonly string[],+ contexts: Map<number, boolean>,+): boolean => {+ let current = index;+ const seen = new Set<number>();+ let inSvg = false;+ while (current >= 0 && current < nodeNames.length && !seen.has(current)) {+ const cached = contexts.get(current);+ if (cached !== undefined) {+ inSvg = cached;+ break;+ }+ seen.add(current);+ if (indexedString(strings, nodeNames[current]) === "svg") {+ inSvg = true;+ break;+ }+ const parent = Math.trunc(parents[current] ?? -1);+ if (parent < 0 || parent === current) break;+ if (indexedString(strings, nodeNames[parent]) === "foreignObject")+ break;+ current = parent;+ }+ // Cache the entire examined chain, including forward references and cycles,+ // so shared ancestors are examined once per document.+ for (const node of seen) contexts.set(node, inSvg);+ return inSvg;+};+const domMetadata = (options: DomMetadataOptions,): {@@ -391,6 +436,7 @@ const domMetadata = (nodeIndex,nodeName,allowedOrigins,+ svgElement,} = options;const pairs = new Map<string, string>();for (let index = 0; index + 1 < attributes.length; index += 2) {@@ -402,7 +448,13 @@ const domMetadata = (}const urls: WebPageInspection["metadata"]["dom_urls"] = [];for (const attribute of domUrlAttributes) {- const value = pairs.get(attribute);+ // Chrome reads SVG xlink:href only when href itself is absent, and an+ // empty href still wins. Report the fallback as href.+ const value =+ pairs.get(attribute) ??+ (attribute === "href" && svgElement+ ? pairs.get("xlink:href")+ : undefined);if (value === undefined) continue;const tagName = nodeName.toLowerCase();// Chrome strips HTML whitespace for FORM.action, while submit controlsdiff --git a/tests/boundary/browser/cdpSvgXlinkHref.test.ts b/tests/boundary/browser/cdpSvgXlinkHref.test.tsnew file mode 100644index 000000000..da69ed7bc--- /dev/null+++ b/tests/boundary/browser/cdpSvgXlinkHref.test.ts@@ -0,0 +1,184 @@+import { existsSync } from "node:fs";+import { createServer } from "node:http";+import { join } from "node:path";++import { expect } from "vitest";++import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";+import { waitForBrowserDevtoolsPort } from "../../../src/browser/BrowserProcessStartup.js";+import {+ inspectWebPageInputSchema,+ listBrowserTargetsInputSchema,+} from "../../../src/domain/browserObservation.js";+import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";+import {+ processTest as it,+ waitForExit,+} from "../../support/process/processFixture.js";++const chromeExecutable = [+ process.env.REA_BROWSER_EXECUTABLE,+ "/usr/bin/google-chrome-stable",+ "/usr/bin/google-chrome",+ "/usr/bin/chromium",+ "/usr/bin/chromium-browser",+].find(+ (candidate): candidate is string =>+ typeof candidate === "string" &&+ candidate.length > 0 &&+ existsSync(candidate),+);++// Issue 1586's page, plus the HTML, image, use, and foreign-origin cases the+// real snapshot must distinguish. The test is skipped when no Chrome-family+// executable is installed.+const pageHtml = `<!doctype html><title>x</title>+<svg width="10" height="10">+ <a id="s2" xlink:href="/xl-s2h"><text>s2</text></a>+ <a id="both" xlink:href="/xlink-loses" href="/href-wins"><text>both</text></a>+ <image id="img" xlink:href="/xl-image" />+ <use id="use" xlink:href="/xl-use" />+ <a id="out" xlink:href="https://cdn.example.test/out"><text>out</text></a>+</svg>+<a id="html" xlink:href="/xl-html">html</a>+`;++it.skipIf(chromeExecutable === undefined)(+ "reports the issue page SVG xlink:href links through headless Chrome",+ async ({ processes }) => {+ const executable = chromeExecutable;+ if (executable === undefined) return;+ const profile = await createTestTempDirectory("rea-svg-xlink-");+ const server = createServer((_request, response) => {+ response.writeHead(200, { "content-type": "text/html; charset=utf-8" });+ response.end(pageHtml);+ });+ await new Promise<void>((resolve) => {+ server.listen(0, "127.0.0.1", resolve);+ });+ const address = server.address();+ if (address === null || typeof address === "string")+ throw new Error("SVG fixture server did not bind a TCP port");+ const origin = `http://127.0.0.1:${String(address.port)}`;+ const pageUrl = `${origin}/x.html`;+ let stderr = "";+ const child = processes.spawn(executable, [+ "--headless=new",+ ...(process.env.REA_BROWSER_NO_SANDBOX === "true"+ ? ["--no-sandbox"]+ : []),+ "--remote-debugging-address=127.0.0.1",+ "--remote-debugging-port=0",+ `--user-data-dir=${profile}`,+ "--no-first-run",+ "--no-default-browser-check",+ "--disable-background-networking",+ "--disable-component-update",+ "--disable-dev-shm-usage",+ "--disable-sync",+ "--metrics-recording-only",+ pageUrl,+ ]);+ child.stdout.on("data", () => undefined);+ child.stderr.on("data", (chunk: Buffer) => {+ if (stderr.length < 8_192) stderr += chunk.toString("utf8");+ });+ try {+ const port = await waitForBrowserDevtoolsPort({+ child,+ executable,+ activePortPath: join(profile, "DevToolsActivePort"),+ stderr: () => stderr,+ timeoutMs: 20_000,+ });+ const endpoint = `http://127.0.0.1:${String(port)}`;+ const provider = new CdpBrowserProvider();+ const targetId = await pageTarget(provider, endpoint, origin, pageUrl);+ const result = await provider.inspectPage(+ inspectWebPageInputSchema.parse({+ cdp_endpoint: endpoint,+ allowed_origins: [origin],+ target_id: targetId,+ observation_ms: 0,+ }),+ );+ if (!result.ok) throw result.error;+ const urls = result.value.metadata.dom_urls;+ const html = result.value.dom.nodes.find(+ (node) =>+ node.node_name === "A" && node.attribute_names.includes("xlink:href"),+ );+ expect(html).toBeDefined();+ expect(urls.some((url) => url.node_index === html?.index)).toBe(false);+ expect(urls).toEqual(+ expect.arrayContaining([+ expect.objectContaining({+ attribute: "href",+ url: `${origin}/xl-s2h`,+ destination_scope: "approved",+ }),+ expect.objectContaining({+ attribute: "href",+ url: `${origin}/href-wins`,+ destination_scope: "approved",+ }),+ expect.objectContaining({+ attribute: "href",+ url: `${origin}/xl-image`,+ destination_scope: "approved",+ }),+ expect.objectContaining({+ attribute: "href",+ url: `${origin}/xl-use`,+ destination_scope: "approved",+ }),+ expect.objectContaining({+ attribute: "href",+ url: null,+ destination_scope: "outside_policy",+ }),+ ]),+ );+ expect(+ urls.some(+ (url) =>+ url.url?.endsWith("/xl-html") === true ||+ url.url?.endsWith("/xlink-loses") === true,+ ),+ ).toBe(false);+ expect(urls.every((url) => url.attribute === "href")).toBe(true);+ expect(urls).toHaveLength(5);+ } finally {+ child.kill("SIGTERM");+ if (!(await waitForExit(child, 2_000))) child.kill("SIGKILL");+ await waitForExit(child, 2_000);+ await new Promise<void>((resolve) => {+ server.close(() => resolve());+ });+ }+ },+);++const pageTarget = async (+ provider: CdpBrowserProvider,+ endpoint: string,+ origin: string,+ pageUrl: string,+): Promise<string> => {+ const deadline = Date.now() + 10_000;+ while (Date.now() < deadline) {+ const listed = await provider.listTargets(+ listBrowserTargetsInputSchema.parse({+ cdp_endpoint: endpoint,+ allowed_origins: [origin],+ }),+ );+ if (!listed.ok) throw listed.error;+ const match = listed.value.targets.find((target) =>+ target.url.startsWith(pageUrl),+ );+ if (match !== undefined) return match.target_id;+ await new Promise((resolve) => setTimeout(resolve, 50));+ }+ throw new Error("Headless Chrome did not expose the SVG fixture page");+};