From dd3640548fe3d1e8b92784a63fac55d757dcc1f0 Mon Sep 17 00:00:00 2001 From: Rudy Mizrahi Celekli <47457359+rudycelekli@users.noreply.github.com> Date: Tue, 6 Oct 2026 21:22:32 -0500 Subject: [PATCH] fix(browser): preserve Link header value delimiters (#805) Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com> --- src/browser/CdpSafeMetadata.ts | 26 ++++++-- .../boundary/browser/cdpSafeMetadata.test.ts | 59 +++++++++++++++++++ 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/src/browser/CdpSafeMetadata.ts b/src/browser/CdpSafeMetadata.ts index da9793aee..ba5c1800d 100644 --- a/src/browser/CdpSafeMetadata.ts +++ b/src/browser/CdpSafeMetadata.ts @@ -213,16 +213,30 @@ const parseLinks = ( return links; }; -const splitLinkHeader = (value: string): string[] => { +const splitLinkHeader = ( + value: string, + delimiter: "," | ";" = ",", +): string[] => { const entries: string[] = []; let start = 0; let quoted = false; + let escaped = false; + let target = false; for (let index = 0; index < value.length; index += 1) { const character = value[index]; - if (character === '"' && value[index - 1] !== "\\") quoted = !quoted; - if (character !== "," || quoted) continue; - entries.push(value.slice(start, index)); - start = index + 1; + if (quoted) { + if (escaped) escaped = false; + else if (character === "\\") escaped = true; + else if (character === '"') quoted = false; + continue; + } + if (character === '"' && !target) quoted = true; + else if (delimiter === "," && character === "<") target = true; + else if (delimiter === "," && character === ">") target = false; + else if (character === delimiter && !target) { + entries.push(value.slice(start, index)); + start = index + 1; + } } entries.push(value.slice(start)); return entries; @@ -230,7 +244,7 @@ const splitLinkHeader = (value: string): string[] => { const linkParameters = (value: string): ReadonlyMap => { const parameters = new Map(); - for (const raw of value.split(";").slice(1)) { + for (const raw of splitLinkHeader(value, ";").slice(1)) { const separator = raw.indexOf("="); const name = (separator < 0 ? raw : raw.slice(0, separator)) .trim() diff --git a/tests/boundary/browser/cdpSafeMetadata.test.ts b/tests/boundary/browser/cdpSafeMetadata.test.ts index f07771fa0..67a52a11f 100644 --- a/tests/boundary/browser/cdpSafeMetadata.test.ts +++ b/tests/boundary/browser/cdpSafeMetadata.test.ts @@ -190,3 +190,62 @@ describe("safe CDP response metadata CSP sources", () => { ]); }); }); + +describe("Link header value delimiters", () => { + const captureLinks = (link: string) => + safeResponseMetadata( + "owned", + "https://owned.test/page", + { + headers: { Link: link }, + }, + new Set(["https://owned.test"]), + ).response.links; + + it("preserves commas in URI references and separates later links", () => { + const links = captureLinks( + '; rel="preload"; as=script, ; rel="modulepreload"', + ); + expect(links.map(({ href }) => href)).toEqual([ + "https://owned.test/assets/red,blue.js", + "https://owned.test/ordinary.js", + ]); + expect(links[0]?.as).toBe("script"); + }); + + it("preserves semicolons and commas inside quoted parameter values", () => { + const links = captureLinks( + '; rel="preload"; type="text/javascript; charset=utf-8"; title="a,b", ; rel="modulepreload"', + ); + expect(links).toHaveLength(2); + expect(links[0]?.type).toBe("text/javascript; charset=utf-8"); + expect(links[1]?.href).toBe("https://owned.test/ordinary.js"); + }); + + it("counts quoted-pair escapes rather than treating every preceding slash as an escape", () => { + const links = captureLinks( + String.raw`; title="ends with \\"; rel="preload", ; rel="modulepreload"`, + ); + expect(links.map(({ href }) => href)).toEqual([ + "https://owned.test/one.js", + "https://owned.test/two.js", + ]); + expect(links[0]?.rel).toEqual(["preload"]); + }); + + it("keeps a quoted escaped quote from exposing a delimiter", () => { + const links = captureLinks( + String.raw`; title="quoted \" comma, value"; rel="preload", ; rel="modulepreload"`, + ); + expect(links).toHaveLength(2); + expect(links[0]?.rel).toEqual(["preload"]); + }); + + it("retains the existing outside-policy classification", () => { + const links = captureLinks( + '; rel="preload", ; rel="modulepreload"', + ); + expect(links[0]?.destination_scope).toBe("outside_policy"); + expect(links[1]?.destination_scope).toBe("approved"); + }); +});