2012-05-29 19:52:43 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-05-24 03:00:42 +04:00
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import('resource://gre/modules/NetUtil.jsm');
|
|
|
|
ChromeUtils.import("resource://gre/modules/Services.jsm");
|
|
|
|
ChromeUtils.import("resource://testing-common/httpd.js");
|
2012-05-24 03:00:42 +04:00
|
|
|
|
2013-07-16 04:13:37 +04:00
|
|
|
var httpServer = new HttpServer();
|
|
|
|
httpServer.start(-1);
|
|
|
|
var testsToFinish = 0;
|
2012-05-24 03:00:42 +04:00
|
|
|
|
2015-07-27 21:57:56 +03:00
|
|
|
var principal;
|
|
|
|
|
2013-07-16 04:13:37 +04:00
|
|
|
const REPORT_SERVER_PORT = httpServer.identity.primaryPort;
|
2012-05-24 03:00:42 +04:00
|
|
|
const REPORT_SERVER_URI = "http://localhost";
|
|
|
|
const REPORT_SERVER_PATH = "/report";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a callback that listens to a report submission and either passes
|
|
|
|
* or fails a test based on what it gets.
|
|
|
|
*/
|
|
|
|
function makeReportHandler(testpath, message, expectedJSON) {
|
|
|
|
return function(request, response) {
|
|
|
|
// we only like "POST" submissions for reports!
|
|
|
|
if (request.method !== "POST") {
|
|
|
|
do_throw("violation report should be a POST request");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-08 15:14:38 +03:00
|
|
|
// check content-type of report is "application/csp-report"
|
|
|
|
var contentType = request.hasHeader("Content-Type")
|
|
|
|
? request.getHeader("Content-Type") : undefined;
|
|
|
|
if (contentType !== "application/csp-report") {
|
|
|
|
do_throw("violation report should have the 'application/csp-report' " +
|
|
|
|
"content-type, when in fact it is " + contentType.toString())
|
|
|
|
}
|
|
|
|
|
2012-05-24 03:00:42 +04:00
|
|
|
// obtain violation report
|
|
|
|
var reportObj = JSON.parse(
|
|
|
|
NetUtil.readInputStreamToString(
|
|
|
|
request.bodyInputStream,
|
|
|
|
request.bodyInputStream.available()));
|
|
|
|
|
2016-01-14 23:37:15 +03:00
|
|
|
// dump("GOT REPORT:\n" + JSON.stringify(reportObj) + "\n");
|
|
|
|
// dump("TESTPATH: " + testpath + "\n");
|
|
|
|
// dump("EXPECTED: \n" + JSON.stringify(expectedJSON) + "\n\n");
|
2012-05-24 03:00:42 +04:00
|
|
|
|
|
|
|
for (var i in expectedJSON)
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.equal(expectedJSON[i], reportObj['csp-report'][i]);
|
2012-05-24 03:00:42 +04:00
|
|
|
|
|
|
|
testsToFinish--;
|
|
|
|
httpServer.registerPathHandler(testpath, null);
|
|
|
|
if (testsToFinish < 1)
|
|
|
|
httpServer.stop(do_test_finished);
|
|
|
|
else
|
|
|
|
do_test_finished();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-10-16 00:54:58 +04:00
|
|
|
/**
|
|
|
|
* Everything created by this assumes it will cause a report. If you want to
|
|
|
|
* add a test here that will *not* cause a report to go out, you're gonna have
|
|
|
|
* to make sure the test cleans up after itself.
|
|
|
|
*/
|
|
|
|
function makeTest(id, expectedJSON, useReportOnlyPolicy, callback) {
|
2012-05-24 03:00:42 +04:00
|
|
|
testsToFinish++;
|
|
|
|
do_test_pending();
|
|
|
|
|
|
|
|
// set up a new CSP instance for each test.
|
2014-07-03 21:36:53 +04:00
|
|
|
var csp = Cc["@mozilla.org/cspcontext;1"]
|
2012-05-24 03:00:42 +04:00
|
|
|
.createInstance(Ci.nsIContentSecurityPolicy);
|
2018-07-06 09:01:49 +03:00
|
|
|
var policy = "default-src 'none' 'report-sample'; " +
|
2012-05-24 03:00:42 +04:00
|
|
|
"report-uri " + REPORT_SERVER_URI +
|
|
|
|
":" + REPORT_SERVER_PORT +
|
|
|
|
"/test" + id;
|
|
|
|
var selfuri = NetUtil.newURI(REPORT_SERVER_URI +
|
|
|
|
":" + REPORT_SERVER_PORT +
|
|
|
|
"/foo/self");
|
|
|
|
|
|
|
|
dump("Created test " + id + " : " + policy + "\n\n");
|
|
|
|
|
2015-07-27 21:57:56 +03:00
|
|
|
let ssm = Cc["@mozilla.org/scriptsecuritymanager;1"]
|
|
|
|
.getService(Ci.nsIScriptSecurityManager);
|
2016-05-24 13:01:34 +03:00
|
|
|
principal = ssm.createCodebasePrincipal(selfuri, {});
|
2015-07-27 21:57:56 +03:00
|
|
|
csp.setRequestContext(null, principal);
|
2012-05-24 03:00:42 +04:00
|
|
|
|
|
|
|
// Load up the policy
|
2012-10-16 00:54:58 +04:00
|
|
|
// set as report-only if that's the case
|
2015-11-15 06:27:59 +03:00
|
|
|
csp.appendPolicy(policy, useReportOnlyPolicy, false);
|
2012-10-16 00:54:58 +04:00
|
|
|
|
2012-05-24 03:00:42 +04:00
|
|
|
// prime the report server
|
|
|
|
var handler = makeReportHandler("/test" + id, "Test " + id, expectedJSON);
|
|
|
|
httpServer.registerPathHandler("/test" + id, handler);
|
|
|
|
|
|
|
|
//trigger the violation
|
|
|
|
callback(csp);
|
|
|
|
}
|
|
|
|
|
|
|
|
function run_test() {
|
|
|
|
var selfuri = NetUtil.newURI(REPORT_SERVER_URI +
|
|
|
|
":" + REPORT_SERVER_PORT +
|
|
|
|
"/foo/self");
|
|
|
|
|
|
|
|
// test that inline script violations cause a report.
|
2018-07-17 12:13:12 +03:00
|
|
|
makeTest(0, {"blocked-uri": "inline"}, false,
|
2012-05-24 03:00:42 +04:00
|
|
|
function(csp) {
|
2015-09-18 08:34:49 +03:00
|
|
|
let inlineOK = true;
|
|
|
|
inlineOK = csp.getAllowsInline(Ci.nsIContentPolicy.TYPE_SCRIPT,
|
|
|
|
"", // aNonce
|
2016-11-08 14:55:23 +03:00
|
|
|
false, // aParserCreated
|
2018-07-10 18:40:21 +03:00
|
|
|
null, // aTriggeringElement
|
|
|
|
"", // aContentOfPseudoScript
|
2018-07-05 09:21:04 +03:00
|
|
|
0, // aLineNumber
|
|
|
|
0); // aColumnNumber
|
2012-10-16 00:54:58 +04:00
|
|
|
|
|
|
|
// this is not a report only policy, so it better block inline scripts
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(!inlineOK);
|
2012-05-24 03:00:42 +04:00
|
|
|
});
|
|
|
|
|
2012-10-16 00:54:58 +04:00
|
|
|
// test that eval violations cause a report.
|
2018-07-17 12:13:12 +03:00
|
|
|
makeTest(1, {"blocked-uri": "eval",
|
2016-10-14 21:07:32 +03:00
|
|
|
// JSON script-sample is UTF8 encoded
|
2018-07-05 09:21:04 +03:00
|
|
|
"script-sample" : "\xc2\xa3\xc2\xa5\xc2\xb5\xe5\x8c\x97\xf0\xa0\x9d\xb9",
|
|
|
|
"line-number": 1,
|
|
|
|
"column-number": 2}, false,
|
2012-05-24 03:00:42 +04:00
|
|
|
function(csp) {
|
2013-09-12 20:25:32 +04:00
|
|
|
let evalOK = true, oReportViolation = {'value': false};
|
2012-10-16 00:54:58 +04:00
|
|
|
evalOK = csp.getAllowsEval(oReportViolation);
|
|
|
|
|
|
|
|
// this is not a report only policy, so it better block eval
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(!evalOK);
|
2012-10-16 00:54:58 +04:00
|
|
|
// ... and cause reports to go out
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(oReportViolation.value);
|
2012-10-16 00:54:58 +04:00
|
|
|
|
2013-09-12 20:25:32 +04:00
|
|
|
if (oReportViolation.value) {
|
|
|
|
// force the logging, since the getter doesn't.
|
|
|
|
csp.logViolationDetails(Ci.nsIContentSecurityPolicy.VIOLATION_TYPE_EVAL,
|
2018-07-10 18:40:21 +03:00
|
|
|
null, // aTriggeringElement
|
2013-09-12 20:25:32 +04:00
|
|
|
selfuri.asciiSpec,
|
2016-10-14 21:07:32 +03:00
|
|
|
// sending UTF-16 script sample to make sure
|
|
|
|
// csp report in JSON is not cut-off, please
|
|
|
|
// note that JSON is UTF8 encoded.
|
|
|
|
"\u00a3\u00a5\u00b5\u5317\ud841\udf79",
|
2018-07-05 09:21:04 +03:00
|
|
|
1, // line number
|
|
|
|
2); // column number
|
2013-09-12 20:25:32 +04:00
|
|
|
}
|
2012-05-24 03:00:42 +04:00
|
|
|
});
|
|
|
|
|
2018-07-18 17:49:18 +03:00
|
|
|
makeTest(2, {"blocked-uri": "http://blocked.test/foo.js"}, false,
|
2012-05-24 03:00:42 +04:00
|
|
|
function(csp) {
|
2012-10-16 00:54:58 +04:00
|
|
|
// shouldLoad creates and sends out the report here.
|
2012-05-24 03:00:42 +04:00
|
|
|
csp.shouldLoad(Ci.nsIContentPolicy.TYPE_SCRIPT,
|
|
|
|
NetUtil.newURI("http://blocked.test/foo.js"),
|
2018-08-01 07:35:24 +03:00
|
|
|
null, null, null, null, true);
|
2012-05-24 03:00:42 +04:00
|
|
|
});
|
2012-10-16 00:54:58 +04:00
|
|
|
|
|
|
|
// test that inline script violations cause a report in report-only policy
|
2018-07-17 12:13:12 +03:00
|
|
|
makeTest(3, {"blocked-uri": "inline"}, true,
|
2012-10-16 00:54:58 +04:00
|
|
|
function(csp) {
|
2015-09-18 08:34:49 +03:00
|
|
|
let inlineOK = true;
|
|
|
|
inlineOK = csp.getAllowsInline(Ci.nsIContentPolicy.TYPE_SCRIPT,
|
|
|
|
"", // aNonce
|
2016-11-08 14:55:23 +03:00
|
|
|
false, // aParserCreated
|
2018-07-10 18:40:21 +03:00
|
|
|
null, // aTriggeringElement
|
|
|
|
"", // aContentOfPseudoScript
|
2018-07-05 09:21:04 +03:00
|
|
|
0, // aLineNumber
|
|
|
|
0); // aColumnNumber
|
2012-10-16 00:54:58 +04:00
|
|
|
|
|
|
|
// this is a report only policy, so it better allow inline scripts
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(inlineOK);
|
2012-10-16 00:54:58 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
// test that eval violations cause a report in report-only policy
|
2018-07-17 12:13:12 +03:00
|
|
|
makeTest(4, {"blocked-uri": "inline"}, true,
|
2012-10-16 00:54:58 +04:00
|
|
|
function(csp) {
|
2013-09-12 20:25:32 +04:00
|
|
|
let evalOK = true, oReportViolation = {'value': false};
|
2012-10-16 00:54:58 +04:00
|
|
|
evalOK = csp.getAllowsEval(oReportViolation);
|
|
|
|
|
|
|
|
// this is a report only policy, so it better allow eval
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(evalOK);
|
2012-10-16 00:54:58 +04:00
|
|
|
// ... but still cause reports to go out
|
2017-12-21 13:08:17 +03:00
|
|
|
Assert.ok(oReportViolation.value);
|
2012-10-16 00:54:58 +04:00
|
|
|
|
2013-09-12 20:25:32 +04:00
|
|
|
if (oReportViolation.value) {
|
|
|
|
// force the logging, since the getter doesn't.
|
|
|
|
csp.logViolationDetails(Ci.nsIContentSecurityPolicy.VIOLATION_TYPE_INLINE_SCRIPT,
|
2018-07-10 18:40:21 +03:00
|
|
|
null, // aTriggeringElement
|
2013-09-12 20:25:32 +04:00
|
|
|
selfuri.asciiSpec,
|
|
|
|
"script sample",
|
2018-07-05 09:21:04 +03:00
|
|
|
4, // line number
|
|
|
|
5); // column number
|
2013-09-12 20:25:32 +04:00
|
|
|
}
|
2012-10-16 00:54:58 +04:00
|
|
|
});
|
2016-01-14 23:37:15 +03:00
|
|
|
|
|
|
|
// test that only the uri's scheme is reported for globally unique identifiers
|
|
|
|
makeTest(5, {"blocked-uri": "data"}, false,
|
|
|
|
function(csp) {
|
|
|
|
var base64data =
|
|
|
|
"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12" +
|
|
|
|
"P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
|
|
|
|
// shouldLoad creates and sends out the report here.
|
|
|
|
csp.shouldLoad(Ci.nsIContentPolicy.TYPE_IMAGE,
|
|
|
|
NetUtil.newURI("data:image/png;base64," + base64data),
|
2018-08-01 07:35:24 +03:00
|
|
|
null, null, null, null, true);
|
2016-01-14 23:37:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// test that only the uri's scheme is reported for globally unique identifiers
|
|
|
|
makeTest(6, {"blocked-uri": "intent"}, false,
|
|
|
|
function(csp) {
|
|
|
|
// shouldLoad creates and sends out the report here.
|
|
|
|
csp.shouldLoad(Ci.nsIContentPolicy.TYPE_SUBDOCUMENT,
|
|
|
|
NetUtil.newURI("intent://mymaps.com/maps?um=1&ie=UTF-8&fb=1&sll"),
|
2018-08-01 07:35:24 +03:00
|
|
|
null, null, null, null, true);
|
2016-01-14 23:37:15 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// test fragment removal
|
|
|
|
var selfSpec = REPORT_SERVER_URI + ":" + REPORT_SERVER_PORT + "/foo/self/foo.js";
|
|
|
|
makeTest(7, {"blocked-uri": selfSpec}, false,
|
|
|
|
function(csp) {
|
|
|
|
var uri = NetUtil
|
|
|
|
// shouldLoad creates and sends out the report here.
|
|
|
|
csp.shouldLoad(Ci.nsIContentPolicy.TYPE_SCRIPT,
|
|
|
|
NetUtil.newURI(selfSpec + "#bar"),
|
2018-08-01 07:35:24 +03:00
|
|
|
null, null, null, null, true);
|
2016-01-14 23:37:15 +03:00
|
|
|
});
|
2016-04-17 21:09:18 +03:00
|
|
|
|
|
|
|
// test scheme of ftp:
|
2018-07-18 17:49:18 +03:00
|
|
|
makeTest(8, {"blocked-uri": "ftp://blocked.test/profile.png"}, false,
|
2016-04-17 21:09:18 +03:00
|
|
|
function(csp) {
|
|
|
|
// shouldLoad creates and sends out the report here.
|
|
|
|
csp.shouldLoad(Ci.nsIContentPolicy.TYPE_SCRIPT,
|
|
|
|
NetUtil.newURI("ftp://blocked.test/profile.png"),
|
2018-08-01 07:35:24 +03:00
|
|
|
null, null, null, null, true);
|
2016-04-17 21:09:18 +03:00
|
|
|
});
|
2012-05-24 03:00:42 +04:00
|
|
|
}
|