Bug 1308951 - Add a pref to whitelist specific domains as SecureContexts r=jcj r=ckerschb
MozReview-Commit-ID: AxihCLsBNRw
--- a/dom/security/nsContentSecurityManager.cpp
+++ b/dom/security/nsContentSecurityManager.cpp
@@ -164,17 +164,17 @@ DoCORSChecks(nsIChannel* aChannel, nsILo
}
static nsresult
DoContentSecurityChecks(nsIChannel* aChannel, nsILoadInfo* aLoadInfo)
{
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
-
+
nsContentPolicyType contentPolicyType =
aLoadInfo->GetExternalContentPolicyType();
nsContentPolicyType internalContentPolicyType =
aLoadInfo->InternalContentPolicyType();
nsCString mimeTypeGuess;
nsCOMPtr<nsINode> requestingContext = nullptr;
#ifdef DEBUG
@@ -666,10 +666,29 @@ nsContentSecurityManager::IsOriginPotent
}
if (host.Equals("127.0.0.1") ||
host.Equals("localhost") ||
host.Equals("::1")) {
*aIsTrustWorthy = true;
return NS_OK;
}
+
+ // If a host is not considered secure according to the default algorithm, then
+ // check to see if it has been whitelisted by the user. We only apply this
+ // whitelist for network resources, i.e., those with scheme "http" or "ws".
+ // The pref should contain a comma-separated list of hostnames.
+ if (scheme.EqualsLiteral("http") || scheme.EqualsLiteral("ws")) {
+ nsAdoptingCString whitelist = Preferences::GetCString("dom.securecontext.whitelist");
+ if (whitelist) {
+ nsCCharSeparatedTokenizer tokenizer(whitelist, ',');
+ while (tokenizer.hasMoreTokens()) {
+ const nsCSubstring& allowedHost = tokenizer.nextToken();
+ if (host.Equals(allowedHost)) {
+ *aIsTrustWorthy = true;
+ return NS_OK;
+ }
+ }
+ }
+ }
+
return NS_OK;
}
--- a/dom/security/test/unit/test_isOriginPotentiallyTrustworthy.js
+++ b/dom/security/test/unit/test_isOriginPotentiallyTrustworthy.js
@@ -14,26 +14,34 @@ Cu.import("resource://gre/modules/XPCOMU
XPCOMUtils.defineLazyServiceGetter(this, "gScriptSecurityManager",
"@mozilla.org/scriptsecuritymanager;1",
"nsIScriptSecurityManager");
XPCOMUtils.defineLazyServiceGetter(this, "gContentSecurityManager",
"@mozilla.org/contentsecuritymanager;1",
"nsIContentSecurityManager");
+var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
+prefs.setCharPref("dom.securecontext.whitelist", "example.net,example.org");
+
add_task(function* test_isOriginPotentiallyTrustworthy() {
for (let [uriSpec, expectedResult] of [
["http://example.com/", false],
["https://example.com/", true],
["http://localhost/", true],
["http://127.0.0.1/", true],
["file:///", true],
["resource:///", true],
+ ["app://", true],
["moz-extension://", true],
+ ["wss://example.com/", true],
["about:config", false],
["urn:generic", false],
+ ["http://example.net/", true],
+ ["ws://example.org/", true],
+ ["chrome://example.net/content/messenger.xul", false],
]) {
let uri = NetUtil.newURI(uriSpec);
let principal = gScriptSecurityManager.getCodebasePrincipal(uri);
Assert.equal(gContentSecurityManager.isOriginPotentiallyTrustworthy(principal),
expectedResult);
}
});