summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_dnr_redirect_transform.js
blob: de01169dea3262d093395a61ba4a33c4c6645a97 (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
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
"use strict";

// The validate_action_redirect_transform task of test_ext_dnr_session_rules.js
// confirms that redirect transform rules meet some minimum bar of validation.
// Despite passing validation, there are still interesting cases to explore,
// ranging from verifying that special characters appear as expected, to
// verifying that an invalid URL (e.g. too long after the transform) is handled
// reasonably well.

add_setup(() => {
  Services.prefs.setBoolPref("extensions.manifestV3.enabled", true);
  Services.prefs.setBoolPref("extensions.dnr.enabled", true);

  // Allow navigation to URLs with embedded credentials, without prompt.
  Services.prefs.setBoolPref("network.auth.confirmAuth.enabled", false);
});

const server = createHttpServer({
  hosts: ["from", "dest", "127.0.0.127", "[::1]", "xn--stra-yna.de", "fqdn."],
});
server.identity.add("http", "dest", 443); // test_redirect_transform_port
server.identity.add("http", "dest", 700); // test_redirect_transform_port
server.identity.add("http", "dest", 777); // Dummy port in test cases.

server.registerPrefixHandler("/", (req, res) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.write("GOOD_RESPONSE");
});

// This function is serialized and called in the context of the test extension's
// background page. dnrTestUtils is passed to the background function.
function makeDnrTestUtils() {
  const dnrTestUtils = {};
  const dnr = browser.declarativeNetRequest;
  function makeRedirectTransformRule(transform) {
    return {
      id: 1,
      condition: { requestDomains: ["from"] },
      action: {
        type: "redirect",
        // redirect to "dest" by default, different from "from", to avoid an
        // infinite redirect loop.
        redirect: { transform: { host: "dest", ...transform } },
      },
    };
  }
  async function setRedirectTransform(transform) {
    await dnr.updateSessionRules({
      removeRuleIds: [1],
      addRules: [makeRedirectTransformRule(transform)],
    });
  }
  // testFetch is simple/fast, but cannot always be used:
  // - when the request URL contains embedded credentials.
  // - when the final URL is supposed to contain a reference fragment.
  async function testFetch(from, to, description) {
    let res = await fetch(from);
    browser.test.assertEq(to, res.url, description);
    browser.test.assertEq("GOOD_RESPONSE", await res.text(), "expected body");
  }
  // testNavigate is the slower, complex version of testFetch. It should be
  // used in tests where the username, password or fragment components of a URL
  // are significant.
  async function testNavigate(from, to, description) {
    let resultPromise = new Promise(resolve => {
      browser.test.onMessage.addListener(function listener(msg, result) {
        if (msg === "test_navigate_result") {
          browser.test.onMessage.removeListener(listener);
          // resolve only resolves on the first call, which is ideal because
          // browser.test.onMessage.removeListener does not work (bug 1428213).
          resolve(result);
        }
      });
    });
    browser.test.sendMessage("test_navigate", from);
    browser.test.assertDeepEq({ from, to }, await resultPromise, description);
  }
  Object.assign(dnrTestUtils, {
    makeRedirectTransformRule,
    setRedirectTransform,
    testFetch,
    testNavigate,
  });
  return dnrTestUtils;
}

async function runAsDNRExtension({ background, manifest }) {
  let extension = ExtensionTestUtils.loadExtension({
    background: `(${background})((${makeDnrTestUtils})())`,
    allowInsecureRequests: true,
    manifest: {
      manifest_version: 3,
      permissions: ["declarativeNetRequest"],
      host_permissions: ["<all_urls>"],
      granted_host_permissions: true,
      web_accessible_resources: [
        { resources: ["war.txt"], matches: ["http://from/*"] },
      ],
      ...manifest,
    },
    temporarilyInstalled: true, // <-- for granted_host_permissions
    files: {
      "war.txt": "GOOD_RESPONSE",
      "nowar.txt": "nowar.txt is not in web_accessible_resources",
    },
  });
  extension.onMessage("test_navigate", async url => {
    // The DNR rule does not redirect the main frame.
    let contentPage = await ExtensionTestUtils.loadContentPage("http://from/");
    info(`Loading ${url}`);
    await contentPage.spawn([url], async url => {
      let { document } = this.content;
      let frame = document.createElement("iframe");
      frame.src = url;
      await new Promise(resolve => {
        frame.onload = resolve;
        document.body.appendChild(frame);
      });
    });
    let finalURL = contentPage.browsingContext.children[0].currentURI.spec;
    await contentPage.close();
    extension.sendMessage("test_navigate_result", { from: url, to: finalURL });
  });
  await extension.startup();
  await extension.awaitFinish();
  await extension.unload();
}

