summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_schemas_privileged.js
blob: ece69a410695535809fbbf338daa17fe11343cb7 (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
"use strict";

const { ExtensionCommon } = ChromeUtils.import(
  "resource://gre/modules/ExtensionCommon.jsm"
);
const { ExtensionAPI } = ExtensionCommon;

AddonTestUtils.init(this);
AddonTestUtils.overrideCertDB();

add_task(async function() {
  const schema = [
    {
      namespace: "privileged",
      permissions: ["mozillaAddons"],
      properties: {
        test: {
          type: "any",
        },
      },
    },
  ];

  class API extends ExtensionAPI {
    getAPI(context) {
      return {
        privileged: {
          test: "hello",
        },
      };
    }
  }

  const modules = {
    privileged: {
      url: URL.createObjectURL(new Blob([API.toString()])),
      schema: `data:,${JSON.stringify(schema)}`,
      scopes: ["addon_parent"],
      paths: [["privileged"]],
    },
  };

  Services.catMan.addCategoryEntry(
    "webextension-modules",
    "test-privileged",
    `data:,${JSON.stringify(modules)}`,
    false,
    false
  );

  AddonTestUtils.createAppInfo(
    "xpcshell@tests.mozilla.org",
    "XPCShell",
    "1",
    "42"
  );
  await AddonTestUtils.promiseStartupManager();

  // Try accessing the privileged namespace.
  async function testOnce() {
    let extension = ExtensionTestUtils.loadExtension({
      manifest: {
        applications: { gecko: { id: "privilegedapi@tests.mozilla.org" } },
        permissions: ["mozillaAddons"],
      },
      background() {
        browser.test.sendMessage(
          "result",
          browser.privileged instanceof Object
        );
      },
      useAddonManager: "permanent",
    });

    await extension.startup();
    let result = await extension.awaitMessage("result");
    await extension.unload();
    return result;
  }

  AddonTestUtils.usePrivilegedSignatures = false;
  let result = await testOnce();
  equal(
    result,
    false,
    "Privileged namespace should not be accessible to a regular webextension"
  );

  AddonTestUtils.usePrivilegedSignatures = true;
  result = await testOnce();
  equal(
    result,
    true,
    "Privileged namespace should be accessible to a webextension signed with Mozilla Extensions"
  );

  await AddonTestUtils.promiseShutdownManager();
  Services.catMan.deleteCategoryEntry(
    "webextension-modules",
    "test-privileged",
    false
  );
});