Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(browser): preserve Link header value delimiters (#805)

Rudy Mizrahi Celeklidd3640548fe3
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<string, string> => {
const parameters = new Map<string, string>();
- 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(
+ '</assets/red,blue.js>; rel="preload"; as=script, </ordinary.js>; 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(
+ '</plugin.js>; rel="preload"; type="text/javascript; charset=utf-8"; title="a,b", </ordinary.js>; 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`</one.js>; title="ends with \\"; rel="preload", </two.js>; 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`</one.js>; title="quoted \" comma, value"; rel="preload", </two.js>; rel="modulepreload"`,
+ );
+ expect(links).toHaveLength(2);
+ expect(links[0]?.rel).toEqual(["preload"]);
+ });
+
+ it("retains the existing outside-policy classification", () => {
+ const links = captureLinks(
+ '<https://elsewhere.test/red,blue.js>; rel="preload", </ordinary.js>; rel="modulepreload"',
+ );
+ expect(links[0]?.destination_scope).toBe("outside_policy");
+ expect(links[1]?.destination_scope).toBe("approved");
+ });
+});