|
| 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