Skip to content

Commit 86e95f9

Browse files
roberto-appleannevk
authored andcommitted
CSP keyword source expressions must match the whole token
ContentSecurityPolicySourceList::parseSource() checks a keyword source expression such as 'self' or 'unsafe-inline' by testing only that the token starts with the keyword. A token with extra characters after a valid keyword like 'unsafe-inline'X is treated as a normal keyword instead of being rejected. Fix by comparing each keyword, including 'none', against the whole token with equalIgnoringASCIICase() instead of matching a prefix. A token that does not match a keyword exactly now falls through to host source parsing, which rejects it. This follows the CSP Level 3 grammar. https://w3c.github.io/webappsec-csp/#grammardef-keyword-source Adds a new test and updates two existing tests to account for this change. WebKit-Bug: https://bugs.webkit.org/show_bug.cgi?id=319577 WebKit-Canonical-Link: https://commits.webkit.org/322470@main
1 parent 0ab92b0 commit 86e95f9

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!DOCTYPE html>
2+
<title>Parsing: a keyword source expression must match the source expression in its entirety</title>
3+
<script src="/resources/testharness.js"></script>
4+
<script src="/resources/testharnessreport.js"></script>
5+
<body>
6+
<script>
7+
// Each sub-test loads its own policy in a srcdoc iframe via <meta>. 'self' there resolves to this page's origin.
8+
9+
function loadIframe(policy, body) {
10+
return new Promise(resolve => {
11+
const iframe = document.createElement("iframe");
12+
iframe.srcdoc = `<meta http-equiv="Content-Security-Policy" content="${policy}">` + body;
13+
iframe.onload = () => resolve(iframe);
14+
document.body.appendChild(iframe);
15+
});
16+
}
17+
18+
promise_test(async t => {
19+
const iframe = await loadIframe("script-src 'unsafe-inline'X", "<script>window.ran = true;</scr" + "ipt>");
20+
t.add_cleanup(() => iframe.remove());
21+
assert_equals(iframe.contentWindow.ran, undefined, "'unsafe-inline'X must not be treated as 'unsafe-inline'.");
22+
}, "script-src 'unsafe-inline'X ignores the invalid token, so inline script is blocked.");
23+
24+
promise_test(async t => {
25+
const iframe = await loadIframe("script-src 'self''unsafe-inline'", "<script>window.ran = true;</scr" + "ipt>");
26+
t.add_cleanup(() => iframe.remove());
27+
assert_equals(iframe.contentWindow.ran, undefined, "'self''unsafe-inline' must not be treated as 'self' or 'unsafe-inline'.");
28+
}, "script-src 'self''unsafe-inline' ignores the invalid token, so inline script is blocked.");
29+
30+
promise_test(async t => {
31+
const iframe = await loadIframe("script-src 'unsafe-inline' 'unsafe-eval'X", "<script>window.evalThrew = false; try { eval('1'); } catch (e) { window.evalThrew = true; }</scr" + "ipt>");
32+
t.add_cleanup(() => iframe.remove());
33+
assert_true(iframe.contentWindow.evalThrew, "'unsafe-eval'X must not be treated as 'unsafe-eval'.");
34+
}, "script-src 'unsafe-inline' 'unsafe-eval'X ignores the invalid token, so eval() is blocked.");
35+
36+
promise_test(async t => {
37+
const iframe = await loadIframe("script-src 'self'/foo/", "<script src=\"/content-security-policy/support/var-a.js\"></scr" + "ipt>");
38+
t.add_cleanup(() => iframe.remove());
39+
assert_equals(iframe.contentWindow.a, undefined, "'self'/foo/ must not be treated as 'self'.");
40+
}, "script-src 'self'/foo/ ignores the invalid token, so a same-origin script is blocked.");
41+
42+
promise_test(t => {
43+
return new Promise(resolve => {
44+
window.addEventListener("message", function handler(e) {
45+
window.removeEventListener("message", handler);
46+
resolve(e.data);
47+
});
48+
const body = `<body><script nonce="abc">
49+
var s = document.createElement("script");
50+
s.src = "/content-security-policy/support/var-a.js";
51+
s.onload = () => parent.postMessage("load", "*");
52+
s.onerror = () => parent.postMessage("error", "*");
53+
document.body.appendChild(s);
54+
</scr` + `ipt>`;
55+
const iframe = document.createElement("iframe");
56+
iframe.srcdoc = `<meta http-equiv="Content-Security-Policy" content="script-src 'nonce-abc' 'strict-dynamic'X">` + body;
57+
t.add_cleanup(() => iframe.remove());
58+
document.body.appendChild(iframe);
59+
}).then(result => assert_equals(result, "error", "'strict-dynamic'X must not be treated as 'strict-dynamic'."));
60+
}, "script-src 'nonce-abc' 'strict-dynamic'X ignores the invalid token, so a dynamically inserted script is blocked.");
61+
62+
promise_test(async t => {
63+
const iframe = await loadIframe("style-src 'strict-dynamic''unsafe-inline'", "<div id=\"target\">target</div><style>#target { background-color: rgb(0, 128, 0); }</style>");
64+
t.add_cleanup(() => iframe.remove());
65+
const color = iframe.contentWindow.getComputedStyle(iframe.contentDocument.getElementById("target")).backgroundColor;
66+
assert_not_equals(color, "rgb(0, 128, 0)", "'strict-dynamic''unsafe-inline' must not be split into two keywords.");
67+
}, "style-src 'strict-dynamic''unsafe-inline' ignores the invalid token, so inline style is blocked.");
68+
</script>

0 commit comments

Comments
 (0)