summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/permissions/browser_temporary_permissions_navigation.js
blob: 7da79b18105c78eb92f5321343fd969bd97ef43d (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test that temporary permissions are removed on user initiated reload only.
add_task(async function testTempPermissionOnReload() {
  let origin = "https://example.com/";
  let principal =
    Services.scriptSecurityManager.createContentPrincipalFromOrigin(origin);
  let id = "geo";

  await BrowserTestUtils.withNewTab(origin, async function (browser) {
    SitePermissions.setForPrincipal(
      principal,
      id,
      SitePermissions.BLOCK,
      SitePermissions.SCOPE_TEMPORARY,
      browser
    );

    let reloaded = BrowserTestUtils.browserLoaded(browser, false, origin);

    Assert.deepEqual(SitePermissions.getForPrincipal(principal, id, browser), {
      state: SitePermissions.BLOCK,
      scope: SitePermissions.SCOPE_TEMPORARY,
    });

    // Reload through the page (should not remove the temp permission).
    await SpecialPowers.spawn(browser, [], () =>
      content.document.location.reload()
    );

    await reloaded;

    Assert.deepEqual(SitePermissions.getForPrincipal(principal, id, browser), {
      state: SitePermissions.BLOCK,
      scope: SitePermissions.SCOPE_TEMPORARY,
    });

    reloaded = BrowserTestUtils.browserLoaded(browser, false, origin);

    // Reload as a user (should remove the temp permission).
    BrowserReload();

    await reloaded;

    Assert.deepEqual(SitePermissions.getForPrincipal(principal, id, browser), {
      state: SitePermissions.UNKNOWN,
      scope: SitePermissions.SCOPE_PERSISTENT,
    });

    // Set the permission again.
    SitePermissions.setForPrincipal(
      principal,
      id,
      SitePermissions.BLOCK,
      SitePermissions.SCOPE_TEMPORARY,
      browser
    );

    // Open the tab context menu.
    let contextMenu = document.getElementById("tabContextMenu");
    // The TabContextMenu initializes its strings only on a focus or mouseover event.
    // Calls focus event on the TabContextMenu early in the test.
    gBrowser.selectedTab.focus();
    let popupShownPromise = BrowserTestUtils.waitForEvent(
      contextMenu,
      "popupshown"
    );
    EventUtils.synthesizeMouseAtCenter(gBrowser.selectedTab, {
      type: "contextmenu",
      button: 2,
    });
    await popupShownPromise;

    let reloadMenuItem = document.getElementById("context_reloadTab");

    reloaded = BrowserTestUtils.browserLoaded(browser, false, origin);

    // Reload as a user through the context menu (should remove the temp permission).
    contextMenu.activateItem(reloadMenuItem);

    await reloaded;

    Assert.deepEqual(SitePermissions.getForPrincipal(principal, id, browser), {
      state: SitePermissions.UNKNOWN,
      scope: SitePermissions.SCOPE_PERSISTENT,
    });

    // Set the permission again.
    SitePermissions.setForPrincipal(
      principal,
      id,
      SitePermissions.BLOCK,
      SitePermissions.SCOPE_TEMPORARY,
      browser
    );

    // Reload as user via return key in urlbar (should remove the temp permission)
    let urlBarInput = document.getElementById("urlbar-input");
    await EventUtils.synthesizeMouseAtCenter(urlBarInput, {});

    reloaded = BrowserTestUtils.browserLoaded(browser, false, origin);

    EventUtils.synthesizeAndWaitKey("VK_RETURN", {});

    await reloaded;

    Assert.deepEqual(SitePermissions.getForPrincipal(principal, id, browser), {
      state: SitePermissions.UNKNOWN,
      scope: SitePermissions.SCOPE_PERSISTENT,
    });

    SitePermissions.removeFromPrincipal(principal, id, browser);
  });
});

// Test that temporary permissions are not removed when reloading all tabs.
add_task(async function testTempPermissionOnReloadAllTabs() {
  let origin = "https://example.com/";
  let principal =
    Services.scriptSecurityManager.createContentPrincipalFromOrigin(origin);
  let id = "geo";

  await BrowserTestUtils.withNewTab(origin, async function (browser) {
    SitePermissions.setForPrincipal(
      principal,
      id,
      SitePermissions.BLOCK,
      SitePermissions.SCOPE_TEMPORARY,
      browser
    );

    // Select all tabs before opening the context menu.
    gBrowser.selectAllTabs();

    // Open the tab context menu.
    let contextMenu = document.getElementById("tabContextMenu");
    // The TabContextMenu initializes its strings only on a focus or mouseover event.
    // Calls focus event on the TabContextMenu early in the test.
    gBrowser.selectedTab.focus();
    let popupShownPromise = BrowserTestUtils.waitForEvent(
      contextMenu,
      "popupshown"
    );
    EventUtils.synthesizeMouseAtCenter(gBrowser.selectedTab, {
      type: "contextmenu",
      button: 2,
    });
    await popupShownPromise;

    let reloadMenuItem = document.getElementById("context_reloadSelectedTabs");

    let reloaded = Promise.all(
      gBrowser.visibleTabs.map(tab =>
        BrowserTestUtils.browserLoaded(gBrowser.getBrowserForTab(tab))
      )
    );
    contextMenu.activateItem(reloadMenuItem);
    await reloaded;

    Assert.deepEqual(SitePermissions.getForPrincipal(principal, id, browser), {
      state: SitePermissions.BLOCK,
      scope: SitePermissions.SCOPE_TEMPORARY,
    });

    SitePermissions.removeFromPrincipal(principal, id, browser);
  });
});

// Test that temporary permissions are persisted through navigation in a tab.
add_task(async function testTempPermissionOnNavigation() {
  let origin = "https://example.com/";
  let principal =
    Services.scriptSecurityManager.createContentPrincipalFromOrigin(origin);
  let id = "geo";

  await BrowserTestUtils.withNewTab(origin, async function (browser) {
    SitePermissions.setForPrincipal(
      principal,
      id,
      SitePermissions.BLOCK,
      SitePermissions.SCOPE_TEMPORARY,
      browser
    );

    Assert.deepEqual(SitePermissions.getForPrincipal(principal, id, browser), {
      state: SitePermissions.BLOCK,
      scope: SitePermissions.SCOPE_TEMPORARY,
    });

    let loaded = BrowserTestUtils.browserLoaded(
      browser,
      false,
      "https://example.org/"
    );

    // Navigate to another domain.
    await SpecialPowers.spawn(
      browser,
      [],
      () => (content.document.location = "https://example.org/")
    );

    await loaded;

    // The temporary permissions for the current URI should be reset.
    Assert.deepEqual(
      SitePermissions.getForPrincipal(browser.contentPrincipal, id, browser),
      {
        state: SitePermissions.UNKNOWN,
        scope: SitePermissions.SCOPE_PERSISTENT,
      }
    );

    loaded = BrowserTestUtils.browserLoaded(browser, false, origin);

    // Navigate to the original domain.
    await SpecialPowers.spawn(
      browser,
      [],
      () => (content.document.location = "https://example.com/")
    );

    await loaded;

    // The temporary permissions for the original URI should still exist.
    Assert.deepEqual(
      SitePermissions.getForPrincipal(browser.contentPrincipal, id, browser),
      {
        state: SitePermissions.BLOCK,
        scope: SitePermissions.SCOPE_TEMPORARY,
      }
    );

    SitePermissions.removeFromPrincipal(browser.contentPrincipal, id, browser);
  });
});