|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <script src="/resources/testharness.js"></script> |
| 6 | + <script src="/resources/testharnessreport.js"></script> |
| 7 | + <title> Test the snapshotted CSP is checked during task creation, not during |
| 8 | + execution. |
| 9 | + </title> |
| 10 | +</head> |
| 11 | +<body> |
| 12 | + <iframe id="iframe"></iframe> |
| 13 | + <script> |
| 14 | + setup({ single_test: true }); |
| 15 | + |
| 16 | + function f() { |
| 17 | + location.href = "javascript:h()"; |
| 18 | + } |
| 19 | + |
| 20 | + let e1Dispatched = false; |
| 21 | + |
| 22 | + document.addEventListener("securitypolicyviolation", (e1) => { |
| 23 | + if (e1.lineNumber == 88) { |
| 24 | + e1Dispatched = true; |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + document.addEventListener("securitypolicyviolation", (e2) => { |
| 29 | + if (e2.lineNumber == 17) { |
| 30 | + assert_true(e1Dispatched, "e1 was dispatched before e2"); |
| 31 | + done(); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + function addCSP() { |
| 36 | + const m = document.createElement("meta"); |
| 37 | + m.setAttribute("http-equiv", "Content-Security-Policy"); |
| 38 | + m.setAttribute("content", "default-src 'none'"); |
| 39 | + document.head.append(m); |
| 40 | + } |
| 41 | + |
| 42 | + window.addEventListener("load", () => { |
| 43 | + // Steps: |
| 44 | + // 1. Execute `javascript:` URL: queues task for executing `f`. |
| 45 | + // 2. Add CSP. |
| 46 | + // 3. Execute `javascript:` URL: queues `securitypolicyviolation` event e1 |
| 47 | + // (expected) or a task for executing `g`. |
| 48 | + |
| 49 | + // `f`: should queue another task, a different `securitypolicyviolation` |
| 50 | + // e2. |
| 51 | + // `g`: doesn't matter, won't be executed. |
| 52 | + |
| 53 | + // Potentially two queues from the spec are relevant here: |
| 54 | + // Queue 1 for the `javascript:` URL navigations: |
| 55 | + // <https://html.spec.whatwg.org/#navigation-and-traversal-task-source>. |
| 56 | + // Queue 2 for the "securitypolicyviolation" events: |
| 57 | + // <https://github.com/w3c/webappsec-csp/issues/696>. |
| 58 | + |
| 59 | + // After step 1: |
| 60 | + // Queue 1: [javascript-f] |
| 61 | + // After step 2: |
| 62 | + // Queue 1: [javascript-f] |
| 63 | + // Expected after step 3: |
| 64 | + // Queue 1: [javascript-f]; Queue 2: [e1] |
| 65 | + // After javascript-f: |
| 66 | + // Queue 1: []; Queue 2: [e1, e2]* |
| 67 | + // Unexpected after step 3: |
| 68 | + // Queue 1: [javascript-f, javascript-g] |
| 69 | + // After javascript-f: |
| 70 | + // Queue 1: [javascript-g]; Queue 2: [e2] |
| 71 | + // After javascript-g: |
| 72 | + // Queue 1: []; Queue 2: [e2, e1]* |
| 73 | + // |
| 74 | + // *: the order or processing two elements of different queues is |
| 75 | + // unspecified. For this test only the order within queue 2 matters. |
| 76 | + // |
| 77 | + // So e1 being dispatched before e2 implies the snapshotted CSP was |
| 78 | + // checked during task creation, not during task execution. |
| 79 | + // |
| 80 | + // That behavior isn't specified; see |
| 81 | + // <https://github.com/whatwg/html/issues/4651#issuecomment-2412623188> |
| 82 | + // and related comments. This test is a first step towards specifying |
| 83 | + // a deterministic behavior. |
| 84 | + |
| 85 | + const iframe = document.getElementById("iframe"); |
| 86 | + iframe.contentWindow.location.href = "javascript:parent.f()"; |
| 87 | + addCSP(); |
| 88 | + iframe.contentWindow.location.href = "javascript:g()"; |
| 89 | + }); |
| 90 | + </script> |
| 91 | +</body> |
| 92 | +</html> |
0 commit comments