4 https://bugzilla.mozilla.org/show_bug.cgi?id=1180921
8 <title>Test for Bug
1180921</title>
9 <script src=
"chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
10 <link rel=
"stylesheet" type=
"text/css" href=
"chrome://global/skin"/>
11 <link rel=
"stylesheet" type=
"text/css" href=
"chrome://mochikit/content/tests/SimpleTest/test.css"/>
12 <script type=
"application/javascript">
14 /** Test for Bug
1180921 */
15 let module = Cu.getGlobalForObject(Services);
16 let ssm = Services.scriptSecurityManager;
18 function StubPolicy(id, subdomain) {
19 /* globals MatchPatternSet */
20 return new module.WebExtensionPolicy({
22 mozExtensionHostname: id,
23 baseURL: `file:///{id}`,
25 allowedOrigins: new MatchPatternSet([`*://${subdomain}.example.org/*`]),
26 localizeCallback() {},
30 /* globals WebExtensionPolicy */
31 let policyA = StubPolicy(
"addona",
"test1");
32 let policyB = StubPolicy(
"addonb",
"test2");
33 policyA.active = true;
34 policyB.active = true;
36 SimpleTest.waitForExplicitFinish();
37 SimpleTest.registerCleanupFunction(function() {
38 policyA.active = false;
39 policyB.active = false;
42 function tryLoad(sb, uri) {
43 let p = new Promise(function(resolve, reject) {
44 Cu.exportFunction(resolve, sb, { defineAs:
"finish" });
45 Cu.exportFunction(reject, sb, { defineAs:
"error" });
46 sb.eval(
"try { (function () { " +
47 " var xhr = new XMLHttpRequest();" +
48 " xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { finish(xhr.status == 200); } };" +
49 " xhr.open('GET', '" + uri +
"', true);" +
51 "})() } catch (e) { error(e); }");
56 let addonA = new Cu.Sandbox(ssm.createContentPrincipal(Services.io.newURI(
"moz-extension://addonA/"), {}),
57 {wantGlobalProperties: [
"XMLHttpRequest"]});
58 let addonB = new Cu.Sandbox(ssm.createContentPrincipal(Services.io.newURI(
"moz-extension://addonB/"), {}),
59 {wantGlobalProperties: [
"XMLHttpRequest"]});
61 function uriForDomain(d) { return d +
"/tests/caps/tests/mochitest/file_data.txt"; }
63 tryLoad(addonA, uriForDomain(
"http://test4.example.org"))
64 .then(function(success) {
65 ok(!success,
"cross-origin load should fail for addon A");
66 return tryLoad(addonA, uriForDomain(
"http://test1.example.org"));
67 }).then(function(success) {
68 ok(success,
"allowlisted cross-origin load of test1 should succeed for addon A");
69 return tryLoad(addonB, uriForDomain(
"http://test1.example.org"));
70 }).then(function(success) {
71 ok(!success,
"non-allowlisted cross-origin load of test1 should fail for addon B");
72 return tryLoad(addonB, uriForDomain(
"http://test2.example.org"));
73 }).then(function(success) {
74 ok(success,
"allowlisted cross-origin load of test2 should succeed for addon B");
75 return tryLoad(addonA, uriForDomain(
"http://test2.example.org"));
76 }).then(function(success) {
77 ok(!success,
"non-allowlisted cross-origin load of test2 should fail for addon A");
80 ok(false,
"Rejected promise chain: " + e);
87 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=1180921">Mozilla Bug
1180921</a>
89 <div id=
"content" style=
"display: none">