Skip to content

Commit

Permalink
Bug 1279420 - Adding in security.csp.experimentalEnabled pref check t…
Browse files Browse the repository at this point in the history
…o require-sri-for directive in CSP. r=ckerschb

MozReview-Commit-ID: 799ZZoW0YiG

--HG--
extra : transplant_source : %CAC%12%16%C6a%10AP%BEc%85%BA%93Z%7Cq%D43%8D
  • Loading branch information
Jonathan Kingston committed Jun 20, 2016
1 parent e62bd5f commit daa6f72
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 9 deletions.
18 changes: 10 additions & 8 deletions dom/security/nsCSPParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/ArrayUtils.h"
#include "mozilla/Preferences.h"
#include "nsCOMPtr.h"
#include "nsCSPParser.h"
#include "nsCSPUtils.h"
Expand Down Expand Up @@ -120,6 +121,7 @@ nsCSPTokenizer::tokenizeCSPPolicy(const nsAString &aPolicyString,
}

/* ===== nsCSPParser ==================== */
bool nsCSPParser::sCSPExperimentalEnabled = false;

nsCSPParser::nsCSPParser(cspTokens& aTokens,
nsIURI* aSelfURI,
Expand All @@ -137,6 +139,11 @@ nsCSPParser::nsCSPParser(cspTokens& aTokens,
, mCSPContext(aCSPContext)
, mDeliveredViaMetaTag(aDeliveredViaMetaTag)
{
static bool initialized = false;
if (!initialized) {
initialized = true;
Preferences::AddBoolVarCache(&sCSPExperimentalEnabled, "security.csp.experimentalEnabled");
}
CSPPARSERLOG(("nsCSPParser::nsCSPParser"));
}

Expand Down Expand Up @@ -1007,13 +1014,6 @@ nsCSPParser::directiveValue(nsTArray<nsCSPBaseSrc*>& outSrcs)
return;
}

// special case handling of the require-sri-for directive (since it doesn't
// contain a source lists but rather types, e.g., style or script)
if (CSP_IsDirective(mCurDir[0], nsIContentSecurityPolicy::REQUIRE_SRI_FOR)) {
// handled in directive()
return;
}

// Otherwise just forward to sourceList
sourceList(outSrcs);
}
Expand All @@ -1027,7 +1027,9 @@ nsCSPParser::directiveName()
NS_ConvertUTF16toUTF8(mCurValue).get()));

// Check if it is a valid directive
if (!CSP_IsValidDirective(mCurToken)) {
if (!CSP_IsValidDirective(mCurToken) ||
(!sCSPExperimentalEnabled &&
CSP_IsDirective(mCurToken, nsIContentSecurityPolicy::REQUIRE_SRI_FOR))) {
const char16_t* params[] = { mCurToken.get() };
logWarningErrorToConsole(nsIScriptError::warningFlag, "couldNotProcessUnknownDirective",
params, ArrayLength(params));
Expand Down
2 changes: 2 additions & 0 deletions dom/security/nsCSPParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class nsCSPParser {
nsCSPContext* aCSPContext,
bool aDeliveredViaMetaTag);

static bool sCSPExperimentalEnabled;

~nsCSPParser();


Expand Down
15 changes: 15 additions & 0 deletions dom/security/test/TestCSPParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ template<class T> class nsReadingIterator;
#include "TestHarness.h"
#include "nsIScriptSecurityManager.h"
#include "mozilla/dom/nsCSPContext.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"

#ifndef MOZILLA_INTERNAL_API
#undef nsString_h___
Expand Down Expand Up @@ -166,10 +168,23 @@ nsresult runTestSuite(const PolicyTest* aPolicies,
uint32_t aPolicyCount,
uint32_t aExpectedPolicyCount) {
nsresult rv;
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
bool experimentalEnabledCache = false;
if (prefs)
{
prefs->GetBoolPref("security.csp.experimentalEnabled", &experimentalEnabledCache);
prefs->SetBoolPref("security.csp.experimentalEnabled", true);
}

for (uint32_t i = 0; i < aPolicyCount; i++) {
rv = runTest(aExpectedPolicyCount, aPolicies[i].policy, aPolicies[i].expectedResult);
NS_ENSURE_SUCCESS(rv, rv);
}

if (prefs) {
prefs->SetBoolPref("security.csp.experimentalEnabled", experimentalEnabledCache);
}

return NS_OK;
}

Expand Down
1 change: 1 addition & 0 deletions dom/security/test/sri/mochitest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ support-files =
[test_style_crossdomain.html]
[test_style_sameorigin.html]
[test_require-sri-for_csp_directive.html]
[test_require-sri-for_csp_directive_disabled.html]
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<iframe style="width:200px;height:200px;" id="test_frame"></iframe>
</body>
<script type="application/javascript">
SpecialPowers.setBoolPref("security.csp.experimentalEnabled", true);
SimpleTest.waitForExplicitFinish();
function handler(event) {
switch (event.data) {
Expand All @@ -29,11 +30,12 @@
case 'finish':
var blackText = frame.contentDocument.getElementById('black-text');
var blackTextColor = frame.contentWindow.getComputedStyle(blackText, null).getPropertyValue('color');
ok(blackTextColor == 'rgb(0, 0, 0)', "The second part should still be black.");
ok(blackTextColor == 'rgb(0, 0, 0)', "The second part should not be black.");
removeEventListener('message', handler);
SimpleTest.finish();
break;
default:
ok(false, 'Something is wrong here');
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
<title>Test for diabled SRI require-sri-for CSP directive</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1265318">Mozilla Bug 1265318</a>
<iframe style="width:200px;height:200px;" id="test_frame"></iframe>
</body>
<script type="application/javascript">
SpecialPowers.setBoolPref("security.csp.experimentalEnabled", false);
SimpleTest.waitForExplicitFinish();
function handler(event) {
switch (event.data) {
case 'good_sriLoaded':
ok(true, "Eligible SRI resources was correctly loaded.");
break;
case 'bad_nonsriLoaded':
ok(true, "Eligible non-SRI resource should be blocked by the CSP!");
break;
case 'good_nonsriBlocked':
ok(false, "Eligible non-SRI resources was correctly blocked by the CSP.");
break;
case 'finish':
var blackText = frame.contentDocument.getElementById('black-text');
var blackTextColor = frame.contentWindow.getComputedStyle(blackText, null).getPropertyValue('color');
ok(blackTextColor != 'rgb(0, 0, 0)', "The second part should still be black.");
removeEventListener('message', handler);
SimpleTest.finish();
break;
default:
ok(false, 'Something is wrong here');
break;
}
}
addEventListener("message", handler);
var frame = document.getElementById("test_frame");
frame.src = "iframe_require-sri-for_main.html";
</script>
</html>

0 comments on commit daa6f72

Please sign in to comment.