browser_checkloaduri.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. "use strict";
  2. let ssm = Services.scriptSecurityManager;
  3. // This will show a directory listing, but we never actually load these so that's OK.
  4. const kDummyPage = getRootDirectory(gTestPath);
  5. const kAboutPagesRegistered = Promise.all([
  6. BrowserTestUtils.registerAboutPage(
  7. registerCleanupFunction, "test-chrome-privs", kDummyPage,
  8. Ci.nsIAboutModule.ALLOW_SCRIPT),
  9. BrowserTestUtils.registerAboutPage(
  10. registerCleanupFunction, "test-chrome-privs2", kDummyPage,
  11. Ci.nsIAboutModule.ALLOW_SCRIPT),
  12. BrowserTestUtils.registerAboutPage(
  13. registerCleanupFunction, "test-unknown-linkable", kDummyPage,
  14. Ci.nsIAboutModule.MAKE_LINKABLE | Ci.nsIAboutModule.ALLOW_SCRIPT),
  15. BrowserTestUtils.registerAboutPage(
  16. registerCleanupFunction, "test-unknown-linkable2", kDummyPage,
  17. Ci.nsIAboutModule.MAKE_LINKABLE | Ci.nsIAboutModule.ALLOW_SCRIPT),
  18. BrowserTestUtils.registerAboutPage(
  19. registerCleanupFunction, "test-unknown-unlinkable", kDummyPage,
  20. Ci.nsIAboutModule.ALLOW_SCRIPT),
  21. BrowserTestUtils.registerAboutPage(
  22. registerCleanupFunction, "test-unknown-unlinkable2", kDummyPage,
  23. Ci.nsIAboutModule.ALLOW_SCRIPT),
  24. BrowserTestUtils.registerAboutPage(
  25. registerCleanupFunction, "test-content-unlinkable", kDummyPage,
  26. Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT | Ci.nsIAboutModule.ALLOW_SCRIPT),
  27. BrowserTestUtils.registerAboutPage(
  28. registerCleanupFunction, "test-content-unlinkable2", kDummyPage,
  29. Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT | Ci.nsIAboutModule.ALLOW_SCRIPT),
  30. BrowserTestUtils.registerAboutPage(
  31. registerCleanupFunction, "test-content-linkable", kDummyPage,
  32. Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT | Ci.nsIAboutModule.MAKE_LINKABLE |
  33. Ci.nsIAboutModule.ALLOW_SCRIPT),
  34. BrowserTestUtils.registerAboutPage(
  35. registerCleanupFunction, "test-content-linkable2", kDummyPage,
  36. Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT | Ci.nsIAboutModule.MAKE_LINKABLE |
  37. Ci.nsIAboutModule.ALLOW_SCRIPT),
  38. ]);
  39. const URLs = new Map([
  40. ["http://www.example.com", [
  41. // For each of these entries, the booleans represent whether the parent URI can:
  42. // - load them
  43. // - load them without principal inheritance
  44. // - whether the URI can be created at all (some protocol handlers will
  45. // refuse to create certain variants)
  46. ["http://www.example2.com", true, true, true],
  47. ["feed:http://www.example2.com", false, false, true],
  48. ["https://www.example2.com", true, true, true],
  49. ["chrome://foo/content/bar.xul", false, false, true],
  50. ["feed:chrome://foo/content/bar.xul", false, false, false],
  51. ["view-source:http://www.example2.com", false, false, true],
  52. ["view-source:https://www.example2.com", false, false, true],
  53. ["view-source:feed:http://www.example2.com", false, false, true],
  54. ["feed:view-source:http://www.example2.com", false, false, false],
  55. ["data:text/html,Hi", true, false, true],
  56. ["view-source:data:text/html,Hi", false, false, true],
  57. ["javascript:alert('hi')", true, false, true],
  58. ["moz://a", false, false, true],
  59. ["about:test-chrome-privs", false, false, true],
  60. ["about:test-unknown-unlinkable", false, false, true],
  61. ["about:test-content-unlinkable", false, false, true],
  62. ["about:test-content-linkable", true, true, true],
  63. // Because this page doesn't have SAFE_FOR_UNTRUSTED, the web can't link to it:
  64. ["about:test-unknown-linkable", false, false, true],
  65. ]],
  66. ["feed:http://www.example.com", [
  67. ["http://www.example2.com", true, true, true],
  68. ["feed:http://www.example2.com", true, true, true],
  69. ["https://www.example2.com", true, true, true],
  70. ["feed:https://www.example2.com", true, true, true],
  71. ["chrome://foo/content/bar.xul", false, false, true],
  72. ["feed:chrome://foo/content/bar.xul", false, false, false],
  73. ["view-source:http://www.example2.com", false, false, true],
  74. ["view-source:https://www.example2.com", false, false, true],
  75. ["view-source:feed:http://www.example2.com", false, false, true],
  76. ["feed:view-source:http://www.example2.com", false, false, false],
  77. ["data:text/html,Hi", true, false, true],
  78. ["view-source:data:text/html,Hi", false, false, true],
  79. ["javascript:alert('hi')", true, false, true],
  80. ["moz://a", false, false, true],
  81. ["about:test-chrome-privs", false, false, true],
  82. ["about:test-unknown-unlinkable", false, false, true],
  83. ["about:test-content-unlinkable", false, false, true],
  84. ["about:test-content-linkable", true, true, true],
  85. // Because this page doesn't have SAFE_FOR_UNTRUSTED, the web can't link to it:
  86. ["about:test-unknown-linkable", false, false, true],
  87. ]],
  88. ["view-source:http://www.example.com", [
  89. ["http://www.example2.com", true, true, true],
  90. ["feed:http://www.example2.com", false, false, true],
  91. ["https://www.example2.com", true, true, true],
  92. ["feed:https://www.example2.com", false, false, true],
  93. ["chrome://foo/content/bar.xul", false, false, true],
  94. ["feed:chrome://foo/content/bar.xul", false, false, false],
  95. ["view-source:http://www.example2.com", true, true, true],
  96. ["view-source:https://www.example2.com", true, true, true],
  97. ["view-source:feed:http://www.example2.com", false, false, true],
  98. ["feed:view-source:http://www.example2.com", false, false, false],
  99. ["data:text/html,Hi", true, false, true],
  100. ["view-source:data:text/html,Hi", true, false, true],
  101. ["javascript:alert('hi')", true, false, true],
  102. ["moz://a", false, false, true],
  103. ["about:test-chrome-privs", false, false, true],
  104. ["about:test-unknown-unlinkable", false, false, true],
  105. ["about:test-content-unlinkable", false, false, true],
  106. ["about:test-content-linkable", true, true, true],
  107. // Because this page doesn't have SAFE_FOR_UNTRUSTED, the web can't link to it:
  108. ["about:test-unknown-linkable", false, false, true],
  109. ]],
  110. // about: related tests.
  111. ["about:test-chrome-privs", [
  112. ["about:test-chrome-privs", true, true, true],
  113. ["about:test-chrome-privs2", true, true, true],
  114. ["about:test-chrome-privs2?foo#bar", true, true, true],
  115. ["about:test-chrome-privs2?foo", true, true, true],
  116. ["about:test-chrome-privs2#bar", true, true, true],
  117. ["about:test-unknown-unlinkable", true, true, true],
  118. ["about:test-content-unlinkable", true, true, true],
  119. ["about:test-content-unlinkable?foo", true, true, true],
  120. ["about:test-content-unlinkable?foo#bar", true, true, true],
  121. ["about:test-content-unlinkable#bar", true, true, true],
  122. ["about:test-content-linkable", true, true, true],
  123. ["about:test-unknown-linkable", true, true, true],
  124. ]],
  125. ["about:test-unknown-unlinkable", [
  126. ["about:test-chrome-privs", false, false, true],
  127. // Can link to ourselves:
  128. ["about:test-unknown-unlinkable", true, true, true],
  129. // Can't link to unlinkable content if we're not sure it's privileged:
  130. ["about:test-unknown-unlinkable2", false, false, true],
  131. ["about:test-content-unlinkable", true, true, true],
  132. ["about:test-content-unlinkable2", true, true, true],
  133. ["about:test-content-unlinkable2?foo", true, true, true],
  134. ["about:test-content-unlinkable2?foo#bar", true, true, true],
  135. ["about:test-content-unlinkable2#bar", true, true, true],
  136. ["about:test-content-linkable", true, true, true],
  137. // Because this page doesn't have SAFE_FOR_UNTRUSTED, the web can't link to it:
  138. ["about:test-unknown-linkable", false, false, true],
  139. ]],
  140. ["about:test-content-unlinkable", [
  141. ["about:test-chrome-privs", false, false, true],
  142. // Can't link to unlinkable content if we're not sure it's privileged:
  143. ["about:test-unknown-unlinkable", false, false, true],
  144. ["about:test-content-unlinkable", true, true, true],
  145. ["about:test-content-unlinkable2", true, true, true],
  146. ["about:test-content-unlinkable2?foo", true, true, true],
  147. ["about:test-content-unlinkable2?foo#bar", true, true, true],
  148. ["about:test-content-unlinkable2#bar", true, true, true],
  149. ["about:test-content-linkable", true, true, true],
  150. ["about:test-unknown-linkable", false, false, true],
  151. ]],
  152. ["about:test-unknown-linkable", [
  153. ["about:test-chrome-privs", false, false, true],
  154. // Linkable content can't link to unlinkable content.
  155. ["about:test-unknown-unlinkable", false, false, true],
  156. ["about:test-content-unlinkable", false, false, true],
  157. ["about:test-content-unlinkable2", false, false, true],
  158. ["about:test-content-unlinkable2?foo", false, false, true],
  159. ["about:test-content-unlinkable2?foo#bar", false, false, true],
  160. ["about:test-content-unlinkable2#bar", false, false, true],
  161. // ... but it can link to other linkable content.
  162. ["about:test-content-linkable", true, true, true],
  163. // Can link to ourselves:
  164. ["about:test-unknown-linkable", true, true, true],
  165. // Because this page doesn't have SAFE_FOR_UNTRUSTED, the web can't link to it:
  166. ["about:test-unknown-linkable2", false, false, true],
  167. ]],
  168. ["about:test-content-linkable", [
  169. ["about:test-chrome-privs", false, false, true],
  170. // Linkable content can't link to unlinkable content.
  171. ["about:test-unknown-unlinkable", false, false, true],
  172. ["about:test-content-unlinkable", false, false, true],
  173. // ... but it can link to itself and other linkable content.
  174. ["about:test-content-linkable", true, true, true],
  175. ["about:test-content-linkable2", true, true, true],
  176. // Because this page doesn't have SAFE_FOR_UNTRUSTED, the web can't link to it:
  177. ["about:test-unknown-linkable", false, false, true],
  178. ]],
  179. ]);
  180. function testURL(source, target, canLoad, canLoadWithoutInherit, canCreate, flags) {
  181. function getPrincipalDesc(principal) {
  182. if (principal.URI) {
  183. return principal.URI.spec;
  184. }
  185. if (principal.isSystemPrincipal) {
  186. return "system principal";
  187. }
  188. if (principal.isNullPrincipal) {
  189. return "null principal";
  190. }
  191. return "unknown principal";
  192. }
  193. let threw = false;
  194. let targetURI;
  195. try {
  196. targetURI = makeURI(target);
  197. } catch (ex) {
  198. ok(!canCreate, "Shouldn't be passing URIs that we can't create. Failed to create: " + target);
  199. return;
  200. }
  201. ok(canCreate, "Created a URI for " + target + " which should " +
  202. (canCreate ? "" : "not ") + "be possible.");
  203. try {
  204. ssm.checkLoadURIWithPrincipal(source, targetURI, flags);
  205. } catch (ex) {
  206. info(ex.message);
  207. threw = true;
  208. }
  209. let inheritDisallowed = flags & ssm.DISALLOW_INHERIT_PRINCIPAL;
  210. let shouldThrow = inheritDisallowed ? !canLoadWithoutInherit : !canLoad;
  211. ok(threw == shouldThrow,
  212. "Should " + (shouldThrow ? "" : "not ") + "throw an error when loading " +
  213. target + " from " + getPrincipalDesc(source) +
  214. (inheritDisallowed ? " without" : " with") + " principal inheritance.");
  215. }
  216. add_task(function* () {
  217. yield kAboutPagesRegistered;
  218. let baseFlags = ssm.STANDARD | ssm.DONT_REPORT_ERRORS;
  219. for (let [sourceString, targetsAndExpectations] of URLs) {
  220. let source;
  221. if (sourceString.startsWith("about:test-chrome-privs")) {
  222. source = ssm.getSystemPrincipal();
  223. } else {
  224. source = ssm.createCodebasePrincipal(makeURI(sourceString), {});
  225. }
  226. for (let [target, canLoad, canLoadWithoutInherit, canCreate] of targetsAndExpectations) {
  227. testURL(source, target, canLoad, canLoadWithoutInherit, canCreate, baseFlags);
  228. testURL(source, target, canLoad, canLoadWithoutInherit, canCreate,
  229. baseFlags | ssm.DISALLOW_INHERIT_PRINCIPAL);
  230. }
  231. }
  232. // Now test blob URIs, which we need to do in-content.
  233. yield BrowserTestUtils.withNewTab("http://www.example.com/", function* (browser) {
  234. yield ContentTask.spawn(
  235. browser,
  236. testURL.toString(),
  237. function* (testURLFn) {
  238. let testURL = eval("(" + testURLFn + ")");
  239. let ssm = Services.scriptSecurityManager;
  240. let baseFlags = ssm.STANDARD | ssm.DONT_REPORT_ERRORS;
  241. let makeURI = Cu.import("resource://gre/modules/BrowserUtils.jsm", {}).BrowserUtils.makeURI;
  242. let b = new content.Blob(["I am a blob"]);
  243. let contentBlobURI = content.URL.createObjectURL(b);
  244. let contentPrincipal = content.document.nodePrincipal;
  245. // Loading this blob URI from the content page should work:
  246. testURL(contentPrincipal, contentBlobURI, true, true, true, baseFlags);
  247. testURL(contentPrincipal, contentBlobURI, true, true, true,
  248. baseFlags | ssm.DISALLOW_INHERIT_PRINCIPAL);
  249. testURL(contentPrincipal, "view-source:" + contentBlobURI, false, false, true,
  250. baseFlags);
  251. testURL(contentPrincipal, "view-source:" + contentBlobURI, false, false, true,
  252. baseFlags | ssm.DISALLOW_INHERIT_PRINCIPAL);
  253. // Feed URIs for blobs can't be created, so need to pass false as the fourth param.
  254. for (let prefix of ["feed:", "view-source:feed:", "feed:view-source:"]) {
  255. testURL(contentPrincipal, prefix + contentBlobURI, false, false, false,
  256. baseFlags);
  257. testURL(contentPrincipal, prefix + contentBlobURI, false, false, false,
  258. baseFlags | ssm.DISALLOW_INHERIT_PRINCIPAL);
  259. }
  260. }
  261. );
  262. });
  263. });