summaryrefslogtreecommitdiffstats
path: root/dom/security/test/https-only/browser_iframe_test.js
blob: eb4c1a97c316c429bb4713a96b0bfd7599afcc95 (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
// Bug 1658264 - Https-Only: HTTPS-Only and iFrames
// https://bugzilla.mozilla.org/show_bug.cgi?id=1658264
"use strict";

// > How does this test work?
// We're sending a request to file_iframe_test.sjs with various
// browser-configurations. The sjs-file returns a website with two iFrames
// loading the same sjs-file again. One iFrame is same origin (example.com) and
// the other cross-origin (example.org) Each request gets saved in a semicolon
// seperated list of strings. The sjs-file gets initialized with the
// query-string "setup" and the result string can be polled with "results". Each
// string has this format: {top/com/org}-{queryString}-{scheme}. In the end
// we're just checking if all expected requests were recorded and had the
// correct scheme. Requests that are meant to fail should explicitly not be
// contained in the list of results.

// The test loads all tabs and evaluates when all have finished loading
// it may take quite a long time.
// This requires more twice as much as the default 45 seconds per test:
requestLongerTimeout(2);
SimpleTest.requestCompleteLog();

add_task(async function () {
  await setup();

  // Using this variable to parallelize and collect tests
  let testSet = [];

  /*
   * HTTPS-Only Mode disabled
   */
  await SpecialPowers.pushPrefEnv({
    set: [["dom.security.https_only_mode", false]],
  });

  // Top-Level scheme: HTTP
  // NOTE(freddyb): Test case temporarily disabled. See bug 1735565
  /*testSet.push(
    runTest({
      queryString: "test1.1",
      topLevelScheme: "http",

      expectedTopLevel: "http",
      expectedSameOrigin: "http",
      expectedCrossOrigin: "http",
    })
  );*/
  // Top-Level scheme: HTTPS
  testSet.push(
    runTest({
      queryString: "test1.2",
      topLevelScheme: "https",

      expectedTopLevel: "https",
      expectedSameOrigin: "fail",
      expectedCrossOrigin: "fail",
    })
  );

  await Promise.all(testSet);
  testSet = [];
  /*
   * HTTPS-Only Mode enabled, no exception
   */
  await SpecialPowers.pushPrefEnv({
    set: [["dom.security.https_only_mode", true]],
  });

  // Top-Level scheme: HTTP
  testSet.push(
    runTest({
      queryString: "test2.1",
      topLevelScheme: "http",

      expectedTopLevel: "https",
      expectedSameOrigin: "https",
      expectedCrossOrigin: "https",
    })
  );
  // Top-Level scheme: HTTPS
  testSet.push(
    runTest({
      queryString: "test2.2",
      topLevelScheme: "https",

      expectedTopLevel: "https",
      expectedSameOrigin: "https",
      expectedCrossOrigin: "https",
    })
  );

  await Promise.all(testSet);
  testSet = [];

  /*
   * HTTPS-Only enabled, with exceptions
   * for http://example.org and http://example.com
   */
  // Exempting example.org (cross-site) should not affect anything
  await SpecialPowers.pushPermissions([
    {
      type: "https-only-load-insecure",
      allow: true,
      context: "http://example.org",
    },
  ]);
  await SpecialPowers.pushPermissions([
    {
      type: "https-only-load-insecure",
      allow: true,
      context: "http://example.com",
    },
  ]);

  // Top-Level scheme: HTTP
  await runTest({
    queryString: "test3.1",
    topLevelScheme: "http",

    expectedTopLevel: "http",
    expectedSameOrigin: "http",
    expectedCrossOrigin: "http",
  });

  await SpecialPowers.popPermissions();
  await SpecialPowers.pushPermissions([
    {
      type: "https-only-load-insecure",
      allow: true,
      context: "https://example.com",
    },
  ]);
  // Top-Level scheme: HTTPS
  await runTest({
    queryString: "test3.2",
    topLevelScheme: "https",

    expectedTopLevel: "https",
    expectedSameOrigin: "fail",
    expectedCrossOrigin: "fail",
  });

  // Remove permissions again (has to be done manually for some reason?)
  await SpecialPowers.popPermissions();
  await SpecialPowers.popPermissions();

  await evaluate();
});

const SERVER_URL = scheme =>
  `${scheme}://example.com/browser/dom/security/test/https-only/file_iframe_test.sjs?`;
let shouldContain = [];
let shouldNotContain = [];

async function setup() {
  info(`TEST-CASE-setup - A`);
  const response = await fetch(SERVER_URL("https") + "setup");
  info(`TEST-CASE-setup - B`);
  const txt = await response.text();
  info(`TEST-CASE-setup - C`);
  if (txt != "ok") {
    ok(false, "Failed to setup test server.");
    finish();
  }
}

async function evaluate() {
  info(`TEST-CASE-evaluate - A`);
  const response = await fetch(SERVER_URL("https") + "results");
  info(`TEST-CASE-evaluate - B`);
  const requestResults = (await response.text()).split(";");
  info(`TEST-CASE-evaluate - C`);

  shouldContain.map(str =>
    ok(requestResults.includes(str), `Results should contain '${str}'.`)
  );
  shouldNotContain.map(str =>
    ok(!requestResults.includes(str), `Results shouldn't contain '${str}'.`)
  );
}

async function runTest(test) {
  const queryString = test.queryString;
  info(`TEST-CASE-${test.queryString} - runTest BEGIN`);
  await BrowserTestUtils.withNewTab("about:blank", async function (browser) {
    let loaded = BrowserTestUtils.browserLoaded(
      browser,
      false, // includeSubFrames
      SERVER_URL(test.expectedTopLevel) + queryString,
      false // maybeErrorPage
    );
    BrowserTestUtils.startLoadingURIString(
      browser,
      SERVER_URL(test.topLevelScheme) + queryString
    );
    info(`TEST-CASE-${test.queryString} - Before 'await loaded'`);
    await loaded;
    info(`TEST-CASE-${test.queryString} - After 'await loaded'`);
  });
  info(`TEST-CASE-${test.queryString} - After 'await withNewTab'`);

  if (test.expectedTopLevel !== "fail") {
    shouldContain.push(`top-${queryString}-${test.expectedTopLevel}`);
  } else {
    shouldNotContain.push(`top-${queryString}-http`);
    shouldNotContain.push(`top-${queryString}-https`);
  }

  if (test.expectedSameOrigin !== "fail") {
    shouldContain.push(`com-${queryString}-${test.expectedSameOrigin}`);
  } else {
    shouldNotContain.push(`com-${queryString}-http`);
    shouldNotContain.push(`com-${queryString}-https`);
  }

  if (test.expectedCrossOrigin !== "fail") {
    shouldContain.push(`org-${queryString}-${test.expectedCrossOrigin}`);
  } else {
    shouldNotContain.push(`org-${queryString}-http`);
    shouldNotContain.push(`org-${queryString}-https`);
  }
  info(`TEST-CASE-${test.queryString} - runTest END`);
}