summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_E10SUtils_workers_remote_types.js
blob: 69fda1905521cf20c9e32334b2875eb391cadc03 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */

const { E10SUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/E10SUtils.sys.mjs"
);

const URI_SECURE_COM = Services.io.newURI("https://example.com");
const URI_SECURE_ORG = Services.io.newURI("https://example.org");
const URI_INSECURE_ORG = Services.io.newURI("http://example.org");
const URI_FILE = Services.io.newURI("file:///path/to/dir");
const URI_EXTENSION = Services.io.newURI("moz-extension://fake-uuid");
const URI_EXT_PROTOCOL = Services.io.newURI("ext+custom://fake-url");
const URI_WEB_PROTOCOL = Services.io.newURI("web+custom://fake-url");
const URI_PRIVILEGEDMOZILLA = Services.io.newURI("https://addons.mozilla.org");

const fakeContentScriptSandbox = Cu.Sandbox(
  ["https://example.org", "moz-extension://fake-uuid"],
  {}
);

const ssm = Services.scriptSecurityManager;
const systemPrincipal = ssm.getSystemPrincipal();
const nullPrincipal = ssm.createNullPrincipal({});
const principalSecureCom = ssm.createContentPrincipal(URI_SECURE_COM, {});
const principalSecureOrg = ssm.createContentPrincipal(URI_SECURE_ORG, {});
const principalInsecureOrg = ssm.createContentPrincipal(URI_INSECURE_ORG, {});
const principalFile = ssm.createContentPrincipal(URI_FILE, {});
const principalExtension = ssm.createContentPrincipal(URI_EXTENSION, {});
const principalExpanded = Cu.getObjectPrincipal(fakeContentScriptSandbox);
const principalExtProtocol = ssm.createContentPrincipal(URI_EXT_PROTOCOL, {});
const principalWebProtocol = ssm.createContentPrincipal(URI_WEB_PROTOCOL, {});
const principalPrivilegedMozilla = ssm.createContentPrincipal(
  URI_PRIVILEGEDMOZILLA,
  {}
);

const {
  EXTENSION_REMOTE_TYPE,
  FILE_REMOTE_TYPE,
  FISSION_WEB_REMOTE_TYPE,
  NOT_REMOTE,
  PRIVILEGEDABOUT_REMOTE_TYPE,
  PRIVILEGEDMOZILLA_REMOTE_TYPE,
  SERVICEWORKER_REMOTE_TYPE,
  WEB_REMOTE_COOP_COEP_TYPE_PREFIX,
  WEB_REMOTE_TYPE,
} = E10SUtils;

const { REMOTE_WORKER_TYPE_SHARED, REMOTE_WORKER_TYPE_SERVICE } =
  Ci.nsIE10SUtils;

// Test ServiceWorker remoteType selection with multiprocess and/or site
// isolation enabled.
add_task(function test_get_remote_type_for_service_worker() {
  // ServiceWorkers with system or null principal are unexpected and we expect
  // the method call to throw.
  for (const principal of [systemPrincipal, nullPrincipal]) {
    Assert.throws(
      () =>
        E10SUtils.getRemoteTypeForWorkerPrincipal(
          principal,
          REMOTE_WORKER_TYPE_SERVICE,
          true,
          false
        ),
      /Unexpected system or null principal/,
      `Did raise an exception on "${principal.origin}" principal ServiceWorker`
    );
  }

  // ServiceWorker test cases:
  // - e10s + fission disabled:
  //   - extension principal + any preferred remote type => extension remote type
  //   - content principal + any preferred remote type => web remote type
  // - fission enabled:
  //   - extension principal + any preferred remote type => extension remote type
  //   - content principal + any preferred remote type => webServiceWorker=siteOrigin remote type
  function* getTestCase(fission = false) {
    const TEST_PRINCIPALS = [
      principalSecureCom,
      principalSecureOrg,
      principalExtension,
    ];

    const PREFERRED_REMOTE_TYPES = [
      E10SUtils.DEFAULT_REMOTE_TYPE,
      E10SUtils.WEB_REMOTE_TYPE,
      "fakeRemoteType",
    ];

    for (const principal of TEST_PRINCIPALS) {
      for (const preferred of PREFERRED_REMOTE_TYPES) {
        const msg = `ServiceWorker, principal=${
          principal.origin
        }, preferredRemoteType=${preferred}, ${fission ? "fission" : "e10s"}`;
        yield [
          msg,
          principal,
          REMOTE_WORKER_TYPE_SERVICE,
          true,
          fission,
          preferred,
        ];
      }
    }
  }

  // Test cases for e10s mode + fission disabled.
  for (const testCase of getTestCase(false)) {
    const [msg, principal, ...args] = testCase;
    let expected = E10SUtils.WEB_REMOTE_TYPE;

    if (principal == principalExtension) {
      expected = WebExtensionPolicy.useRemoteWebExtensions
        ? E10SUtils.EXTENSION_REMOTE_TYPE
        : E10SUtils.NOT_REMOTE;
    }

    equal(
      E10SUtils.getRemoteTypeForWorkerPrincipal(principal, ...args),
      expected,
      msg
    );
  }

  // Test cases for e10s mode + fission enabled.
  for (const testCase of getTestCase(true)) {
    const [msg, principal, ...args] = testCase;
    let expected = `${SERVICEWORKER_REMOTE_TYPE}=${principal.siteOrigin}`;

    if (principal == principalExtension) {
      expected = WebExtensionPolicy.useRemoteWebExtensions
        ? E10SUtils.EXTENSION_REMOTE_TYPE
        : E10SUtils.NOT_REMOTE;
    }

    equal(
      E10SUtils.getRemoteTypeForWorkerPrincipal(principal, ...args),
      expected,
      msg
    );
  }
});

