From ea829544cdc8da064ceb4ef18df5c17cd87c8fb7 Mon Sep 17 00:00:00 2001 From: Richard Barnes Date: Mon, 10 Oct 2016 11:32:24 -0400 Subject: [PATCH] Bug 1308951 - Add a pref to whitelist specific domains as SecureContexts r=ckerschb,jcj MozReview-Commit-ID: AxihCLsBNRw --HG-- extra : rebase_source : bd2800c65af839ef67f4ca9a841f08884ac9c539 --- dom/security/nsContentSecurityManager.cpp | 21 ++++++++++++++++++- .../test_isOriginPotentiallyTrustworthy.js | 8 +++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/dom/security/nsContentSecurityManager.cpp b/dom/security/nsContentSecurityManager.cpp index 0f7d7d2816f0..8432ba6958bd 100644 --- a/dom/security/nsContentSecurityManager.cpp +++ b/dom/security/nsContentSecurityManager.cpp @@ -169,7 +169,7 @@ DoContentSecurityChecks(nsIChannel* aChannel, nsILoadInfo* aLoadInfo) nsCOMPtr uri; nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(uri)); NS_ENSURE_SUCCESS(rv, rv); - + nsContentPolicyType contentPolicyType = aLoadInfo->GetExternalContentPolicyType(); nsContentPolicyType internalContentPolicyType = @@ -671,5 +671,24 @@ nsContentSecurityManager::IsOriginPotentiallyTrustworthy(nsIPrincipal* aPrincipa *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; } diff --git a/dom/security/test/unit/test_isOriginPotentiallyTrustworthy.js b/dom/security/test/unit/test_isOriginPotentiallyTrustworthy.js index 12c59108dc27..7de8faa8f9cb 100644 --- a/dom/security/test/unit/test_isOriginPotentiallyTrustworthy.js +++ b/dom/security/test/unit/test_isOriginPotentiallyTrustworthy.js @@ -19,6 +19,9 @@ 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], @@ -27,9 +30,14 @@ add_task(function* test_isOriginPotentiallyTrustworthy() { ["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);