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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
const { newURI } = Services.io;
add_task(async function test_WebExtensionPolicy() {
const id = "foo@bar.baz";
const uuid = "ca9d3f23-125c-4b24-abfc-1ca2692b0610";
const baseURL = "file:///foo/";
const mozExtURL = `moz-extension://${uuid}/`;
const mozExtURI = newURI(mozExtURL);
let policy = new WebExtensionPolicy({
id,
mozExtensionHostname: uuid,
baseURL,
localizeCallback(str) {
return `<${str}>`;
},
allowedOrigins: new MatchPatternSet(["http://foo.bar/", "*://*.baz/"], {
ignorePath: true,
}),
permissions: ["<all_urls>"],
webAccessibleResources: ["/foo/*", "/bar.baz"].map(
glob => new MatchGlob(glob)
),
});
equal(policy.active, false, "Active attribute should initially be false");
// GetURL
equal(
policy.getURL(),
mozExtURL,
"getURL() should return the correct root URL"
);
equal(
policy.getURL("path/foo.html"),
`${mozExtURL}path/foo.html`,
"getURL(path) should return the correct URL"
);
// Permissions
deepEqual(
policy.permissions,
["<all_urls>"],
"Initial permissions should be correct"
);
ok(
policy.hasPermission("<all_urls>"),
"hasPermission should match existing permission"
);
ok(
!policy.hasPermission("history"),
"hasPermission should not match nonexistent permission"
);
Assert.throws(
() => {
policy.permissions[0] = "foo";
},
TypeError,
"Permissions array should be frozen"
);
policy.permissions = ["history"];
deepEqual(
policy.permissions,
["history"],
"Permissions should be updateable as a set"
);
ok(
policy.hasPermission("history"),
"hasPermission should match existing permission"
);
ok(
!policy.hasPermission("<all_urls>"),
"hasPermission should not match nonexistent permission"
);
// Origins
ok(
policy.canAccessURI(newURI("http://foo.bar/quux")),
"Should be able to access permitted URI"
);
ok(
policy.canAccessURI(newURI("https://x.baz/foo")),
"Should be able to access permitted URI"
);
ok(
!policy.canAccessURI(newURI("https://foo.bar/quux")),
"Should not be able to access non-permitted URI"
);
policy.allowedOrigins = new MatchPatternSet(["https://foo.bar/"], {
ignorePath: true,
});
ok(
policy.canAccessURI(newURI("https://foo.bar/quux")),
"Should be able to access updated permitted URI"
);
ok(
!policy.canAccessURI(newURI("https://x.baz/foo")),
"Should not be able to access removed permitted URI"
);
// Web-accessible resources
ok(
policy.isPathWebAccessible("/foo/bar"),
"Web-accessible glob should be web-accessible"
);
ok(
policy.isPathWebAccessible("/bar.baz"),
"Web-accessible path should be web-accessible"
);
ok(
!policy.isPathWebAccessible("/bar.baz/quux"),
"Non-web-accessible path should not be web-accessible"
);
// Localization
equal(
policy.localize("foo"),
"<foo>",
"Localization callback should work as expected"
);
// Protocol and lookups.
let proto = Services.io
.getProtocolHandler("moz-extension", uuid)
.QueryInterface(Ci.nsISubstitutingProtocolHandler);
deepEqual(
WebExtensionPolicy.getActiveExtensions(),
[],
"Should have no active extensions"
);
equal(
WebExtensionPolicy.getByID(id),
null,
"ID lookup should not return extension when not active"
);
equal(
WebExtensionPolicy.getByHostname(uuid),
null,
"Hostname lookup should not return extension when not active"
);
Assert.throws(
() => proto.resolveURI(mozExtURI),
/NS_ERROR_NOT_AVAILABLE/,
"URL should not resolve when not active"
);
policy.active = true;
equal(policy.active, true, "Active attribute should be updated");
let exts = WebExtensionPolicy.getActiveExtensions();
equal(exts.length, 1, "Should have one active extension");
equal(exts[0], policy, "Should have the correct active extension");
equal(
WebExtensionPolicy.getByID(id),
policy,
"ID lookup should return extension when active"
);
equal(
WebExtensionPolicy.getByHostname(uuid),
policy,
"Hostname lookup should return extension when active"
);
equal(
proto.resolveURI(mozExtURI),
baseURL,
"URL should resolve correctly while active"
);
policy.active = false;
equal(policy.active, false, "Active attribute should be updated");
deepEqual(
WebExtensionPolicy.getActiveExtensions(),
[],
"Should have no active extensions"
);
equal(
WebExtensionPolicy.getByID(id),
null,
"ID lookup should not return extension when not active"
);
equal(
WebExtensionPolicy.getByHostname(uuid),
null,
"Hostname lookup should not return extension when not active"
);
Assert.throws(
() => proto.resolveURI(mozExtURI),
/NS_ERROR_NOT_AVAILABLE/,
"URL should not resolve when not active"
);
// Conflicting policies.
// This asserts in debug builds, so only test in non-debug builds.
if (!AppConstants.DEBUG) {
policy.active = true;
let attrs = [
{ id, uuid },
{ id, uuid: "d916886c-cfdf-482e-b7b1-d7f5b0facfa5" },
{ id: "foo@quux", uuid },
];
// eslint-disable-next-line no-shadow
for (let { id, uuid } of attrs) {
let policy2 = new WebExtensionPolicy({
id,
mozExtensionHostname: uuid,
baseURL: "file://bar/",
localizeCallback() {},
allowedOrigins: new MatchPatternSet([]),
});
Assert.throws(
() => {
policy2.active = true;
},
/NS_ERROR_UNEXPECTED/,
`Should not be able to activate conflicting policy: ${id} ${uuid}`
);
}
policy.active = false;
}
});
add_task(async function test_WebExtensionPolicy_registerContentScripts() {
const id = "foo@bar.baz";
const uuid = "77a7b9d3-e73c-4cf3-97fb-1824868fe00f";
const id2 = "foo-2@bar.baz";
const uuid2 = "89383c45-7db4-4999-83f7-f4cc246372cd";
const baseURL = "file:///foo/";
const mozExtURL = `moz-extension://${uuid}/`;
const mozExtURL2 = `moz-extension://${uuid2}/`;
let policy = new WebExtensionPolicy({
id,
mozExtensionHostname: uuid,
baseURL,
localizeCallback() {},
allowedOrigins: new MatchPatternSet([]),
permissions: ["<all_urls>"],
});
let policy2 = new WebExtensionPolicy({
id: id2,
mozExtensionHostname: uuid2,
baseURL,
localizeCallback() {},
allowedOrigins: new MatchPatternSet([]),
permissions: ["<all_urls>"],
});
let script1 = new WebExtensionContentScript(policy, {
run_at: "document_end",
js: [`${mozExtURL}/registered-content-script.js`],
matches: new MatchPatternSet(["http://localhost/data/*"]),
});
let script2 = new WebExtensionContentScript(policy, {
run_at: "document_end",
css: [`${mozExtURL}/registered-content-style.css`],
matches: new MatchPatternSet(["http://localhost/data/*"]),
});
let script3 = new WebExtensionContentScript(policy2, {
run_at: "document_end",
css: [`${mozExtURL2}/registered-content-style.css`],
matches: new MatchPatternSet(["http://localhost/data/*"]),
});
deepEqual(
policy.contentScripts,
[],
"The policy contentScripts is initially empty"
);
policy.registerContentScript(script1);
deepEqual(
policy.contentScripts,
[script1],
"script1 has been added to the policy contentScripts"
);
Assert.throws(
() => policy.registerContentScript(script1),
e => e.result == Cr.NS_ERROR_ILLEGAL_VALUE,
"Got the expected NS_ERROR_ILLEGAL_VALUE when trying to register a script more than once"
);
Assert.throws(
() => policy.registerContentScript(script3),
e => e.result == Cr.NS_ERROR_ILLEGAL_VALUE,
"Got the expected NS_ERROR_ILLEGAL_VALUE when trying to register a script related to " +
"a different extension"
);
Assert.throws(
() => policy.unregisterContentScript(script3),
e => e.result == Cr.NS_ERROR_ILLEGAL_VALUE,
"Got the expected NS_ERROR_ILLEGAL_VALUE when trying to unregister a script related to " +
"a different extension"
);
deepEqual(
policy.contentScripts,
[script1],
"script1 has not been added twice"
);
policy.registerContentScript(script2);
deepEqual(
policy.contentScripts,
[script1, script2],
"script2 has the last item of the policy contentScripts array"
);
policy.unregisterContentScript(script1);
deepEqual(
policy.contentScripts,
[script2],
"script1 has been removed from the policy contentscripts"
);
Assert.throws(
() => policy.unregisterContentScript(script1),
e => e.result == Cr.NS_ERROR_ILLEGAL_VALUE,
"Got the expected NS_ERROR_ILLEGAL_VALUE when trying to unregister a script more than once"
);
deepEqual(
policy.contentScripts,
[script2],
"the policy contentscripts is unmodified when unregistering an unknown contentScript"
);
policy.unregisterContentScript(script2);
deepEqual(
policy.contentScripts,
[],
"script2 has been removed from the policy contentScripts"
);
});
|