fix(browser): report SVG xlink:href links in dom_urls (#1602)

* fix(browser): report SVG xlink:href links in dom_urls

DOM snapshots have no namespace URI, so SVG context is an svg ancestor
outside foreignObject. When href is missing, report xlink:href as href
with the same base URL and origin policy.

Fixes #1586

* fix(browser): cache SVG ancestry during DOM capture

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
Nithilan Kumaran
2026-10-11 06:23:07 +08:00
committed by GitHub
co-authored by Cursor Agent morluto
parent 423b11c8ef
commit 0f605bca09
3 changed files with 566 additions and 1 deletions
@@ -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([]);
});
});
+53 -1
View File
@@ -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 controls