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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
"use strict";
const { HttpServer } = ChromeUtils.importESModule(
"resource://testing-common/httpd.sys.mjs"
);
var h2Port;
var prefs;
var http2pref;
var altsvcpref1;
var altsvcpref2;
// https://foo.example.com:(h2Port)
// https://bar.example.com:(h2Port) <- invalid for bar, but ok for foo
var h1Foo; // server http://foo.example.com:(h1Foo.identity.primaryPort)
var h1Bar; // server http://bar.example.com:(h1bar.identity.primaryPort)
var otherServer; // server socket listening for other connection.
var h2FooRoute; // foo.example.com:H2PORT
var h2BarRoute; // bar.example.com:H2PORT
var h2Route; // :H2PORT
var httpFooOrigin; // http://foo.exmaple.com:PORT/
var httpsFooOrigin; // https://foo.exmaple.com:PORT/
var httpBarOrigin; // http://bar.example.com:PORT/
var httpsBarOrigin; // https://bar.example.com:PORT/
function run_test() {
h2Port = Services.env.get("MOZHTTP2_PORT");
Assert.notEqual(h2Port, null);
Assert.notEqual(h2Port, "");
// Set to allow the cert presented by our H2 server
do_get_profile();
prefs = Services.prefs;
http2pref = prefs.getBoolPref("network.http.http2.enabled");
altsvcpref1 = prefs.getBoolPref("network.http.altsvc.enabled");
altsvcpref2 = prefs.getBoolPref("network.http.altsvc.oe", true);
prefs.setBoolPref("network.http.http2.enabled", true);
prefs.setBoolPref("network.http.altsvc.enabled", true);
prefs.setBoolPref("network.http.altsvc.oe", true);
prefs.setCharPref(
"network.dns.localDomains",
"foo.example.com, bar.example.com"
);
// The moz-http2 cert is for foo.example.com and is signed by http2-ca.pem
// so add that cert to the trust list as a signing cert. The same cert is used
// for both h2FooRoute and h2BarRoute though it is only valid for
// the foo.example.com domain name.
let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
Ci.nsIX509CertDB
);
addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
h1Foo = new HttpServer();
h1Foo.registerPathHandler("/altsvc-test", h1Server);
h1Foo.registerPathHandler("/.well-known/http-opportunistic", h1ServerWK);
h1Foo.start(-1);
h1Foo.identity.setPrimary(
"http",
"foo.example.com",
h1Foo.identity.primaryPort
);
h1Bar = new HttpServer();
h1Bar.registerPathHandler("/altsvc-test", h1Server);
h1Bar.start(-1);
h1Bar.identity.setPrimary(
"http",
"bar.example.com",
h1Bar.identity.primaryPort
);
h2FooRoute = "foo.example.com:" + h2Port;
h2BarRoute = "bar.example.com:" + h2Port;
h2Route = ":" + h2Port;
httpFooOrigin = "http://foo.example.com:" + h1Foo.identity.primaryPort + "/";
httpsFooOrigin = "https://" + h2FooRoute + "/";
httpBarOrigin = "http://bar.example.com:" + h1Bar.identity.primaryPort + "/";
httpsBarOrigin = "https://" + h2BarRoute + "/";
dump(
"http foo - " +
httpFooOrigin +
"\n" +
"https foo - " +
httpsFooOrigin +
"\n" +
"http bar - " +
httpBarOrigin +
"\n" +
"https bar - " +
httpsBarOrigin +
"\n"
);
doTest1();
}
function h1Server(metadata, response) {
response.setStatusLine(metadata.httpVersion, 200, "OK");
response.setHeader("Content-Type", "text/plain", false);
response.setHeader("Connection", "close", false);
response.setHeader("Cache-Control", "no-cache", false);
response.setHeader("Access-Control-Allow-Origin", "*", false);
response.setHeader("Access-Control-Allow-Method", "GET", false);
response.setHeader("Access-Control-Allow-Headers", "x-altsvc", false);
try {
var hval = "h2=" + metadata.getHeader("x-altsvc");
response.setHeader("Alt-Svc", hval, false);
} catch (e) {}
var body = "Q: What did 0 say to 8? A: Nice Belt!\n";
response.bodyOutputStream.write(body, body.length);
}
function h1ServerWK(metadata, response) {
response.setStatusLine(metadata.httpVersion, 200, "OK");
response.setHeader("Content-Type", "application/json", false);
response.setHeader("Connection", "close", false);
response.setHeader("Cache-Control", "no-cache", false);
response.setHeader("Access-Control-Allow-Origin", "*", false);
response.setHeader("Access-Control-Allow-Method", "GET", false);
response.setHeader("Access-Control-Allow-Headers", "x-altsvc", false);
var body = '["http://foo.example.com:' + h1Foo.identity.primaryPort + '"]';
response.bodyOutputStream.write(body, body.length);
}
function resetPrefs() {
prefs.setBoolPref("network.http.http2.enabled", http2pref);
prefs.setBoolPref("network.http.altsvc.enabled", altsvcpref1);
prefs.setBoolPref("network.http.altsvc.oe", altsvcpref2);
prefs.clearUserPref("network.dns.localDomains");
prefs.clearUserPref("network.security.ports.banned");
}
function makeChan(origin) {
return NetUtil.newChannel({
uri: origin + "altsvc-test",
loadUsingSystemPrincipal: true,
}).QueryInterface(Ci.nsIHttpChannel);
}
var origin;
var xaltsvc;
var loadWithoutClearingMappings = false;
var disallowH3 = false;
var disallowH2 = false;
var nextTest;
var expectPass = true;
var waitFor = 0;
var originAttributes = {};
var Listener = function () {};
Listener.prototype = {
onStartRequest: function testOnStartRequest(request) {
Assert.ok(request instanceof Ci.nsIHttpChannel);
if (expectPass) {
if (!Components.isSuccessCode(request.status)) {
do_throw(
"Channel should have a success code! (" + request.status + ")"
);
}
Assert.equal(request.responseStatus, 200);
} else {
Assert.equal(Components.isSuccessCode(request.status), false);
}
},
onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
read_stream(stream, cnt);
},
onStopRequest: function testOnStopRequest(request, status) {
var routed = "";
try {
routed = request.getRequestHeader("Alt-Used");
} catch (e) {}
dump("routed is " + routed + "\n");
Assert.equal(Components.isSuccessCode(status), expectPass);
if (waitFor != 0) {
Assert.equal(routed, "");
do_test_pending();
loadWithoutClearingMappings = true;
do_timeout(waitFor, doTest);
waitFor = 0;
xaltsvc = "NA";
} else if (xaltsvc == "NA") {
Assert.equal(routed, "");
nextTest();
} else if (routed == xaltsvc) {
Assert.equal(routed, xaltsvc); // always true, but a useful log
nextTest();
} else {
dump("poll later for alt svc mapping\n");
do_test_pending();
loadWithoutClearingMappings = true;
do_timeout(500, doTest);
}
do_test_finished();
},
};
function testsDone() {
dump("testDone\n");
resetPrefs();
do_test_pending();
otherServer.close();
do_test_pending();
h1Foo.stop(do_test_finished);
do_test_pending();
h1Bar.stop(do_test_finished);
}
function doTest() {
dump("execute doTest " + origin + "\n");
var chan = makeChan(origin);
var listener = new Listener();
if (xaltsvc != "NA") {
chan.setRequestHeader("x-altsvc", xaltsvc, false);
}
if (loadWithoutClearingMappings) {
chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
} else {
chan.loadFlags =
Ci.nsIRequest.LOAD_FRESH_CONNECTION |
Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
}
if (disallowH3) {
let internalChannel = chan.QueryInterface(Ci.nsIHttpChannelInternal);
internalChannel.allowHttp3 = false;
disallowH3 = false;
}
if (disallowH2) {
let internalChannel = chan.QueryInterface(Ci.nsIHttpChannelInternal);
internalChannel.allowSpdy = false;
disallowH2 = false;
}
loadWithoutClearingMappings = false;
chan.loadInfo.originAttributes = originAttributes;
chan.asyncOpen(listener);
}
// xaltsvc is overloaded to do two things..
// 1] it is sent in the x-altsvc request header, and the response uses the value in the Alt-Svc response header
// 2] the test polls until necko sets Alt-Used to that value (i.e. it uses that route)
//
// When xaltsvc is set to h2Route (i.e. :port with the implied hostname) it doesn't match the alt-used,
// which is always explicit, so it needs to be changed after the channel is created but before the
// listener is invoked
// http://foo served from h2=:port
function doTest1() {
dump("doTest1()\n");
origin = httpFooOrigin;
xaltsvc = h2Route;
nextTest = doTest2;
do_test_pending();
doTest();
xaltsvc = h2FooRoute;
}
// http://foo served from h2=foo:port
function doTest2() {
dump("doTest2()\n");
origin = httpFooOrigin;
xaltsvc = h2FooRoute;
nextTest = doTest3;
do_test_pending();
doTest();
}
// http://foo served from h2=bar:port
// requires cert for foo
function doTest3() {
dump("doTest3()\n");
origin = httpFooOrigin;
xaltsvc = h2BarRoute;
nextTest = doTest4;
do_test_pending();
doTest();
}
// https://bar should fail because host bar has cert for foo
function doTest4() {
dump("doTest4()\n");
origin = httpsBarOrigin;
xaltsvc = "";
expectPass = false;
nextTest = doTest5;
do_test_pending();
doTest();
}
// https://foo no alt-svc (just check cert setup)
function doTest5() {
dump("doTest5()\n");
origin = httpsFooOrigin;
xaltsvc = "NA";
expectPass = true;
nextTest = doTest6;
do_test_pending();
doTest();
}
// https://foo via bar (bar has cert for foo)
function doTest6() {
dump("doTest6()\n");
origin = httpsFooOrigin;
xaltsvc = h2BarRoute;
nextTest = doTest7;
do_test_pending();
doTest();
}
// check again https://bar should fail because host bar has cert for foo
function doTest7() {
dump("doTest7()\n");
origin = httpsBarOrigin;
xaltsvc = "";
expectPass = false;
nextTest = doTest8;
do_test_pending();
doTest();
}
// http://bar via h2 on bar
// should not use TLS/h2 because h2BarRoute is not auth'd for bar
// however the test ought to PASS (i.e. get a 200) because fallback
// to plaintext happens.. thus the timeout
function doTest8() {
dump("doTest8()\n");
origin = httpBarOrigin;
xaltsvc = h2BarRoute;
expectPass = true;
waitFor = 500;
nextTest = doTest9;
do_test_pending();
doTest();
}
// http://bar served from h2=:port, which is like the bar route in 8
function doTest9() {
dump("doTest9()\n");
origin = httpBarOrigin;
xaltsvc = h2Route;
expectPass = true;
waitFor = 500;
nextTest = doTest10;
do_test_pending();
doTest();
xaltsvc = h2BarRoute;
}
// check again https://bar should fail because host bar has cert for foo
function doTest10() {
dump("doTest10()\n");
origin = httpsBarOrigin;
xaltsvc = "";
expectPass = false;
nextTest = doTest11;
do_test_pending();
doTest();
}
// http://bar served from h2=foo, should fail because host foo only has
// cert for foo. Fail in this case means alt-svc is not used, but content
// is served
function doTest11() {
dump("doTest11()\n");
origin = httpBarOrigin;
xaltsvc = h2FooRoute;
expectPass = true;
waitFor = 500;
nextTest = doTest12;
do_test_pending();
doTest();
}
// Test 12-15:
// Insert a cache of http://foo served from h2=:port with origin attributes.
function doTest12() {
dump("doTest12()\n");
origin = httpFooOrigin;
xaltsvc = h2Route;
originAttributes = {
userContextId: 1,
firstPartyDomain: "a.com",
};
nextTest = doTest13;
do_test_pending();
doTest();
xaltsvc = h2FooRoute;
}
// Make sure we get a cache miss with a different userContextId.
function doTest13() {
dump("doTest13()\n");
origin = httpFooOrigin;
xaltsvc = "NA";
originAttributes = {
userContextId: 2,
firstPartyDomain: "a.com",
};
loadWithoutClearingMappings = true;
nextTest = doTest14;
do_test_pending();
doTest();
}
// Make sure we get a cache miss with a different firstPartyDomain.
function doTest14() {
dump("doTest14()\n");
origin = httpFooOrigin;
xaltsvc = "NA";
originAttributes = {
userContextId: 1,
firstPartyDomain: "b.com",
};
loadWithoutClearingMappings = true;
nextTest = doTest15;
do_test_pending();
doTest();
}
//
// Make sure we get a cache hit with the same origin attributes.
function doTest15() {
dump("doTest15()\n");
origin = httpFooOrigin;
xaltsvc = "NA";
originAttributes = {
userContextId: 1,
firstPartyDomain: "a.com",
};
loadWithoutClearingMappings = true;
nextTest = doTest16;
do_test_pending();
doTest();
// This ensures a cache hit.
xaltsvc = h2FooRoute;
}
// Make sure we do not use H2 if it is disabled on a channel.
function doTest16() {
dump("doTest16()\n");
origin = httpFooOrigin;
xaltsvc = "NA";
disallowH2 = true;
originAttributes = {
userContextId: 1,
firstPartyDomain: "a.com",
};
loadWithoutClearingMappings = true;
nextTest = doTest17;
do_test_pending();
doTest();
}
// Make sure we use H2 if only Http3 is disabled on a channel.
function doTest17() {
dump("doTest17()\n");
origin = httpFooOrigin;
xaltsvc = h2Route;
disallowH3 = true;
originAttributes = {
userContextId: 1,
firstPartyDomain: "a.com",
};
loadWithoutClearingMappings = true;
nextTest = doTest18;
do_test_pending();
doTest();
// This should ensures a cache hit.
xaltsvc = h2FooRoute;
}
// Check we don't connect to blocked ports
function doTest18() {
dump("doTest18()\n");
origin = httpFooOrigin;
nextTest = testsDone;
otherServer = Cc["@mozilla.org/network/server-socket;1"].createInstance(
Ci.nsIServerSocket
);
otherServer.init(-1, true, -1);
xaltsvc = "localhost:" + otherServer.port;
Services.prefs.setCharPref(
"network.security.ports.banned",
"" + otherServer.port
);
dump("Blocked port: " + otherServer.port);
waitFor = 500;
otherServer.asyncListen({
onSocketAccepted() {
Assert.ok(false, "Got connection to socket when we didn't expect it!");
},
onStopListening() {
// We get closed when the entire file is done, which guarantees we get the socket accept
// if we do connect to the alt-svc header
do_test_finished();
},
});
nextTest = doTest19;
do_test_pending();
doTest();
}
// Check we don't connect to blocked ports
function doTest19() {
dump("doTest19()\n");
origin = httpFooOrigin;
nextTest = testsDone;
otherServer = Cc["@mozilla.org/network/server-socket;1"].createInstance(
Ci.nsIServerSocket
);
const BAD_PORT_U32 = 6667 + 65536;
otherServer.init(BAD_PORT_U32, true, -1);
Assert.ok(otherServer.port == 6667, "Trying to listen on port 6667");
xaltsvc = "localhost:" + BAD_PORT_U32;
dump("Blocked port: " + otherServer.port);
waitFor = 500;
otherServer.asyncListen({
onSocketAccepted() {
Assert.ok(false, "Got connection to socket when we didn't expect it!");
},
onStopListening() {
// We get closed when the entire file is done, which guarantees we get the socket accept
// if we do connect to the alt-svc header
do_test_finished();
},
});
nextTest = doTest20;
do_test_pending();
doTest();
}
function doTest20() {
dump("doTest20()\n");
origin = httpFooOrigin;
nextTest = testsDone;
otherServer = Cc["@mozilla.org/network/server-socket;1"].createInstance(
Ci.nsIServerSocket
);
const BAD_PORT_U64 = 6666 + 429496729;
otherServer.init(6666, true, -1);
Assert.ok(otherServer.port == 6666, "Trying to listen on port 6666");
xaltsvc = "localhost:" + BAD_PORT_U64;
dump("Blocked port: " + otherServer.port);
waitFor = 500;
otherServer.asyncListen({
onSocketAccepted() {
Assert.ok(false, "Got connection to socket when we didn't expect it!");
},
onStopListening() {
// We get closed when the entire file is done, which guarantees we get the socket accept
// if we do connect to the alt-svc header
do_test_finished();
},
});
nextTest = doTest21;
do_test_pending();
doTest();
}
// Port 65535 should be OK
function doTest21() {
dump("doTest21()\n");
origin = httpFooOrigin;
nextTest = testsDone;
otherServer = Cc["@mozilla.org/network/server-socket;1"].createInstance(
Ci.nsIServerSocket
);
const GOOD_PORT = 65535;
otherServer.init(65535, true, -1);
Assert.ok(otherServer.port == 65535, "Trying to listen on port 65535");
xaltsvc = "localhost:" + GOOD_PORT;
dump("Allowed port: " + otherServer.port);
waitFor = 500;
otherServer.asyncListen({
onSocketAccepted() {
Assert.ok(true, "Got connection to socket when we didn't expect it!");
},
onStopListening() {
// We get closed when the entire file is done, which guarantees we get the socket accept
// if we do connect to the alt-svc header
do_test_finished();
},
});
do_test_pending();
doTest();
}
|