fix(browser): classify CSP host sources by their own origin (#707)

* fix(browser): classify CSP host sources by their own origin

A Content-Security-Policy host-source without a scheme (for example
`cdn.example.com` or `*.trusted.test`) was resolved with
`new URL(token, baseUrl)`, so it became a page-relative path and was
reported as the observed page's own origin. Third-party script, style, and
connect sources were therefore misreported as approved origins, and a bare
`*` wildcard collapsed to the page origin as well.

Resolve host-sources per CSP3 \u00a72.3.1: a scheme-less source inherits the
protected resource's scheme, wildcard host/port sources stay classified as a
set of origins, and non-host tokens remain `other`. Keyword and scheme
sources are unchanged.

Adds boundary coverage for scheme-less host sources, wildcards, and scheme
inheritance against the approved-origin set.

* fix(browser): validate CSP host source syntax

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
Dery Ferdika
2026-10-06 22:26:57 +08:00
committed by GitHub
co-authored by morluto
parent 739039092f
commit 1d15bcb9ec
2 changed files with 100 additions and 6 deletions
@@ -142,3 +142,51 @@ describe("safe CDP response metadata", () => {
);
});
});
describe("safe CDP response metadata CSP sources", () => {
it("classifies scheme-less CSP host sources by their own host, not the page origin", () => {
const captured = safeResponseMetadata(
"request-csp",
`${origin}/api`,
{
headers: {
"Content-Security-Policy":
"script-src cdn.example.test *.trusted.test * 'self' https://api.other.test",
},
},
new Set([origin]),
);
expect(captured.response.csp.directives).toEqual([
{
name: "script-src",
sources: [
{ kind: "external_origin", value: null },
{ kind: "external_origin", value: null },
{ kind: "external_origin", value: null },
{ kind: "keyword", value: "'self'" },
{ kind: "external_origin", value: null },
],
},
]);
});
it("inherits the protected resource scheme for a scheme-less host source", () => {
const captured = safeResponseMetadata(
"request-csp-scheme",
`${origin}/api`,
{
headers: {
"Content-Security-Policy":
"script-src cdn.example.test app.example.test",
},
},
new Set([origin, "https://cdn.example.test"]),
);
expect(captured.response.csp.directives[0]?.sources).toEqual([
{ kind: "approved_origin", value: "https://cdn.example.test" },
{ kind: "approved_origin", value: origin },
]);
});
});