dedicated-worker-import-referrer.html (10243B)
1 <!DOCTYPE html> 2 <title>DedicatedWorker: Referrer</title> 3 <meta name="timeout" content="long"> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script> 7 8 async function openWindow(url) { 9 const win = window.open(url, '_blank'); 10 add_result_callback(() => win.close()); 11 const msg_event = await new Promise(resolve => window.onmessage = resolve); 12 assert_equals(msg_event.data, 'LOADED'); 13 return win; 14 } 15 16 // Removes URL parameters from the given URL string and returns it. 17 function removeParams(url_string) { 18 if (!url_string) 19 return url_string; 20 let url = new URL(url_string); 21 for (var key of url.searchParams.keys()) 22 url.searchParams.delete(key); 23 return url.href; 24 } 25 26 // Generates a referrer given a fetchType, referrer policy, and a request URL 27 // (used to determine whether request is remote-origin or not). This function 28 // is used to generate expected referrers. 29 function generateExpectedReferrer(referrerURL, referrerPolicy, requestURL) { 30 let generatedReferrer = ""; 31 if (referrerPolicy === 'no-referrer') 32 generatedReferrer = ""; 33 else if (referrerPolicy === 'origin') 34 generatedReferrer = new URL('resources/' + referrerURL, location.href).origin + '/'; 35 else if (referrerPolicy === 'same-origin') 36 generatedReferrer = requestURL.includes('remote') ? "" : new URL('resources/' + referrerURL, location.href).href; 37 else 38 generatedReferrer = new URL('resources/' + referrerURL, location.href).href; 39 40 return generatedReferrer; 41 } 42 43 // Runs a referrer policy test with the given settings. This opens a new window 44 // that starts a dedicated worker. 45 // 46 // |settings| has options as follows: 47 // 48 // settings = { 49 // scriptURL: 'resources/referrer-checker.sub.js', 50 // windowReferrerPolicy: 'no-referrer', 51 // workerReferrerPolicy: 'same-origin', 52 // fetchType: 'top-level' or 'descendant-static' or 'descendant-dynamic' 53 // }; 54 // 55 // - |scriptURL| is used for starting a new worker. 56 // - |windowReferrerPolicy| is to set the ReferrerPolicy HTTP header of the 57 // window. This policy should be applied to top-level worker module script 58 // loading and static imports. 59 // - |workerReferrerPolicy| is to set the ReferrerPolicy HTTP header of the 60 // worker. This policy should be applied to dynamic imports. 61 // - |fetchType| indicates a script whose referrer will be tested. 62 function import_referrer_test(settings, description) { 63 promise_test(async () => { 64 let windowURL = 'resources/new-worker-window.html'; 65 if (settings.windowReferrerPolicy) { 66 windowURL += 67 `?pipe=header(Referrer-Policy, ${settings.windowReferrerPolicy})`; 68 } 69 70 let scriptURL = settings.scriptURL; 71 if (settings.workerReferrerPolicy) { 72 // 'sub' is necessary even if the |scriptURL| contains the '.sub' 73 // extension because the extension doesn't work with the 'pipe' parameter. 74 // See an issue on the WPT's repository: 75 // https://github.com/web-platform-tests/wpt/issues/9345 76 scriptURL += 77 `?pipe=sub|header(Referrer-Policy, ${settings.workerReferrerPolicy})`; 78 } 79 80 const win = await openWindow(windowURL); 81 win.postMessage(scriptURL, '*'); 82 const msgEvent = await new Promise(resolve => window.onmessage = resolve); 83 84 // Generate the expected referrer, given: 85 // - The fetchType of the test 86 // - Referrer URL to be used 87 // - Relevant referrer policy 88 // - Request URL 89 let expectedReferrer; 90 if (settings.fetchType === 'top-level') { 91 // Top-level worker requests have their outgoing referrers set given their 92 // containing window's URL and referrer policy. 93 expectedReferrer = generateExpectedReferrer('new-worker-window.html', settings.windowReferrerPolicy, settings.scriptURL); 94 } else if (settings.fetchType === 'descendant-static') { 95 // Static descendant script requests from a worker have their outgoing 96 // referrer set given their containing script's URL and containing 97 // window's referrer policy. 98 99 // In the below cases, the referrer URL and the request URLs are the same 100 // because the initial request (for the top-level worker script) should 101 // act as the referrer for future descendant requests. 102 expectedReferrer = generateExpectedReferrer(settings.scriptURL, settings.windowReferrerPolicy, settings.scriptURL); 103 } else if (settings.fetchType === 'descendant-dynamic') { 104 // Dynamic descendant script requests from a worker have their outgoing 105 // referrer set given their containing script's URL and referrer policy. 106 expectedReferrer = generateExpectedReferrer(settings.scriptURL, settings.workerReferrerPolicy, settings.scriptURL); 107 } 108 109 // Delete query parameters from the actual referrer to make it easy to match 110 // it with the expected referrer. 111 const actualReferrer = removeParams(msgEvent.data); 112 113 assert_equals(actualReferrer, expectedReferrer); 114 }, description); 115 } 116 117 // Tests for top-level worker module script loading. 118 // 119 // Top-level worker module scripts should inherit the containing window's 120 // referrer policy when using the containing window's URL as the referrer. 121 // 122 // [Current document] 123 // --(open)--> [Window] whose referrer policy is |windowReferrerPolicy|. 124 // --(new Worker)--> [Worker] should respect [windowReferrerPolicy| when 125 // using [Window]'s URL as the referrer. 126 127 import_referrer_test( 128 { scriptURL: 'postmessage-referrer-checker.py', 129 windowReferrerPolicy: 'no-referrer', 130 fetchType: 'top-level' }, 131 'Same-origin top-level module script loading with "no-referrer" referrer ' + 132 'policy'); 133 134 import_referrer_test( 135 { scriptURL: 'postmessage-referrer-checker.py', 136 windowReferrerPolicy: 'origin', 137 fetchType: 'top-level' }, 138 'Same-origin top-level module script loading with "origin" referrer ' + 139 'policy'); 140 141 import_referrer_test( 142 { scriptURL: 'postmessage-referrer-checker.py', 143 windowReferrerPolicy: 'same-origin', 144 fetchType: 'top-level' }, 145 'Same-origin top-level module script loading with "same-origin" referrer ' + 146 'policy'); 147 148 // Tests for static imports. 149 // 150 // Static imports should inherit the containing window's referrer policy when 151 // using the containing module script's URL as the referrer. 152 // Note: This is subject to change in the future; see 153 // https://github.com/w3c/webappsec-referrer-policy/issues/111. 154 // 155 // [Current document] 156 // --(open)--> [Window] whose referrer policy is |windowReferrerPolicy|. 157 // --(new Worker)--> [Worker] 158 // --(static import)--> [Script] should respect |windowReferrerPolicy| when 159 // using [Worker]'s URL as the referrer. 160 161 import_referrer_test( 162 { scriptURL: 'static-import-same-origin-referrer-checker-worker.js', 163 windowReferrerPolicy: 'no-referrer', 164 fetchType: 'descendant-static' }, 165 'Same-origin static import with "no-referrer" referrer policy.'); 166 167 import_referrer_test( 168 { scriptURL: 'static-import-same-origin-referrer-checker-worker.js', 169 windowReferrerPolicy: 'origin', 170 fetchType: 'descendant-static' }, 171 'Same-origin static import with "origin" referrer policy.'); 172 173 import_referrer_test( 174 { scriptURL: 'static-import-same-origin-referrer-checker-worker.js', 175 windowReferrerPolicy: 'same-origin', 176 fetchType: 'descendant-static' }, 177 'Same-origin static import with "same-origin" referrer policy.'); 178 179 import_referrer_test( 180 { scriptURL: 'static-import-remote-origin-referrer-checker-worker.sub.js', 181 windowReferrerPolicy: 'no-referrer', 182 fetchType: 'descendant-static' }, 183 'Cross-origin static import with "no-referrer" referrer policy.'); 184 185 import_referrer_test( 186 { scriptURL: 'static-import-remote-origin-referrer-checker-worker.sub.js', 187 windowReferrerPolicy: 'origin', 188 fetchType: 'descendant-static' }, 189 'Cross-origin static import with "origin" referrer policy.'); 190 191 import_referrer_test( 192 { scriptURL: 'static-import-remote-origin-referrer-checker-worker.sub.js', 193 windowReferrerPolicy: 'same-origin', 194 fetchType: 'descendant-static' }, 195 'Cross-origin static import with "same-origin" referrer policy.'); 196 197 // Tests for dynamic imports. 198 // 199 // Dynamic imports should inherit the containing script's referrer policy if 200 // set, and use the default referrer policy otherwise, when using the 201 // containing script's URL as the referrer. 202 // 203 // [Current document] 204 // --(open)--> [Window] 205 // --(new Worker)--> [Worker] whose referrer policy is |workerReferrerPolicy|. 206 // --(dynamic import)--> [Script] should respect |workerReferrerPolicy| when 207 // using [Worker]'s URL as the referrer. 208 209 import_referrer_test( 210 { scriptURL: 'dynamic-import-same-origin-referrer-checker-worker.js', 211 workerReferrerPolicy: 'no-referrer', 212 fetchType: 'descendant-dynamic' }, 213 'Same-origin dynamic import with "no-referrer" referrer policy.'); 214 215 import_referrer_test( 216 { scriptURL: 'dynamic-import-same-origin-referrer-checker-worker.js', 217 workerReferrerPolicy: 'origin', 218 fetchType: 'descendant-dynamic' }, 219 'Same-origin dynamic import with "origin" referrer policy.'); 220 221 import_referrer_test( 222 { scriptURL: 'dynamic-import-same-origin-referrer-checker-worker.js', 223 workerReferrerPolicy: 'same-origin', 224 fetchType: 'descendant-dynamic' }, 225 'Same-origin dynamic import with "same-origin" referrer policy.'); 226 227 import_referrer_test( 228 { scriptURL: 'dynamic-import-remote-origin-referrer-checker-worker.sub.js', 229 workerReferrerPolicy: 'no-referrer', 230 fetchType: 'descendant-dynamic' }, 231 'Cross-origin dynamic import with "no-referrer" referrer policy.'); 232 233 import_referrer_test( 234 { scriptURL: 'dynamic-import-remote-origin-referrer-checker-worker.sub.js', 235 workerReferrerPolicy: 'origin', 236 fetchType: 'descendant-dynamic' }, 237 'Cross-origin dynamic import with "origin" referrer policy.'); 238 239 import_referrer_test( 240 { scriptURL: 'dynamic-import-remote-origin-referrer-checker-worker.sub.js', 241 workerReferrerPolicy: 'same-origin', 242 fetchType: 'descendant-dynamic' }, 243 'Cross-origin dynamic import with "same-origin" referrer policy.'); 244 </script>