summaryrefslogtreecommitdiffstats
path: root/docshell/test/iframesandbox/test_top_navigation_by_location_exotic.html
blob: fcc1f865967858b09f9b7d45d2efcdcc5eedcd47 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=785310
html5 sandboxed iframe should not be able to perform top navigation with scripts allowed
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 785310 - iframe sandbox top navigation by location via exotic means tests</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script>
  SimpleTest.waitForExplicitFinish();

  var testWin;

  function runScriptNavigationTest(testCase) {
    window.onmessage = function(event) {
      if (event.data.name != 'newWindow') {
        ok(false, "event.data.name: got '" + event.data.name + "', expected 'newWindow'");
      }
      var diag = "top navigation was " + (event.data.blocked ? "" : "NOT ") + "blocked";
      ok((testCase.shouldBeBlocked == event.data.blocked), testCase.desc, diag);
      runNextTest();
    };
    try {
      testWin[testCase.iframeName].eval(testCase.script);
    } catch(e) {
      ok(testCase.shouldBeBlocked, testCase.desc, e.message);
      runNextTest();
    }
  }

  var testCaseIndex = -1;
  testCases = [
    {
      desc: "Test 1: location.replace.call(top.location, ...) should be blocked when sandboxed without allow-top-navigation",
      script: "location.replace.call(top.location, 'file_top_navigation_by_location_exotic.html')",
      iframeName: "if1",
      shouldBeBlocked: true
    },
    {
      desc: "Test 2: location.replace.bind(top.location, ...) should be blocked when sandboxed without allow-top-navigation",
      script: "location.replace.bind(top.location, 'file_top_navigation_by_location_exotic.html')()",
      iframeName: "if1",
      shouldBeBlocked: true
    },
    {
      desc: "Test 3: Function.bind.call(location.replace, top.location, ...) should be blocked when sandboxed without allow-top-navigation",
      script: "Function.bind.call(location.replace, top.location, 'file_top_navigation_by_location_exotic.html')()",
      iframeName: "if1",
      shouldBeBlocked: true
    },
    {
      desc: "Test 4: location.replace.call(top.location, ...) should NOT be blocked when sandboxed with allow-top-navigation",
      script: "location.replace.call(top.location, 'file_top_navigation_by_location_exotic.html')",
      iframeName: "if2",
      shouldBeBlocked: false
    },
    {
      desc: "Test 5: location.replace.bind(top.location, ...) should NOT be blocked when sandboxed with allow-top-navigation",
      script: "location.replace.bind(top.location, 'file_top_navigation_by_location_exotic.html')()",
      iframeName: "if2",
      shouldBeBlocked: false
    },
    {
      desc: "Test 6: Function.bind.call(location.replace, top.location, ...) should NOT be blocked when sandboxed with allow-top-navigation",
      script: "Function.bind.call(location.replace, top.location, 'file_top_navigation_by_location_exotic.html')()",
      iframeName: "if2",
      shouldBeBlocked: false
    },
    {
      desc: "Test 7: top.location.href, via setTimeout, should be blocked when sandboxed without allow-top-navigation",
      script: "setTimeout(function() { try { top.location.href = 'file_top_navigation_by_location_exotic.html' } catch (e) { top.onBlock() } }, 0)",
      iframeName: "if1",
      shouldBeBlocked: true
    },
    {
      desc: "Test 8: top.location.href, via setTimeout, should NOT be blocked when sandboxed with allow-top-navigation",
      script: "setTimeout(function() { try { top.location.href = 'file_top_navigation_by_location_exotic.html' } catch(e) { top.onBlock() } }, 0)",
      iframeName: "if2",
      shouldBeBlocked: false
    },
    {
      desc: "Test 9: top.location.href, via eval, should be blocked when sandboxed without allow-top-navigation",
      script: "eval('top.location.href = \"file_top_navigation_by_location_exotic.html\"')",
      iframeName: "if1",
      shouldBeBlocked: true
    },
    {
      desc: "Test 10: top.location.href, via eval, should NOT be blocked when sandboxed with allow-top-navigation",
      script: "eval('top.location.href = \"file_top_navigation_by_location_exotic.html\"')",
      iframeName: "if2",
      shouldBeBlocked: false
    },
    {
      desc: "Test 11: top.location.href, via anonymous function, should be blocked when sandboxed without allow-top-navigation",
      script: "(function() { top.location.href = 'file_top_navigation_by_location_exotic.html' })()",
      iframeName: "if1",
      shouldBeBlocked: true
    },
    {
      desc: "Test 12: top.location.href, via anonymous function, should NOT be blocked when sandboxed with allow-top-navigation",
      script: "(function() { top.location.href = 'file_top_navigation_by_location_exotic.html' })()",
      iframeName: "if2",
      shouldBeBlocked: false
    },
    {
      desc: "Test 13: top.location.href, via function inserted in top, should be blocked when sandboxed without allow-top-navigation",
      script: "top.doTest = function() { top.location.href = 'file_top_navigation_by_location_exotic.html' }; top.doTest();",
      iframeName: "if1",
      shouldBeBlocked: true
    },
    {
      desc: "Test 14: top.location.href, via function inserted in top, should NOT be blocked when sandboxed with allow-top-navigation",
      script: "top.doTest = function() { top.location.href = 'file_top_navigation_by_location_exotic.html' }; top.doTest();",
      iframeName: "if2",
      shouldBeBlocked: false
    },
    {
      desc: "Test 15: top.location.href, via function inserted in us by top, should NOT be blocked when sandboxed without allow-top-navigation",
      script: "top.eval('window[\"if1\"].doTest = function() { top.location.href = \"file_top_navigation_by_location_exotic.html\" };'), doTest();",
      iframeName: "if1",
      shouldBeBlocked: false
    },
    {
      desc: "Test 16: top.location.href, via function inserted in top, should NOT be blocked when sandboxed with allow-top-navigation",
      script: "top.eval('window[\"if2\"].doTest = function() { top.location.href = \"file_top_navigation_by_location_exotic.html\" };'), doTest();",
      iframeName: "if2",
      shouldBeBlocked: false
    },
    {
      desc: "Test 17: top.location.href, via function in top, should NOT be blocked when sandboxed without allow-top-navigation",
      script: "top.setOwnHref()",
      iframeName: "if1",
      shouldBeBlocked: false
    },
    {
      desc: "Test 18: top.location.href, via function in top, should NOT be blocked when sandboxed with allow-top-navigation",
      script: "top.setOwnHref()",
      iframeName: "if2",
      shouldBeBlocked: false
    },
    {
      desc: "Test 19: top.location.href, via eval in top, should NOT be blocked when sandboxed without allow-top-navigation",
      script: "top.eval('location.href = \"file_top_navigation_by_location_exotic.html\"')",
      iframeName: "if1",
      shouldBeBlocked: false
    },
    {
      desc: "Test 20: top.location.href, via eval in top, should NOT be blocked when sandboxed with allow-top-navigation",
      script: "top.eval('location.href = \"file_top_navigation_by_location_exotic.html\"')",
      iframeName: "if2",
      shouldBeBlocked: false
    },
    {
      desc: "Test 21: top.location.href, via eval in top calling us, should be blocked when sandboxed without allow-top-navigation",
      script: "function doTest() { top.location.href = 'file_top_navigation_by_location_exotic.html' } top.eval('window[\"if1\"].doTest()');",
      iframeName: "if1",
      shouldBeBlocked: true
    },
    {
      desc: "Test 22: top.location.href, via eval in top calling us, should NOT be blocked when sandboxed with allow-top-navigation",
      script: "function doTest() { top.location.href = 'file_top_navigation_by_location_exotic.html' } top.eval('window[\"if2\"].doTest()');",
      iframeName: "if2",
      shouldBeBlocked: false
    },
    {
      desc: "Test 23: top.location.href, via function bound to top, should be blocked when sandboxed without allow-top-navigation",
      script: "(function() { top.location.href = 'file_top_navigation_by_location_exotic.html' }).bind(top)();",
      iframeName: "if1",
      shouldBeBlocked: true
    },
    {
      desc: "Test 24: top.location.href, via function bound to top, should NOT be blocked when sandboxed with allow-top-navigation",
      script: "(function() { top.location.href = 'file_top_navigation_by_location_exotic.html' }).bind(top)();",
      iframeName: "if2",
      shouldBeBlocked: false
    }
  ];

  function runNextTest() {
    ++testCaseIndex;
    if (testCaseIndex == testCases.length) {
      testWin.close();
      SimpleTest.finish();
      return;
    }

    runScriptNavigationTest(testCases[testCaseIndex]);
  }

  window.onmessage = runNextTest;
  testWin = window.open('file_top_navigation_by_location_exotic.html', "newWindow");
</script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=785310">Mozilla Bug 785310</a>
<p id="display"></p>
<div id="content">
Tests for Bug 785310
</div>
</body>
</html>