summaryrefslogtreecommitdiffstats
path: root/security/manager/ssl/tests/mochitest/browser/browser_HSTS.js
blob: f578ac7c4f8e32046f5a5e8f9869608bcf9ce7d4 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests that HTTP Strict Transport Security (HSTS) headers are noted as appropriate.

// Register a cleanup function to clear all accumulated HSTS state when this
// test is done.
add_task(async function register_cleanup() {
  registerCleanupFunction(() => {
    let sss = Cc["@mozilla.org/ssservice;1"].getService(
      Ci.nsISiteSecurityService
    );
    sss.clearAll();
  });
});

// In the absense of HSTS information, no upgrade should happen.
add_task(async function test_no_hsts_information_no_upgrade() {
  let httpUrl =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "http://example.com"
    ) + "some_content.html";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, httpUrl);
  Assert.equal(gBrowser.selectedBrowser.currentURI.scheme, "http");
  gBrowser.removeCurrentTab();
});

// Visit a secure site that sends an HSTS header to set up the rest of the
// test.
add_task(async function see_hsts_header() {
  let setHstsUrl =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "https://example.com"
    ) + "hsts_headers.sjs";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, setHstsUrl);
  gBrowser.removeCurrentTab();
});

// Given a known HSTS host, future http navigations to that domain will be
// upgraded.
add_task(async function test_http_upgrade() {
  let httpUrl =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "http://example.com"
    ) + "some_content.html";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, httpUrl);
  Assert.equal(gBrowser.selectedBrowser.currentURI.scheme, "https");
  gBrowser.removeCurrentTab();
});

// http navigations to unrelated hosts should not be upgraded.
add_task(async function test_unrelated_domain_no_upgrade() {
  let differentHttpUrl =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "http://example.org"
    ) + "some_content.html";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, differentHttpUrl);
  Assert.equal(gBrowser.selectedBrowser.currentURI.scheme, "http");
  gBrowser.removeCurrentTab();
});

// http navigations in private contexts shouldn't use information from
// non-private contexts, so no upgrade should occur.
add_task(async function test_private_window_no_upgrade() {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.security.https_first_pbm", false]],
  });
  let privateWindow = OpenBrowserWindow({ private: true });
  await BrowserTestUtils.firstBrowserLoaded(privateWindow, false);
  let url =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "http://example.com"
    ) + "some_content.html";
  await BrowserTestUtils.openNewForegroundTab(privateWindow.gBrowser, url);
  Assert.equal(
    privateWindow.gBrowser.selectedBrowser.currentURI.scheme,
    "http"
  );
  privateWindow.gBrowser.removeCurrentTab();
  privateWindow.close();
});

// Since the header didn't specify "includeSubdomains", visiting a subdomain
// should not result in an upgrade.
add_task(async function test_subdomain_no_upgrade() {
  let subdomainHttpUrl =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "http://test1.example.com"
    ) + "some_content.html";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, subdomainHttpUrl);
  Assert.equal(gBrowser.selectedBrowser.currentURI.scheme, "http");
  gBrowser.removeCurrentTab();
});

// Now visit a secure site that sends an HSTS header that also includes subdomains.
add_task(async function see_hsts_header_include_subdomains() {
  let setHstsUrl =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "https://example.com"
    ) + "hsts_headers.sjs?includeSubdomains";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, setHstsUrl);
  gBrowser.removeCurrentTab();
});

// Now visiting a subdomain should result in an upgrade.
add_task(async function test_subdomain_upgrade() {
  let subdomainHttpUrl =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "http://test1.example.com"
    ) + "some_content.html";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, subdomainHttpUrl);
  Assert.equal(gBrowser.selectedBrowser.currentURI.scheme, "https");
  gBrowser.removeCurrentTab();
});

// Visiting a subdomain with https should result in an https URL (this isn't an
// upgrade - this test is essentially a consistency check).
add_task(async function test_already_https() {
  let subdomainHttpsUrl =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "https://test2.example.com"
    ) + "some_content.html";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, subdomainHttpsUrl);
  Assert.equal(gBrowser.selectedBrowser.currentURI.scheme, "https");
  gBrowser.removeCurrentTab();
});

