summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/data/file_permission_xhr.html
blob: 6f1bb4648b5261faf0940466cbdd440d4d90bb19 (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
<!DOCTYPE HTML>

<html>
<head>
<meta charset="utf-8">
</head>
<body>

<script>
"use strict";

/* globals privilegedFetch, privilegedXHR */
/* eslint-disable mozilla/balanced-listeners */

addEventListener("message", function rcv(event) {
  removeEventListener("message", rcv, false);

  function assertTrue(condition, description) {
    postMessage({msg: "assertTrue", condition, description}, "*");
  }

  function assertThrows(func, expectedError, msg) {
    try {
      func();
    } catch (e) {
      assertTrue(expectedError.test(e), msg + ": threw " + e);
      return;
    }

    assertTrue(false, "Function did not throw, " +
                      "expected error should have matched " + expectedError);
  }

  function passListener() {
    assertTrue(true, "Content XHR has no elevated privileges");
    postMessage({"msg": "finish"}, "*");
  }

  function failListener() {
    assertTrue(false, "Content XHR has no elevated privileges");
    postMessage({"msg": "finish"}, "*");
  }

  assertThrows(function() { new privilegedXHR(); },
               /Permission denied to access object/,
               "Content should not be allowed to construct a privileged XHR constructor");

  assertThrows(function() { new privilegedFetch(); },
               / is not a constructor/,
               "Content should not be allowed to construct a privileged fetch() constructor");

  let req = new XMLHttpRequest();
  req.addEventListener("load", failListener);
  req.addEventListener("error", passListener);
  req.open("GET", "http://example.org/example.txt");
  req.send();
}, false);
</script>

</body>
</html>