summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/private-network-access/mixed-content-fetch.tentative.https.window.js
blob: dbae5193b5cb02e3b0eba3531483aac372ba8700 (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
// META: script=/common/utils.js
// META: script=resources/support.sub.js
// META: timeout=long
//
// Spec: https://wicg.github.io/private-network-access
//
// These tests verify that secure contexts can fetch non-secure subresources
// from more private address spaces, avoiding mixed context checks, as long as
// they specify a valid `targetAddressSpace` fetch option that matches the
// target server's address space.

setup(() => {
  // Making sure we are in a secure context, as expected.
  assert_true(window.isSecureContext);
});

// Given `addressSpace`, returns the other three possible IP address spaces.
function otherAddressSpaces(addressSpace) {
  switch (addressSpace) {
    case "local": return ["unknown", "private", "public"];
    case "private": return ["unknown", "local", "public"];
    case "public": return ["unknown", "local", "private"];
  }
}

// Generates tests of `targetAddressSpace` for the given (source, target)
// address space pair, expecting fetches to succeed iff `targetAddressSpace` is
// correct.
//
// Scenarios exercised:
//
//  - cors mode:
//    - missing targetAddressSpace option
//    - incorrect targetAddressSpace option (x3, see `otherAddressSpaces()`)
//    - failed preflight
//    - success
//    - success with PUT method (non-"simple" request)
//  - no-cors mode:
//    - success
//
function makeTests({ source, target }) {
  const sourceServer = Server.get("https", source);
  const targetServer = Server.get("http", target);

  const makeTest = ({
    fetchOptions,
    targetBehavior,
    name,
    expected
  }) => {
    promise_test_parallel(t => fetchTest(t, {
      source: { server: sourceServer },
      target: {
        server: targetServer,
        behavior: targetBehavior,
      },
      fetchOptions,
      expected,
    }), `${sourceServer.name} to ${targetServer.name}: ${name}.`);
  };

  makeTest({
    name: "missing targetAddressSpace",
    targetBehavior: {
      preflight: PreflightBehavior.success(token()),
      response: ResponseBehavior.allowCrossOrigin(),
    },
    expected: FetchTestResult.FAILURE,
  });

  const correctAddressSpace = targetServer.addressSpace;

  for (const targetAddressSpace of otherAddressSpaces(correctAddressSpace)) {
    makeTest({
      name: `wrong targetAddressSpace "${targetAddressSpace}"`,
      targetBehavior: {
        preflight: PreflightBehavior.success(token()),
        response: ResponseBehavior.allowCrossOrigin(),
      },
      fetchOptions: { targetAddressSpace },
      expected: FetchTestResult.FAILURE,
    });
  }

  makeTest({
    name: "failed preflight",
    targetBehavior: {
      preflight: PreflightBehavior.failure(),
      response: ResponseBehavior.allowCrossOrigin(),
    },
    fetchOptions: { targetAddressSpace: correctAddressSpace },
    expected: FetchTestResult.FAILURE,
  });

  makeTest({
    name: "success",
    targetBehavior: {
      preflight: PreflightBehavior.success(token()),
      response: ResponseBehavior.allowCrossOrigin(),
    },
    fetchOptions: { targetAddressSpace: correctAddressSpace },
    expected: FetchTestResult.SUCCESS,
  });

  makeTest({
    name: "PUT success",
    targetBehavior: {
      preflight: PreflightBehavior.success(token()),
      response: ResponseBehavior.allowCrossOrigin(),
    },
    fetchOptions: {
      targetAddressSpace: correctAddressSpace,
      method: "PUT",
    },
    expected: FetchTestResult.SUCCESS,
  });

  makeTest({
    name: "no-cors success",
    targetBehavior: {
      preflight: PreflightBehavior.success(token()),
      response: ResponseBehavior.allowCrossOrigin(),
    },
    fetchOptions: {
      targetAddressSpace: correctAddressSpace,
      mode: "no-cors",
    },
    expected: FetchTestResult.OPAQUE,
  });
}

// Generates tests for the given (source, target) address space pair expecting
// that `targetAddressSpace` cannot be used to bypass mixed content.
//
// Scenarios exercised:
//
// - wrong `targetAddressSpace` (x3, see `otherAddressSpaces()`)
// - correct `targetAddressSpace`
//
function makeNoBypassTests({ source, target }) {
  const sourceServer = Server.get("https", source);
  const targetServer = Server.get("http", target);

  const prefix = `${sourceServer.name} to ${targetServer.name}: `;

  const correctAddressSpace = targetServer.addressSpace;
  for (const targetAddressSpace of otherAddressSpaces(correctAddressSpace)) {
    promise_test_parallel(t => fetchTest(t, {
      source: { server: sourceServer },
      target: {
        server: targetServer,
        behavior: {
          preflight: PreflightBehavior.success(token()),
          response: ResponseBehavior.allowCrossOrigin(),
        },
      },
      fetchOptions: { targetAddressSpace },
      expected: FetchTestResult.FAILURE,
    }), prefix + `wrong targetAddressSpace "${targetAddressSpace}".`);
  }

  promise_test_parallel(t => fetchTest(t, {
    source: { server: sourceServer },
    target: {
      server: targetServer,
      behavior: {
        preflight: PreflightBehavior.success(token()),
        response: ResponseBehavior.allowCrossOrigin(),
      },
    },
    fetchOptions: { targetAddressSpace: correctAddressSpace },
    expected: FetchTestResult.FAILURE,
  }), prefix + 'not a private network request.');
}

// Source: local secure context.
//
// Fetches to the local and private address spaces cannot use
// `targetAddressSpace` to bypass mixed content, as they are not otherwise
// blocked by Private Network Access.

makeNoBypassTests({ source: "local", target: "local" });
makeNoBypassTests({ source: "local", target: "private" });
makeNoBypassTests({ source: "local", target: "public" });

// Source: private secure context.
//
// Fetches to the local address space requires the right `targetAddressSpace`
// option, as well as a successful preflight response carrying a PNA-specific
// header.
//
// Fetches to the private address space cannot use `targetAddressSpace` to
// bypass mixed content, as they are not otherwise blocked by Private Network
// Access.

makeTests({ source: "private", target: "local" });

makeNoBypassTests({ source: "private", target: "private" });
makeNoBypassTests({ source: "private", target: "public" });

// Source: public secure context.
//
// Fetches to the local and private address spaces require the right
// `targetAddressSpace` option, as well as a successful preflight response
// carrying a PNA-specific header.

makeTests({ source: "public", target: "local" });
makeTests({ source: "public", target: "private" });

makeNoBypassTests({ source: "public", target: "public" });

// These tests verify that documents fetched from the `local` address space yet
// carrying the `treat-as-public-address` CSP directive are treated as if they
// had been fetched from the `public` address space.

promise_test_parallel(t => fetchTest(t, {
  source: {
    server: Server.HTTPS_LOCAL,
    treatAsPublic: true,
  },
  target: {
    server: Server.HTTP_LOCAL,
    behavior: {
      preflight: PreflightBehavior.optionalSuccess(token()),
      response: ResponseBehavior.allowCrossOrigin(),
    },
  },
  fetchOptions: { targetAddressSpace: "private" },
  expected: FetchTestResult.FAILURE,
}), 'https-treat-as-public to http-local: wrong targetAddressSpace "private".');

promise_test_parallel(t => fetchTest(t, {
  source: {
    server: Server.HTTPS_LOCAL,
    treatAsPublic: true,
  },
  target: {
    server: Server.HTTP_LOCAL,
    behavior: {
      preflight: PreflightBehavior.optionalSuccess(token()),
      response: ResponseBehavior.allowCrossOrigin(),
    },
  },
  fetchOptions: { targetAddressSpace: "local" },
  expected: FetchTestResult.SUCCESS,
}), "https-treat-as-public to http-local: success.");

promise_test_parallel(t => fetchTest(t, {
  source: {
    server: Server.HTTPS_LOCAL,
    treatAsPublic: true,
  },
  target: {
    server: Server.HTTP_PRIVATE,
    behavior: {
      preflight: PreflightBehavior.success(token()),
      response: ResponseBehavior.allowCrossOrigin(),
    },
  },
  fetchOptions: { targetAddressSpace: "local" },
  expected: FetchTestResult.FAILURE,
}), 'https-treat-as-public to http-private: wrong targetAddressSpace "local".');

promise_test_parallel(t => fetchTest(t, {
  source: {
    server: Server.HTTPS_LOCAL,
    treatAsPublic: true,
  },
  target: {
    server: Server.HTTP_PRIVATE,
    behavior: {
      preflight: PreflightBehavior.success(token()),
      response: ResponseBehavior.allowCrossOrigin(),
    },
  },
  fetchOptions: { targetAddressSpace: "private" },
  expected: FetchTestResult.SUCCESS,
}), "https-treat-as-public to http-private: success.");