summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/preload/subresource-integrity.html
blob: 58f59126edb566e65dd25274b8d713dff449ba85 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<!DOCTYPE html>
<meta charset=utf-8>
<title>Subresource Integrity</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/sriharness.js"></script>
<script src="/common/utils.js"></script>
<script src="/subresource-integrity/sri-test-helpers.sub.js"></script>
<script src="./resources/preload_helper.js"></script>


<div id="log"></div>

<div id="container"></div>
<script>
  // This is a list of information for each preload destination. The information
  // is used in a loop iterating over the below tests, so that each test is run
  // for each destination.
  const preload_destination_info = [
    {
      destination: 'script', ext: '.js', supports_sri: true,
      sha256: 'sha256-Bu681KMnQ15RYHFvsYdWumweeFAw0hJDTFt9seErghA=',
      sha384: 'sha384-cINXh+nCzEHPWzXS7eoT+vYMBpyqczOybRLNU3XAButFWCRhHT5hLByIbPRqIm2f',
      sha512: 'sha512-KZdenhzBd7X7Q/vmaOSyvFz1CGdoVt26xzCZjlkU9lfBEK+V/ougGys7iYDi0+tOHIQSQa87bIqx95R7GU7I9Q=='
    },
    {
      destination: 'style', ext: '.css', supports_sri: true,
      sha256: 'sha256-CzHgdJ7wOccM8L89n4bhcJMz3F+SPLT7YZk7gyCWUV4=',
      sha384: 'sha384-wDAWxH4tOWBwAwHfBn9B7XuNmFxHTMeigAMwn0iVQ0zq3FtmYMLxihcGnU64CwcX',
      sha512: 'sha512-9wXDjd6Wq3H6nPAhI9zOvG7mJkUr03MTxaO+8ztTKnfJif42laL93Be/IF6YYZHHF4esitVYxiwpY2HSZX4l6w=='
    },
    {
      destination: 'image', ext: '.png', supports_sri: false,
      sha256: 'sha256-h7rQ5CQooD7qmTmrNxykCgjz3lDM1CBl2hkY1CTpB2I=',
      sha384: 'sha384-DqrhF5pyW9u4FJsleRwjTAwKDSspQbxk9oux9BtcaANyji0kzpb7b4Cw3TM4MGNk',
      sha512: 'sha512-wyY+ChJ1B5ovayDkbBeEv7nuHJ0uws14KoLyFSLKngFzHzm6VaTNA/ndx/Lnt/vPx6BN1cJB7+JNa4aAUGOlgg=='
    },
    // TODO(domfarolino): Add more destinations.
  ];

  for (const info of preload_destination_info) {
    const {destination, ext, supports_sri, sha256, sha384, sha512} = info;

    // Preload + Subresource Integrity tests. These tests work by passing some
    // destination-specific information (defined in |preload_destination_info|)
    // to the below tests, which do the following:
    // Create a <link rel="preload"> for the given destination, with the
    // specified `integrity`. After this has either loaded or failed to load,
    // the subresource element corresponding to |destination| will be created,
    // attempting to re-use the preloaded resource. `integrity` may be specified
    // on the subresource elements that support SRI as well. The subresource
    // will either load or fail to load, and the result will be compared with an
    // expectation passed to the test.
    SRIPreloadTest(
        true,                                                   /* preload_sri_success */
        true,                                                   /* subresource_sri_success */
        `Same-origin ${destination} with correct sha256 hash.`, /* name */
        1,                                                      /* number_of_requests */
        destination,                                            /* destination */
        same_origin_prefix + destination + ext + `?${token()}`, /* resource_url (for preload + subresource) */
        {integrity: sha256},                                    /* link_attrs */
        {}                                                      /* subresource_attrs */
    )

    SRIPreloadTest(
        true,
        true,
        `Same-origin ${destination} with correct sha384 hash.`,
        1,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: sha384},
        {}
    )

    SRIPreloadTest(
        true,
        true,
        `Same-origin ${destination} with correct sha512 hash.`,
        1,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: sha512},
        {}
    )

    SRIPreloadTest(
        true,
        true,
        `Same-origin ${destination} with empty integrity.`,
        1,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {},
        {}
    )

    SRIPreloadTest(
        false,
        false,
        `Same-origin ${destination} with incorrect hash.`,
        1,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: "sha256-deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdead"},
        {}
    )

    SRIPreloadTest(
        true,
        true,
        `Same-origin ${destination} with multiple sha256 hashes, including correct.`,
        1,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: `${sha256} sha256-deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdead`},
        {}
    )

    SRIPreloadTest(
        true,
        true,
        `Same-origin ${destination} with multiple sha256 hashes, including unknown algorithm.`,
        1,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: `${sha256} foo666-deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdead`},
        {}
    )

    SRIPreloadTest(
        true,
        true,
        `Same-origin ${destination} with sha256 mismatch, sha512 match`,
        1,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: `${sha512} sha256-deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdead`},
        {}
    )

    SRIPreloadTest(
        false,
        false,
        `Same-origin ${destination} with sha256 match, sha512 mismatch`,
        1,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: `sha512-deadbeefspbnUnwooKGNNCb39nvg+EW0O9hDScTXeo/9pVZztLSUYU3LNV6H0lZapo8bCJUpyPPLAzE9fDzpxg== ${sha256}`},
        {}
    )

    SRIPreloadTest(
        true,
        true,
        `<crossorigin='anonymous'> ${destination} with correct hash, ACAO: *`,
        1,
        destination,
        xorigin_prefix + destination + ext + `?${token()}` + anonymous,
        {integrity: sha256, crossOrigin: 'anonymous'},
        {crossOrigin: "anonymous"}
    )

    SRIPreloadTest(
        false,
        false,
        `<crossorigin='anonymous'> ${destination} with incorrect hash, ACAO: *`,
        1,
        destination,
        xorigin_prefix + destination + ext + `?${token()}` + anonymous,
        {integrity: "sha256-sha256-deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdead", crossOrigin: "anonymous"},
        {crossOrigin: "anonymous"}
    )

    SRIPreloadTest(
        true,
        true,
        `<crossorigin='use-credentials'> ${destination} with correct hash, CORS-eligible`,
        1,
        destination,
        xorigin_prefix + destination + ext + `?${token()}` + use_credentials,
        {integrity: sha256, crossOrigin: "use-credentials"},
        {crossOrigin: "use-credentials"}
    )

    SRIPreloadTest(
        false,
        false,
        `<crossorigin='use-credentials'> ${destination} with incorrect hash CORS-eligible`,
        1,
        destination,
        xorigin_prefix + destination + ext + `?${token()}` + use_credentials,
        {integrity: "sha256-deadbeef2S+pTRZgiw3DWrhC6JLDlt2zRyGpwH7unU8=", crossOrigin: "use-credentials"},
        {crossOrigin: "use-credentials"}
    )

    SRIPreloadTest(
        false,
        false,
        `<crossorigin='anonymous'> ${destination} with CORS-ineligible resource`,
        1,
        destination,
        // not piping ACAO header makes this CORS-ineligible
        xorigin_prefix + destination + ext + `?${token()}`,
        {integrity: sha256, crossOrigin: "anonymous"},
        {crossOrigin: "anonymous"}
    )

    SRIPreloadTest(
        false,
        false,
        `Cross-origin ${destination}, not CORS request, with correct hash`,
        1,
        destination,
        xorigin_prefix + destination + ext + `?${token()}` + anonymous,
        {integrity: sha256},
        {}
    )

    SRIPreloadTest(
        false,
        false,
        `Cross-origin ${destination}, not CORS request, with hash mismatch`,
        1,
        destination,
        xorigin_prefix + destination + ext + `?${token()}` + anonymous,
        {integrity: "sha256-deadbeef01Y0yKSx3/UoIKtIY2UQ9+H8WGyyMuOWOC0="},
        {}
    )

    SRIPreloadTest(
        true,
        true,
        `Cross-origin ${destination}, empty integrity`,
        1,
        destination,
        xorigin_prefix + destination + ext + `?${token()}` + anonymous,
        {},
        {}
    )

    SRIPreloadTest(
        true,
        true,
        `Same-origin ${destination} with correct hash, options.`,
        1,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: `${sha256}?foo=bar?spam=eggs`},
        {}
    )

    SRIPreloadTest(
        true,
        true,
        `Same-origin ${destination} with unknown algorithm only.`,
        1,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: "foo666-8aBiAJl3ukQwSJ6eTs5wl6hGjnOtyXjcTRdAf89uIfY="},
        {}
    )

    // The below tests are specific to subresource destinations that support
    // SRI. See |supports_sri|.
    if (supports_sri) {

      SRIPreloadTest(
        true,
        true,
        `Same-origin ${destination} with matching digest re-uses preload with matching digest.`,
        1,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: sha256},
        {integrity: sha256}
      )

      SRIPreloadTest(
        true,
        true,
        `Same-origin ${destination} with matching digest re-uses preload with matching digest and options.`,
        1,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: `${sha256}?dummy-option=value`},
        {integrity: sha256}
      )

      SRIPreloadTest(
        true,
        false,
        `Same-origin ${destination} with non-matching digest does not re-use preload with matching digest.`,
        2,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: sha256},
        {integrity: "sha256-deadbeefQ15RYHFvsYdWumweeFAw0hJDTFt9seErghA="}
      )

      SRIPreloadTest(
        false,
        true,
        `Same-origin ${destination} with matching digest does not re-use preload with non-matching digest.`,
        2,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: "sha256-deadbeefQ15RYHFvsYdWumweeFAw0hJDTFt9seErghA="},
        {integrity: sha256}
      )

      SRIPreloadTest(
        false,
        false,
        `Same-origin ${destination} with non-matching digest does not re-use preload with non-matching digest.`,
        2,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: "sha256-deadbeefQ15RYHFvsYdWumweeFAw0hJDTFt9seErghA="},
        {integrity: "sha256-deaddeadbeefYHFvsYdWumweeFAw0hJDTFt9seErghA="}
      )

      SRIPreloadTest(
        true,
        true,
        `Same-origin ${destination} with matching digest does not reuse preload without digest.`,
        2,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {},
        {integrity: sha256}
      )

      SRIPreloadTest(
        true,
        true,
        `Same-origin ${destination} with matching digest does not reuse preload with matching but stronger digest.`,
        2,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: sha384},
        {integrity: sha256},
      )

      SRIPreloadTest(
        true,
        false,
        `Same-origin ${destination} with wrong digest does not reuse preload with correct and stronger digest.`,
        2,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: sha384},
        {integrity: "sha256-deadbeefQ15RYHFvsYdWumweeFAw0hJDTFt9seErghA="}
      )

      SRIPreloadTest(
        true,
        true,
        `Same-origin ${destination} with matching digest does not reuse preload with matching but weaker digest.`,
        2,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {integrity: sha256},
        {integrity: sha384},
      )

      SRIPreloadTest(
        true,
        false,
        `Same-origin ${destination} with non-matching digest reuses preload with no digest but fails.`,
        2,
        destination,
        same_origin_prefix + destination + ext + `?${token()}`,
        {},
        {integrity: "sha256-sha256-deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdead"},
      )

    } // if.

  } // for-of.
</script>