fix(browser): report only grammatical CORP and X-Content-Type-Options values

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.
This commit is contained in:
XiaoBinGan authored and Ysopwnable committed 2026-11-21 21:10:02 +01:00
1 parent 985fa2fac9
commit 1aa394d62d
2 files changed
+80 -5

No files matched your search

+52
View File
@@ -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);
},
);
});
+28 -5
View File
@@ -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<string> = 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.