summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_http3_dns_retry.js
blob: 82c1eb3f8109610f8b3b7b726503fc6ddf84c8d6 (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
278
279
280
281
282
283
/* 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";

var { setTimeout } = ChromeUtils.importESModule(
  "resource://gre/modules/Timer.sys.mjs"
);

let h2Port;
let h3Port;
let trrServer;

const { TestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TestUtils.sys.mjs"
);
const certOverrideService = Cc[
  "@mozilla.org/security/certoverride;1"
].getService(Ci.nsICertOverrideService);

add_setup(async function setup() {
  h2Port = Services.env.get("MOZHTTP2_PORT");
  Assert.notEqual(h2Port, null);
  Assert.notEqual(h2Port, "");

  h3Port = Services.env.get("MOZHTTP3_PORT");
  Assert.notEqual(h3Port, null);
  Assert.notEqual(h3Port, "");

  trr_test_setup();

  if (mozinfo.socketprocess_networking) {
    Services.dns; // Needed to trigger socket process.
    await TestUtils.waitForCondition(() => Services.io.socketProcessLaunched);
  }

  Services.prefs.setIntPref("network.trr.mode", 2); // TRR first
  Services.prefs.setBoolPref("network.http.http3.enable", true);
  Services.prefs.setIntPref("network.http.speculative-parallel-limit", 6);
  Services.prefs.setBoolPref(
    "network.http.http3.block_loopback_ipv6_addr",
    true
  );

  registerCleanupFunction(async () => {
    trr_clear_prefs();
    Services.prefs.clearUserPref("network.http.http3.block_loopback_ipv6_addr");
    if (trrServer) {
      await trrServer.stop();
    }
  });
});

function makeChan(url) {
  let chan = NetUtil.newChannel({
    uri: url,
    loadUsingSystemPrincipal: true,
    contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
  }).QueryInterface(Ci.nsIHttpChannel);
  chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
  return chan;
}

function channelOpenPromise(chan, flags) {
  // eslint-disable-next-line no-async-promise-executor
  return new Promise(async 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));
  });
}

async function registerDoHAnswers(host, ipv4Answers, ipv6Answers, httpsRecord) {
  trrServer = new TRRServer();
  await trrServer.start();

  Services.prefs.setIntPref("network.trr.mode", 3);
  Services.prefs.setCharPref(
    "network.trr.uri",
    `https://foo.example.com:${trrServer.port}/dns-query`
  );

  await trrServer.registerDoHAnswers(host, "HTTPS", {
    answers: httpsRecord,
  });

  await trrServer.registerDoHAnswers(host, "AAAA", {
    answers: ipv6Answers,
  });

  await trrServer.registerDoHAnswers(host, "A", {
    answers: ipv4Answers,
  });

  Services.dns.clearCache(true);
}

// Test if we retry IPv4 address for Http/3 properly.
add_task(async function test_retry_with_ipv4() {
  let host = "test.http3_retry.com";
  let ipv4answers = [
    {
      name: host,
      ttl: 55,
      type: "A",
      flush: false,
      data: "127.0.0.1",
    },
  ];
  // The UDP socket will return connection refused error because we set
  // "network.http.http3.block_loopback_ipv6_addr" to true.
  let ipv6answers = [
    {
      name: host,
      ttl: 55,
      type: "AAAA",
      flush: false,
      data: "::1",
    },
  ];
  let httpsRecord = [
    {
      name: host,
      ttl: 55,
      type: "HTTPS",
      flush: false,
      data: {
        priority: 1,
        name: host,
        values: [
          { key: "alpn", value: "h3-29" },
          { key: "port", value: h3Port },
        ],
      },
    },
  ];

  await registerDoHAnswers(host, ipv4answers, ipv6answers, httpsRecord);

  let chan = makeChan(`https://${host}`);
  let [req] = await channelOpenPromise(chan);
  Assert.equal(req.protocolVersion, "h3-29");

  await trrServer.stop();
});

add_task(async function test_retry_with_ipv4_disabled() {
  let host = "test.http3_retry_ipv4_blocked.com";
  let ipv4answers = [
    {
      name: host,
      ttl: 55,
      type: "A",
      flush: false,
      data: "127.0.0.1",
    },
  ];
  // The UDP socket will return connection refused error because we set
  // "network.http.http3.block_loopback_ipv6_addr" to true.
  let ipv6answers = [
    {
      name: host,
      ttl: 55,
      type: "AAAA",
      flush: false,
      data: "::1",
    },
  ];
  let httpsRecord = [
    {
      name: host,
      ttl: 55,
      type: "HTTPS",
      flush: false,
      data: {
        priority: 1,
        name: host,
        values: [
          { key: "alpn", value: "h3-29" },
          { key: "port", value: h3Port },
        ],
      },
    },
  ];

  await registerDoHAnswers(host, ipv4answers, ipv6answers, httpsRecord);

  let chan = makeChan(`https://${host}`);
  chan.QueryInterface(Ci.nsIHttpChannelInternal);
  chan.setIPv4Disabled();

  await channelOpenPromise(chan, CL_EXPECT_FAILURE);
  await trrServer.stop();
});

// See bug 1837252. There is no way to observe the outcome of this test, because
// the crash in bug 1837252 is only triggered by speculative connection.
// The outcome of this test is no crash.
add_task(async function test_retry_with_ipv4_failed() {
  let host = "test.http3_retry_failed.com";
  // Return a wrong answer intentionally.
  let ipv4answers = [
    {
      name: host,
      ttl: 55,
      type: "AAAA",
      flush: false,
      data: "127.0.0.1",
    },
  ];
  // The UDP socket will return connection refused error because we set
  // "network.http.http3.block_loopback_ipv6_addr" to true.
  let ipv6answers = [
    {
      name: host,
      ttl: 55,
      type: "AAAA",
      flush: false,
      data: "::1",
    },
  ];
  let httpsRecord = [
    {
      name: host,
      ttl: 55,
      type: "HTTPS",
      flush: false,
      data: {
        priority: 1,
        name: host,
        values: [
          { key: "alpn", value: "h3-29" },
          { key: "port", value: h3Port },
        ],
      },
    },
  ];

  await registerDoHAnswers(host, ipv4answers, ipv6answers, httpsRecord);

  // This speculative connection is used to trigger the mechanism to retry
  // Http/3 connection with a IPv4 address.
  // We want to make the connection entry's IP preference known,
  // so DnsAndConnectSocket::mRetryWithDifferentIPFamily will be set to true
  // before the second speculative connection.
  let uri = Services.io.newURI(`https://test.http3_retry_failed.com`);
  Services.io.speculativeConnect(
    uri,
    Services.scriptSecurityManager.getSystemPrincipal(),
    null,
    false
  );

  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(resolve => setTimeout(resolve, 3000));

  // When this speculative connection is created, the connection entry is
  // already set to prefer IPv4. Since we provided an invalid A response,
  // DnsAndConnectSocket::OnLookupComplete is called with an error.
  // Since DnsAndConnectSocket::mRetryWithDifferentIPFamily is true, we do
  // retry DNS lookup. During retry, we should not create UDP connection.
  Services.io.speculativeConnect(
    uri,
    Services.scriptSecurityManager.getSystemPrincipal(),
    null,
    false
  );

  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(resolve => setTimeout(resolve, 3000));
  await trrServer.stop();
});