// Test SharedWorker remoteType selection with multiprocess and/or site
// isolation enabled.
add_task(function test_get_remote_type_for_shared_worker() {
  // Verify that for shared worker registered from a web coop+coep remote type
  // we are going to select a web or fission remote type.
  for (const [principal, preferredRemoteType] of [
    [
      principalSecureCom,
      `${WEB_REMOTE_COOP_COEP_TYPE_PREFIX}=${principalSecureCom.siteOrigin}`,
    ],
  ]) {
    equal(
      E10SUtils.getRemoteTypeForWorkerPrincipal(
        principal,
        REMOTE_WORKER_TYPE_SHARED,
        true,
        false,
        preferredRemoteType
      ),
      WEB_REMOTE_TYPE,
      `Got WEB_REMOTE_TYPE on preferred ${preferredRemoteType} and fission disabled`
    );

    equal(
      E10SUtils.getRemoteTypeForWorkerPrincipal(
        principal,
        REMOTE_WORKER_TYPE_SHARED,
        true,
        true,
        preferredRemoteType
      ),
      `${FISSION_WEB_REMOTE_TYPE}=${principal.siteOrigin}`,
      `Got WEB_REMOTE_TYPE on preferred ${preferredRemoteType} and fission enabled`
    );
  }

  // For System principal shared worker we do select NOT_REMOTE or the preferred
  // remote type if is one of the explicitly allowed (NOT_REMOTE and
  // PRIVILEGEDABOUT_REMOTE_TYPE).
  for (const [principal, preferredRemoteType] of [
    [systemPrincipal, NOT_REMOTE],
    [systemPrincipal, PRIVILEGEDABOUT_REMOTE_TYPE],
  ]) {
    equal(
      E10SUtils.getRemoteTypeForWorkerPrincipal(
        principal,
        REMOTE_WORKER_TYPE_SHARED,
        true,
        true,
        preferredRemoteType
      ),
      preferredRemoteType,
      `Selected the preferred ${preferredRemoteType} on system principal shared worker`
    );
  }

  Assert.throws(
    () =>
      E10SUtils.getRemoteTypeForWorkerPrincipal(
        systemPrincipal,
        REMOTE_WORKER_TYPE_SHARED,
        true,
        true,
        "fakeRemoteType"
      ),
    /Failed to get a remoteType/,
    "Does fail explicitly on system worker for arbitrary preferredRemoteType"
  );

  // Behavior NOT_REMOTE preferredRemoteType for content principals with
  // multiprocess enabled.
  for (const [principal, expectedRemoteType] of [
    [
      principalSecureCom,
      `${FISSION_WEB_REMOTE_TYPE}=${principalSecureCom.siteOrigin}`,
    ],
    [
      principalExtension,
      WebExtensionPolicy.useRemoteWebExtensions
        ? EXTENSION_REMOTE_TYPE
        : NOT_REMOTE,
    ],
  ]) {
    equal(
      E10SUtils.getRemoteTypeForWorkerPrincipal(
        principal,
        REMOTE_WORKER_TYPE_SHARED,
        true,
        true,
        NOT_REMOTE
      ),
      expectedRemoteType,
      `Got ${expectedRemoteType} for content principal ${principal.siteOrigin}`
    );
  }

  // Shared worker registered for web+custom urls.
  for (const [preferredRemoteType, expectedRemoteType] of [
    [WEB_REMOTE_TYPE, WEB_REMOTE_TYPE],
    ["fakeRemoteType", "fakeRemoteType"],
    // This seems to be actually failing with a SecurityError
    // as soon as the SharedWorker constructor is being called with
    // a web+...:// url from an extension principal:
    //
    // [EXTENSION_REMOTE_TYPE, EXTENSION_REMOTE_TYPE],
  ]) {
    equal(
      E10SUtils.getRemoteTypeForWorkerPrincipal(
        principalWebProtocol,
        REMOTE_WORKER_TYPE_SHARED,
        true,
        true,
        preferredRemoteType
      ),
      expectedRemoteType,
      "Selected expected process for web+custom:// shared worker"
    );
  }

  // Shared worker registered for ext+custom urls.
  for (const [preferredRemoteType, expectedRemoteType] of [
    [WEB_REMOTE_TYPE, WEB_REMOTE_TYPE],
    ["fakeRemoteType", "fakeRemoteType"],
    // This seems to be actually prevented by failing a ClientIsValidPrincipalInfo
    // check (but only when the remote worker is being launched in the child process
    // and so after a remote Type has been selected).
    // [EXTENSION_REMOTE_TYPE, EXTENSION_REMOTE_TYPE],
  ]) {
    equal(
      E10SUtils.getRemoteTypeForWorkerPrincipal(
        principalExtProtocol,
        REMOTE_WORKER_TYPE_SHARED,
        true,
        true,
        preferredRemoteType
      ),
      expectedRemoteType,
      "Selected expected process for ext+custom:// shared worker"
    );
  }

  // Shared worker with a file principal.
  // NOTE: on android useSeparateFileUriProcess will be false and file uri are
  // going to run in the default web process.
  const expectedFileRemoteType = Services.prefs.getBoolPref(
    "browser.tabs.remote.separateFileUriProcess",
    false
  )
    ? FILE_REMOTE_TYPE
    : WEB_REMOTE_TYPE;

  for (const [preferredRemoteType, expectedRemoteType] of [
    [WEB_REMOTE_TYPE, expectedFileRemoteType],
    ["fakeRemoteType", expectedFileRemoteType],
  ]) {
    equal(
      E10SUtils.getRemoteTypeForWorkerPrincipal(
        principalFile,
        REMOTE_WORKER_TYPE_SHARED,
        true,
        true,
        preferredRemoteType
      ),
      expectedRemoteType,
      "Got expected remote type on file principal shared worker"
    );
  }

  // Shared worker related to a privilegedmozilla domain.
  // NOTE: separatePrivilegedMozilla will be false on android builds.
  const usePrivilegedMozilla = Services.prefs.getBoolPref(
    "browser.tabs.remote.separatePrivilegedMozillaWebContentProcess",
    false
  );
  const expectedRemoteType = usePrivilegedMozilla
    ? PRIVILEGEDMOZILLA_REMOTE_TYPE
    : `${FISSION_WEB_REMOTE_TYPE}=https://mozilla.org`;
  for (const preferredRemoteType of [
    PRIVILEGEDMOZILLA_REMOTE_TYPE,
    "fakeRemoteType",
  ]) {
    equal(
      E10SUtils.getRemoteTypeForWorkerPrincipal(
        principalPrivilegedMozilla,
        REMOTE_WORKER_TYPE_SHARED,
        true,
        true,
        preferredRemoteType
      ),
      expectedRemoteType,
      "Got expected remote type on privilegedmozilla principal shared worker"
    );
  }
});

