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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
<!DOCTYPE HTML>
<html>
<head>
<title>Bug 1045897 - Test CSP base-uri directive</title>
<!-- Including SimpleTest.js so we can use waitForExplicitFinish !-->
<script type="text/javascript" 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 in an iframe (served over http://example.com) that tries to
* modify the 'base' either through setting or also removing the base-uri. We
* load that page using different policies and verify that setting the base-uri
* is correctly blocked by CSP.
*/
SimpleTest.waitForExplicitFinish();
var tests = [
{ csp: "base-uri http://mochi.test;",
base1: "http://mochi.test",
base2: "",
action: "enforce-csp",
result: "http://mochi.test",
desc: "CSP allows base uri"
},
{ csp: "base-uri http://example.com;",
base1: "http://mochi.test",
base2: "",
action: "enforce-csp",
result: "http://example.com",
desc: "CSP blocks base uri"
},
{ csp: "base-uri https:",
base1: "http://mochi.test",
base2: "",
action: "enforce-csp",
result: "http://example.com",
desc: "CSP blocks http base"
},
{ csp: "base-uri 'none'",
base1: "http://mochi.test",
base2: "",
action: "enforce-csp",
result: "http://example.com",
desc: "CSP allows no base modification"
},
{ csp: "",
base1: "http://foo:foo/",
base2: "",
action: "enforce-csp",
result: "http://example.com",
desc: "Invalid base should be ignored"
},
{ csp: "base-uri http://mochi.test",
base1: "http://mochi.test",
base2: "http://test1.example.com",
action: "remove-base1",
result: "http://example.com",
desc: "Removing first base should result in fallback base"
},
{ csp: "",
base1: "http://mochi.test",
base2: "http://test1.example.com",
action: "remove-base1",
result: "http://test1.example.com",
desc: "Removing first base should result in the second base"
},
];
// initializing to -1 so we start at index 0 when we start the test
var counter = -1;
function finishTest() {
window.removeEventListener("message", receiveMessage, false);
SimpleTest.finish();
}
// a postMessage handler that is used by sandboxed iframes without
// 'allow-same-origin' to bubble up results back to this main page.
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event) {
var result = event.data.result;
// we only care about the base uri, so instead of comparing the complete uri
// we just make sure that the base is correct which is sufficient here.
ok(result.startsWith(tests[counter].result),
`${tests[counter].desc}: Expected a base URI that starts
with ${tests[counter].result} but got ${result}`);
loadNextTest();
}
function loadNextTest() {
counter++;
if (counter == tests.length) {
finishTest();
return;
}
var src = "http://example.com/tests/dom/security/test/csp/file_base_uri_server.sjs";
// append the CSP that should be used to serve the file
// please note that we have to include 'unsafe-inline' to permit sending the postMessage
src += "?csp=" + escape("script-src 'unsafe-inline'; " + tests[counter].csp);
// append potential base tags
src += "&base1=" + escape(tests[counter].base1);
src += "&base2=" + escape(tests[counter].base2);
// append potential action
src += "&action=" + escape(tests[counter].action);
document.getElementById("testframe").src = src;
}
// start running the tests
loadNextTest();
</script>
</body>
</html>
|