summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_ext_webrequest_redirect_bypass_cors.html
blob: 7ba92f5c80dd62b774505f9eec15190cdaa48fbe (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 1450965: Skip Cors Check for Early WebExtention Redirects </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";

/* Description of the test:
 * We try to Check if a WebExtention can redirect a request and bypass CORS
 * We're redirecting a fetch request in onBeforeRequest
 * which should not be blocked, even though we do not have
 * the CORS information yet.
 */

const WIN_URL =
  "http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest/file_redirect_cors_bypass.html";


add_task(async function test_webRequest_redirect_cors_bypass() {
  // disable third-party storage isolation so the test works as expected
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.partition.always_partition_third_party_non_cookie_storage", false]],
  });

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: [
        "webRequest",
        "webRequestBlocking",
        "<all_urls>",
      ],
    },
    background() {
      browser.webRequest.onBeforeRequest.addListener((details) => {
        if (details.url.includes("file_cors_blocked.txt")) {
          // File_cors_blocked does not need to exist, because we're redirecting anyway.
          const testPath = "example.org/tests/toolkit/components/extensions/test/mochitest";
          let redirectUrl = `https://${testPath}/file_sample.txt`;

          // If the WebExtion cant bypass CORS, the fetch will throw a CORS-Exception
          // because we do not have the CORS header yet for 'file-cors-blocked.txt'
          return {redirectUrl};
        }
      }, {urls: ["<all_urls>"]}, ["blocking"]);
    },

  });

  await extension.startup();
  let win = window.open(WIN_URL);
  // Creating a message channel to the new tab.
  const channel = new BroadcastChannel("test_bus");
  await new Promise((resolve) => {
    channel.onmessage = async function(fetch_result) {
      // Fetch result data will either be the text content of file_sample.txt -> 'Sample'
      // or a network-Error.
      // In case it's 'Sample' the redirect did happen correctly.
      ok(fetch_result.data == "Sample", "Cors was Bypassed");
      win.close();
      await extension.unload();
      resolve();
    };
  });
});

</script>
</body>
</html>