summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_basicAuth_rateLimit.js
blob: 1c12a8fec5705e0011e74f21d7243b69195d9024 (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
/* 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/. */

// This tests that the basic auth dialog can not be used for DOS attacks
// and that the protections are reset on user-initiated navigation/reload.

let promptModalType = Services.prefs.getIntPref("prompts.modalType.httpAuth");

function promiseAuthWindowShown() {
  return PromptTestUtils.handleNextPrompt(
    window,
    { modalType: promptModalType, promptType: "promptUserAndPass" },
    { buttonNumClick: 1 }
  );
}

add_task(async function test() {
  await BrowserTestUtils.withNewTab(
    "https://example.com",
    async function (browser) {
      let cancelDialogLimit = Services.prefs.getIntPref(
        "prompts.authentication_dialog_abuse_limit"
      );

      let authShown = promiseAuthWindowShown();
      let browserLoaded = BrowserTestUtils.browserLoaded(browser);
      BrowserTestUtils.startLoadingURIString(
        browser,
        "https://example.com/browser/toolkit/components/passwordmgr/test/browser/authenticate.sjs"
      );
      await authShown;
      Assert.ok(true, "Seen dialog number 1");
      await browserLoaded;
      Assert.ok(true, "Loaded document number 1");

      // Reload the document a bit more often than should be allowed.
      // As long as we're in the acceptable range we should receive
      // auth prompts, otherwise we should not receive them and the
      // page should just load.
      // We've already seen the dialog once, hence we start the loop at 1.
      for (let i = 1; i < cancelDialogLimit + 2; i++) {
        if (i < cancelDialogLimit) {
          authShown = promiseAuthWindowShown();
        }
        browserLoaded = BrowserTestUtils.browserLoaded(browser);
        SpecialPowers.spawn(browser, [], function () {
          content.document.location.reload();
        });
        if (i < cancelDialogLimit) {
          await authShown;
          Assert.ok(true, `Seen dialog number ${i + 1}`);
        }
        await browserLoaded;
        Assert.ok(true, `Loaded document number ${i + 1}`);
      }

      let reloadButton = document.getElementById("reload-button");
      await TestUtils.waitForCondition(
        () => !reloadButton.hasAttribute("disabled")
      );

      // Verify that we can click the reload button to reset the counter.
      authShown = promiseAuthWindowShown();
      browserLoaded = BrowserTestUtils.browserLoaded(browser);
      reloadButton.click();
      await authShown;
      Assert.ok(true, "Seen dialog number 1");
      await browserLoaded;
      Assert.ok(true, "Loaded document number 1");

      // Now check loading subresources with auth on the page.
      browserLoaded = BrowserTestUtils.browserLoaded(browser);
      BrowserTestUtils.startLoadingURIString(browser, "https://example.com");
      await browserLoaded;

      // We've already seen the dialog once, hence we start the loop at 1.
      for (let i = 1; i < cancelDialogLimit + 2; i++) {
        if (i < cancelDialogLimit) {
          authShown = promiseAuthWindowShown();
        }

        let iframeLoaded = SpecialPowers.spawn(browser, [], async function () {
          let doc = content.document;
          let iframe = doc.createElement("iframe");
          doc.body.appendChild(iframe);
          let loaded = new Promise(resolve => {
            iframe.addEventListener(
              "load",
              function (e) {
                resolve();
              },
              { once: true }
            );
          });
          iframe.src =
            "https://example.com/browser/toolkit/components/passwordmgr/test/browser/authenticate.sjs";
          await loaded;
        });

        if (i < cancelDialogLimit) {
          await authShown;
          Assert.ok(true, `Seen dialog number ${i + 1}`);
        }

        await iframeLoaded;
        Assert.ok(true, `Loaded iframe number ${i + 1}`);
      }

      // Verify that third party subresources can not spawn new auth dialogs.
      let iframeLoaded = SpecialPowers.spawn(browser, [], async function () {
        let doc = content.document;
        let iframe = doc.createElement("iframe");
        doc.body.appendChild(iframe);
        let loaded = new Promise(resolve => {
          iframe.addEventListener(
            "load",
            function (e) {
              resolve();
            },
            { once: true }
          );
        });
        iframe.src =
          "https://example.org/browser/toolkit/components/passwordmgr/test/browser/authenticate.sjs";
        await loaded;
      });

      await iframeLoaded;
      Assert.ok(
        true,
        "Loaded a third party iframe without showing the auth dialog"
      );

      // Verify that pressing enter in the urlbar also resets the counter.
      authShown = promiseAuthWindowShown();
      browserLoaded = BrowserTestUtils.browserLoaded(browser);
      gURLBar.value =
        "https://example.com/browser/toolkit/components/passwordmgr/test/browser/authenticate.sjs";
      gURLBar.focus();
      EventUtils.synthesizeKey("KEY_Enter");
      await authShown;
      await browserLoaded;
    }
  );
});