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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
var { setTimeout } = ChromeUtils.import("resource://gre/modules/Timer.jsm");
let prefs;
let h2Port;
let h3Port;
let listen;
let trrServer;
const dns = Cc["@mozilla.org/network/dns-service;1"].getService(
Ci.nsIDNSService
);
const certOverrideService = Cc[
"@mozilla.org/security/certoverride;1"
].getService(Ci.nsICertOverrideService);
const threadManager = Cc["@mozilla.org/thread-manager;1"].getService(
Ci.nsIThreadManager
);
const mainThread = threadManager.currentThread;
const defaultOriginAttributes = {};
function setup() {
let env = Cc["@mozilla.org/process/environment;1"].getService(
Ci.nsIEnvironment
);
h2Port = env.get("MOZHTTP2_PORT");
Assert.notEqual(h2Port, null);
Assert.notEqual(h2Port, "");
h3Port = env.get("MOZHTTP3_PORT_NO_RESPONSE");
Assert.notEqual(h3Port, null);
Assert.notEqual(h3Port, "");
// Set to allow the cert presented by our H2 server
do_get_profile();
prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
prefs.setBoolPref("network.security.esni.enabled", false);
prefs.setBoolPref("network.http.spdy.enabled", true);
prefs.setBoolPref("network.http.spdy.enabled.http2", true);
// the TRR server is on 127.0.0.1
prefs.setCharPref("network.trr.bootstrapAddress", "127.0.0.1");
// make all native resolve calls "secretly" resolve localhost instead
prefs.setBoolPref("network.dns.native-is-localhost", true);
// 0 - off, 1 - race, 2 TRR first, 3 TRR only, 4 shadow
prefs.setIntPref("network.trr.mode", 2); // TRR first
prefs.setBoolPref("network.trr.wait-for-portal", false);
// don't confirm that TRR is working, just go!
prefs.setCharPref("network.trr.confirmationNS", "skip");
// So we can change the pref without clearing the cache to check a pushed
// record with a TRR path that fails.
Services.prefs.setBoolPref("network.trr.clear-cache-on-pref-change", false);
Services.prefs.setBoolPref("network.http.http3.enabled", true);
// 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 foo.example.com domain name.
const certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
Ci.nsIX509CertDB
);
addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
}
setup();
registerCleanupFunction(async () => {
prefs.clearUserPref("network.security.esni.enabled");
prefs.clearUserPref("network.http.spdy.enabled");
prefs.clearUserPref("network.http.spdy.enabled.http2");
prefs.clearUserPref("network.dns.localDomains");
prefs.clearUserPref("network.dns.native-is-localhost");
prefs.clearUserPref("network.trr.mode");
prefs.clearUserPref("network.trr.uri");
prefs.clearUserPref("network.trr.credentials");
prefs.clearUserPref("network.trr.wait-for-portal");
prefs.clearUserPref("network.trr.allow-rfc1918");
prefs.clearUserPref("network.trr.useGET");
prefs.clearUserPref("network.trr.confirmationNS");
prefs.clearUserPref("network.trr.bootstrapAddress");
prefs.clearUserPref("network.trr.request-timeout");
prefs.clearUserPref("network.trr.clear-cache-on-pref-change");
prefs.clearUserPref("network.dns.upgrade_with_https_rr");
prefs.clearUserPref("network.dns.use_https_rr_as_altsvc");
prefs.clearUserPref("network.dns.echconfig.enabled");
prefs.clearUserPref("network.dns.echconfig.fallback_to_origin");
prefs.clearUserPref("network.dns.httpssvc.reset_exclustion_list");
prefs.clearUserPref("network.http.http3.enabled");
prefs.clearUserPref("network.dns.httpssvc.http3_fast_fallback_timeout");
Services.prefs.clearUserPref(
"network.http.http3.alt-svc-mapping-for-testing"
);
Services.prefs.clearUserPref("network.http.http3.backup_timer_delay");
Services.prefs.clearUserPref("network.http.speculative-parallel-limit");
if (trrServer) {
await trrServer.stop();
}
});
class DNSListener {
constructor() {
this.promise = new Promise(resolve => {
this.resolve = resolve;
});
}
onLookupComplete(inRequest, inRecord, inStatus) {
this.resolve([inRequest, inRecord, inStatus]);
}
// So we can await this as a promise.
then() {
return this.promise.then.apply(this.promise, arguments);
}
}
DNSListener.prototype.QueryInterface = ChromeUtils.generateQI([
"nsIDNSListener",
]);
function makeChan(url) {
let chan = NetUtil.newChannel({
uri: url,
loadUsingSystemPrincipal: true,
}).QueryInterface(Ci.nsIHttpChannel);
chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
return chan;
}
function channelOpenPromise(chan, flags) {
return new Promise(resolve => {
function finish(req, buffer) {
resolve([req, buffer]);
certOverrideService.setDisableAllSecurityChecksAndLetAttackersInterceptMyData(
false
);
}
let internal = chan.QueryInterface(Ci.nsIHttpChannelInternal);
internal.setWaitForHTTPSSVCRecord();
certOverrideService.setDisableAllSecurityChecksAndLetAttackersInterceptMyData(
true
);
chan.asyncOpen(new ChannelListener(finish, null, flags));
});
}
let CheckOnlyHttp2Listener = function() {};
CheckOnlyHttp2Listener.prototype = {
onStartRequest: function testOnStartRequest(request) {},
onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
read_stream(stream, cnt);
},
onStopRequest: function testOnStopRequest(request, status) {
Assert.equal(status, Cr.NS_OK);
let httpVersion = "";
try {
httpVersion = request.protocolVersion;
} catch (e) {}
Assert.equal(httpVersion, "h2");
let routed = "NA";
try {
routed = request.getRequestHeader("Alt-Used");
} catch (e) {}
dump("routed is " + routed + "\n");
Assert.ok(routed === "0" || routed === "NA");
this.finish();
},
};
async function fast_fallback_test() {
let result = 1;
// We need to loop here because we need to wait for AltSvc storage to
// to be started.
// We also do not have a way to verify that HTTP3 has been tried, because
// the fallback is automatic, so try a couple of times.
do {
// We need to close HTTP2 connections, otherwise our connection pooling
// will dispatch the request over already existing HTTP2 connection.
Services.obs.notifyObservers(null, "net:prune-all-connections");
let chan = makeChan(`https://foo.example.com:${h2Port}/`);
let listener = new CheckOnlyHttp2Listener();
await altsvcSetupPromise(chan, listener);
result++;
} while (result < 3);
}
// Test the case when speculative connection is enabled. In this case, when the
// backup connection is ready, the http transaction is still in pending
// queue because the h3 connection is never ready to accept transactions.
add_task(async function test_fast_fallback_with_speculative_connection() {
Services.prefs.setBoolPref("network.http.http3.enabled", true);
Services.prefs.setCharPref("network.dns.localDomains", "foo.example.com");
// Set AltSvc to point to not existing HTTP3 server on port 443
Services.prefs.setCharPref(
"network.http.http3.alt-svc-mapping-for-testing",
"foo.example.com;h3-27=:" + h3Port
);
Services.prefs.setBoolPref("network.dns.disableIPv6", true);
Services.prefs.setIntPref("network.http.speculative-parallel-limit", 6);
await fast_fallback_test();
});
let HTTPObserver = {
observeActivity(
aChannel,
aType,
aSubtype,
aTimestamp,
aSizeData,
aStringData
) {
aChannel.QueryInterface(Ci.nsIChannel);
if (aChannel.URI.spec == `https://foo.example.com:${h2Port}/`) {
if (
aType == Ci.nsIHttpActivityDistributor.ACTIVITY_TYPE_HTTP_TRANSACTION &&
aSubtype ==
Ci.nsIHttpActivityDistributor.ACTIVITY_SUBTYPE_REQUEST_HEADER
) {
// We need to enable speculative connection again, since the backup
// connection is done by using speculative connection.
Services.prefs.setIntPref("network.http.speculative-parallel-limit", 6);
let observerService = Cc[
"@mozilla.org/network/http-activity-distributor;1"
].getService(Ci.nsIHttpActivityDistributor);
observerService.removeObserver(HTTPObserver);
}
}
},
};
// Test the case when speculative connection is disabled. In this case, when the
// back connection is ready, the http transaction is already activated,
// but the socket is not ready to write.
add_task(async function test_fast_fallback_without_speculative_connection() {
// Make sure the h3 connection created by the previous test is cleared.
Services.obs.notifyObservers(null, "net:cancel-all-connections");
await new Promise(resolve => setTimeout(resolve, 1000));
// Clear the h3 excluded list, otherwise the Alt-Svc mapping will not be used.
Services.obs.notifyObservers(null, "network:reset-http3-excluded-list");
Services.prefs.setIntPref("network.http.speculative-parallel-limit", 0);
let observerService = Cc[
"@mozilla.org/network/http-activity-distributor;1"
].getService(Ci.nsIHttpActivityDistributor);
observerService.addObserver(HTTPObserver);
await fast_fallback_test();
Services.prefs.clearUserPref(
"network.http.http3.alt-svc-mapping-for-testing"
);
});
// Test when echConfig is disabled and we have https rr for http3. We use a
// longer timeout in this test, so when fast fallback timer is triggered, the
// http transaction is already activated.
add_task(async function testFastfallback() {
trrServer = new TRRServer();
await trrServer.start();
Services.prefs.setBoolPref("network.dns.upgrade_with_https_rr", true);
Services.prefs.setBoolPref("network.dns.use_https_rr_as_altsvc", true);
Services.prefs.setBoolPref("network.dns.echconfig.enabled", false);
Services.prefs.setIntPref("network.trr.mode", 3);
Services.prefs.setCharPref(
"network.trr.uri",
`https://foo.example.com:${trrServer.port}/dns-query`
);
Services.prefs.setBoolPref("network.http.http3.enabled", true);
Services.prefs.setIntPref(
"network.dns.httpssvc.http3_fast_fallback_timeout",
1000
);
await trrServer.registerDoHAnswers("test.fastfallback.com", "HTTPS", [
{
name: "test.fastfallback.com",
ttl: 55,
type: "HTTPS",
flush: false,
data: {
priority: 1,
name: "test.fastfallback1.com",
values: [
{ key: "alpn", value: "h3-27" },
{ key: "port", value: h3Port },
{ key: "echconfig", value: "456..." },
],
},
},
{
name: "test.fastfallback.com",
ttl: 55,
type: "HTTPS",
flush: false,
data: {
priority: 2,
name: "test.fastfallback2.com",
values: [
{ key: "alpn", value: "h2" },
{ key: "port", value: h2Port },
{ key: "echconfig", value: "456..." },
],
},
},
]);
await trrServer.registerDoHAnswers("test.fastfallback1.com", "A", [
{
name: "test.fastfallback1.com",
ttl: 55,
type: "A",
flush: false,
data: "127.0.0.1",
},
]);
await trrServer.registerDoHAnswers("test.fastfallback2.com", "A", [
{
name: "test.fastfallback2.com",
ttl: 55,
type: "A",
flush: false,
data: "127.0.0.1",
},
]);
let chan = makeChan(`https://test.fastfallback.com/server-timing`);
let [req] = await channelOpenPromise(chan);
Assert.equal(req.protocolVersion, "h2");
let internal = req.QueryInterface(Ci.nsIHttpChannelInternal);
Assert.equal(internal.remotePort, h2Port);
await trrServer.stop();
});
// Like the previous test, but with a shorter timeout, so when fast fallback
// timer is triggered, the http transaction is still in pending queue.
add_task(async function testFastfallback1() {
trrServer = new TRRServer();
await trrServer.start();
Services.prefs.setBoolPref("network.dns.upgrade_with_https_rr", true);
Services.prefs.setBoolPref("network.dns.use_https_rr_as_altsvc", true);
Services.prefs.setBoolPref("network.dns.echconfig.enabled", false);
Services.prefs.setIntPref("network.trr.mode", 3);
Services.prefs.setCharPref(
"network.trr.uri",
`https://foo.example.com:${trrServer.port}/dns-query`
);
Services.prefs.setBoolPref("network.http.http3.enabled", true);
Services.prefs.setIntPref(
"network.dns.httpssvc.http3_fast_fallback_timeout",
10
);
await trrServer.registerDoHAnswers("test.fastfallback.org", "HTTPS", [
{
name: "test.fastfallback.org",
ttl: 55,
type: "HTTPS",
flush: false,
data: {
priority: 1,
name: "test.fastfallback1.org",
values: [
{ key: "alpn", value: "h3-27" },
{ key: "port", value: h3Port },
{ key: "echconfig", value: "456..." },
],
},
},
{
name: "test.fastfallback.org",
ttl: 55,
type: "HTTPS",
flush: false,
data: {
priority: 2,
name: "test.fastfallback2.org",
values: [
{ key: "alpn", value: "h2" },
{ key: "port", value: h2Port },
{ key: "echconfig", value: "456..." },
],
},
},
]);
await trrServer.registerDoHAnswers("test.fastfallback1.org", "A", [
{
name: "test.fastfallback1.org",
ttl: 55,
type: "A",
flush: false,
data: "127.0.0.1",
},
]);
await trrServer.registerDoHAnswers("test.fastfallback2.org", "A", [
{
name: "test.fastfallback2.org",
ttl: 55,
type: "A",
flush: false,
data: "127.0.0.1",
},
]);
let chan = makeChan(`https://test.fastfallback.org/server-timing`);
let [req] = await channelOpenPromise(chan);
Assert.equal(req.protocolVersion, "h2");
let internal = req.QueryInterface(Ci.nsIHttpChannelInternal);
Assert.equal(internal.remotePort, h2Port);
await trrServer.stop();
});
// Test when echConfig is enabled, we can sucessfully fallback to the last
// record.
add_task(async function testFastfallbackWithEchConfig() {
trrServer = new TRRServer();
await trrServer.start();
Services.prefs.setBoolPref("network.dns.upgrade_with_https_rr", true);
Services.prefs.setBoolPref("network.dns.use_https_rr_as_altsvc", true);
Services.prefs.setBoolPref("network.dns.echconfig.enabled", true);
Services.prefs.setIntPref("network.trr.mode", 3);
Services.prefs.setCharPref(
"network.trr.uri",
`https://foo.example.com:${trrServer.port}/dns-query`
);
Services.prefs.setBoolPref("network.http.http3.enabled", true);
Services.prefs.setIntPref(
"network.dns.httpssvc.http3_fast_fallback_timeout",
1000
);
await trrServer.registerDoHAnswers("test.ech.org", "HTTPS", [
{
name: "test.ech.org",
ttl: 55,
type: "HTTPS",
flush: false,
data: {
priority: 1,
name: "test.ech1.org",
values: [
{ key: "alpn", value: "h3-27" },
{ key: "port", value: h3Port },
{ key: "echconfig", value: "456..." },
],
},
},
{
name: "test.ech.org",
ttl: 55,
type: "HTTPS",
flush: false,
data: {
priority: 2,
name: "test.ech2.org",
values: [
{ key: "alpn", value: "h2" },
{ key: "port", value: h2Port },
{ key: "echconfig", value: "456..." },
],
},
},
{
name: "test.ech.org",
ttl: 55,
type: "HTTPS",
flush: false,
data: {
priority: 3,
name: "test.ech3.org",
values: [
{ key: "alpn", value: "h2" },
{ key: "port", value: h2Port },
{ key: "echconfig", value: "456..." },
],
},
},
]);
await trrServer.registerDoHAnswers("test.ech1.org", "A", [
{
name: "test.ech1.org",
ttl: 55,
type: "A",
flush: false,
data: "127.0.0.1",
},
]);
await trrServer.registerDoHAnswers("test.ech3.org", "A", [
{
name: "test.ech3.org",
ttl: 55,
type: "A",
flush: false,
data: "127.0.0.1",
},
]);
let chan = makeChan(`https://test.ech.org/server-timing`);
let [req] = await channelOpenPromise(chan);
Assert.equal(req.protocolVersion, "h2");
let internal = req.QueryInterface(Ci.nsIHttpChannelInternal);
Assert.equal(internal.remotePort, h2Port);
await trrServer.stop();
});
// Test when echConfig is enabled, the connection should fail when not all
// records have echConfig.
add_task(async function testFastfallbackWithpartialEchConfig() {
trrServer = new TRRServer();
await trrServer.start();
Services.prefs.setBoolPref("network.dns.upgrade_with_https_rr", true);
Services.prefs.setBoolPref("network.dns.use_https_rr_as_altsvc", true);
Services.prefs.setBoolPref("network.dns.echconfig.enabled", true);
Services.prefs.setIntPref("network.trr.mode", 3);
Services.prefs.setCharPref(
"network.trr.uri",
`https://foo.example.com:${trrServer.port}/dns-query`
);
Services.prefs.setBoolPref("network.http.http3.enabled", true);
Services.prefs.setIntPref(
"network.dns.httpssvc.http3_fast_fallback_timeout",
1000
);
await trrServer.registerDoHAnswers("test.partial_ech.org", "HTTPS", [
{
name: "test.partial_ech.org",
ttl: 55,
type: "HTTPS",
flush: false,
data: {
priority: 1,
name: "test.partial_ech1.org",
values: [
{ key: "alpn", value: "h3-27" },
{ key: "port", value: h3Port },
{ key: "echconfig", value: "456..." },
],
},
},
{
name: "test.partial_ech.org",
ttl: 55,
type: "HTTPS",
flush: false,
data: {
priority: 2,
name: "test.partial_ech2.org",
values: [
{ key: "alpn", value: "h2" },
{ key: "port", value: h2Port },
],
},
},
]);
await trrServer.registerDoHAnswers("test.partial_ech1.org", "A", [
{
name: "test.partial_ech1.org",
ttl: 55,
type: "A",
flush: false,
data: "127.0.0.1",
},
]);
let chan = makeChan(`https://test.partial_ech.org/server-timing`);
await channelOpenPromise(chan, CL_EXPECT_LATE_FAILURE | CL_ALLOW_UNKNOWN_CL);
await trrServer.stop();
});
|