add_task(async function test_redirect_transform_all_at_once() {
  await runAsDNRExtension({
    background: async dnrTestUtils => {
      const { setRedirectTransform, testFetch, testNavigate } = dnrTestUtils;

      await setRedirectTransform({
        scheme: "http",
        username: "a",
        password: "b",
        host: "dest",
        port: "777",
        path: "/d",
        query: "?e",
        queryTransform: null,
        fragment: "#f",
      });
      await testFetch(
        "https://from",
        "http://a:b@dest:777/d?e", // note: fetch cannot see '#f'.
        "Adds components to minimal URL (fetch)"
      );
      await testNavigate(
        "https://from",
        "http://a:b@dest:777/d?e#f",
        "Adds components to minimal URL (navigation)"
      );

      await browser.test.assertRejects(
        testFetch("https://user:pass@from:777/path?query#ref"),
        "Window.fetch: https://user:pass@from:777/path?query#ref is an url with embedded credentials.",
        "fetch does not work with embedded credentials"
      );
      await testNavigate(
        "https://user:pass@from:777/path?query#ref",
        "http://a:b@dest:777/d?e#f",
        "Replaces all components in existing URL (navigation)"
      );

      browser.test.notifyPass();
    },
  });
});

add_task(async function test_redirect_transform_scheme() {
  await runAsDNRExtension({
    background: async dnrTestUtils => {
      const { setRedirectTransform, testFetch, testNavigate } = dnrTestUtils;

      await setRedirectTransform({ scheme: "http" });
      await testFetch("https://from/", "http://dest/", "scheme change");
      await testNavigate(
        "https://user:pass@from:777/path?query#ref",
        "http://user:pass@dest:777/path?query#ref",
        "scheme change in complex URL with embedded credentials"
      );

      await setRedirectTransform({
        scheme: "moz-extension",
        host: location.hostname,
      });
      await testFetch(
        "http://from/war.txt",
        browser.runtime.getURL("war.txt"),
        "Scheme change to moz-extension:-URL"
      );
      await testNavigate(
        "http://from/war.txt",
        browser.runtime.getURL("war.txt"),
        "Scheme change to moz-extension:-URL (navigation)"
      );
      // While the initiator (extension) would be allowed to read the resource
      // due to it being same-origin, the pre-redirect URL (http://from) is not
      // matching web_accessible_resources[].matches, so the load is rejected.
      // This scenario is also tested in test_ext_dnr_without_webrequest.js, at
      // the redirect_request_with_dnr_to_extensionPath task.
      await browser.test.assertRejects(
        testFetch("http://from/nowar.txt"),
        "NetworkError when attempting to fetch resource.",
        "Cannot load redirect to moz-extension: not in web_accessible_resources"
      );

      browser.test.notifyPass();
    },
  });
});

add_task(async function test_redirect_transform_username() {
  await runAsDNRExtension({
    background: async dnrTestUtils => {
      const { setRedirectTransform, testFetch, testNavigate } = dnrTestUtils;

      await setRedirectTransform({ username: "" });
      await testNavigate(
        "http://user:pass@from:777/path?query#ref",
        "http://:pass@dest:777/path?query#ref",
        "username cleared"
      );

      await setRedirectTransform({ username: "new" });
      // Cannot pass credentials to fetch, but can read from response.url:
      await testFetch("http://from/", "http://new@dest/", "username added");
      await testNavigate("http://from/", "http://new@dest/", "username added");
      await testNavigate(
        "http://user:pass@from:777/path?query#ref",
        "http://new:pass@dest:777/path?query#ref",
        "username changed"
      );

      await setRedirectTransform({ username: "new User:name@%%20/" });
      await testNavigate(
        "http://user:pass@from:777/path?query#ref",
        "http://new%20User%3Aname%40%%20%2F:pass@dest:777/path?query#ref",
        "username changed to complex value"
      );

      browser.test.notifyPass();
    },
  });
});