// Test that we do throw on expanded principals.
add_task(function test_get_remote_type_throws_on_expanded_principals() {
  for (const workerType of [
    REMOTE_WORKER_TYPE_SHARED,
    REMOTE_WORKER_TYPE_SERVICE,
  ]) {
    Assert.throws(
      () =>
        E10SUtils.getRemoteTypeForWorkerPrincipal(
          principalExpanded,
          workerType,
          true,
          false
        ),
      /Unexpected expanded principal/,
      "Did raise an exception as expected"
    );
  }
});

// Test that NO_REMOTE is the remote type selected when multiprocess is disabled,
// there is no other checks special behaviors on particular principal or worker type.
add_task(function test_get_remote_type_multiprocess_disabled() {
  function* getTestCase() {
    const TEST_PRINCIPALS = [
      systemPrincipal,
      nullPrincipal,
      principalSecureCom,
      principalSecureOrg,
      principalInsecureOrg,
      principalFile,
      principalExtension,
    ];

    const PREFERRED_REMOTE_TYPES = [
      E10SUtils.DEFAULT_REMOTE_TYPE,
      E10SUtils.WEB_REMOTE_TYPE,
      "fakeRemoteType",
    ];

    for (const principal of TEST_PRINCIPALS) {
      for (const preferred of PREFERRED_REMOTE_TYPES) {
        const msg = `SharedWorker, principal=${principal.origin}, preferredRemoteType=${preferred}`;
        yield [
          msg,
          principal,
          REMOTE_WORKER_TYPE_SHARED,
          false,
          false,
          preferred,
        ];
      }
    }

    for (const principal of TEST_PRINCIPALS) {
      // system and null principals are disallowed for service workers, we throw
      // if passed to the E10SUtils method and we cover this scenario with a
      // separate test.
      if (principal.isSystemPrincipal || principal.isNullPrincipal) {
        continue;
      }
      for (const preferred of PREFERRED_REMOTE_TYPES) {
        const msg = `ServiceWorker with principal ${principal.origin} and preferredRemoteType ${preferred}`;
        yield [
          msg,
          principal,
          REMOTE_WORKER_TYPE_SERVICE,
          false,
          false,
          preferred,
        ];
      }
    }
  }

  for (const testCase of getTestCase()) {
    const [msg, ...args] = testCase;
    equal(
      E10SUtils.getRemoteTypeForWorkerPrincipal(...args),
      E10SUtils.NOT_REMOTE,
      `Expect NOT_REMOTE on disabled multiprocess: ${msg}`
    );
  }
});