2014-01-24 03:34:56 +04:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
2017-08-04 15:10:21 +03:00
|
|
|
// This test is two fold:
|
|
|
|
// a) if security.data_uri.unique_opaque_origin == false, then
|
|
|
|
// this tests that session restore component does restore the right
|
|
|
|
// content security policy with the document. (The policy being
|
|
|
|
// tested disallows inline scripts).
|
|
|
|
// b) if security.data_uri.unique_opaque_origin == true, then
|
|
|
|
// this tests that data: URIs do not inherit the CSP from
|
|
|
|
// it's enclosing context.
|
2014-01-24 03:34:56 +04:00
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
add_task(async function test() {
|
2017-09-13 07:56:42 +03:00
|
|
|
// allow top level data: URI navigations, otherwise clicking a data: link fails
|
|
|
|
await SpecialPowers.pushPrefEnv({
|
2018-08-31 08:59:17 +03:00
|
|
|
"set": [["security.data_uri.block_toplevel_data_uri_navigations", false]],
|
2017-09-13 07:56:42 +03:00
|
|
|
});
|
2017-08-04 15:10:21 +03:00
|
|
|
let dataURIPref = Services.prefs.getBoolPref("security.data_uri.unique_opaque_origin");
|
2014-01-24 03:34:56 +04:00
|
|
|
// create a tab that has a CSP
|
|
|
|
let testURL = "http://mochi.test:8888/browser/browser/components/sessionstore/test/browser_911547_sample.html";
|
2017-05-15 22:49:50 +03:00
|
|
|
let tab = gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, testURL);
|
2014-01-24 03:34:56 +04:00
|
|
|
gBrowser.selectedTab = tab;
|
|
|
|
|
|
|
|
let browser = tab.linkedBrowser;
|
2017-05-12 15:42:39 +03:00
|
|
|
await promiseBrowserLoaded(browser);
|
2014-01-24 03:34:56 +04:00
|
|
|
|
|
|
|
// this is a baseline to ensure CSP is active
|
|
|
|
// attempt to inject and run a script via inline (pre-restore, allowed)
|
2017-08-04 15:10:21 +03:00
|
|
|
await injectInlineScript(browser, `document.getElementById("test_id1").value = "id1_modified";`);
|
2014-01-24 03:34:56 +04:00
|
|
|
|
2017-03-09 22:36:28 +03:00
|
|
|
let loadedPromise = promiseBrowserLoaded(browser);
|
2017-05-12 15:42:39 +03:00
|
|
|
await ContentTask.spawn(browser, null, function() {
|
2017-08-04 15:10:21 +03:00
|
|
|
is(content.document.getElementById("test_id1").value, "id1_initial",
|
2017-03-09 22:36:28 +03:00
|
|
|
"CSP should block the inline script that modifies test_id");
|
|
|
|
|
2017-08-04 15:10:21 +03:00
|
|
|
|
|
|
|
// (a) if security.data_uri.unique_opaque_origin == false:
|
|
|
|
// attempt to click a link to a data: URI (will inherit the CSP of
|
|
|
|
// the origin document) and navigate to the data URI in the link.
|
|
|
|
// (b) if security.data_uri.unique_opaque_origin == true:
|
|
|
|
// attempt to click a link to a data: URI (will *not* inherit the CSP of
|
|
|
|
// the origin document) and navigate to the data URI in the link.
|
2017-03-09 22:36:28 +03:00
|
|
|
content.document.getElementById("test_data_link").click();
|
|
|
|
});
|
|
|
|
|
2017-05-12 15:42:39 +03:00
|
|
|
await loadedPromise;
|
2014-01-24 03:34:56 +04:00
|
|
|
|
2017-08-04 15:10:21 +03:00
|
|
|
await ContentTask.spawn(browser, {dataURIPref}, function( {dataURIPref}) { // eslint-disable-line
|
|
|
|
if (dataURIPref) {
|
|
|
|
is(content.document.getElementById("test_id2").value, "id2_modified",
|
|
|
|
"data: URI should *not* inherit the CSP of the enclosing context");
|
|
|
|
} else {
|
|
|
|
is(content.document.getElementById("test_id2").value, "id2_initial",
|
|
|
|
"CSP should block the script loaded by the clicked data URI");
|
|
|
|
}
|
2017-03-09 22:36:28 +03:00
|
|
|
});
|
2014-01-24 03:34:56 +04:00
|
|
|
|
|
|
|
// close the tab
|
2018-03-19 05:12:13 +03:00
|
|
|
await promiseRemoveTabAndSessionState(tab);
|
2014-01-24 03:34:56 +04:00
|
|
|
|
|
|
|
// open new tab and recover the state
|
|
|
|
tab = ss.undoCloseTab(window, 0);
|
2017-05-12 15:42:39 +03:00
|
|
|
await promiseTabRestored(tab);
|
2014-01-24 03:34:56 +04:00
|
|
|
browser = tab.linkedBrowser;
|
|
|
|
|
2017-08-04 15:10:21 +03:00
|
|
|
await ContentTask.spawn(browser, {dataURIPref}, function({dataURIPref}) { // eslint-disable-line
|
|
|
|
if (dataURIPref) {
|
|
|
|
is(content.document.getElementById("test_id2").value, "id2_modified",
|
|
|
|
"data: URI should *not* inherit the CSP of the enclosing context");
|
|
|
|
} else {
|
|
|
|
is(content.document.getElementById("test_id2").value, "id2_initial",
|
|
|
|
"CSP should block the script loaded by the clicked data URI after restore");
|
|
|
|
}
|
2017-03-09 22:36:28 +03:00
|
|
|
});
|
2014-01-24 03:34:56 +04:00
|
|
|
|
|
|
|
// clean up
|
|
|
|
gBrowser.removeTab(tab);
|
2015-01-29 11:41:57 +03:00
|
|
|
});
|
2014-01-24 03:34:56 +04:00
|
|
|
|
|
|
|
// injects an inline script element (with a text body)
|
|
|
|
function injectInlineScript(browser, scriptText) {
|
2017-03-09 22:36:28 +03:00
|
|
|
return ContentTask.spawn(browser, scriptText, function(text) {
|
|
|
|
let scriptElt = content.document.createElement("script");
|
|
|
|
scriptElt.type = "text/javascript";
|
|
|
|
scriptElt.text = text;
|
|
|
|
content.document.body.appendChild(scriptElt);
|
|
|
|
});
|
2014-01-24 03:34:56 +04:00
|
|
|
}
|