summaryrefslogtreecommitdiffstats
path: root/caps/tests/mochitest/test_extensionURL.html
blob: 315c473213bfe839ac75b0245d196dd68b835feb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1161831
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1161831</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript">

  /** Test for Bug 1161831 **/
  SimpleTest.waitForExplicitFinish();

  var aps = SpecialPowers.Cc["@mozilla.org/addons/policy-service;1"]
                         .getService(SpecialPowers.Ci.nsIAddonPolicyService).wrappedJSObject;
  var oldLoadCallback = aps.setExtensionURILoadCallback(null);
  var oldMapCallback = aps.setExtensionURIToAddonIdCallback(null);
  var resourceHandler = SpecialPowers.Services.io.getProtocolHandler("resource")
                                     .QueryInterface(SpecialPowers.Ci.nsISubstitutingProtocolHandler);
  var extensionHandler = SpecialPowers.Services.io.getProtocolHandler("moz-extension")
                                     .QueryInterface(SpecialPowers.Ci.nsISubstitutingProtocolHandler);

  SimpleTest.registerCleanupFunction(function() {
      extensionHandler.setSubstitution('cherise', null);
      extensionHandler.setSubstitution('liebchen', null);
      aps.setExtensionURILoadCallback(oldLoadCallback);
      aps.setExtensionURIToAddonIdCallback(oldMapCallback);
  });

  addLoadEvent(function() {

    // First, get a file:// URI to something - open to suggestions on how to do
    //  this more easily.
    var resURI = SpecialPowers.Services.io.newURI('resource://testing-common/resource_test_file.html', null, null);
    var filePath = resourceHandler.resolveURI(resURI);
    ok(filePath.startsWith('file://'), 'resource:// URI resolves where we expect: ' + filePath);
    var fileURI = SpecialPowers.Services.io.newURI(filePath, null, null);

    // Register a moz-extension:// URI.
    extensionHandler.setSubstitution('cherise', fileURI);

    // Alias the above.
    extensionHandler.setSubstitution('liebchen', SpecialPowers.Services.io.newURI('moz-extension://cherise', null, null));

    //
    // Make sure that non-file:// URIs don't work.
    //

    // resource://
    try {
      extensionHandler.setSubstitution('interdit', resURI);
      ok(false, "Should have thrown for mapping moz-extension to resource");
    } catch (e) {
      ok(true, "Threw correctly: " + e);
    }

    // chrome://
    try {
      var chromeURI = SpecialPowers.Services.io.newURI('chrome://global/content/mozilla.xhtml', null, null);
      extensionHandler.setSubstitution('verboten', chromeURI);
      ok(false, "Should have thrown for mapping moz-extension to chrome");
    } catch (e) {
      ok(true, "Threw correctly: " + e);
    }

    function navigateWithLocation(ifr, url) { ifr.contentWindow.location = url; }
    function navigateWithSrc(ifr, url) { ifr.setAttribute('src', url); }
    function navigateFromChromeWithLocation(ifr, url) { SpecialPowers.wrap(ifr).contentWindow.location = url; }
    function navigateFromChromeWithWebNav(ifr, url) {
      SpecialPowers.wrap(ifr).contentWindow
                   .QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor)
                   .getInterface(SpecialPowers.Ci.nsIWebNavigation)
                   .loadURI(url, 0, null, null, null);
    }


    function setWhitelistCallback(rgxp) {
      var cb = SpecialPowers.wrapCallback(function(uri) { return rgxp.test(uri.spec); });
      aps.setExtensionURILoadCallback(cb);
    }

    aps.setExtensionURIToAddonIdCallback(SpecialPowers.wrapCallback(function (uri) { return 'imaginaryaddon-' + uri.host[0]; }));

    function testLoad(url, navigate, shouldThrow) {
      var ifr = document.createElement('iframe');
      var p = new Promise(function(resolve, reject) {
        ifr.onload = function() {
          ok(true, 'Loaded ' + url);
          var prin = SpecialPowers.wrap(ifr.contentWindow).document.nodePrincipal;
          function stripTrailingSlash(s) { return s.replace(/\/$/, ''); };
          is(stripTrailingSlash(prin.URI.spec), url, 'Principal uri is correct: ' + url);
          function stripPath(s) { return s.replace(/(.*\/\/.+)\/.*/, '$1'); };
          is(prin.originNoSuffix, stripPath(url), 'Principal origin is correct: ' + prin.originNoSuffix);
          is(prin.originAttributes.addonId, 'imaginaryaddon-' + url[url.indexOf('/') + 2], 'addonId is correct');
          if (/_blank/.test(url)) {
            is(SpecialPowers.wrap(ifr.contentWindow).document.documentElement.innerHTML,
               '<head></head><body></body>', 'blank document looks right');
          } else {
            is(SpecialPowers.wrap(ifr.contentWindow).document.title, 'resource test file',
               'document looks right');
          }
          ifr.remove();
          resolve();
        };
        document.body.appendChild(ifr);

        var threw = false;
        try {
          navigate(ifr, url);
        } catch (e) {
          ifr.remove();
          threw = true;
          ok(/denied|insecure/.test(e), "exception correct: " + e);
        }
        is(threw, !!shouldThrow, "Correct throwing behavior for: " + url);
        !threw || resolve();
      });

      return p;
    }

    function testXHR(url, shouldError) {
      return new Promise(function(resolve, reject) {
        var xhr = new XMLHttpRequest();
        xhr.addEventListener("load", () => { ok(!shouldError, `XHR to ${url} should succeed`); resolve(); });
        xhr.addEventListener("error", () => { ok(shouldError, `XHR to ${url} should fail`); resolve(); });
        xhr.open("GET", url, true);
        xhr.send();
      });
    }

    //
    // Perform some loads and make sure they work correctly.
    //
    testLoad.bind(null, 'moz-extension://cherise', navigateFromChromeWithLocation)()
    .then(testLoad.bind(null, 'moz-extension://cherise', navigateFromChromeWithWebNav))
    .then(testLoad.bind(null, 'moz-extension://cherise', navigateWithLocation, /* shouldThrow = */ true))
    .then(testXHR.bind(null, 'moz-extension://cherise', /* shouldError = */ true))
    .then(setWhitelistCallback.bind(null, /cherise/))
    .then(testLoad.bind(null, 'moz-extension://cherise', navigateWithLocation))
    .then(testXHR.bind(null, 'moz-extension://cherise'))
    .then(testLoad.bind(null, 'moz-extension://liebchen', navigateWithLocation, /* shouldThrow = */ true))
    .then(testXHR.bind(null, 'moz-extension://liebchen', /* shouldError = */ true))
    .then(setWhitelistCallback.bind(null, /cherise|liebchen/))
    .then(testLoad.bind(null, 'moz-extension://liebchen', navigateWithLocation))
    .then(testLoad.bind(null, 'moz-extension://liebchen', navigateWithSrc))
    .then(testLoad.bind(null, 'moz-extension://cherise', navigateWithSrc))
    .then(testLoad.bind(null, 'moz-extension://cherise/_blank.html', navigateWithSrc))
    .then(SimpleTest.finish.bind(SimpleTest),
          function(e) { ok(false, "rejected promise: " + e); SimpleTest.finish() }
    );
  });

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1161831">Mozilla Bug 1161831</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>
</html>