summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_browserAction_pageAction_icon_permissions.js
blob: 5a94a0dde19b4efcfc08ecf5271103dd8bc1369b (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

PromiseTestUtils.allowMatchingRejectionsGlobally(/packaging errors/);

// Test that an error is thrown when providing invalid icon sizes
add_task(async function testInvalidIconSizes() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      browser_action: {
        default_area: "navbar",
      },
      page_action: {},
    },

    background: function () {
      browser.tabs.query({ active: true, currentWindow: true }, tabs => {
        let tabId = tabs[0].id;

        let promises = [];
        for (let api of ["pageAction", "browserAction"]) {
          // helper function to run setIcon and check if it fails
          let assertSetIconThrows = function (detail, error, message) {
            detail.tabId = tabId;
            browser.test.assertThrows(
              () => browser[api].setIcon(detail),
              /an unexpected .* property/,
              "setIcon with invalid icon size"
            );
          };

          let imageData = new ImageData(1, 1);

          // test invalid icon size inputs
          for (let type of ["path", "imageData"]) {
            let img = type == "imageData" ? imageData : "test.png";

            assertSetIconThrows({ [type]: { abcdef: img } });
            assertSetIconThrows({ [type]: { "48px": img } });
            assertSetIconThrows({ [type]: { 20.5: img } });
            assertSetIconThrows({ [type]: { "5.0": img } });
            assertSetIconThrows({ [type]: { "-300": img } });
            assertSetIconThrows({ [type]: { abc: img, 5: img } });
          }

          assertSetIconThrows({
            imageData: { abcdef: imageData },
            path: { 5: "test.png" },
          });
          assertSetIconThrows({
            path: { abcdef: "test.png" },
            imageData: { 5: imageData },
          });
        }

        Promise.all(promises).then(() => {
          browser.test.notifyPass("setIcon with invalid icon size");
        });
      });
    },
  });

  await Promise.all([
    extension.startup(),
    extension.awaitFinish("setIcon with invalid icon size"),
  ]);

  await extension.unload();
});

// Test that default icon details in the manifest.json file are handled
// correctly.
add_task(async function testDefaultDetails() {
  // TODO: Test localized variants.
  let icons = [
    "foo/bar.png",
    "/foo/bar.png",
    { 38: "foo/bar.png" },
    { 70: "foo/bar.png" },
  ];

  if (window.devicePixelRatio > 1) {
    icons.push({ 38: "baz/quux.png", 70: "foo/bar.png" });
  } else {
    icons.push({ 38: "foo/bar.png", 70: "baz/quux@2x.png" });
  }

  let expectedURL = new RegExp(
    String.raw`^moz-extension://[^/]+/foo/bar\.png$`
  );

  for (let icon of icons) {
    let extension = ExtensionTestUtils.loadExtension({
      manifest: {
        browser_action: { default_icon: icon, default_area: "navbar" },
        page_action: { default_icon: icon },
      },

      background: function () {
        browser.tabs.query({ active: true, currentWindow: true }, tabs => {
          let tabId = tabs[0].id;

          browser.pageAction.show(tabId).then(() => {
            browser.test.sendMessage("ready");
          });
        });
      },

      files: {
        "foo/bar.png": imageBuffer,
        "baz/quux.png": imageBuffer,
        "baz/quux@2x.png": imageBuffer,
      },
    });

    await Promise.all([extension.startup(), extension.awaitMessage("ready")]);

    let browserActionId = makeWidgetId(extension.id) + "-browser-action";
    let pageActionId = BrowserPageActions.urlbarButtonNodeIDForActionID(
      makeWidgetId(extension.id)
    );

    await promiseAnimationFrame();

    let browserActionButton =
      document.getElementById(browserActionId).firstElementChild;
    let image = getListStyleImage(browserActionButton);

    ok(
      expectedURL.test(image),
      `browser action image ${image} matches ${expectedURL}`
    );

    let pageActionImage = document.getElementById(pageActionId);
    image = getListStyleImage(pageActionImage);

    ok(
      expectedURL.test(image),
      `page action image ${image} matches ${expectedURL}`
    );

    await extension.unload();

    let node = document.getElementById(pageActionId);
    is(node, null, "pageAction image removed from document");
  }
});

// Check that attempts to load a privileged URL as an icon image fail.
add_task(async function testSecureURLsDenied() {
  // Test URLs passed to setIcon.

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      browser_action: { default_area: "navbar" },
      page_action: {},
    },

    background: function () {
      browser.tabs.query({ active: true, currentWindow: true }, tabs => {
        let tabId = tabs[0].id;

        let urls = [
          "chrome://browser/content/browser.xhtml",
          "javascript:true",
        ];

        let promises = [];
        for (let url of urls) {
          for (let api of ["pageAction", "browserAction"]) {
            promises.push(
              browser.test.assertRejects(
                browser[api].setIcon({ tabId, path: url }),
                /Illegal URL/,
                `Load of '${url}' should fail.`
              )
            );
          }
        }

        Promise.all(promises).then(() => {
          browser.test.notifyPass("setIcon security tests");
        });
      });
    },
  });

  await extension.startup();

  await extension.awaitFinish("setIcon security tests");
  await extension.unload();
});

add_task(async function testSecureManifestURLsDenied() {
  // Test URLs included in the manifest.

  let urls = ["chrome://browser/content/browser.xhtml", "javascript:true"];

  let apis = ["browser_action", "page_action"];

  for (let url of urls) {
    for (let api of apis) {
      info(`TEST ${api} icon url: ${url}`);

      let matchURLForbidden = url => ({
        message: new RegExp(`match the format "strictRelativeUrl"`),
      });

      let messages = [matchURLForbidden(url)];

      let waitForConsole = new Promise(resolve => {
        // Not necessary in browser-chrome tests, but monitorConsole gripes
        // if we don't call it.
        SimpleTest.waitForExplicitFinish();

        SimpleTest.monitorConsole(resolve, messages);
      });

      let extension = ExtensionTestUtils.loadExtension({
        manifest: {
          [api]: {
            default_icon: url,
            default_area: "navbar",
          },
        },
      });

      await Assert.rejects(
        extension.startup(),
        /startup failed/,
        "Manifest rejected"
      );

      SimpleTest.endMonitorConsole();
      await waitForConsole;
    }
  }
});