summaryrefslogtreecommitdiffstats
path: root/dom/security/test/csp/test_leading_wildcard.html
blob: 53994b00139cca7aff204a4edc128534d8cccfc3 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 1032303 - CSP - Keep FULL STOP when matching *.foo.com to disallow loads from foo.com</title>
  <!-- Including SimpleTest.js so we can use waitForExplicitFinish !-->
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
  <p id="display"></p>
  <div id="content" style="visibility: hidden">
    <iframe style="width:100%;" id="testframe"></iframe>
  </div>

<script class="testbody" type="text/javascript">

/*
 * Description of the test:
 *   We load a page with a CSP that allows scripts to be loaded from *.example.com.
 *   On that page we try to load two scripts:
 *     a) [allowed] leading_wildcard_allowed.js which is served from test1.example.com
 *     b) [blocked] leading_wildcard_blocked.js which is served from example.com
 *
 *   We verify that only the allowed script executes by registering observers which listen
 *   to CSP violations and http-notifications. Please note that both scripts do *not* exist
 *   in the file system. The test just verifies that CSP blocks correctly.
 */

SimpleTest.waitForExplicitFinish();

var policy =  "default-src 'none' script-src *.example.com";
var testsExecuted = 0;

function finishTest() {
  if (++testsExecuted < 2) {
    return;
  }
  window.wildCardExaminer.remove();
  SimpleTest.finish();
}

// We use the examiner to identify requests that hit the wire and requests
// that are blocked by CSP.
function examiner() {
  SpecialPowers.addObserver(this, "csp-on-violate-policy");
  SpecialPowers.addObserver(this, "specialpowers-http-notify-request");
}
examiner.prototype  = {
  observe(subject, topic, data) {

   // allowed requests
   if (topic === "specialpowers-http-notify-request") {
      if (data.includes("leading_wildcard_allowed.js")) {
        ok (true, "CSP should allow file_leading_wildcard_allowed.js!");
        finishTest();
      }
      if (data.includes("leading_wildcard_blocked.js")) {
        ok(false, "CSP should not allow file_leading_wildcard_blocked.js!");
        finishTest();
      }
    }

    // blocked requests
    if (topic === "csp-on-violate-policy") {
      var asciiSpec = SpecialPowers.getPrivilegedProps(
                        SpecialPowers.do_QueryInterface(subject, "nsIURI"),
                        "asciiSpec");

      if (asciiSpec.includes("leading_wildcard_allowed.js")) {
        ok (false, "CSP should not block file_leading_wildcard_allowed.js!");
        finishTest();
      }
      if (asciiSpec.includes("leading_wildcard_blocked.js")) {
        ok (true, "CSP should block file_leading_wildcard_blocked.js!");
        finishTest();
      }
    }
  },
  remove() {
    SpecialPowers.removeObserver(this, "csp-on-violate-policy");
    SpecialPowers.removeObserver(this, "specialpowers-http-notify-request");
  }
}
window.wildCardExaminer = new examiner();

function runTest() {
  var src = "file_testserver.sjs";
  // append the file that should be served
  src += "?file=" + escape("tests/dom/security/test/csp/file_leading_wildcard.html");
  // append the CSP that should be used to serve the file
  src += "&csp=" + escape(policy);

  document.getElementById("testframe").src = src;
}

// start running the tests
runTest();

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