add_task(async function test_redirect_transform_password() {
  await runAsDNRExtension({
    background: async dnrTestUtils => {
      const { setRedirectTransform, testFetch, testNavigate } = dnrTestUtils;

      await setRedirectTransform({ password: "" });
      await testNavigate(
        "http://user:pass@from:777/path?query#ref",
        "http://user@dest:777/path?query#ref",
        "password cleared"
      );

      await setRedirectTransform({ password: "new" });
      // Cannot pass credentials to fetch, but can read from response.url:
      await testFetch("http://from/", "http://:new@dest/", "password added");
      await testNavigate("http://from/", "http://:new@dest/", "password added");
      await testNavigate(
        "http://user:pass@from:777/path?query#ref",
        "http://user:new@dest:777/path?query#ref",
        "password changed"
      );

      await setRedirectTransform({ password: "new Pass:@%%20/" });
      await testNavigate(
        "http://user:pass@from:777/path?query#ref",
        "http://user:new%20Pass%3A%40%%20%2F@dest:777/path?query#ref",
        "password changed to complex value"
      );
      browser.test.notifyPass();
    },
  });
});

add_task(async function test_redirect_transform_host() {
  await runAsDNRExtension({
    background: async dnrTestUtils => {
      const { setRedirectTransform, testFetch, testNavigate } = dnrTestUtils;

      await setRedirectTransform({ host: "dest" });
      await testFetch(
        "http://from:777/path?query",
        "http://dest:777/path?query",
        "host changed"
      );
      await testNavigate(
        "http://user:pass@from:777/path?query#ref",
        "http://user:pass@dest:777/path?query#ref",
        "host changed without affecting embedded credentials"
      );

      await setRedirectTransform({ host: "DEST" });
      await testFetch(
        "http://from/",
        "http://dest/",
        "host changed (non-canonical, upper case)"
      );

      await setRedirectTransform({ host: "%44%65%73%54" }); // "DesT", escaped.
      await testFetch(
        "http://from:777/",
        "http://dest:777/",
        "host changed (non-canonical, percent-escaped)"
      );

      await setRedirectTransform({ host: "127.0.0.127" });
      await testFetch(
        "http://from/",
        "http://127.0.0.127/",
        "host change to IPv4"
      );

      await setRedirectTransform({ host: "[::1]" });
      await testFetch("http://from/", "http://[::1]/", "host change to IPv6");

      await setRedirectTransform({ host: "xn--stra-yna.de" });
      await testFetch(
        "http://from/",
        "http://xn--stra-yna.de/",
        "host change to IDN (internationalized domain name, in punycode)"
      );

      await setRedirectTransform({ host: "straß.de" });
      await testFetch(
        "http://from/",
        "http://xn--stra-yna.de/",
        "host change to IDN (not punycode-encoded)"
      );

      await setRedirectTransform({ host: "fqdn." });
      await testFetch(
        "http://from/",
        "http://fqdn./",
        "host change to FQDN (fully-qualified domain name)"
      );

      browser.test.notifyPass();
    },
  });
});

