From 1aa394d62d6f60809e9fbc1ca6cbfdb3341bc95f Mon Sep 17 00:00:00 2001 From: XiaoBinGan <35291372+XiaoBinGan@users.noreply.github.com> Date: Thu, 12 Nov 2026 16:56:57 +0800 Subject: [PATCH] fix(browser): report only grammatical CORP and X-Content-Type-Options values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One regex served both headers — `^[a-z][a-z0-9_.\x2d]*$` after lowercasing — so any token counted as a declared policy. Measured through `safeResponseMetadata`: | `Cross-Origin-Resource-Policy` | published | Fetch 3.7 | | --- | --- | --- | | `same-origin` | `same-origin` | `same-origin` | | `SAME-ORIGIN` | `same-origin` | none | | `Same-Site` | `same-site` | none | | `same-origin-ish` | `same-origin-ish` | none | | `same_origin` | `same_origin` | none | | `unsafe-none` | `unsafe-none` | none | The value ABNF is `%s"same-origin" / %s"same-site" / %s"cross-origin"`, and `%s` is case-sensitive, so the internal check nulls every other spelling. Case folding made `SAME-ORIGIN` worse than a bad echo: it turned a value browsers ignore into the enforced spelling, so an observation claimed embedding isolation the page did not have. `X-Content-Type-Options` declares exactly one case-insensitive `nosniff`, and "determine nosniff" tests `values[0]` of the combined header. The strict whole-value match also dropped valid declarations — `nosniff` sent on two lines, or `nosniff, sniff`, both published nothing. The reader takes the first list member now. | `X-Content-Type-Options` | before | after | | --- | --- | --- | | `nosniff` | `nosniff` | `nosniff` | | `NOSNIFF` | `nosniff` | `nosniff` | | `nosniff\nnosniff` | none | `nosniff` | | `nosniff, sniff` | none | `nosniff` | | `sniff, nosniff` | none | none | | `nosniff-ish` | `nosniff-ish` | none | Each header has its own grammar now, and `policyToken` is gone. `src/browser/CdpSafeMetadata.test.ts` — 133 passing, 17 new cases. Reverting either reader fails them. --- src/browser/CdpSafeMetadata.test.ts | 52 +++++++++++++++++++++++++++++ src/browser/CdpSafeMetadata.ts | 33 +++++++++++++++--- 2 files changed, 80 insertions(+), 5 deletions(-) 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.