diff --git a/src/browser/CdpSafeMetadata.test.ts b/src/browser/CdpSafeMetadata.test.ts index 1d4c4f6ac7..3e3707a900 100644 --- a/src/browser/CdpSafeMetadata.test.ts +++ b/src/browser/CdpSafeMetadata.test.ts @@ -774,3 +774,55 @@ describe("response metadata whitespace boundaries", () => { expect(captured.response.policies.referrer_policy).toBe("no-referrer"); }); }); + +describe("cross-origin policy value grammars", () => { + const policies = (headers: UnknownRecord) => + safeResponseMetadata( + "request-policy-grammar", + `${origin}/api`, + { headers }, + new Set([origin]), + ).response.policies; + + it.each([ + // Fetch 3.7 writes the value ABNF with `%s`, so it is case-sensitive and every other + // spelling declares no policy; the internal check has nothing to enforce. + ["same-origin", "same-origin"], + ["same-site", "same-site"], + ["cross-origin", "cross-origin"], + ["SAME-ORIGIN", null], + ["Same-Site", null], + ["same-origin-ish", null], + ["same_origin", null], + // `unsafe-none` is an embedder-policy value; it is never a resource policy. + ["unsafe-none", null], + ["same-site, same-origin", null], + ] as const)( + "reports only a grammatical CORP value for %j", + (raw, expected) => { + expect(policies({ "Cross-Origin-Resource-Policy": raw }).corp).toBe( + expected, + ); + }, + ); + + it.each([ + // Fetch 3.6 declares one case-insensitive value and "determine nosniff" tests values[0] + // of the combined header, so a repeated line and a two-member list both still declare it. + ["nosniff", "nosniff"], + ["NOSNIFF", "nosniff"], + [" nosniff ", "nosniff"], + ["nosniff\nnosniff", "nosniff"], + ["nosniff, sniff", "nosniff"], + ["sniff, nosniff", null], + ["nosniff-ish", null], + ["sniff", null], + ] as const)( + "reports only a grammatical X-Content-Type-Options value for %j", + (raw, expected) => { + expect( + policies({ "X-Content-Type-Options": raw }).x_content_type_options, + ).toBe(expected); + }, + ); +}); diff --git a/src/browser/CdpSafeMetadata.ts b/src/browser/CdpSafeMetadata.ts index b3147e7dcb..b64517ea87 100644 --- a/src/browser/CdpSafeMetadata.ts +++ b/src/browser/CdpSafeMetadata.ts @@ -61,9 +61,11 @@ export const safeResponseMetadata = ( policies: { coop: structuredFieldToken(field("cross-origin-opener-policy")), coep: structuredFieldToken(field("cross-origin-embedder-policy")), - corp: policyToken(field("cross-origin-resource-policy")), + corp: crossOriginResourcePolicy(field("cross-origin-resource-policy")), referrer_policy: referrerPolicy(field("referrer-policy")), - x_content_type_options: policyToken(field("x-content-type-options")), + x_content_type_options: xContentTypeOptions( + field("x-content-type-options"), + ), permissions_policy_features: permissionFeatures( field("permissions-policy"), ), @@ -427,9 +429,30 @@ const referrerPolicy = (value: string | undefined): string | null => { return policy; }; -const policyToken = (value: string | undefined): string | null => { - const token = trimHttpWhitespace(value ?? "").toLowerCase(); - return /^[a-z][a-z0-9_.\x2d]*$/u.test(token) ? token : null; +// Fetch 3.7: `Cross-Origin-Resource-Policy = %s"same-origin" / %s"same-site" / +// %s"cross-origin"`. The `%s` productions make the value case-sensitive, so `SAME-ORIGIN` +// declares no policy rather than being folded into the enforced spelling. +const crossOriginResourcePolicies: ReadonlySet = new Set([ + "same-origin", + "same-site", + "cross-origin", +]); + +const crossOriginResourcePolicy = ( + value: string | undefined, +): string | null => { + const token = trimHttpWhitespace(value ?? ""); + return crossOriginResourcePolicies.has(token) ? token : null; +}; + +// Fetch 3.6: `X-Content-Type-Options = "nosniff"` is case-insensitive, and "determine nosniff" +// tests `values[0]` of the combined header. A response whose middleware and application each +// send `nosniff` still declares it, and `nosniff, sniff` still takes the first member. +const xContentTypeOptions = (value: string | undefined): string | null => { + const [first = ""] = (value ?? "").split(","); + return trimHttpWhitespace(first).toLowerCase() === "nosniff" + ? "nosniff" + : null; }; // RFC 9110 section 8.6: Content-Length = 1*DIGIT. Other spellings declare no length.