add_task(async function test_redirect_transform_port() {
  await runAsDNRExtension({
    background: async dnrTestUtils => {
      const { setRedirectTransform, testFetch, testNavigate } = dnrTestUtils;

      await setRedirectTransform({ port: "" });
      await testFetch("http://from:777/", "http://dest/", "port cleared");
      await testNavigate(
        "http://user:pass@from:777/path?query#ref",
        "http://user:pass@dest/path?query#ref",
        "port cleared from URL with embedded credentials"
      );

      await setRedirectTransform({ port: "700" });
      await testFetch("http://from/", "http://dest:700/", "port added");
      await testFetch("http://from:777/", "http://dest:700/", "port changed");

      // 0-padded should not be misinterpreted as an octal number.
      await setRedirectTransform({ port: "0700" });
      await testFetch(
        "http://from:777/",
        "http://dest:700/",
        "port changed (non-canonical, 0-padded port)"
      );

      await setRedirectTransform({ port: "80" });
      await testFetch(
        "http://from:777/",
        "http://dest/",
        "port cleared if default protocol"
      );

      await setRedirectTransform({ scheme: "http", port: "443" });
      await testFetch(
        "https://from/",
        "http://dest:443/",
        "port added if new port is not default port of new protocol"
      );

      await setRedirectTransform({ scheme: "http", port: "80" });
      await testFetch(
        "https://from:777/",
        "http://dest/",
        "port cleared if new port is default port of new protocol"
      );

      browser.test.notifyPass();
    },
  });
});

add_task(async function test_redirect_transform_path() {
  await runAsDNRExtension({
    background: async dnrTestUtils => {
      const { setRedirectTransform, testFetch, testNavigate } = dnrTestUtils;

      await setRedirectTransform({ path: "" });
      await testFetch("http://from/path", "http://dest/", "path cleared");
      await testNavigate(
        "http://user:pass@from:777/path?query#ref",
        "http://user:pass@dest:777/?query#ref",
        "path cleared from URL with embedded credentials"
      );

      await setRedirectTransform({ path: "/new" });
      await testFetch("http://from/", "http://dest/new", "path added");
      await testFetch("http://from/path", "http://dest/new", "path changed");

      await setRedirectTransform({ path: "///" });
      await testFetch("http://from/", "http://dest///", "path added (///)");

      await setRedirectTransform({ path: "path" });
      await testFetch(
        "http://from/",
        "http://dest/path",
        "path added (non-canonical, missing slash)"
      );

      // " " -> "%20" (space)
      // "\x00" -> "%00" (null byte)
      // "<>" -> "%3C%3E" (URL encoding of angle brackets)
      // "%", "%20", "%3A", "%3a" -> not changed (%-encoding kept as-is).
      await setRedirectTransform({ path: "/Path_%_ _%20_?_#_\x00_<>_%3A%3a" });
      await testFetch(
        "http://from/",
        "http://dest/Path_%_%20_%20_%3F_%23_%00_%3C%3E_%3A%3a",
        "path added (non-canonical, partial percent encoding)"
      );

      browser.test.notifyPass();
    },
  });
});

