|
| 1 | +<!DOCTYPE html> |
| 2 | +<title> |
| 3 | + Credentials in WebBundle subresource loading |
| 4 | +</title> |
| 5 | +<link |
| 6 | + rel="help" |
| 7 | + href="https://github.com/WICG/webpackage/blob/main/explainers/subresource-loading.md#requests-mode-and-credentials-mode" |
| 8 | +/> |
| 9 | +<script src="/resources/testharness.js"></script> |
| 10 | +<script src="/resources/testharnessreport.js"></script> |
| 11 | +<script src="../resources/test-helpers.js"></script> |
| 12 | +<body> |
| 13 | + <script> |
| 14 | + // In this wpt, we test a request's credential mode, which controls |
| 15 | + // whether UA sends a credential or not to fetch a bundle. |
| 16 | + |
| 17 | + // If UA sends a credential, check-cookie-and-return-{cross-oriigin}-bundle.py |
| 18 | + // returns a valid format webbundle. Then, a subresource fetch should be successful. |
| 19 | + // Otherwise, a subresource fetch should be rejected. |
| 20 | + |
| 21 | + window.TEST_WEB_BUNDLE_ELEMENT_TYPE = "script"; |
| 22 | + setup(() => { |
| 23 | + assert_true(HTMLScriptElement.supports("webbundle")); |
| 24 | + }); |
| 25 | + |
| 26 | + document.cookie = "milk=1; path=/"; |
| 27 | + |
| 28 | + // Make sure to set a cookie for a cross-origin domain from where a cross |
| 29 | + // origin bundle is served. |
| 30 | + const setCookiePromise = fetch( |
| 31 | + "https://{{domains[www1]}}:{{ports[https][0]}}/cookies/resources/set-cookie.py?name=milk&path=/web-bundle/resources/", |
| 32 | + { |
| 33 | + mode: "no-cors", |
| 34 | + credentials: "include", |
| 35 | + } |
| 36 | + ); |
| 37 | + |
| 38 | + const same_origin_bundle = "../resources/check-cookie-and-return-bundle.py"; |
| 39 | + const cross_origin_bundle = "https://{{domains[www1]}}:{{ports[https][0]}}/web-bundle/resources/check-cookie-and-return-bundle.py?bundle=cross-origin"; |
| 40 | + |
| 41 | + const same_origin_bundle_subresource = "../resources/wbn/root.js"; |
| 42 | + const cross_origin_bundle_subresource = "https://{{domains[www1]}}:{{ports[https][0]}}/web-bundle/resources/wbn/simple-cross-origin.txt"; |
| 43 | + |
| 44 | + async function assertSubresourceCanBeFetched() { |
| 45 | + const response = await fetch(same_origin_bundle_subresource); |
| 46 | + const text = await response.text(); |
| 47 | + assert_equals(text, "export * from './submodule.js';\n"); |
| 48 | + } |
| 49 | + |
| 50 | + async function assertCrossOriginSubresourceCanBeFetched() { |
| 51 | + const response = await fetch(cross_origin_bundle_subresource); |
| 52 | + const text = await response.text(); |
| 53 | + assert_equals(text, "hello from simple-cross-origin.txt"); |
| 54 | + } |
| 55 | + |
| 56 | + function createScriptWebBundle(credentials) { |
| 57 | + const options = {}; |
| 58 | + if (credentials) { |
| 59 | + options.credentials = credentials; |
| 60 | + } |
| 61 | + return createWebBundleElement(same_origin_bundle, [same_origin_bundle_subresource], options); |
| 62 | + } |
| 63 | + |
| 64 | + function createScriptWebBundleCrossOrigin(credentials) { |
| 65 | + const options = {}; |
| 66 | + if (credentials) { |
| 67 | + options.credentials = credentials; |
| 68 | + } |
| 69 | + return createWebBundleElement(cross_origin_bundle, [cross_origin_bundle_subresource], options); |
| 70 | + } |
| 71 | + |
| 72 | + promise_test(async (t) => { |
| 73 | + const script = createScriptWebBundle(); |
| 74 | + document.body.append(script); |
| 75 | + t.add_cleanup(() => script.remove()); |
| 76 | + |
| 77 | + await assertSubresourceCanBeFetched(); |
| 78 | + }, "The default should send a credential to a same origin bundle"); |
| 79 | + |
| 80 | + promise_test(async (t) => { |
| 81 | + const script = createScriptWebBundle("invalid"); |
| 82 | + document.body.append(script); |
| 83 | + t.add_cleanup(() => script.remove()); |
| 84 | + |
| 85 | + await assertSubresourceCanBeFetched(); |
| 86 | + }, "An invalid value should send a credential to a same origin bundle"); |
| 87 | + |
| 88 | + promise_test(async (t) => { |
| 89 | + const script = createScriptWebBundle("omit"); |
| 90 | + document.body.append(script); |
| 91 | + t.add_cleanup(() => script.remove()); |
| 92 | + |
| 93 | + return promise_rejects_js(t, TypeError, fetch(same_origin_bundle_subresource)) |
| 94 | + }, "'omit' should not send a credential to a same origin bundle"); |
| 95 | + |
| 96 | + promise_test(async (t) => { |
| 97 | + const script = createScriptWebBundle("same-origin"); |
| 98 | + document.body.append(script); |
| 99 | + t.add_cleanup(() => script.remove()); |
| 100 | + |
| 101 | + await assertSubresourceCanBeFetched(); |
| 102 | + }, "'same-origin' should send a credential to a same origin bundle"); |
| 103 | + |
| 104 | + promise_test(async (t) => { |
| 105 | + const script = createScriptWebBundle("include"); |
| 106 | + document.body.append(script); |
| 107 | + t.add_cleanup(() => script.remove()); |
| 108 | + |
| 109 | + await assertSubresourceCanBeFetched(); |
| 110 | + }, "'include' should send a credential to a same origin bundle"); |
| 111 | + |
| 112 | + promise_test(async (t) => { |
| 113 | + await setCookiePromise; |
| 114 | + |
| 115 | + const script = createScriptWebBundleCrossOrigin("omit"); |
| 116 | + document.body.append(script); |
| 117 | + t.add_cleanup(() => script.remove()); |
| 118 | + |
| 119 | + return promise_rejects_js(t, TypeError, fetch(cross_origin_bundle_subresource)) |
| 120 | + }, "'omit' should not send a credential to a cross origin bundle"); |
| 121 | + |
| 122 | + promise_test(async (t) => { |
| 123 | + await setCookiePromise; |
| 124 | + |
| 125 | + const script = createScriptWebBundleCrossOrigin("same-origin"); |
| 126 | + document.body.append(script); |
| 127 | + t.add_cleanup(() => script.remove()); |
| 128 | + |
| 129 | + return promise_rejects_js(t, TypeError, fetch(cross_origin_bundle_subresource)) |
| 130 | + }, "'same-origin' should not send a credential to a cross origin bundle"); |
| 131 | + |
| 132 | + promise_test(async (t) => { |
| 133 | + await setCookiePromise; |
| 134 | + |
| 135 | + const script = createScriptWebBundleCrossOrigin("include"); |
| 136 | + document.body.append(script); |
| 137 | + t.add_cleanup(() => script.remove()); |
| 138 | + |
| 139 | + await assertCrossOriginSubresourceCanBeFetched(); |
| 140 | + }, "'include' should send a credential to a cross origin bundle"); |
| 141 | + |
| 142 | + |
| 143 | + promise_test(async (t) => { |
| 144 | + const script = createScriptWebBundleCrossOrigin("invalid"); |
| 145 | + document.body.append(script); |
| 146 | + t.add_cleanup(() => script.remove()); |
| 147 | + |
| 148 | + return promise_rejects_js(t, TypeError, fetch(cross_origin_bundle_subresource)) |
| 149 | + }, "An invalid value should not send a credential to a cross origin bundle"); |
| 150 | + </script> |
| 151 | +</body> |
0 commit comments