summaryrefslogtreecommitdiffstats
path: root/dom/webauthn/tests/browser/browser_abort_visibility.js
blob: e462c5f1ad85718084e0303d0d80d1799c4a7620 (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
/* 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/browser/dom/webauthn/tests/browser/tab_webauthn_result.html";

add_task(async function test_setup() {
  return SpecialPowers.pushPrefEnv({
    set: [
      ["security.webauth.webauthn_enable_softtoken", false],
      ["security.webauth.webauthn_enable_usbtoken", true],
    ],
  });
});
add_task(test_switch_tab);
add_task(test_new_window_make);
add_task(test_new_window_get);
add_task(test_minimize_make);
add_task(test_minimize_get);

async function assertStatus(tab, expected) {
  let actual = await SpecialPowers.spawn(
    tab.linkedBrowser,
    [],
    async function () {
      info("visbility state: " + content.document.visibilityState);
      info("active: " + content.browsingContext.isActive);
      return content.document.getElementById("status").value;
    }
  );
  is(actual, expected, "webauthn request " + expected);
}

async function waitForStatus(tab, expected) {
  /* eslint-disable no-shadow */
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [[expected]],
    async function (expected) {
      return ContentTaskUtils.waitForCondition(() => {
        info(
          "expecting " +
            expected +
            ", visbility state: " +
            content.document.visibilityState
        );
        info(
          "expecting " +
            expected +
            ", active: " +
            content.browsingContext.isActive
        );
        return content.document.getElementById("status").value == expected;
      });
    }
  );
  /* eslint-enable no-shadow */

  await assertStatus(tab, expected);
}

function startMakeCredentialRequest(tab) {
  return SpecialPowers.spawn(tab.linkedBrowser, [], 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 }],
    };

    let status = content.document.getElementById("status");

    info(
      "Attempting to create credential for origin: " +
        content.document.nodePrincipal.origin
    );
    content.navigator.credentials
      .create({ publicKey })
      .then(() => {
        status.value = "completed";
      })
      .catch(() => {
        status.value = "aborted";
      });

    status.value = "pending";
  });
}

function startGetAssertionRequest(tab) {
  return SpecialPowers.spawn(tab.linkedBrowser, [], 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],
    };

    let status = content.document.getElementById("status");

    info(
      "Attempting to get credential for origin: " +
        content.document.nodePrincipal.origin
    );
    content.navigator.credentials
      .get({ publicKey })
      .then(() => {
        status.value = "completed";
      })
      .catch(ex => {
        info("aborted: " + ex);
        status.value = "aborted";
      });

    status.value = "pending";
  });
}

// Test that MakeCredential() and GetAssertion() requests
// are aborted when the current tab loses its focus.
async function test_switch_tab() {
  // Create a new tab for the MakeCredential() request.
  let tab_create = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    TEST_URL
  );

  // Start the request.
  await startMakeCredentialRequest(tab_create);
  await assertStatus(tab_create, "pending");

  // Open another tab and switch to it. The first will lose focus.
  let tab_get = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);
  await assertStatus(tab_create, "pending");

  // Start a GetAssertion() request in the second tab, the first is aborted
  await startGetAssertionRequest(tab_get);
  await waitForStatus(tab_create, "aborted");
  await assertStatus(tab_get, "pending");

  // Start a second request in the second tab. It should abort.
  await startGetAssertionRequest(tab_get);
  await waitForStatus(tab_get, "aborted");

  // Close tabs.
  BrowserTestUtils.removeTab(tab_create);
  BrowserTestUtils.removeTab(tab_get);
}

function waitForWindowActive(win, active) {
  return Promise.all([
    BrowserTestUtils.waitForEvent(win, active ? "focus" : "blur"),
    BrowserTestUtils.waitForEvent(win, active ? "activate" : "deactivate"),
  ]);
}

async function test_new_window_make() {
  // Create a new tab for the MakeCredential() request.
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);

  // Start a MakeCredential request.
  await startMakeCredentialRequest(tab);
  await assertStatus(tab, "pending");

  let windowGonePromise = waitForWindowActive(window, false);
  // Open a new window. The tab will lose focus.
  let win = await BrowserTestUtils.openNewBrowserWindow();
  await windowGonePromise;
  await assertStatus(tab, "pending");

  let windowBackPromise = waitForWindowActive(window, true);
  await BrowserTestUtils.closeWindow(win);
  await windowBackPromise;

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

async function test_new_window_get() {
  // Create a new tab for the GetAssertion() request.
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, TEST_URL);

  // Start a GetAssertion request.
  await startGetAssertionRequest(tab);
  await assertStatus(tab, "pending");

  let windowGonePromise = waitForWindowActive(window, false);
  // Open a new window. The tab will lose focus.
  let win = await BrowserTestUtils.openNewBrowserWindow();
  await windowGonePromise;
  await assertStatus(tab, "pending");

  let windowBackPromise = waitForWindowActive(window, true);
  await BrowserTestUtils.closeWindow(win);
  await windowBackPromise;

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

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

  // Create a new window for the MakeCredential() request.
  let win = await BrowserTestUtils.openNewBrowserWindow();
  let tab = await BrowserTestUtils.openNewForegroundTab(win.gBrowser, TEST_URL);

  // Start a MakeCredential request.
  await startMakeCredentialRequest(tab);
  await assertStatus(tab, "pending");

  // Minimize the window.
  let windowGonePromise = waitForWindowActive(win, false);
  win.minimize();
  await assertStatus(tab, "pending");
  await windowGonePromise;

  // Restore the window.
  await new Promise(resolve => SimpleTest.waitForFocus(resolve, win));
  await assertStatus(tab, "pending");

  // Close window and wait for main window to be focused again.
  let windowBackPromise = waitForWindowActive(window, true);
  await BrowserTestUtils.closeWindow(win);
  await windowBackPromise;
}

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

  // Create a new window for the GetAssertion() request.
  let win = await BrowserTestUtils.openNewBrowserWindow();
  let tab = await BrowserTestUtils.openNewForegroundTab(win.gBrowser, TEST_URL);

  // Start a GetAssertion request.
  await startGetAssertionRequest(tab);
  await assertStatus(tab, "pending");

  // Minimize the window.
  let windowGonePromise = waitForWindowActive(win, false);
  win.minimize();
  await assertStatus(tab, "pending");
  await windowGonePromise;

  // Restore the window.
  await new Promise(resolve => SimpleTest.waitForFocus(resolve, win));
  await assertStatus(tab, "pending");

  // Close window and wait for main window to be focused again.
  let windowBackPromise = waitForWindowActive(window, true);
  await BrowserTestUtils.closeWindow(win);
  await windowBackPromise;
}