add_task(async function test_redirect_transform_query() {
  await runAsDNRExtension({
    background: async dnrTestUtils => {
      const { setRedirectTransform, testFetch, testNavigate } = dnrTestUtils;

      await setRedirectTransform({ query: "" });
      await testFetch("http://from/?query", "http://dest/", "query cleared");
      await testNavigate(
        "http://user:pass@from:777/path?query#ref",
        "http://user:pass@dest:777/path#ref",
        "query cleared from URL with embedded credentials"
      );

      await setRedirectTransform({ query: "?new" });
      await testFetch("http://from/", "http://dest/?new", "query added");
      await testFetch(
        "http://from/?query",
        "http://dest/?new",
        "query changed"
      );

      await setRedirectTransform({ query: "?" });
      await testFetch("http://from/", "http://dest/?", "query set to just '?'");

      await setRedirectTransform({ query: "?Query_#_ _%20_%3a%3A_<>_\x00" });
      await testFetch(
        "http://from/",
        "http://dest/?Query_%23_%20_%20_%3a%3A_%3C%3E_%00",
        "query added (non-canonical, partial percent encoding)"
      );

      // Now rule.action.redirect.transform.queryTransform:
      await setRedirectTransform({
        queryTransform: {
          removeParams: ["query"],
        },
      });
      await testFetch(
        "http://from/?query",
        "http://dest/",
        "queryTransform removed query"
      );
      await testFetch(
        "http://from/?prefix&query&suffix",
        "http://dest/?prefix&suffix",
        "queryTransform removed part of query"
      );
      await testFetch(
        "http://from/?query&aquery&queryb&query=withvalue&not=query&QUERY&",
        "http://dest/?aquery&queryb&not=query&QUERY&",
        "queryTransform removed all occurrences of 'query' key"
      );
      await testFetch(
        "http://from/??query",
        "http://dest/??query",
        "queryTransform does not match param when it starts with '??'"
      );

      await setRedirectTransform({
        queryTransform: {
          removeParams: ["query"],
          addOrReplaceParams: [{ key: "query", value: "newvalue" }],
        },
      });
      await testFetch(
        "http://from/",
        "http://dest/?query=newvalue",
        "queryTransform appended query despite new param being in removeParams"
      );
      await testFetch(
        "http://from/?prefix&query&suffix",
        "http://dest/?prefix&suffix&query=newvalue",
        "queryTransform removed query, and appended new value"
      );
      await testFetch(
        "http://from/??query",
        "http://dest/??query&query=newvalue",
        "queryTransform ignores existing param starting with '??', and appends"
      );

      await setRedirectTransform({
        queryTransform: {
          addOrReplaceParams: [{ key: "query", value: "newvalue" }],
        },
      });
      await testFetch(
        "http://from/",
        "http://dest/?query=newvalue",
        "queryTransform appended query"
      );
      await testFetch(
        "http://from/?prefix&query=oldvalue&query=2&query=3",
        "http://dest/?prefix&query=newvalue&query=2&query=3",
        "queryTransform replaced the first occurrence and kept the others"
      );

      await setRedirectTransform({
        queryTransform: {
          addOrReplaceParams: [
            { key: "r", value: "default" }, // default:false
            { key: "r", value: "false", replaceOnly: false },
            { key: "r", value: "true", replaceOnly: true },
            { key: "r", value: "false2", replaceOnly: false },
            { key: "r", value: "true2", replaceOnly: true },
          ],
        },
      });
      // r=true and r=true2 are missing because there are no matching "r".
      await testFetch(
        "http://from/",
        "http://dest/?r=default&r=false&r=false2",
        "queryTransform appends all except replaceOnly=true"
      );
      // r=true2 should be missing because there is no matching "r".
      await testFetch(
        "http://from/?r=1&r=2&r=3&___",
        "http://dest/?r=default&r=false&r=true&___&r=false2",
        "queryTransform replaced in order and ignores last replaceOnly=true"
      );

      await setRedirectTransform({
        queryTransform: {
          addOrReplaceParams: [
            { key: "a", value: "appenda" },
            { key: "b", value: "b1" },
            { key: "c", value: "c1" },
            { key: "c", value: "c2" },
            { key: "c", value: "appendc" },
            { key: "d", value: "d1" },
          ],
        },
      });
      // Test case has:         b  c  c       d.
      // Rule only has: appenda b1 c2 appendc d1.
      // Expected out :         b1 c2         d1 appenda appendc.
      await testFetch(
        "http://from/?b=01&c=02&c=03&d=06",
        "http://dest/?b=b1&c=c1&c=c2&d=d1&a=appenda&c=appendc",
        "queryTransform replaces matched queries and appends the rest, in order"
      );

      await setRedirectTransform({
        queryTransform: {
          addOrReplaceParams: [{ key: "query", value: " _+_%00_#" }],
        },
      });
      await testFetch(
        "http://from/",
        "http://dest/?query=+_%2B_%2500_%23",
        "queryTransform urlencodes values"
      );

      // This part tests how param names with non-alphanumeric characters can be
      // (and not be) matched and replaced. This follows Chrome's behavior, see
      // https://bugzilla.mozilla.org/show_bug.cgi?id=1801870#c1
      await setRedirectTransform({
        queryTransform: {
          removeParams: ["?x", "%3Fx", "&x", "%26x"],
          addOrReplaceParams: [
            // Internally interpreted as: %3Fp:
            { key: "?p", value: "rawq", replaceOnly: true },
            // Internally interpreted as: %253Fp:
            { key: "%3Fp", value: "escape_upper_q", replaceOnly: true },
            // Internally interpreted as: %253fp:
            { key: "%3fp", value: "escape_lower_q", replaceOnly: true },
            // Internally interpreted as: %26p:
            { key: "&p", value: "rawa", replaceOnly: true },
            // Internally interpreted as: %2526p:
            { key: "%26p", value: "escape_a", replaceOnly: true },
          ],
        },
      });
      await testFetch(
        "http://from/?x&x&?x",
        "http://dest/?x&x&?x",
        "queryTransform does not match the '?' or '&' separators"
      );
      await testFetch(
        "http://from/??p&&p&?p",
        "http://dest/??p&&p&?p",
        "queryTransform cannot match literal '?p' because it is not urlencoded"
      );
      await testFetch(
        "http://from/?%3Fp",
        "http://dest/?%3Fp=rawq",
        "queryTransform matches already-urlencoded '%3Fp' with raw '?p'"
      );
      await testFetch(
        "http://from/?%3fp",
        "http://dest/?%3fp",
        "queryTransform cannot match non-canonical percent encoding (lowercase)"
      );
      await testFetch(
        "http://from/?%253fp&%253Fp",
        "http://dest/?%253fp=escape_lower_q&%253Fp=escape_upper_q",
        "queryTransform matches double-urlencoded '?p' with single-encoded '?p'"
      );
      await testFetch(
        "http://from/?%26p",
        "http://dest/?%26p=rawa",
        "queryTransform matches already-urlencoded '%26p' with raw '&p'"
      );

      browser.test.notifyPass();
    },
  });
});

