Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Errors
- This test gets skipped with pattern: http3 OR http2
- This test failed 1 times in the preceding 30 days. quicksearch this test
- Manifest: dom/security/test/csp/mochitest.toml
<!DOCTYPE HTML>
<html>
<head>
<!-- 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:
* 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 = [
base2: "",
action: "enforce-csp",
desc: "CSP allows base uri"
},
base2: "",
action: "enforce-csp",
desc: "CSP blocks base uri"
},
{ csp: "base-uri https:",
base2: "",
action: "enforce-csp",
desc: "CSP blocks http base"
},
{ csp: "base-uri 'none'",
base2: "",
action: "enforce-csp",
desc: "CSP allows no base modification"
},
{ csp: "",
base2: "",
action: "enforce-csp",
desc: "Invalid base should be ignored"
},
action: "remove-base1",
desc: "Removing first base should result in fallback base"
},
{ csp: "",
action: "remove-base1",
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);
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);
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;
}
// 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>