File: test_ext_request_urlClassification.html

package info (click to toggle)
firefox 134.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,345,684 kB
  • sloc: cpp: 7,244,582; javascript: 6,236,669; ansic: 3,654,775; python: 1,359,774; xml: 618,542; asm: 426,944; java: 183,315; sh: 66,206; makefile: 19,398; perl: 13,009; objc: 12,453; yacc: 4,583; cs: 3,846; pascal: 2,989; lex: 1,720; ruby: 1,194; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (169 lines) | stat: -rw-r--r-- 7,459 bytes parent folder | download | duplicates (5)
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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for WebRequest urlClassification</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

add_task(async function setup() {
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.trackingprotection.enabled", true]],
  });

  let chromeScript = SpecialPowers.loadChromeScript(async _ => {
    /* eslint-env mozilla/chrome-script */
    const {UrlClassifierTestUtils} = ChromeUtils.importESModule(
      "resource://testing-common/UrlClassifierTestUtils.sys.mjs"
    );
    await UrlClassifierTestUtils.addTestTrackers();
    sendAsyncMessage("trackersLoaded");
  });
  await chromeScript.promiseOneMessage("trackersLoaded");
  chromeScript.destroy();
});

add_task(async function test_urlClassification() {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.security.https_first", false]],
  });

  // All URLs below should be distinct from the ones in |expected| below,
  // especially the final png URL. If the PNG URL is identical, the image may
  // be read from the image cache, instead of through a new request.
  const PATH_REDIRECT_TO = "/tests/toolkit/components/extensions/test/mochitest/redirect_to.sjs";
  // Cross-origin redirect target. The path of this URL is also in file_third_party.html:
  const REDIRECT_TARGET = "http://another-tracking.example.net/tests/toolkit/components/extensions/test/mochitest/file_image_bad.png";
  const REDIRECT_TEST = {
    TOP_URL: `http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest/file_third_party.html?domain=mochi.test:8888${PATH_REDIRECT_TO}?http://another-tracking.example.net`,
    // file_third_party.html embeds http://mochi.test:8888<PATH_REDIRECT_TO>?<REDIRECT_TARGET>, which redirects to REDIRECT_TARGET.
    EMBEDDED_PRE_REDIRECT_URL: `http://mochi.test:8888${PATH_REDIRECT_TO}?${REDIRECT_TARGET}`,
    REDIRECT_TARGET,
  };

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      description: JSON.stringify({ REDIRECT_TEST }),
      permissions: ["webRequest", "webRequestBlocking", "proxy", "<all_urls>"],
    },
    background() {
      const { REDIRECT_TEST } = JSON.parse(browser.runtime.getManifest().description);
      let expected = {
        "http://tracking.example.org/": {first: "tracking", thirdParty: false, },
        "http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest/file_third_party.html?domain=tracking.example.org": { thirdParty: false, },
        "http://tracking.example.org/tests/toolkit/components/extensions/test/mochitest/file_image_bad.png": {third: "tracking", thirdParty: true, },
        "http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest/file_third_party.html?domain=example.net": { thirdParty: false, },
        "http://example.net/tests/toolkit/components/extensions/test/mochitest/file_image_bad.png": { thirdParty: true, },
        [REDIRECT_TEST.TOP_URL]: { thirdParty: false, },
        [REDIRECT_TEST.EMBEDDED_PRE_REDIRECT_URL]: { thirdParty: false, },
        [REDIRECT_TEST.REDIRECT_TARGET]: {third: "tracking", thirdParty: true, },
      };
      function testRequest(details) {
        let expect = expected[details.url];
        if (expect) {
          if (expect.first) {
            browser.test.assertTrue(details.urlClassification.firstParty.includes("tracking"), "tracking firstParty");
          } else {
            browser.test.assertEq(details.urlClassification.firstParty.length, 0, "not tracking firstParty");
          }
          if (expect.third) {
            browser.test.assertTrue(details.urlClassification.thirdParty.includes("tracking"), "tracking thirdParty");
          } else {
            browser.test.assertEq(details.urlClassification.thirdParty.length, 0, "not tracking thirdParty");
          }

          browser.test.assertEq(details.thirdParty, expect.thirdParty, "3rd party flag matches");
          return true;
        }
        return false;
      }

      const urls = [
        "http://mochi.test/tests/*",
        "http://tracking.example.org/*",
        "http://another-tracking.example.net/*",
        "http://example.net/*",
      ];
      browser.proxy.onRequest.addListener(details => {
        browser.test.log(`proxy.onRequest ${JSON.stringify(details)}`);
        testRequest(details);
      }, {urls});
      browser.webRequest.onBeforeRequest.addListener(async (details) => {
        browser.test.log(`webRequest.onBeforeRequest ${JSON.stringify(details)}`);
        testRequest(details);
      }, {urls}, ["blocking"]);
      browser.webRequest.onCompleted.addListener(async (details) => {
        browser.test.log(`webRequest.onCompleted ${JSON.stringify(details)}`);
        if (testRequest(details)) {
          browser.test.sendMessage("classification", details.url);
        }
      }, {urls});
    },
  });
  await extension.startup();

  // Test first party tracking classification.
  let url = "http://tracking.example.org/";
  let win = window.open(url);
  is(await extension.awaitMessage("classification"), url, "request completed");
  win.close();

  // Test third party tracking classification, expecting two results.
  url = "http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest/file_third_party.html?domain=tracking.example.org";
  win = window.open(url);
  is(await extension.awaitMessage("classification"), url);
  is(await extension.awaitMessage("classification"),
     "http://tracking.example.org/tests/toolkit/components/extensions/test/mochitest/file_image_bad.png",
     "request completed");
  win.close();

  // Test third party tracking classification, expecting two results.
  url = "http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest/file_third_party.html?domain=example.net";
  win = window.open(url);
  is(await extension.awaitMessage("classification"), url);
  is(await extension.awaitMessage("classification"),
     "http://example.net/tests/toolkit/components/extensions/test/mochitest/file_image_bad.png",
     "request completed");
  win.close();

  // Test third party tracking classification for redirected requests.
  win = window.open(REDIRECT_TEST.TOP_URL);
  is(await extension.awaitMessage("classification"), REDIRECT_TEST.TOP_URL);
  // Note: REDIRECT_TEST.EMBEDDED_PRE_REDIRECT_URL is not observed because
  // onCompleted is not seen for that URL due to a redirect to REDIRECT_TARGET.
  is(await extension.awaitMessage("classification"),
     REDIRECT_TEST.REDIRECT_TARGET,
     "request completed");
  win.close();

  await extension.unload();
});

add_task(async function teardown() {
  let chromeScript = SpecialPowers.loadChromeScript(async _ => {
    /* eslint-env mozilla/chrome-script */
    // Cleanup cache
    await new Promise(resolve => {
      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, () => resolve());
    });

    const {UrlClassifierTestUtils} = ChromeUtils.importESModule(
      "resource://testing-common/UrlClassifierTestUtils.sys.mjs"
    );
    await UrlClassifierTestUtils.cleanupTestTrackers();
    sendAsyncMessage("trackersUnloaded");
  });
  await chromeScript.promiseOneMessage("trackersUnloaded");
  chromeScript.destroy();
});

</script>

</body>
</html>