fix(browser): preserve Link header value delimiters (#805)
Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user