Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / caps / tests / unit / test_origin.js
blobc0cbc2996a8d9bae1a61d53362479df809575c47
1 var ssm = Services.scriptSecurityManager;
2 function makeURI(uri) {
3   return Services.io.newURI(uri);
6 function checkThrows(f) {
7   var threw = false;
8   try {
9     f();
10   } catch (e) {
11     threw = true;
12   }
13   Assert.ok(threw);
16 function checkCrossOrigin(a, b) {
17   Assert.ok(!a.equals(b));
18   Assert.ok(!a.equalsConsideringDomain(b));
19   Assert.ok(!a.subsumes(b));
20   Assert.ok(!a.subsumesConsideringDomain(b));
21   Assert.ok(!b.subsumes(a));
22   Assert.ok(!b.subsumesConsideringDomain(a));
25 function checkOriginAttributes(prin, attrs, suffix) {
26   attrs = attrs || {};
27   Assert.equal(
28     prin.originAttributes.inIsolatedMozBrowser,
29     attrs.inIsolatedMozBrowser || false
30   );
31   Assert.equal(prin.originSuffix, suffix || "");
32   Assert.equal(ChromeUtils.originAttributesToSuffix(attrs), suffix || "");
33   Assert.ok(
34     ChromeUtils.originAttributesMatchPattern(prin.originAttributes, attrs)
35   );
36   if (!prin.isNullPrincipal && !prin.origin.startsWith("[")) {
37     Assert.ok(ssm.createContentPrincipalFromOrigin(prin.origin).equals(prin));
38   } else {
39     checkThrows(() => ssm.createContentPrincipalFromOrigin(prin.origin));
40   }
43 function checkSandboxOriginAttributes(arr, attrs, options) {
44   options = options || {};
45   var sandbox = Cu.Sandbox(arr, options);
46   checkOriginAttributes(
47     Cu.getObjectPrincipal(sandbox),
48     attrs,
49     ChromeUtils.originAttributesToSuffix(attrs)
50   );
53 // utility function useful for debugging
54 // eslint-disable-next-line no-unused-vars
55 function printAttrs(name, attrs) {
56   info(
57     name +
58       " {\n" +
59       "\tuserContextId: " +
60       attrs.userContextId +
61       ",\n" +
62       "\tinIsolatedMozBrowser: " +
63       attrs.inIsolatedMozBrowser +
64       ",\n" +
65       "\tprivateBrowsingId: '" +
66       attrs.privateBrowsingId +
67       "',\n" +
68       "\tfirstPartyDomain: '" +
69       attrs.firstPartyDomain +
70       "'\n}"
71   );
74 function checkValues(attrs, values) {
75   values = values || {};
76   // printAttrs("attrs", attrs);
77   // printAttrs("values", values);
78   Assert.equal(attrs.userContextId, values.userContextId || 0);
79   Assert.equal(
80     attrs.inIsolatedMozBrowser,
81     values.inIsolatedMozBrowser || false
82   );
83   Assert.equal(attrs.privateBrowsingId, values.privateBrowsingId || "");
84   Assert.equal(attrs.firstPartyDomain, values.firstPartyDomain || "");
87 function run_test() {
88   // Attributeless origins.
89   Assert.equal(ssm.getSystemPrincipal().origin, "[System Principal]");
90   checkOriginAttributes(ssm.getSystemPrincipal());
91   var exampleOrg = ssm.createContentPrincipal(
92     makeURI("http://example.org"),
93     {}
94   );
95   Assert.equal(exampleOrg.origin, "http://example.org");
96   checkOriginAttributes(exampleOrg);
97   var exampleCom = ssm.createContentPrincipal(
98     makeURI("https://www.example.com:123"),
99     {}
100   );
101   Assert.equal(exampleCom.origin, "https://www.example.com:123");
102   checkOriginAttributes(exampleCom);
103   var nullPrin = Cu.getObjectPrincipal(new Cu.Sandbox(null));
104   Assert.ok(
105     /^moz-nullprincipal:\{([0-9]|[a-z]|\-){36}\}$/.test(nullPrin.origin)
106   );
107   checkOriginAttributes(nullPrin);
108   var ipv6Prin = ssm.createContentPrincipal(
109     makeURI("https://[2001:db8::ff00:42:8329]:123"),
110     {}
111   );
112   Assert.equal(ipv6Prin.origin, "https://[2001:db8::ff00:42:8329]:123");
113   checkOriginAttributes(ipv6Prin);
114   var ipv6NPPrin = ssm.createContentPrincipal(
115     makeURI("https://[2001:db8::ff00:42:8329]"),
116     {}
117   );
118   Assert.equal(ipv6NPPrin.origin, "https://[2001:db8::ff00:42:8329]");
119   checkOriginAttributes(ipv6NPPrin);
120   var ep = Cu.getObjectPrincipal(
121     Cu.Sandbox([exampleCom, nullPrin, exampleOrg])
122   );
123   checkOriginAttributes(ep);
124   checkCrossOrigin(exampleCom, exampleOrg);
125   checkCrossOrigin(exampleOrg, nullPrin);
127   // nsEP origins should be in lexical order.
128   Assert.equal(
129     ep.origin,
130     `[Expanded Principal [${exampleCom.origin}, ${nullPrin.origin}, ${exampleOrg.origin}]]`
131   );
133   // Make sure createContentPrincipal does what the rest of gecko does.
134   Assert.ok(
135     exampleOrg.equals(
136       Cu.getObjectPrincipal(new Cu.Sandbox("http://example.org"))
137     )
138   );
140   //
141   // Test origin attributes.
142   //
144   // Just browser.
145   var exampleOrg_browser = ssm.createContentPrincipal(
146     makeURI("http://example.org"),
147     { inIsolatedMozBrowser: true }
148   );
149   var nullPrin_browser = ssm.createNullPrincipal({
150     inIsolatedMozBrowser: true,
151   });
152   checkOriginAttributes(
153     exampleOrg_browser,
154     { inIsolatedMozBrowser: true },
155     "^inBrowser=1"
156   );
157   checkOriginAttributes(
158     nullPrin_browser,
159     { inIsolatedMozBrowser: true },
160     "^inBrowser=1"
161   );
162   Assert.equal(exampleOrg_browser.origin, "http://example.org^inBrowser=1");
164   // First party Uri
165   var exampleOrg_firstPartyDomain = ssm.createContentPrincipal(
166     makeURI("http://example.org"),
167     { firstPartyDomain: "example.org" }
168   );
169   checkOriginAttributes(
170     exampleOrg_firstPartyDomain,
171     { firstPartyDomain: "example.org" },
172     "^firstPartyDomain=example.org"
173   );
174   Assert.equal(
175     exampleOrg_firstPartyDomain.origin,
176     "http://example.org^firstPartyDomain=example.org"
177   );
179   // Just userContext.
180   var exampleOrg_userContext = ssm.createContentPrincipal(
181     makeURI("http://example.org"),
182     { userContextId: 42 }
183   );
184   checkOriginAttributes(
185     exampleOrg_userContext,
186     { userContextId: 42 },
187     "^userContextId=42"
188   );
189   Assert.equal(
190     exampleOrg_userContext.origin,
191     "http://example.org^userContextId=42"
192   );
194   checkSandboxOriginAttributes(null, {});
195   checkSandboxOriginAttributes("http://example.org", {});
196   checkSandboxOriginAttributes(
197     "http://example.org",
198     {},
199     { originAttributes: {} }
200   );
201   checkSandboxOriginAttributes(["http://example.org"], {});
202   checkSandboxOriginAttributes(
203     ["http://example.org"],
204     {},
205     { originAttributes: {} }
206   );
208   // Check that all of the above are cross-origin.
209   checkCrossOrigin(exampleOrg_browser, nullPrin_browser);
210   checkCrossOrigin(exampleOrg_firstPartyDomain, exampleOrg);
211   checkCrossOrigin(exampleOrg_userContext, exampleOrg);
213   // Check Principal kinds.
214   function checkKind(prin, kind) {
215     Assert.equal(prin.isNullPrincipal, kind == "nullPrincipal");
216     Assert.equal(prin.isContentPrincipal, kind == "contentPrincipal");
217     Assert.equal(prin.isExpandedPrincipal, kind == "expandedPrincipal");
218     Assert.equal(prin.isSystemPrincipal, kind == "systemPrincipal");
219   }
220   checkKind(ssm.createNullPrincipal({}), "nullPrincipal");
221   checkKind(
222     ssm.createContentPrincipal(makeURI("http://www.example.com"), {}),
223     "contentPrincipal"
224   );
225   checkKind(
226     Cu.getObjectPrincipal(
227       Cu.Sandbox([
228         ssm.createContentPrincipal(makeURI("http://www.example.com"), {}),
229       ])
230     ),
231     "expandedPrincipal"
232   );
233   checkKind(ssm.getSystemPrincipal(), "systemPrincipal");
235   //
236   // Test Origin Attribute Manipulation
237   //
239   // check that we can create an empty origin attributes dict with default
240   // members and values.
241   var emptyAttrs = ChromeUtils.fillNonDefaultOriginAttributes({});
242   checkValues(emptyAttrs);
244   var uri = "http://example.org";
245   var tests = [
246     ["", {}],
247     ["^userContextId=3", { userContextId: 3 }],
248     ["^inBrowser=1", { inIsolatedMozBrowser: true }],
249     ["^firstPartyDomain=example.org", { firstPartyDomain: "example.org" }],
250   ];
252   // check that we can create an origin attributes from an origin properly
253   tests.forEach(t => {
254     let attrs = ChromeUtils.createOriginAttributesFromOrigin(uri + t[0]);
255     checkValues(attrs, t[1]);
256     Assert.equal(ChromeUtils.originAttributesToSuffix(attrs), t[0]);
257   });
259   // check that we can create an origin attributes from a dict properly
260   tests.forEach(t => {
261     let attrs = ChromeUtils.fillNonDefaultOriginAttributes(t[1]);
262     checkValues(attrs, t[1]);
263     Assert.equal(ChromeUtils.originAttributesToSuffix(attrs), t[0]);
264   });
266   // each row in the dflt_tests array has these values:
267   // [0] - the suffix used to create an origin attribute from
268   // [1] - the expected result of creating an origin attributes from [0]
269   // [2] - the expected result after setting userContextId to the default
270   // [3] - the expected result of creating a suffix from [2]
271   var dflt_tests = [
272     ["", {}, {}, ""],
273     ["^userContextId=3", { userContextId: 3 }, {}, ""],
274   ];
276   // check that we can set the userContextId to default properly
277   dflt_tests.forEach(t => {
278     let orig = ChromeUtils.createOriginAttributesFromOrigin(uri + t[0]);
279     checkValues(orig, t[1]);
280     let mod = orig;
281     mod.userContextId = 0;
282     checkValues(mod, t[2]);
283     Assert.equal(ChromeUtils.originAttributesToSuffix(mod), t[3]);
284   });
286   // each row in the dflt2_tests array has these values:
287   // [0] - the suffix used to create an origin attribute from
288   // [1] - the expected result of creating an origin attributes from [0]
289   // [2] - the expected result after setting firstPartyUri to the default
290   // [3] - the expected result of creating a suffix from [2]
291   var dflt2_tests = [
292     ["", {}, {}, ""],
293     ["^firstPartyDomain=foo.com", { firstPartyDomain: "foo.com" }, {}, ""],
294   ];
296   // check that we can set the userContextId to default properly
297   dflt2_tests.forEach(t => {
298     let orig = ChromeUtils.createOriginAttributesFromOrigin(uri + t[0]);
299     checkValues(orig, t[1]);
300     let mod = orig;
301     mod.firstPartyDomain = "";
302     checkValues(mod, t[2]);
303     Assert.equal(ChromeUtils.originAttributesToSuffix(mod), t[3]);
304   });
306   var fileURI = makeURI("file:///foo/bar").QueryInterface(Ci.nsIFileURL);
307   var fileTests = [
308     [true, fileURI.spec],
309     [false, "file://UNIVERSAL_FILE_URI_ORIGIN"],
310   ];
311   fileTests.forEach(t => {
312     Services.prefs.setBoolPref("security.fileuri.strict_origin_policy", t[0]);
313     var filePrin = ssm.createContentPrincipal(fileURI, {});
314     Assert.equal(filePrin.origin, t[1]);
315   });
316   Services.prefs.clearUserPref("security.fileuri.strict_origin_policy");
318   var aboutBlankURI = makeURI("about:blank");
319   var aboutBlankPrin = ssm.createContentPrincipal(aboutBlankURI, {});
320   Assert.ok(
321     /^moz-nullprincipal:\{([0-9]|[a-z]|\-){36}\}$/.test(aboutBlankPrin.origin)
322   );