summaryrefslogtreecommitdiffstats
path: root/dom/credentialmanagement/tests/browser/browser_active_document.js
blob: eced461630af36cf5612f131e838713abfa0e465 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const TEST_URL = "https://example.com/";

function arrivingHereIsBad(aResult) {
  ok(false, "Bad result! Received a: " + aResult);
}

function expectNotAllowedError(aResult) {
  let expected = "NotAllowedError";
  is(aResult.slice(0, expected.length), expected, `Expecting a ${expected}`);
}

function promiseMakeCredential(tab) {
  return ContentTask.spawn(tab.linkedBrowser, null, async function () {
    const cose_alg_ECDSA_w_SHA256 = -7;

    let publicKey = {
      rp: { id: content.document.domain, name: "none", icon: "none" },
      user: {
        id: new Uint8Array(),
        name: "none",
        icon: "none",
        displayName: "none",
      },
      challenge: content.crypto.getRandomValues(new Uint8Array(16)),
      timeout: 5000, // the minimum timeout is actually 15 seconds
      pubKeyCredParams: [{ type: "public-key", alg: cose_alg_ECDSA_w_SHA256 }],
    };

    return content.navigator.credentials.create({ publicKey });
  });
}

function promiseGetAssertion(tab) {
  return ContentTask.spawn(tab.linkedBrowser, null, async function () {
    let newCredential = {
      type: "public-key",
      id: content.crypto.getRandomValues(new Uint8Array(16)),
      transports: ["usb"],
    };

    let publicKey = {
      challenge: content.crypto.getRandomValues(new Uint8Array(16)),
      timeout: 5000, // the minimum timeout is actually 15 seconds
      rpId: content.document.domain,
      allowCredentials: [newCredential],
    };

    return content.navigator.credentials.get({ publicKey });
  });
}

add_task(async function test_setup() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["security.webauth.webauthn", true],
      ["security.webauth.webauthn_enable_softtoken", true],
      ["security.webauth.webauthn_enable_usbtoken", false],
    ],
  });
});

add_task(async function test_background_tab() {
  // Open two tabs, the last one will selected.
  let tab_bg = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);
  let tab_fg = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);

  // Requests from background tabs must fail.
  await promiseMakeCredential(tab_bg)
    .then(arrivingHereIsBad)
    .catch(expectNotAllowedError);

  // Requests from background tabs must fail.
  await promiseGetAssertion(tab_bg)
    .then(arrivingHereIsBad)
    .catch(expectNotAllowedError);

  // Close tabs.
  await BrowserTestUtils.removeTab(tab_bg);
  await BrowserTestUtils.removeTab(tab_fg);
});

add_task(async function test_background_window() {
  // Open a tab, then a new window.
  let tab_bg = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);
  let win = await BrowserTestUtils.openNewBrowserWindow();

  // Wait until the new window is really focused.
  await new Promise(resolve => SimpleTest.waitForFocus(resolve, win));

  // Requests from selected tabs not in the active window must fail.
  await promiseMakeCredential(tab_bg)
    .then(arrivingHereIsBad)
    .catch(expectNotAllowedError);

  // Requests from selected tabs not in the active window must fail.
  await promiseGetAssertion(tab_bg)
    .then(arrivingHereIsBad)
    .catch(expectNotAllowedError);

  // Close tab and window.
  await BrowserTestUtils.closeWindow(win);
  await BrowserTestUtils.removeTab(tab_bg);
});

add_task(async function test_minimized() {
  // Minimizing windows doesn't supported in headless mode.
  if (Services.env.get("MOZ_HEADLESS")) {
    return;
  }

  // Open a window with a tab.
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);

  // Minimize the window.
  window.minimize();
  await TestUtils.waitForCondition(() => !tab.linkedBrowser.docShellIsActive);

  // Requests from minimized windows must fail.
  await promiseMakeCredential(tab)
    .then(arrivingHereIsBad)
    .catch(expectNotAllowedError);

  // Requests from minimized windows must fail.
  await promiseGetAssertion(tab)
    .then(arrivingHereIsBad)
    .catch(expectNotAllowedError);

  // Restore the window.
  await new Promise(resolve => SimpleTest.waitForFocus(resolve, window));

  // Close tab.
  await BrowserTestUtils.removeTab(tab);
});