summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/web-bundle/subresource-loading/credentials.https.tentative.sub.html
blob: 37efc37e6d722e38f082ac3e2373f48cee4cc6e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<title>Credentials in WebBundle subresource loading</title>
<link
  rel="help"
  href="https://github.com/WICG/webpackage/blob/main/explainers/subresource-loading.md#requests-mode-and-credentials-mode"
/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/test-helpers.js"></script>
<body>
  <script>
    // In this wpt, we test a request's credential mode, which controls
    // whether UA sends a credential or not to fetch a bundle.

    // If UA sends a credential, check-cookie-and-return-{cross-oriigin}-bundle.py
    // returns a valid format webbundle. Then, a subresource fetch should be successful.
    // Otherwise, a subresource fetch should be rejected.

    setup(() => {
      assert_true(HTMLScriptElement.supports("webbundle"));
    });

    document.cookie = "milk=1; path=/";

    // Make sure to set a cookie for a cross-origin domain from where a cross
    // origin bundle is served.
    const setCookiePromise = fetch(
      "https://{{domains[www1]}}:{{ports[https][0]}}/cookies/resources/set-cookie.py?name=milk&path=/web-bundle/resources/",
      {
        mode: "no-cors",
        credentials: "include",
      }
    );

    const same_origin_bundle = "../resources/check-cookie-and-return-bundle.py";
    const cross_origin_bundle =
      "https://{{domains[www1]}}:{{ports[https][0]}}/web-bundle/resources/check-cookie-and-return-bundle.py?bundle=cross-origin";

    const same_origin_bundle_subresource = "../resources/wbn/root.js";
    const cross_origin_bundle_subresource =
      "https://{{domains[www1]}}:{{ports[https][0]}}/web-bundle/resources/wbn/simple-cross-origin.txt";

    async function assertSubresourceCanBeFetched() {
      const response = await fetch(same_origin_bundle_subresource);
      const text = await response.text();
      assert_equals(text, "export * from './submodule.js';\n");
    }

    async function assertCrossOriginSubresourceCanBeFetched() {
      const response = await fetch(cross_origin_bundle_subresource);
      const text = await response.text();
      assert_equals(text, "hello from simple-cross-origin.txt");
    }

    function createScriptWebBundle(credentials) {
      const options = {};
      if (credentials) {
        options.credentials = credentials;
      }
      return createWebBundleElement(
        same_origin_bundle,
        [same_origin_bundle_subresource],
        options
      );
    }

    function createScriptWebBundleCrossOrigin(credentials) {
      const options = {};
      if (credentials) {
        options.credentials = credentials;
      }
      return createWebBundleElement(
        cross_origin_bundle,
        [cross_origin_bundle_subresource],
        options
      );
    }

    promise_test(async (t) => {
      const script = createScriptWebBundle();
      document.body.append(script);
      t.add_cleanup(() => script.remove());

      await assertSubresourceCanBeFetched();
    }, "The default should send a credential to a same origin bundle");

    promise_test(async (t) => {
      const script = createScriptWebBundle("invalid");
      document.body.append(script);
      t.add_cleanup(() => script.remove());

      await assertSubresourceCanBeFetched();
    }, "An invalid value should send a credential to a same origin bundle");

    promise_test(async (t) => {
      const script = createScriptWebBundle("omit");
      document.body.append(script);
      t.add_cleanup(() => script.remove());

      return promise_rejects_js(
        t,
        TypeError,
        fetch(same_origin_bundle_subresource)
      );
    }, "'omit' should not send a credential to a same origin bundle");

    promise_test(async (t) => {
      const script = createScriptWebBundle("same-origin");
      document.body.append(script);
      t.add_cleanup(() => script.remove());

      await assertSubresourceCanBeFetched();
    }, "'same-origin' should send a credential to a same origin bundle");

    promise_test(async (t) => {
      const script = createScriptWebBundle("include");
      document.body.append(script);
      t.add_cleanup(() => script.remove());

      await assertSubresourceCanBeFetched();
    }, "'include' should send a credential to a same origin bundle");

    promise_test(async (t) => {
      await setCookiePromise;

      const script = createScriptWebBundleCrossOrigin("omit");
      document.body.append(script);
      t.add_cleanup(() => script.remove());

      return promise_rejects_js(
        t,
        TypeError,
        fetch(cross_origin_bundle_subresource)
      );
    }, "'omit' should not send a credential to a cross origin bundle");

    promise_test(async (t) => {
      await setCookiePromise;

      const script = createScriptWebBundleCrossOrigin("same-origin");
      document.body.append(script);
      t.add_cleanup(() => script.remove());

      return promise_rejects_js(
        t,
        TypeError,
        fetch(cross_origin_bundle_subresource)
      );
    }, "'same-origin' should not send a credential to a cross origin bundle");

    promise_test(async (t) => {
      await setCookiePromise;

      const script = createScriptWebBundleCrossOrigin("include");
      document.body.append(script);
      t.add_cleanup(() => script.remove());

      await assertCrossOriginSubresourceCanBeFetched();
    }, "'include' should send a credential to a cross origin bundle");

    promise_test(async (t) => {
      const script = createScriptWebBundleCrossOrigin("invalid");
      document.body.append(script);
      t.add_cleanup(() => script.remove());

      return promise_rejects_js(
        t,
        TypeError,
        fetch(cross_origin_bundle_subresource)
      );
    }, "An invalid value should not send a credential to a cross origin bundle");
  </script>
</body>