test_extensionURL.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=1161831
  5. -->
  6. <head>
  7. <meta charset="utf-8">
  8. <title>Test for Bug 1161831</title>
  9. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  10. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  11. <script type="application/javascript">
  12. /** Test for Bug 1161831 **/
  13. SimpleTest.waitForExplicitFinish();
  14. var aps = SpecialPowers.Cc["@mozilla.org/addons/policy-service;1"]
  15. .getService(SpecialPowers.Ci.nsIAddonPolicyService).wrappedJSObject;
  16. var oldLoadCallback = aps.setExtensionURILoadCallback(null);
  17. var oldMapCallback = aps.setExtensionURIToAddonIdCallback(null);
  18. var resourceHandler = SpecialPowers.Services.io.getProtocolHandler("resource")
  19. .QueryInterface(SpecialPowers.Ci.nsISubstitutingProtocolHandler);
  20. var extensionHandler = SpecialPowers.Services.io.getProtocolHandler("moz-extension")
  21. .QueryInterface(SpecialPowers.Ci.nsISubstitutingProtocolHandler);
  22. SimpleTest.registerCleanupFunction(function() {
  23. extensionHandler.setSubstitution('cherise', null);
  24. extensionHandler.setSubstitution('liebchen', null);
  25. aps.setExtensionURILoadCallback(oldLoadCallback);
  26. aps.setExtensionURIToAddonIdCallback(oldMapCallback);
  27. });
  28. addLoadEvent(function() {
  29. // First, get a file:// URI to something - open to suggestions on how to do
  30. // this more easily.
  31. var resURI = SpecialPowers.Services.io.newURI('resource://testing-common/resource_test_file.html', null, null);
  32. var filePath = resourceHandler.resolveURI(resURI);
  33. ok(filePath.startsWith('file://'), 'resource:// URI resolves where we expect: ' + filePath);
  34. var fileURI = SpecialPowers.Services.io.newURI(filePath, null, null);
  35. // Register a moz-extension:// URI.
  36. extensionHandler.setSubstitution('cherise', fileURI);
  37. // Alias the above.
  38. extensionHandler.setSubstitution('liebchen', SpecialPowers.Services.io.newURI('moz-extension://cherise', null, null));
  39. //
  40. // Make sure that non-file:// URIs don't work.
  41. //
  42. // resource://
  43. try {
  44. extensionHandler.setSubstitution('interdit', resURI);
  45. ok(false, "Should have thrown for mapping moz-extension to resource");
  46. } catch (e) {
  47. ok(true, "Threw correctly: " + e);
  48. }
  49. // chrome://
  50. try {
  51. var chromeURI = SpecialPowers.Services.io.newURI('chrome://global/content/mozilla.xhtml', null, null);
  52. extensionHandler.setSubstitution('verboten', chromeURI);
  53. ok(false, "Should have thrown for mapping moz-extension to chrome");
  54. } catch (e) {
  55. ok(true, "Threw correctly: " + e);
  56. }
  57. function navigateWithLocation(ifr, url) { ifr.contentWindow.location = url; }
  58. function navigateWithSrc(ifr, url) { ifr.setAttribute('src', url); }
  59. function navigateFromChromeWithLocation(ifr, url) { SpecialPowers.wrap(ifr).contentWindow.location = url; }
  60. function navigateFromChromeWithWebNav(ifr, url) {
  61. SpecialPowers.wrap(ifr).contentWindow
  62. .QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor)
  63. .getInterface(SpecialPowers.Ci.nsIWebNavigation)
  64. .loadURI(url, 0, null, null, null);
  65. }
  66. function setWhitelistCallback(rgxp) {
  67. var cb = SpecialPowers.wrapCallback(function(uri) { return rgxp.test(uri.spec); });
  68. aps.setExtensionURILoadCallback(cb);
  69. }
  70. aps.setExtensionURIToAddonIdCallback(SpecialPowers.wrapCallback(function (uri) { return 'imaginaryaddon-' + uri.host[0]; }));
  71. function testLoad(url, navigate, shouldThrow) {
  72. var ifr = document.createElement('iframe');
  73. var p = new Promise(function(resolve, reject) {
  74. ifr.onload = function() {
  75. ok(true, 'Loaded ' + url);
  76. var prin = SpecialPowers.wrap(ifr.contentWindow).document.nodePrincipal;
  77. function stripTrailingSlash(s) { return s.replace(/\/$/, ''); };
  78. is(stripTrailingSlash(prin.URI.spec), url, 'Principal uri is correct: ' + url);
  79. function stripPath(s) { return s.replace(/(.*\/\/.+)\/.*/, '$1'); };
  80. is(prin.originNoSuffix, stripPath(url), 'Principal origin is correct: ' + prin.originNoSuffix);
  81. is(prin.originAttributes.addonId, 'imaginaryaddon-' + url[url.indexOf('/') + 2], 'addonId is correct');
  82. if (/_blank/.test(url)) {
  83. is(SpecialPowers.wrap(ifr.contentWindow).document.documentElement.innerHTML,
  84. '<head></head><body></body>', 'blank document looks right');
  85. } else {
  86. is(SpecialPowers.wrap(ifr.contentWindow).document.title, 'resource test file',
  87. 'document looks right');
  88. }
  89. ifr.remove();
  90. resolve();
  91. };
  92. document.body.appendChild(ifr);
  93. var threw = false;
  94. try {
  95. navigate(ifr, url);
  96. } catch (e) {
  97. ifr.remove();
  98. threw = true;
  99. ok(/denied|insecure/.test(e), "exception correct: " + e);
  100. }
  101. is(threw, !!shouldThrow, "Correct throwing behavior for: " + url);
  102. !threw || resolve();
  103. });
  104. return p;
  105. }
  106. function testXHR(url, shouldError) {
  107. return new Promise(function(resolve, reject) {
  108. var xhr = new XMLHttpRequest();
  109. xhr.addEventListener("load", () => { ok(!shouldError, `XHR to ${url} should succeed`); resolve(); });
  110. xhr.addEventListener("error", () => { ok(shouldError, `XHR to ${url} should fail`); resolve(); });
  111. xhr.open("GET", url, true);
  112. xhr.send();
  113. });
  114. }
  115. //
  116. // Perform some loads and make sure they work correctly.
  117. //
  118. testLoad.bind(null, 'moz-extension://cherise', navigateFromChromeWithLocation)()
  119. .then(testLoad.bind(null, 'moz-extension://cherise', navigateFromChromeWithWebNav))
  120. .then(testLoad.bind(null, 'moz-extension://cherise', navigateWithLocation, /* shouldThrow = */ true))
  121. .then(testXHR.bind(null, 'moz-extension://cherise', /* shouldError = */ true))
  122. .then(setWhitelistCallback.bind(null, /cherise/))
  123. .then(testLoad.bind(null, 'moz-extension://cherise', navigateWithLocation))
  124. .then(testXHR.bind(null, 'moz-extension://cherise'))
  125. .then(testLoad.bind(null, 'moz-extension://liebchen', navigateWithLocation, /* shouldThrow = */ true))
  126. .then(testXHR.bind(null, 'moz-extension://liebchen', /* shouldError = */ true))
  127. .then(setWhitelistCallback.bind(null, /cherise|liebchen/))
  128. .then(testLoad.bind(null, 'moz-extension://liebchen', navigateWithLocation))
  129. .then(testLoad.bind(null, 'moz-extension://liebchen', navigateWithSrc))
  130. .then(testLoad.bind(null, 'moz-extension://cherise', navigateWithSrc))
  131. .then(testLoad.bind(null, 'moz-extension://cherise/_blank.html', navigateWithSrc))
  132. .then(SimpleTest.finish.bind(SimpleTest),
  133. function(e) { ok(false, "rejected promise: " + e); SimpleTest.finish() }
  134. );
  135. });
  136. </script>
  137. </head>
  138. <body>
  139. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1161831">Mozilla Bug 1161831</a>
  140. <p id="display"></p>
  141. <div id="content" style="display: none">
  142. </div>
  143. <pre id="test">
  144. </pre>
  145. </body>
  146. </html>