tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_103_referrer_policy.js (5044B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 Services.prefs.setBoolPref("network.early-hints.enabled", true);
      6 
      7 async function test_referrer_policy(input, expected_results) {
      8  // reset the count
      9  let headers = new Headers();
     10  headers.append("X-Early-Hint-Count-Start", "");
     11  await fetch(
     12    "https://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs",
     13    { headers }
     14  );
     15 
     16  await fetch(
     17    "https://example.com/browser/netwerk/test/browser/early_hint_referrer_policy_html.sjs?action=reset_referrer_results"
     18  );
     19 
     20  let requestUrl = `https://example.com/browser/netwerk/test/browser/early_hint_referrer_policy_html.sjs?as=${
     21    input.resource_type
     22  }&hinted=${input.hinted ? "1" : "0"}${
     23    input.header_referrer_policy
     24      ? "&header_referrer_policy=" + input.header_referrer_policy
     25      : ""
     26  }
     27    ${
     28      input.link_referrer_policy
     29        ? "&link_referrer_policy=" + input.link_referrer_policy
     30        : ""
     31    }`;
     32 
     33  await BrowserTestUtils.withNewTab(
     34    {
     35      gBrowser,
     36      url: requestUrl,
     37      waitForLoad: true,
     38    },
     39    async function () {}
     40  );
     41 
     42  let gotRequestCount = await fetch(
     43    "https://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs"
     44  ).then(response => response.json());
     45 
     46  // Retrieve the request referrer from the server
     47  let referrer_response = await fetch(
     48    "https://example.com/browser/netwerk/test/browser/early_hint_referrer_policy_html.sjs?action=get_request_referrer_results"
     49  ).then(response => response.text());
     50 
     51  Assert.strictEqual(
     52    referrer_response,
     53    expected_results.referrer,
     54    "Request referrer matches expected - " + input.test_name
     55  );
     56 
     57  await Assert.deepEqual(
     58    gotRequestCount,
     59    { hinted: expected_results.hinted, normal: expected_results.normal },
     60    `${input.testName} (${input.resource_type}): Unexpected amount of requests made`
     61  );
     62 }
     63 
     64 add_task(async function test_103_referrer_policies() {
     65  let tests = [
     66    {
     67      input: {
     68        test_name: "image - no policies",
     69        resource_type: "image",
     70        header_referrer_policy: "",
     71        link_referrer_policy: "",
     72        hinted: true,
     73      },
     74      expected: {
     75        hinted: 1,
     76        normal: 0,
     77        referrer:
     78          "https://example.com/browser/netwerk/test/browser/early_hint_referrer_policy_html.sjs?as=image&hinted=1",
     79      },
     80    },
     81    {
     82      input: {
     83        test_name: "image - origin on header",
     84        resource_type: "image",
     85        header_referrer_policy: "origin",
     86        link_referrer_policy: "",
     87        hinted: true,
     88      },
     89      expected: { hinted: 1, normal: 0, referrer: "https://example.com/" },
     90    },
     91    {
     92      input: {
     93        test_name: "image - origin on link",
     94        resource_type: "image",
     95        header_referrer_policy: "",
     96        link_referrer_policy: "origin",
     97        hinted: true,
     98      },
     99      expected: { hinted: 1, normal: 0, referrer: "https://example.com/" },
    100    },
    101    {
    102      input: {
    103        test_name: "image - origin on both",
    104        resource_type: "image",
    105        header_referrer_policy: "origin",
    106        link_referrer_policy: "origin",
    107        hinted: true,
    108      },
    109      expected: { hinted: 1, normal: 0, referrer: "https://example.com/" },
    110    },
    111    {
    112      input: {
    113        test_name: "image - no-referrer on header",
    114        resource_type: "image",
    115        header_referrer_policy: "no-referrer",
    116        link_referrer_policy: "",
    117        hinted: true,
    118      },
    119      expected: { hinted: 1, normal: 0, referrer: "" },
    120    },
    121    {
    122      input: {
    123        test_name: "image - no-referrer on link",
    124        resource_type: "image",
    125        header_referrer_policy: "",
    126        link_referrer_policy: "no-referrer",
    127        hinted: true,
    128      },
    129      expected: { hinted: 1, normal: 0, referrer: "" },
    130    },
    131    {
    132      input: {
    133        test_name: "image - no-referrer on both",
    134        resource_type: "image",
    135        header_referrer_policy: "no-referrer",
    136        link_referrer_policy: "no-referrer",
    137        hinted: true,
    138      },
    139      expected: { hinted: 1, normal: 0, referrer: "" },
    140    },
    141    {
    142      // link referrer policy takes precedence
    143      input: {
    144        test_name: "image - origin on header, no-referrer on link",
    145        resource_type: "image",
    146        header_referrer_policy: "origin",
    147        link_referrer_policy: "no-referrer",
    148        hinted: true,
    149      },
    150      expected: { hinted: 1, normal: 0, referrer: "" },
    151    },
    152    {
    153      // link referrer policy takes precedence
    154      input: {
    155        test_name: "image - no-referrer on header, origin on link",
    156        resource_type: "image",
    157        header_referrer_policy: "no-referrer",
    158        link_referrer_policy: "origin",
    159        hinted: true,
    160      },
    161      expected: { hinted: 1, normal: 0, referrer: "https://example.com/" },
    162    },
    163  ];
    164 
    165  for (let test of tests) {
    166    await test_referrer_policy(test.input, test.expected);
    167  }
    168 });