// Test that subresources are upgraded.
add_task(async function test_iframe_upgrade() {
  let framedUrl =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "https://example.com"
    ) + "some_content_framed.html";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, framedUrl);
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    await ContentTaskUtils.waitForCondition(() => {
      let frame = content.document.getElementById("frame");
      if (frame) {
        return frame.baseURI.startsWith("https://");
      }
      return false;
    });
  });
  gBrowser.removeCurrentTab();
});

// Clear state.
add_task(async function clear_hsts_state() {
  let sss = Cc["@mozilla.org/ssservice;1"].getService(
    Ci.nsISiteSecurityService
  );
  sss.clearAll();
});

// Make sure this test is valid.
add_task(async function test_no_hsts_information_no_upgrade_again() {
  let httpUrl =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "http://example.com"
    ) + "some_content.html";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, httpUrl);
  Assert.equal(gBrowser.selectedBrowser.currentURI.scheme, "http");
  gBrowser.removeCurrentTab();
});

// Visit a site with an iframe that loads first-party content that sends an
// HSTS header. The header should be heeded because it's first-party.
add_task(async function see_hsts_header_in_framed_first_party_context() {
  let framedUrl =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "https://example.com"
    ) + "hsts_headers_framed.html";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, framedUrl);
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    await ContentTaskUtils.waitForCondition(() => {
      return content.document.getElementById("done");
    });
  });
  gBrowser.removeCurrentTab();
});

// Check that the framed, first-party header was heeded.
add_task(async function test_http_upgrade_after_framed_first_party_header() {
  let httpUrl =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "http://example.com"
    ) + "some_content.html";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, httpUrl);
  Assert.equal(gBrowser.selectedBrowser.currentURI.scheme, "https");
  gBrowser.removeCurrentTab();
});

// Visit a site with an iframe that loads third-party content that sends an
// HSTS header. The header should be ignored because it's third-party.
add_task(async function see_hsts_header_in_third_party_context() {
  let framedUrl =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "https://example.com"
    ) + "hsts_headers_framed.html?third-party";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, framedUrl);
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    await ContentTaskUtils.waitForCondition(() => {
      return content.document.getElementById("done");
    });
  });
  gBrowser.removeCurrentTab();
});

// Since the HSTS header was not received in a first-party context, no upgrade
// should occur.
add_task(async function test_no_upgrade_for_third_party_header() {
  let url =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      "http://example.org"
    ) + "some_content.html";
  await BrowserTestUtils.openNewForegroundTab(gBrowser, url);
  Assert.equal(gBrowser.selectedBrowser.currentURI.scheme, "http");
  gBrowser.removeCurrentTab();
});

// Clear state again.
add_task(async function clear_hsts_state_again() {
  let sss = Cc["@mozilla.org/ssservice;1"].getService(
    Ci.nsISiteSecurityService
  );
  sss.clearAll();
});

// HSTS information encountered in private contexts should not be used in
// non-private contexts.
add_task(
  async function test_no_upgrade_for_HSTS_information_from_private_window() {
    await SpecialPowers.pushPrefEnv({
      set: [["dom.security.https_first_pbm", false]],
    });
    let privateWindow = OpenBrowserWindow({ private: true });
    await BrowserTestUtils.firstBrowserLoaded(privateWindow, false);
    let setHstsUrl =
      getRootDirectory(gTestPath).replace(
        "chrome://mochitests/content",
        "https://example.com"
      ) + "hsts_headers.sjs";
    await BrowserTestUtils.openNewForegroundTab(
      privateWindow.gBrowser,
      setHstsUrl
    );
    privateWindow.gBrowser.removeCurrentTab();

    let httpUrl =
      getRootDirectory(gTestPath).replace(
        "chrome://mochitests/content",
        "http://example.com"
      ) + "some_content.html";
    await BrowserTestUtils.openNewForegroundTab(gBrowser, httpUrl);
    Assert.equal(gBrowser.selectedBrowser.currentURI.scheme, "http");
    gBrowser.removeCurrentTab();

    privateWindow.close();
  }
);