summaryrefslogtreecommitdiffstats
path: root/dom/security/test/csp/test_path_matching_redirect.html
blob: d3b2771d0aefa83dfd74d87e9056e9c86a035348 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 808292 - Implement path-level host-source matching to CSP (redirects)</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">

SimpleTest.waitForExplicitFinish();

/* Description of the test:
 * First, we try to load a script where the *path* does not match.
 * Second, we try to load a script which is allowed by the CSPs
 * script-src directive. The script then gets redirected to
 * an URL where the host matches, but the path wouldn't.
 * Since 'paths' should not be taken into account after redirects,
 * that load should succeed. We are using a similar test setup
 * as described in the spec, see:
 * http://www.w3.org/TR/CSP11/#source-list-paths-and-redirects
 */

var policy = "script-src http://example.com http://test1.example.com/CSPAllowsScriptsInThatFolder";

var tests = [
  {
    // the script in file_path_matching.html
    // <script src="http://test1.example.com/tests/dom/security/..">
    // is not within the allowlisted path by the csp-policy
    // hence the script is 'blocked' by CSP.
    expected: "blocked",
    uri: "tests/dom/security/test/csp/file_path_matching.html"
  },
  {
    // the script in file_path_matching_redirect.html
    // <script src="http://example.com/tests/dom/..">
    // gets redirected to: http://test1.example.com/tests/dom
    // where after the redirect the path of the policy is not enforced
    // anymore and hence execution of the script is 'allowed'.
    expected: "allowed",
    uri: "tests/dom/security/test/csp/file_path_matching_redirect.html"
  },
];

var counter = 0;
var curTest;

function checkResult() {
  try {
    document.getElementById("testframe").removeEventListener('load', checkResult);
    var testframe = document.getElementById("testframe");
    var divcontent = testframe.contentWindow.document.getElementById('testdiv').innerHTML;
    is(divcontent, curTest.expected, "should be blocked in test " + (counter - 1) + "!");
  }
  catch (e) {
    ok(false, "ERROR: could not access content in test " + (counter - 1) + "!");
  }
  loadNextTest();
}

function loadNextTest() {
  if (counter == tests.length) {
    SimpleTest.finish();
  }
  else {
    curTest = tests[counter++];
    var src = "file_testserver.sjs";
    // append the file that should be served
    src += "?file=" + escape(curTest.uri);
    // append the CSP that should be used to serve the file
    src += "&csp=" + escape(policy);

    document.getElementById("testframe").addEventListener("load", checkResult);
    document.getElementById("testframe").src = src;
  }
}

loadNextTest();

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