test_origin.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. var Cc = Components.classes;
  2. var Ci = Components.interfaces;
  3. var Cu = Components.utils;
  4. Cu.import("resource://gre/modules/Services.jsm");
  5. var ssm = Services.scriptSecurityManager;
  6. function makeURI(uri) { return Services.io.newURI(uri, null, null); }
  7. function checkThrows(f) {
  8. var threw = false;
  9. try { f(); } catch (e) { threw = true }
  10. do_check_true(threw);
  11. }
  12. function checkCrossOrigin(a, b) {
  13. do_check_false(a.equals(b));
  14. do_check_false(a.equalsConsideringDomain(b));
  15. do_check_false(a.subsumes(b));
  16. do_check_false(a.subsumesConsideringDomain(b));
  17. do_check_false(b.subsumes(a));
  18. do_check_false(b.subsumesConsideringDomain(a));
  19. }
  20. function checkOriginAttributes(prin, attrs, suffix) {
  21. attrs = attrs || {};
  22. do_check_eq(prin.originAttributes.appId, attrs.appId || 0);
  23. do_check_eq(prin.originAttributes.inIsolatedMozBrowser, attrs.inIsolatedMozBrowser || false);
  24. do_check_eq(prin.originSuffix, suffix || '');
  25. do_check_eq(ChromeUtils.originAttributesToSuffix(attrs), suffix || '');
  26. do_check_true(ChromeUtils.originAttributesMatchPattern(prin.originAttributes, attrs));
  27. if (!prin.isNullPrincipal && !prin.origin.startsWith('[')) {
  28. do_check_true(ssm.createCodebasePrincipalFromOrigin(prin.origin).equals(prin));
  29. } else {
  30. checkThrows(() => ssm.createCodebasePrincipalFromOrigin(prin.origin));
  31. }
  32. }
  33. function checkSandboxOriginAttributes(arr, attrs, options) {
  34. options = options || {};
  35. var sandbox = Cu.Sandbox(arr, options);
  36. checkOriginAttributes(Cu.getObjectPrincipal(sandbox), attrs,
  37. ChromeUtils.originAttributesToSuffix(attrs));
  38. }
  39. // utility function useful for debugging
  40. function printAttrs(name, attrs) {
  41. do_print(name + " {\n" +
  42. "\tappId: " + attrs.appId + ",\n" +
  43. "\tuserContextId: " + attrs.userContextId + ",\n" +
  44. "\tinIsolatedMozBrowser: " + attrs.inIsolatedMozBrowser + ",\n" +
  45. "\taddonId: '" + attrs.addonId + "',\n" +
  46. "\tprivateBrowsingId: '" + attrs.privateBrowsingId + "',\n" +
  47. "\tfirstPartyDomain: '" + attrs.firstPartyDomain + "'\n}");
  48. }
  49. function checkValues(attrs, values) {
  50. values = values || {};
  51. //printAttrs("attrs", attrs);
  52. //printAttrs("values", values);
  53. do_check_eq(attrs.appId, values.appId || 0);
  54. do_check_eq(attrs.userContextId, values.userContextId || 0);
  55. do_check_eq(attrs.inIsolatedMozBrowser, values.inIsolatedMozBrowser || false);
  56. do_check_eq(attrs.addonId, values.addonId || '');
  57. do_check_eq(attrs.privateBrowsingId, values.privateBrowsingId || '');
  58. do_check_eq(attrs.firstPartyDomain, values.firstPartyDomain || '');
  59. }
  60. function run_test() {
  61. // Attributeless origins.
  62. do_check_eq(ssm.getSystemPrincipal().origin, '[System Principal]');
  63. checkOriginAttributes(ssm.getSystemPrincipal());
  64. var exampleOrg = ssm.createCodebasePrincipal(makeURI('http://example.org'), {});
  65. do_check_eq(exampleOrg.origin, 'http://example.org');
  66. checkOriginAttributes(exampleOrg);
  67. var exampleCom = ssm.createCodebasePrincipal(makeURI('https://www.example.com:123'), {});
  68. do_check_eq(exampleCom.origin, 'https://www.example.com:123');
  69. checkOriginAttributes(exampleCom);
  70. var nullPrin = Cu.getObjectPrincipal(new Cu.Sandbox(null));
  71. do_check_true(/^moz-nullprincipal:\{([0-9]|[a-z]|\-){36}\}$/.test(nullPrin.origin));
  72. checkOriginAttributes(nullPrin);
  73. var ipv6Prin = ssm.createCodebasePrincipal(makeURI('https://[2001:db8::ff00:42:8329]:123'), {});
  74. do_check_eq(ipv6Prin.origin, 'https://[2001:db8::ff00:42:8329]:123');
  75. checkOriginAttributes(ipv6Prin);
  76. var ipv6NPPrin = ssm.createCodebasePrincipal(makeURI('https://[2001:db8::ff00:42:8329]'), {});
  77. do_check_eq(ipv6NPPrin.origin, 'https://[2001:db8::ff00:42:8329]');
  78. checkOriginAttributes(ipv6NPPrin);
  79. var ep = Cu.getObjectPrincipal(Cu.Sandbox([exampleCom, nullPrin, exampleOrg]));
  80. checkOriginAttributes(ep);
  81. checkCrossOrigin(exampleCom, exampleOrg);
  82. checkCrossOrigin(exampleOrg, nullPrin);
  83. // nsEP origins should be in lexical order.
  84. do_check_eq(ep.origin, `[Expanded Principal [${exampleOrg.origin}, ${exampleCom.origin}, ${nullPrin.origin}]]`);
  85. // Make sure createCodebasePrincipal does what the rest of gecko does.
  86. do_check_true(exampleOrg.equals(Cu.getObjectPrincipal(new Cu.Sandbox('http://example.org'))));
  87. //
  88. // Test origin attributes.
  89. //
  90. // Just app.
  91. var exampleOrg_app = ssm.createCodebasePrincipal(makeURI('http://example.org'), {appId: 42});
  92. var nullPrin_app = ssm.createNullPrincipal({appId: 42});
  93. checkOriginAttributes(exampleOrg_app, {appId: 42}, '^appId=42');
  94. checkOriginAttributes(nullPrin_app, {appId: 42}, '^appId=42');
  95. do_check_eq(exampleOrg_app.origin, 'http://example.org^appId=42');
  96. // Just browser.
  97. var exampleOrg_browser = ssm.createCodebasePrincipal(makeURI('http://example.org'), {inIsolatedMozBrowser: true});
  98. var nullPrin_browser = ssm.createNullPrincipal({inIsolatedMozBrowser: true});
  99. checkOriginAttributes(exampleOrg_browser, {inIsolatedMozBrowser: true}, '^inBrowser=1');
  100. checkOriginAttributes(nullPrin_browser, {inIsolatedMozBrowser: true}, '^inBrowser=1');
  101. do_check_eq(exampleOrg_browser.origin, 'http://example.org^inBrowser=1');
  102. // App and browser.
  103. var exampleOrg_appBrowser = ssm.createCodebasePrincipal(makeURI('http://example.org'), {inIsolatedMozBrowser: true, appId: 42});
  104. var nullPrin_appBrowser = ssm.createNullPrincipal({inIsolatedMozBrowser: true, appId: 42});
  105. checkOriginAttributes(exampleOrg_appBrowser, {appId: 42, inIsolatedMozBrowser: true}, '^appId=42&inBrowser=1');
  106. checkOriginAttributes(nullPrin_appBrowser, {appId: 42, inIsolatedMozBrowser: true}, '^appId=42&inBrowser=1');
  107. do_check_eq(exampleOrg_appBrowser.origin, 'http://example.org^appId=42&inBrowser=1');
  108. // App and browser, different domain.
  109. var exampleCom_appBrowser = ssm.createCodebasePrincipal(makeURI('https://www.example.com:123'), {appId: 42, inIsolatedMozBrowser: true});
  110. checkOriginAttributes(exampleCom_appBrowser, {appId: 42, inIsolatedMozBrowser: true}, '^appId=42&inBrowser=1');
  111. do_check_eq(exampleCom_appBrowser.origin, 'https://www.example.com:123^appId=42&inBrowser=1');
  112. // Addon.
  113. var exampleOrg_addon = ssm.createCodebasePrincipal(makeURI('http://example.org'), {addonId: 'dummy'});
  114. checkOriginAttributes(exampleOrg_addon, { addonId: "dummy" }, '^addonId=dummy');
  115. do_check_eq(exampleOrg_addon.origin, 'http://example.org^addonId=dummy');
  116. // First party Uri
  117. var exampleOrg_firstPartyDomain = ssm.createCodebasePrincipal(makeURI('http://example.org'), {firstPartyDomain: 'example.org'});
  118. checkOriginAttributes(exampleOrg_firstPartyDomain, { firstPartyDomain: "example.org" }, '^firstPartyDomain=example.org');
  119. do_check_eq(exampleOrg_firstPartyDomain.origin, 'http://example.org^firstPartyDomain=example.org');
  120. // Make sure we don't crash when serializing principals with UNKNOWN_APP_ID.
  121. try {
  122. let binaryStream = Cc["@mozilla.org/binaryoutputstream;1"].
  123. createInstance(Ci.nsIObjectOutputStream);
  124. let pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
  125. pipe.init(false, false, 0, 0xffffffff, null);
  126. binaryStream.setOutputStream(pipe.outputStream);
  127. binaryStream.writeCompoundObject(simplePrin, Ci.nsISupports, true);
  128. binaryStream.close();
  129. } catch (e) {
  130. do_check_true(true);
  131. }
  132. // Just userContext.
  133. var exampleOrg_userContext = ssm.createCodebasePrincipal(makeURI('http://example.org'), {userContextId: 42});
  134. checkOriginAttributes(exampleOrg_userContext, { userContextId: 42 }, '^userContextId=42');
  135. do_check_eq(exampleOrg_userContext.origin, 'http://example.org^userContextId=42');
  136. // UserContext and Addon.
  137. var exampleOrg_userContextAddon = ssm.createCodebasePrincipal(makeURI('http://example.org'), {addonId: 'dummy', userContextId: 42});
  138. var nullPrin_userContextAddon = ssm.createNullPrincipal({addonId: 'dummy', userContextId: 42});
  139. checkOriginAttributes(exampleOrg_userContextAddon, {addonId: 'dummy', userContextId: 42}, '^addonId=dummy&userContextId=42');
  140. checkOriginAttributes(nullPrin_userContextAddon, {addonId: 'dummy', userContextId: 42}, '^addonId=dummy&userContextId=42');
  141. do_check_eq(exampleOrg_userContextAddon.origin, 'http://example.org^addonId=dummy&userContextId=42');
  142. // UserContext and App.
  143. var exampleOrg_userContextApp = ssm.createCodebasePrincipal(makeURI('http://example.org'), {appId: 24, userContextId: 42});
  144. var nullPrin_userContextApp = ssm.createNullPrincipal({appId: 24, userContextId: 42});
  145. checkOriginAttributes(exampleOrg_userContextApp, {appId: 24, userContextId: 42}, '^appId=24&userContextId=42');
  146. checkOriginAttributes(nullPrin_userContextApp, {appId: 24, userContextId: 42}, '^appId=24&userContextId=42');
  147. do_check_eq(exampleOrg_userContextApp.origin, 'http://example.org^appId=24&userContextId=42');
  148. checkSandboxOriginAttributes(null, {});
  149. checkSandboxOriginAttributes('http://example.org', {});
  150. checkSandboxOriginAttributes('http://example.org', {}, {originAttributes: {}});
  151. checkSandboxOriginAttributes('http://example.org', {appId: 42}, {originAttributes: {appId: 42}});
  152. checkSandboxOriginAttributes(['http://example.org'], {});
  153. checkSandboxOriginAttributes(['http://example.org'], {}, {originAttributes: {}});
  154. checkSandboxOriginAttributes(['http://example.org'], {appId: 42}, {originAttributes: {appId: 42}});
  155. // Check that all of the above are cross-origin.
  156. checkCrossOrigin(exampleOrg_app, exampleOrg);
  157. checkCrossOrigin(exampleOrg_app, nullPrin_app);
  158. checkCrossOrigin(exampleOrg_browser, exampleOrg_app);
  159. checkCrossOrigin(exampleOrg_browser, nullPrin_browser);
  160. checkCrossOrigin(exampleOrg_appBrowser, exampleOrg_app);
  161. checkCrossOrigin(exampleOrg_appBrowser, nullPrin_appBrowser);
  162. checkCrossOrigin(exampleOrg_appBrowser, exampleCom_appBrowser);
  163. checkCrossOrigin(exampleOrg_addon, exampleOrg);
  164. checkCrossOrigin(exampleOrg_firstPartyDomain, exampleOrg);
  165. checkCrossOrigin(exampleOrg_userContext, exampleOrg);
  166. checkCrossOrigin(exampleOrg_userContextAddon, exampleOrg);
  167. checkCrossOrigin(exampleOrg_userContext, exampleOrg_userContextAddon);
  168. checkCrossOrigin(exampleOrg_userContext, exampleOrg_userContextApp);
  169. // Check Principal kinds.
  170. function checkKind(prin, kind) {
  171. do_check_eq(prin.isNullPrincipal, kind == 'nullPrincipal');
  172. do_check_eq(prin.isCodebasePrincipal, kind == 'codebasePrincipal');
  173. do_check_eq(prin.isExpandedPrincipal, kind == 'expandedPrincipal');
  174. do_check_eq(prin.isSystemPrincipal, kind == 'systemPrincipal');
  175. }
  176. checkKind(ssm.createNullPrincipal({}), 'nullPrincipal');
  177. checkKind(ssm.createCodebasePrincipal(makeURI('http://www.example.com'), {}), 'codebasePrincipal');
  178. checkKind(Cu.getObjectPrincipal(Cu.Sandbox([ssm.createCodebasePrincipal(makeURI('http://www.example.com'), {})])), 'expandedPrincipal');
  179. checkKind(ssm.getSystemPrincipal(), 'systemPrincipal');
  180. //
  181. // Test Origin Attribute Manipulation
  182. //
  183. // check that we can create an empty origin attributes dict with default
  184. // members and values.
  185. var emptyAttrs = ChromeUtils.fillNonDefaultOriginAttributes({});
  186. checkValues(emptyAttrs);
  187. var uri = "http://example.org";
  188. var tests = [
  189. [ "", {} ],
  190. [ "^appId=5", {appId: 5} ],
  191. [ "^userContextId=3", {userContextId: 3} ],
  192. [ "^addonId=fooBar", {addonId: "fooBar"} ],
  193. [ "^inBrowser=1", {inIsolatedMozBrowser: true} ],
  194. [ "^firstPartyDomain=example.org", {firstPartyDomain: "example.org"} ],
  195. [ "^appId=3&inBrowser=1&userContextId=6",
  196. {appId: 3, userContextId: 6, inIsolatedMozBrowser: true} ] ];
  197. // check that we can create an origin attributes from an origin properly
  198. tests.forEach(t => {
  199. let attrs = ChromeUtils.createOriginAttributesFromOrigin(uri + t[0]);
  200. checkValues(attrs, t[1]);
  201. do_check_eq(ChromeUtils.originAttributesToSuffix(attrs), t[0]);
  202. });
  203. // check that we can create an origin attributes from a dict properly
  204. tests.forEach(t => {
  205. let attrs = ChromeUtils.fillNonDefaultOriginAttributes(t[1]);
  206. checkValues(attrs, t[1]);
  207. do_check_eq(ChromeUtils.originAttributesToSuffix(attrs), t[0]);
  208. });
  209. // each row in the set_tests array has these values:
  210. // [0] - the suffix used to create an origin attribute from
  211. // [1] - the expected result of creating an origin attribute from [0]
  212. // [2] - the pattern to set on the origin attributes
  213. // [3] - the expected result of setting [2] values on [1]
  214. // [4] - the expected result of creating a suffix from [3]
  215. var set_tests = [
  216. [ "", {}, {appId: 5}, {appId: 5}, "^appId=5" ],
  217. [ "^appId=5", {appId: 5}, {appId: 3}, {appId: 3}, "^appId=3" ],
  218. [ "^appId=5", {appId: 5}, {userContextId: 3}, {appId: 5, userContextId: 3}, "^appId=5&userContextId=3" ],
  219. [ "^appId=5", {appId: 5}, {appId: 3, userContextId: 7}, {appId: 3, userContextId: 7}, "^appId=3&userContextId=7" ] ];
  220. // check that we can set origin attributes values properly
  221. set_tests.forEach(t => {
  222. let orig = ChromeUtils.createOriginAttributesFromOrigin(uri + t[0]);
  223. checkValues(orig, t[1]);
  224. let mod = orig;
  225. for (var key in t[2]) {
  226. mod[key] = t[2][key];
  227. }
  228. checkValues(mod, t[3]);
  229. do_check_eq(ChromeUtils.originAttributesToSuffix(mod), t[4]);
  230. });
  231. // each row in the dflt_tests array has these values:
  232. // [0] - the suffix used to create an origin attribute from
  233. // [1] - the expected result of creating an origin attributes from [0]
  234. // [2] - the expected result after setting userContextId to the default
  235. // [3] - the expected result of creating a suffix from [2]
  236. var dflt_tests = [
  237. [ "", {}, {}, "" ],
  238. [ "^userContextId=3", {userContextId: 3}, {}, "" ],
  239. [ "^appId=5", {appId: 5}, {appId: 5}, "^appId=5" ],
  240. [ "^appId=5&userContextId=3", {appId: 5, userContextId: 3}, {appId: 5}, "^appId=5" ] ];
  241. // check that we can set the userContextId to default properly
  242. dflt_tests.forEach(t => {
  243. let orig = ChromeUtils.createOriginAttributesFromOrigin(uri + t[0]);
  244. checkValues(orig, t[1]);
  245. let mod = orig;
  246. mod['userContextId'] = 0;
  247. checkValues(mod, t[2]);
  248. do_check_eq(ChromeUtils.originAttributesToSuffix(mod), t[3]);
  249. });
  250. // each row in the dflt2_tests array has these values:
  251. // [0] - the suffix used to create an origin attribute from
  252. // [1] - the expected result of creating an origin attributes from [0]
  253. // [2] - the expected result after setting firstPartyUri to the default
  254. // [3] - the expected result of creating a suffix from [2]
  255. var dflt2_tests = [
  256. [ "", {}, {}, "" ],
  257. [ "^firstPartyDomain=foo.com", {firstPartyDomain: "foo.com"}, {}, "" ],
  258. [ "^appId=5", {appId: 5}, {appId: 5}, "^appId=5" ],
  259. [ "^appId=5&firstPartyDomain=foo.com", {appId: 5, firstPartyDomain: "foo.com"}, {appId: 5}, "^appId=5" ] ];
  260. // check that we can set the userContextId to default properly
  261. dflt2_tests.forEach(t => {
  262. let orig = ChromeUtils.createOriginAttributesFromOrigin(uri + t[0]);
  263. checkValues(orig, t[1]);
  264. let mod = orig;
  265. mod['firstPartyDomain'] = "";
  266. checkValues(mod, t[2]);
  267. do_check_eq(ChromeUtils.originAttributesToSuffix(mod), t[3]);
  268. });
  269. }