3 var gSSService = Cc["@mozilla.org/ssservice;1"].getService(
4 Ci.nsISiteSecurityService
9 observe(subject, topic) {
10 if (topic == "last-pb-context-exited") {
16 var gObserver = new Observer();
19 Services.obs.removeObserver(gObserver, "last-pb-context-exited");
20 gSSService.clearAll();
26 registerCleanupFunction(cleanup);
27 Services.obs.addObserver(gObserver, "last-pb-context-exited");
30 add_test(test_private_browsing1);
31 add_test(test_private_browsing2);
36 function test_part1() {
37 // check that a host not in the list is not identified as an sts host
39 !gSSService.isSecureURI(
40 Services.io.newURI("https://nonexistent.example.com")
44 // check that an ancestor domain is not identified as an sts host
45 ok(!gSSService.isSecureURI(Services.io.newURI("https://com")));
47 // check that the pref to toggle using the preload list works
48 Services.prefs.setBoolPref(
49 "network.stricttransportsecurity.preloadlist",
53 !gSSService.isSecureURI(
54 Services.io.newURI("https://includesubdomains.preloaded.test")
57 Services.prefs.setBoolPref(
58 "network.stricttransportsecurity.preloadlist",
62 gSSService.isSecureURI(
63 Services.io.newURI("https://includesubdomains.preloaded.test")
67 // check that a subdomain is an sts host (includeSubdomains is set)
69 gSSService.isSecureURI(
70 Services.io.newURI("https://subdomain.includesubdomains.preloaded.test")
74 // check that another subdomain is an sts host (includeSubdomains is set)
76 gSSService.isSecureURI(
77 Services.io.newURI("https://a.b.c.def.includesubdomains.preloaded.test")
81 // check that a subdomain is not an sts host (includeSubdomains is not set)
83 !gSSService.isSecureURI(
84 Services.io.newURI("https://subdomain.noincludesubdomains.preloaded.test")
88 // check that a host with a dot on the end won't break anything
90 !gSSService.isSecureURI(
91 Services.io.newURI("https://notsts.nonexistent.example.com.")
95 // check that processing a header with max-age: 0 will remove a preloaded
97 let uri = Services.io.newURI("https://includesubdomains.preloaded.test");
98 let subDomainUri = Services.io.newURI(
99 "https://subdomain.includesubdomains.preloaded.test"
101 gSSService.processHeader(uri, "max-age=0");
102 ok(!gSSService.isSecureURI(uri));
103 ok(!gSSService.isSecureURI(subDomainUri));
104 // check that processing another header (with max-age non-zero) will
105 // re-enable a site's sts status
106 gSSService.processHeader(uri, "max-age=1000");
107 ok(gSSService.isSecureURI(uri));
108 // but this time include subdomains was not set, so test for that
109 ok(!gSSService.isSecureURI(subDomainUri));
110 gSSService.clearAll();
112 // check that processing a header with max-age: 0 from a subdomain of a site
113 // will not remove that (ancestor) site from the list
114 uri = Services.io.newURI(
115 "https://subdomain.noincludesubdomains.preloaded.test"
117 gSSService.processHeader(uri, "max-age=0");
119 gSSService.isSecureURI(
120 Services.io.newURI("https://noincludesubdomains.preloaded.test")
123 ok(!gSSService.isSecureURI(uri));
125 uri = Services.io.newURI(
126 "https://subdomain.includesubdomains.preloaded.test"
128 gSSService.processHeader(uri, "max-age=0");
129 // we received a header with "max-age=0", so we have "no information"
130 // regarding the sts state of subdomain.includesubdomains.preloaded.test specifically,
131 // but it is actually still an STS host, because of the preloaded
132 // includesubdomains.preloaded.test including subdomains.
134 // |-- includesubdomains.preloaded.test (in preload list, includes subdomains) IS sts host
135 // |-- subdomain.includesubdomains.preloaded.test IS sts host
136 // | `-- another.subdomain.includesubdomains.preloaded.test IS sts host
137 // `-- sibling.includesubdomains.preloaded.test IS sts host
139 gSSService.isSecureURI(
140 Services.io.newURI("https://includesubdomains.preloaded.test")
144 gSSService.isSecureURI(
145 Services.io.newURI("https://subdomain.includesubdomains.preloaded.test")
149 gSSService.isSecureURI(
150 Services.io.newURI("https://sibling.includesubdomains.preloaded.test")
154 gSSService.isSecureURI(
156 "https://another.subdomain.includesubdomains.preloaded.test"
161 gSSService.processHeader(uri, "max-age=1000");
162 // Here's what we have now:
163 // |-- includesubdomains.preloaded.test (in preload list, includes subdomains) IS sts host
164 // |-- subdomain.includesubdomains.preloaded.test (include subdomains is false) IS sts host
165 // | `-- another.subdomain.includesubdomains.preloaded.test IS sts host
166 // `-- sibling.includesubdomains.preloaded.test IS sts host
167 // Note that another.subdomain.includesubdomains.preloaded.test IS still an sts host, because
168 // there exists a superdomain that is sts and asserts includeSubdomains (namely,
169 // includesubdomains.preloaded.test)
171 gSSService.isSecureURI(
172 Services.io.newURI("https://subdomain.includesubdomains.preloaded.test")
176 gSSService.isSecureURI(
177 Services.io.newURI("https://sibling.includesubdomains.preloaded.test")
181 gSSService.isSecureURI(
183 "https://another.subdomain.includesubdomains.preloaded.test"
188 // Test that an expired non-private browsing entry results in correctly
189 // identifying a host that is on the preload list as no longer sts.
190 // (This happens when we're in regular browsing mode, we get a header from
191 // a site on the preload list, and that header later expires. We need to
192 // then treat that host as no longer an sts host.)
193 // (sanity check first - this should be in the preload list)
194 uri = Services.io.newURI("https://includesubdomains2.preloaded.test");
195 ok(gSSService.isSecureURI(uri));
196 gSSService.processHeader(uri, "max-age=1");
197 do_timeout(1250, function () {
198 ok(!gSSService.isSecureURI(uri));
203 const PRIVATE_ORIGIN_ATTRIBUTES = { privateBrowsingId: 1 };
205 function test_private_browsing1() {
206 gSSService.clearAll();
207 let uri = Services.io.newURI("https://includesubdomains.preloaded.test");
208 let subDomainUri = Services.io.newURI(
209 "https://a.b.c.subdomain.includesubdomains.preloaded.test"
211 // sanity - includesubdomains.preloaded.test is preloaded, includeSubdomains set
212 ok(gSSService.isSecureURI(uri, PRIVATE_ORIGIN_ATTRIBUTES));
213 ok(gSSService.isSecureURI(subDomainUri, PRIVATE_ORIGIN_ATTRIBUTES));
215 gSSService.processHeader(uri, "max-age=0", PRIVATE_ORIGIN_ATTRIBUTES);
216 ok(!gSSService.isSecureURI(uri, PRIVATE_ORIGIN_ATTRIBUTES));
217 ok(!gSSService.isSecureURI(subDomainUri, PRIVATE_ORIGIN_ATTRIBUTES));
219 // check adding it back in
220 gSSService.processHeader(uri, "max-age=1000", PRIVATE_ORIGIN_ATTRIBUTES);
221 ok(gSSService.isSecureURI(uri, PRIVATE_ORIGIN_ATTRIBUTES));
222 // but no includeSubdomains this time
223 ok(!gSSService.isSecureURI(subDomainUri, PRIVATE_ORIGIN_ATTRIBUTES));
225 // do the hokey-pokey...
226 gSSService.processHeader(uri, "max-age=0", PRIVATE_ORIGIN_ATTRIBUTES);
227 ok(!gSSService.isSecureURI(uri, PRIVATE_ORIGIN_ATTRIBUTES));
228 ok(!gSSService.isSecureURI(subDomainUri, PRIVATE_ORIGIN_ATTRIBUTES));
230 // Test that an expired private browsing entry results in correctly
231 // identifying a host that is on the preload list as no longer sts.
232 // (This happens when we're in private browsing mode, we get a header from
233 // a site on the preload list, and that header later expires. We need to
234 // then treat that host as no longer an sts host.)
235 // (sanity check first - this should be in the preload list)
236 uri = Services.io.newURI("https://includesubdomains2.preloaded.test");
237 ok(gSSService.isSecureURI(uri, PRIVATE_ORIGIN_ATTRIBUTES));
238 gSSService.processHeader(uri, "max-age=1", PRIVATE_ORIGIN_ATTRIBUTES);
239 do_timeout(1250, function () {
240 ok(!gSSService.isSecureURI(uri, PRIVATE_ORIGIN_ATTRIBUTES));
241 // Simulate leaving private browsing mode
242 Services.obs.notifyObservers(null, "last-pb-context-exited");
246 function test_private_browsing2() {
247 // if this test gets this far, it means there's a private browsing service
249 gSSService.isSecureURI(
250 Services.io.newURI("https://includesubdomains.preloaded.test")
253 // the includesubdomains.preloaded.test entry has includeSubdomains set
255 gSSService.isSecureURI(
256 Services.io.newURI("https://subdomain.includesubdomains.preloaded.test")
260 // Now that we're out of private browsing mode, we need to make sure
261 // we've "forgotten" that we "forgot" this site's sts status.
263 gSSService.isSecureURI(
264 Services.io.newURI("https://includesubdomains2.preloaded.test")