add_task(async function test_redirect_transform_fragment() {
  await runAsDNRExtension({
    background: async dnrTestUtils => {
      // Note: not using testFetch because it cannot see fragment changes.
      const { setRedirectTransform, testNavigate } = dnrTestUtils;

      await setRedirectTransform({ fragment: "" });
      await testNavigate(
        "http://user:pass@from:777/path?query#ref",
        "http://user:pass@dest:777/path?query",
        "fragment cleared from URL with embedded credentials"
      );

      await setRedirectTransform({ fragment: "#new" });
      await testNavigate("http://from/", "http://dest/#new", "fragment added");
      await testNavigate(
        "http://from/#ref",
        "http://dest/#new",
        "fragment changed"
      );
      browser.test.notifyPass();
    },
  });
});

add_task(async function test_redirect_transform_failed_at_runtime() {
  await runAsDNRExtension({
    background: async dnrTestUtils => {
      const { setRedirectTransform } = dnrTestUtils;

      // Maximum length of a UTL is 1048576 (network.standard-url.max-length).
      const network_standard_url_max_length = 1048576;
      // updateSessionRules does some validation on the limit (as seen by
      // validate_action_redirect_transform in test_ext_dnr_session_rules.js),
      // but it is still possible to pass validation and fail in practice when
      // the existing URL + new component exceeds the limit.
      const VERY_LONG_STRING = "x".repeat(network_standard_url_max_length - 20);

      // Like testFetch, except truncates URLs in log messages to avoid logspam.
      async function testFetchPossiblyLongUrl(from, to, body, description) {
        let res = await fetch(from);
        const shortx = s => s.replace(/x{10,}/g, xxx => `x{${xxx.length}}`);
        // VERY_LONG_STRING consists of many 'X'. Shorten to avoid logspam.
        browser.test.assertEq(shortx(to), shortx(res.url), description);
        browser.test.assertEq(body, await res.text(), "expected body");
      }

      await setRedirectTransform({ query: "?" + VERY_LONG_STRING });
      await testFetchPossiblyLongUrl(
        "http://from/short",
        `http://dest/short?${VERY_LONG_STRING}`,
        // Somehow the httpd server raises NS_ERROR_MALFORMED_URI when it tries
        // to use newURI to parse the received URL. But the server responding
        // with that implies that the redirect was successful, so for the
        // purpose of this test, that response is acceptable.
        "Bad request\n",
        "Can redirect to URL near (but not over) url max-length"
      );

      // This check confirms that not only does the request not redirect to
      // an invalid URL, but also that the request does not somehow end up in
      // an infinite redirect loop.
      await testFetchPossiblyLongUrl(
        "http://from/1234567890_1234567890",
        "http://from/1234567890_1234567890",
        "GOOD_RESPONSE",
        "Redirect to URL over max length is ignored; request continues"
      );

      browser.test.notifyPass();
    },
  });
});