summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/test/mochitest/test_ext_contentscript_api_injection.html
blob: abf3d349fb41a6b79c5a9df289580707babf1af1 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for privilege escalation into iframe with content script APIs</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" 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>

<!-- WORKAROUND: this textarea hack is used to contain the html page source without escaping it -->
<textarea id="test-asset">
  <!DOCTYPE HTML>
  <html>
    <head>
      <meta charset="utf-8">
      <script type="text/javascript" src="./content_script_iframe.js">
      </script>
    </head>
  </html>
</textarea>

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

add_task(function* test_contentscript_api_injection() {
  function contentScript() {
    let iframe = document.createElement("iframe");
    iframe.setAttribute("src", browser.runtime.getURL("content_script_iframe.html"));
    document.body.appendChild(iframe);
  }

  function contentScriptIframe() {
    const BASE = "http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest";
    window.location = `${BASE}/file_privilege_escalation.html`;
  }

  let extensionData = {
    manifest: {
      content_scripts: [
        {
          "matches": ["http://mochi.test/*/file_sample.html"],
          "js": ["content_script.js"],
          "run_at": "document_idle",
        },
      ],
      "web_accessible_resources": [
        "content_script_iframe.html",
      ],
    },

    files: {
      "content_script.js": contentScript,
      "content_script_iframe.js": contentScriptIframe,
      "content_script_iframe.html": document.querySelector("#test-asset").textContent,
    },
  };

  let extension = ExtensionTestUtils.loadExtension(extensionData);

  let awaitConsole = new Promise(resolve => {
    let chromeScript = SpecialPowers.loadChromeScript(
      SimpleTest.getTestFileURL("file_ext_test_api_injection.js"));

    chromeScript.addMessageListener("console-message", resolve);
  });

  yield extension.startup();
  info("extension loaded");

  let win = window.open("file_sample.html");

  let message = yield awaitConsole;

  ok(message.message.includes("WebExt Privilege Escalation: typeof(browser) = undefined"),
     "Document does not have `browser` APIs.");

  win.close();

  yield extension.unload();
  info("extension unloaded");
});
</script>

</body>
</html>