summaryrefslogtreecommitdiffstats
path: root/browser/modules/test/browser/browser_SitePermissions_tab_urls.js
blob: 36a17ddbe04ac53e747cd3aa5ad501218f7725c1 (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
/* 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";

function newPrincipal(origin) {
  return Services.scriptSecurityManager.createContentPrincipalFromOrigin(
    origin
  );
}

// This tests the key used to store the URI -> permission map on a tab.
add_task(async function testTemporaryPermissionTabURLs() {
  // Prevent showing a dialog for https://name:password@example.com
  SpecialPowers.pushPrefEnv({
    set: [["network.http.phishy-userpass-length", 2048]],
  });

  // This usually takes about 60 seconds on 32bit Linux debug,
  // due to the combinatory nature of the test that is hard to fix.
  requestLongerTimeout(2);

  let same = [
    newPrincipal("https://example.com"),
    newPrincipal("https://example.com:443"),
    newPrincipal("https://test1.example.com"),
    newPrincipal("https://name:password@example.com"),
    newPrincipal("http://example.com"),
  ];
  let different = [
    newPrincipal("https://example.com"),
    newPrincipal("http://example.org"),
    newPrincipal("http://example.net"),
  ];

  let id = "microphone";

  await BrowserTestUtils.withNewTab("about:blank", async function (browser) {
    for (let principal of same) {
      let loaded = BrowserTestUtils.browserLoaded(
        browser,
        false,
        principal.spec
      );
      BrowserTestUtils.loadURIString(browser, principal.spec);
      await loaded;

      SitePermissions.setForPrincipal(
        principal,
        id,
        SitePermissions.BLOCK,
        SitePermissions.SCOPE_TEMPORARY,
        browser
      );

      for (let principal2 of same) {
        let loaded2 = BrowserTestUtils.browserLoaded(
          browser,
          false,
          principal2.URI.spec
        );
        BrowserTestUtils.loadURIString(browser, principal2.URI.spec);
        await loaded2;

        Assert.deepEqual(
          SitePermissions.getForPrincipal(principal2, id, browser),
          {
            state: SitePermissions.BLOCK,
            scope: SitePermissions.SCOPE_TEMPORARY,
          },
          `${principal.spec} should share tab permissions with ${principal2.spec}`
        );
      }

      SitePermissions.clearTemporaryBlockPermissions(browser);
    }

    for (let principal of different) {
      let loaded = BrowserTestUtils.browserLoaded(
        browser,
        false,
        principal.spec
      );
      BrowserTestUtils.loadURIString(browser, principal.spec);
      await loaded;

      SitePermissions.setForPrincipal(
        principal,
        id,
        SitePermissions.BLOCK,
        SitePermissions.SCOPE_TEMPORARY,
        browser
      );

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

      for (let principal2 of different) {
        loaded = BrowserTestUtils.browserLoaded(
          browser,
          false,
          principal2.URI.spec
        );
        BrowserTestUtils.loadURIString(browser, principal2.URI.spec);
        await loaded;

        if (principal2 != principal) {
          Assert.deepEqual(
            SitePermissions.getForPrincipal(principal2, id, browser),
            {
              state: SitePermissions.UNKNOWN,
              scope: SitePermissions.SCOPE_PERSISTENT,
            },
            `${principal.spec} should not share tab permissions with ${principal2.spec}`
          );
        }
      }

      SitePermissions.clearTemporaryBlockPermissions(browser);
    }
  });
});