summaryrefslogtreecommitdiffstats
path: root/caps/tests/mochitest/test_disableScript.xul
blob: cef5f401a291b2ced060c525e29aafda23924ddb (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=840488
-->
<window title="Mozilla Bug 840488"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=840488"
     target="_blank">Mozilla Bug 840488</a>
  </body>

  <iframe id="root" name="root" type="content"/>
  <iframe id="chromeFrame" name="chromeFrame" type="content"/>

  <!-- test code goes here -->
  <script type="application/javascript">
  <![CDATA[

  /** Test for all the different ways that script can be disabled for a given global. **/

  SimpleTest.waitForExplicitFinish();
  const Cu = Components.utils;
  const Ci = Components.interfaces;
  Cu.import("resource://gre/modules/Promise.jsm");
  Cu.import("resource://gre/modules/Services.jsm");
  const ssm = Services.scriptSecurityManager;
  function makeURI(uri) { return Services.io.newURI(uri, null, null); }
  const path = "/tests/caps/tests/mochitest/file_disableScript.html";
  const uri = "http://www.example.com" + path;
  var rootFrame = document.getElementById('root');
  var chromeFrame = document.getElementById('chromeFrame');
  navigateFrame(rootFrame, uri + "?name=rootframe").then(function() {
    navigateFrame(chromeFrame, "file_disableScript.html").then(go);
  });

  function navigateFrame(ifr, src) {
    let deferred = Promise.defer();
    function onload() {
      ifr.removeEventListener('load', onload);
      deferred.resolve();
    }
    ifr.addEventListener('load', onload, false);
    ifr.setAttribute('src', src);
    return deferred.promise;
  }

  function navigateBack(ifr) {
    let deferred = Promise.defer();

    // pageshow events don't fire on the iframe element, so we need to use the
    // chrome event handler for the docshell.
    var browser = ifr.contentWindow
                     .QueryInterface(Ci.nsIInterfaceRequestor)
                     .getInterface(Ci.nsIWebNavigation)
                     .QueryInterface(Ci.nsIDocShell)
                     .chromeEventHandler;
    function onpageshow(evt) {
      info("Navigated back. Persisted: " + evt.persisted);
      browser.removeEventListener('pageshow', onpageshow);
      deferred.resolve();
    }
    browser.addEventListener('pageshow', onpageshow, false);
    ifr.contentWindow.history.back();
    return deferred.promise;
  }

  function addFrame(parentWin, name, expectOnload) {
    let ifr = parentWin.document.createElement('iframe');
    parentWin.document.body.appendChild(ifr);
    ifr.setAttribute('name', name);
    let deferred = Promise.defer();
    // We need to append 'name' to avoid running afoul of recursive frame detection.
    let frameURI = uri + "?name=" + name;
    navigateFrame(ifr, frameURI).then(function() {
      is(String(ifr.contentWindow.location), frameURI, "Successful load");
      is(!!ifr.contentWindow.wrappedJSObject.gFiredOnload, expectOnload,
         "onload should only fire when scripts are enabled");
      deferred.resolve();
    });
    return deferred.promise;
  }

  function checkScriptEnabled(win, expectEnabled) {
    win.wrappedJSObject.gFiredOnclick = false;
    win.document.body.dispatchEvent(new win.Event('click'));
    is(win.wrappedJSObject.gFiredOnclick, expectEnabled, "Checking script-enabled for " + win.name + " (" + win.location + ")");
  }

  function setScriptEnabledForDocShell(win, enabled) {
    win.QueryInterface(Ci.nsIInterfaceRequestor)
       .getInterface(Ci.nsIDocShell)
       .allowJavascript = enabled;
  }

  function testList(expectEnabled, win, list, idx) {
    idx = idx || 0;
    let deferred = Promise.defer();
    let target = list[idx] + path;
    info("Testing scriptability for: " + target + ". expecting " + expectEnabled);
    navigateFrame(win.frameElement, target).then(function() {
      checkScriptEnabled(win, expectEnabled);
      if (idx == list.length - 1)
        deferred.resolve();
      else
      testList(expectEnabled, win, list, idx + 1).then(function() { deferred.resolve(); });
    });
    return deferred.promise;
  }

  function testDomainPolicy(defaultScriptability, exceptions, superExceptions,
                            exempt, notExempt, set, superSet, win) {
    // Populate our sets.
    for (var e of exceptions)
      set.add(makeURI(e));
    for (var e of superExceptions)
      superSet.add(makeURI(e));

    return testList(defaultScriptability, win, notExempt).then(function() {
      return testList(!defaultScriptability, win, exempt);
    });
  }

  function setScriptEnabledForBrowser(enabled) {
    var prefname = "javascript.enabled";
    Services.prefs.setBoolPref(prefname, enabled);
  }

  function reloadFrame(frame) {
    let deferred = Promise.defer();
    frame.addEventListener('load', function onload() {
      deferred.resolve();
      frame.removeEventListener('load', onload);
    }, false);
    frame.contentWindow.location.reload(true);
    return deferred.promise;
  }

  function go() {
    var rootWin = rootFrame.contentWindow;
    var chromeWin = chromeFrame.contentWindow;

    // Test simple docshell enable/disable.
    checkScriptEnabled(rootWin, true);
    setScriptEnabledForDocShell(rootWin, false);
    checkScriptEnabled(rootWin, false);
    setScriptEnabledForDocShell(rootWin, true);
    checkScriptEnabled(rootWin, true);

    // Privileged frames are immune to docshell flags.
    ok(ssm.isSystemPrincipal(chromeWin.document.nodePrincipal), "Sanity check for System Principal");
    setScriptEnabledForDocShell(chromeWin, false);
    checkScriptEnabled(chromeWin, true);
    setScriptEnabledForDocShell(chromeWin, true);

    // Play around with the docshell tree and make sure everything works as
    // we expect.
    addFrame(rootWin, 'parent', true).then(function() {
      checkScriptEnabled(rootWin[0], true);
      return addFrame(rootWin[0], 'childA', true);
    }).then(function() {
      checkScriptEnabled(rootWin[0][0], true);
      setScriptEnabledForDocShell(rootWin[0], false);
      checkScriptEnabled(rootWin, true);
      checkScriptEnabled(rootWin[0], false);
      checkScriptEnabled(rootWin[0][0], false);
      return addFrame(rootWin[0], 'childB', false);
    }).then(function() {
      checkScriptEnabled(rootWin[0][1], false);
      setScriptEnabledForDocShell(rootWin[0][0], false);
      setScriptEnabledForDocShell(rootWin[0], true);
      checkScriptEnabled(rootWin[0], true);
      checkScriptEnabled(rootWin[0][0], false);
      setScriptEnabledForDocShell(rootWin[0][0], true);

      // Flags are inherited from the parent docshell at attach time. Note that
      // the flag itself is inherited, regardless of whether or not scripts are
      // currently allowed on the parent (which could depend on the parent's
      // parent). Check that.
      checkScriptEnabled(rootWin[0][1], false);
      setScriptEnabledForDocShell(rootWin[0], false);
      setScriptEnabledForDocShell(rootWin[0][1], true);
      return addFrame(rootWin[0][1], 'grandchild', false);
    }).then(function() {
      checkScriptEnabled(rootWin[0], false);
      checkScriptEnabled(rootWin[0][1], false);
      checkScriptEnabled(rootWin[0][1][0], false);
      setScriptEnabledForDocShell(rootWin[0], true);
      checkScriptEnabled(rootWin[0], true);
      checkScriptEnabled(rootWin[0][1], true);
      checkScriptEnabled(rootWin[0][1][0], true);

    // Try navigating two frames, then munging docshell scriptability, then
    // pulling the frames out of the bfcache to make sure that flags are
    // properly propagated to inactive inner windows. We do this both for an
    // 'own' docshell, as well as for an ancestor docshell.
      return navigateFrame(rootWin[0][0].frameElement, rootWin[0][0].location + '-navigated');
    }).then(function() { return navigateFrame(rootWin[0][1][0].frameElement, rootWin[0][1][0].location + '-navigated'); })
      .then(function() {
      checkScriptEnabled(rootWin[0][0], true);
      checkScriptEnabled(rootWin[0][1][0], true);
      setScriptEnabledForDocShell(rootWin[0][0], false);
      setScriptEnabledForDocShell(rootWin[0][1], false);
      checkScriptEnabled(rootWin[0][0], false);
      checkScriptEnabled(rootWin[0][1][0], false);
      return navigateBack(rootWin[0][0].frameElement);
    }).then(function() { return navigateBack(rootWin[0][1][0].frameElement); })
      .then(function() {
      checkScriptEnabled(rootWin[0][0], false);
      checkScriptEnabled(rootWin[0][1][0], false);

    // Disable JS via the global pref pref. This is only guaranteed to have an effect
    // for subsequent loads.
      setScriptEnabledForBrowser(false);
      return reloadFrame(rootFrame);
    }).then(function() {
      checkScriptEnabled(rootWin, false);
      checkScriptEnabled(chromeWin, true);
      setScriptEnabledForBrowser(true);
      return reloadFrame(rootFrame);
    }).then(function() {
      checkScriptEnabled(rootWin, true);

    // Play around with dynamically blocking script for a given global.
    // This takes effect immediately.
      Cu.blockScriptForGlobal(rootWin);
      Cu.blockScriptForGlobal(rootWin);
      Cu.unblockScriptForGlobal(rootWin);
      checkScriptEnabled(rootWin, false);
      Cu.unblockScriptForGlobal(rootWin);
      checkScriptEnabled(rootWin, true);
      Cu.blockScriptForGlobal(rootWin);
      try {
        Cu.blockScriptForGlobal(chromeWin);
        ok(false, "Should have thrown");
      } catch (e) {
        ok(/may not be disabled/.test(e),
           "Shouldn't be able to programmatically block script for system globals");
      }
      return reloadFrame(rootFrame);
    }).then(function() {
      checkScriptEnabled(rootWin, true);

    // Test system-wide domain policy. This only takes effect for subsequently-
    // loaded globals.

    // Check the basic semantics of the sets.
    is(ssm.domainPolicyActive, false, "not enabled");
    window.policy = ssm.activateDomainPolicy();
    ok(policy instanceof Ci.nsIDomainPolicy, "Got a policy");
    try {
      ssm.activateDomainPolicy();
      ok(false, "Should have thrown");
    } catch (e) {
      ok(true, "can't have two live domain policies");
    }
    var sbRef = policy.superBlacklist;
    isnot(sbRef, null, "superBlacklist non-null");
    ok(!sbRef.contains(makeURI('http://www.example.com')));
    sbRef.add(makeURI('http://www.example.com/foopy'));
    ok(sbRef.contains(makeURI('http://www.example.com')));
    sbRef.remove(makeURI('http://www.example.com'));
    ok(!sbRef.contains(makeURI('http://www.example.com')));
    sbRef.add(makeURI('http://www.example.com/foopy/this.that/'));
    ok(sbRef.contains(makeURI('http://www.example.com/baz')));
    ok(!sbRef.contains(makeURI('https://www.example.com')));
    ok(!sbRef.contains(makeURI('https://www.example.com:88')));
    ok(!sbRef.contains(makeURI('http://foo.www.example.com')));
    ok(sbRef.containsSuperDomain(makeURI('http://foo.www.example.com')));
    ok(sbRef.containsSuperDomain(makeURI('http://foo.bar.www.example.com')));
    ok(!sbRef.containsSuperDomain(makeURI('http://foo.bar.www.exxample.com')));
    ok(!sbRef.containsSuperDomain(makeURI('http://example.com')));
    ok(!sbRef.containsSuperDomain(makeURI('http://com/this.that/')));
    ok(!sbRef.containsSuperDomain(makeURI('https://foo.www.example.com')));
    ok(sbRef.contains(makeURI('http://www.example.com')));
    policy.deactivate();
    is(ssm.domainPolicyActive, false, "back to inactive");
    ok(!sbRef.contains(makeURI('http://www.example.com')),
       "Disabling domain policy clears the set");
    policy = ssm.activateDomainPolicy();
    ok(policy.superBlacklist);
    isnot(sbRef, policy.superBlacklist, "Mint new sets each time!");
    policy.deactivate();
    is(policy.blacklist, null, "blacklist nulled out");
    policy = ssm.activateDomainPolicy();
    isnot(policy.blacklist, null, "non-null again");
    isnot(policy.blacklist, sbRef, "freshly minted");
    policy.deactivate();

    //
    // Now, create and apply a mock-policy. We check the same policy both as
    // a blacklist and as a whitelist.
    //

    window.testPolicy = {
      // The policy.
      exceptions: ['http://test1.example.com', 'http://example.com'],
      superExceptions: ['http://test2.example.org', 'https://test1.example.com'],

      // The testcases.
      exempt: ['http://test1.example.com', 'http://example.com',
               'http://test2.example.org', 'http://sub1.test2.example.org',
               'https://sub1.test1.example.com'],

      notExempt: ['http://test2.example.com', 'http://sub1.test1.example.com',
                  'http://www.example.com', 'https://test2.example.com',
                  'https://example.com', 'http://test1.example.org'],
    };

    policy = ssm.activateDomainPolicy();
    info("Testing Blacklist-style Domain Policy");
    return testDomainPolicy(true, testPolicy.exceptions,
                            testPolicy.superExceptions, testPolicy.exempt,
                            testPolicy.notExempt, policy.blacklist,
                            policy.superBlacklist, rootWin);
  }).then(function() {
    policy.deactivate();
    policy = ssm.activateDomainPolicy();
    info("Testing Whitelist-style Domain Policy");
    setScriptEnabledForBrowser(false);
    return testDomainPolicy(false, testPolicy.exceptions,
                            testPolicy.superExceptions, testPolicy.exempt,
                            testPolicy.notExempt, policy.whitelist,
                            policy.superWhitelist, rootWin);
  }).then(function() {
    setScriptEnabledForBrowser(true);
    policy.deactivate();

    SimpleTest.finish();
    });
  }

  ]]>
  </script>
</window>