2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00: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/. */
|
2010-01-22 21:38:21 +00:00
|
|
|
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsCSPService.h"
|
|
|
|
#include "nsIContentSecurityPolicy.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2010-08-05 02:15:55 +00:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
|
|
|
#include "nsAsyncRedirectVerifyHelper.h"
|
2011-05-28 23:42:57 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-12-05 17:42:08 +00:00
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsContentUtils.h"
|
2014-10-16 02:12:53 +00:00
|
|
|
#include "nsContentPolicyUtils.h"
|
2014-08-29 00:20:27 +00:00
|
|
|
#include "nsPrincipal.h"
|
2011-05-28 23:42:57 +00:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2010-01-22 21:38:21 +00:00
|
|
|
|
|
|
|
/* Keeps track of whether or not CSP is enabled */
|
2011-09-29 06:19:26 +00:00
|
|
|
bool CSPService::sCSPEnabled = true;
|
2010-01-22 21:38:21 +00:00
|
|
|
|
|
|
|
static PRLogModuleInfo* gCspPRLog;
|
|
|
|
|
|
|
|
CSPService::CSPService()
|
|
|
|
{
|
2011-05-28 23:42:57 +00:00
|
|
|
Preferences::AddBoolVarCache(&sCSPEnabled, "security.csp.enable");
|
2010-01-22 21:38:21 +00:00
|
|
|
|
|
|
|
if (!gCspPRLog)
|
|
|
|
gCspPRLog = PR_NewLogModule("CSP");
|
|
|
|
}
|
|
|
|
|
|
|
|
CSPService::~CSPService()
|
|
|
|
{
|
2013-10-17 17:56:12 +00:00
|
|
|
mAppStatusCache.Clear();
|
2010-01-22 21:38:21 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(CSPService, nsIContentPolicy, nsIChannelEventSink)
|
2010-01-22 21:38:21 +00:00
|
|
|
|
2014-11-20 22:59:53 +00:00
|
|
|
// Helper function to identify protocols not subject to CSP.
|
|
|
|
bool
|
|
|
|
subjectToCSP(nsIURI* aURI) {
|
|
|
|
// The three protocols: data:, blob: and filesystem: share the same
|
|
|
|
// protocol flag (URI_IS_LOCAL_RESOURCE) with other protocols, like
|
|
|
|
// chrome:, resource:, moz-icon:, but those three protocols get
|
|
|
|
// special attention in CSP and are subject to CSP, hence we have
|
|
|
|
// to make sure those protocols are subject to CSP, see:
|
|
|
|
// http://www.w3.org/TR/CSP2/#source-list-guid-matching
|
|
|
|
bool match = false;
|
|
|
|
nsresult rv = aURI->SchemeIs("data", &match);
|
|
|
|
if (NS_SUCCEEDED(rv) && match) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
rv = aURI->SchemeIs("blob", &match);
|
|
|
|
if (NS_SUCCEEDED(rv) && match) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
rv = aURI->SchemeIs("filesystem", &match);
|
|
|
|
if (NS_SUCCEEDED(rv) && match) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// finally we have to whitelist "about:" which does not fall in
|
|
|
|
// any of the two categories underneath but is not subject to CSP.
|
|
|
|
rv = aURI->SchemeIs("about", &match);
|
|
|
|
if (NS_SUCCEEDED(rv) && match) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other protocols are not subject to CSP and can be whitelisted:
|
|
|
|
// * URI_IS_LOCAL_RESOURCE
|
|
|
|
// e.g. chrome:, data:, blob:, resource:, moz-icon:
|
|
|
|
// * URI_INHERITS_SECURITY_CONTEXT
|
|
|
|
// e.g. javascript:
|
|
|
|
//
|
|
|
|
// Please note that it should be possible for websites to
|
|
|
|
// whitelist their own protocol handlers with respect to CSP,
|
|
|
|
// hence we use protocol flags to accomplish that.
|
|
|
|
rv = NS_URIChainHasFlags(aURI, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE, &match);
|
|
|
|
if (NS_SUCCEEDED(rv) && match) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
rv = NS_URIChainHasFlags(aURI, nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT, &match);
|
|
|
|
if (NS_SUCCEEDED(rv) && match) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// all other protocols are subject To CSP.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-01-22 21:38:21 +00:00
|
|
|
/* nsIContentPolicy implementation */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
CSPService::ShouldLoad(uint32_t aContentType,
|
2010-01-22 21:38:21 +00:00
|
|
|
nsIURI *aContentLocation,
|
|
|
|
nsIURI *aRequestOrigin,
|
|
|
|
nsISupports *aRequestContext,
|
|
|
|
const nsACString &aMimeTypeGuess,
|
|
|
|
nsISupports *aExtra,
|
2012-07-02 23:16:11 +00:00
|
|
|
nsIPrincipal *aRequestPrincipal,
|
2012-08-22 15:56:38 +00:00
|
|
|
int16_t *aDecision)
|
2010-01-22 21:38:21 +00:00
|
|
|
{
|
2014-11-20 22:59:53 +00:00
|
|
|
if (!aContentLocation) {
|
2013-09-12 16:25:32 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-11-20 22:59:53 +00:00
|
|
|
}
|
2010-01-22 21:38:21 +00:00
|
|
|
|
2015-05-11 21:22:04 +00:00
|
|
|
if (PR_LOG_TEST(gCspPRLog, PR_LOG_DEBUG)) {
|
2013-09-12 16:25:32 +00:00
|
|
|
nsAutoCString location;
|
|
|
|
aContentLocation->GetSpec(location);
|
|
|
|
PR_LOG(gCspPRLog, PR_LOG_DEBUG,
|
|
|
|
("CSPService::ShouldLoad called for %s", location.get()));
|
|
|
|
}
|
2012-12-14 22:53:29 +00:00
|
|
|
|
2013-09-12 16:25:32 +00:00
|
|
|
// default decision, CSP can revise it if there's a policy to enforce
|
|
|
|
*aDecision = nsIContentPolicy::ACCEPT;
|
|
|
|
|
2014-11-20 22:59:53 +00:00
|
|
|
// No need to continue processing if CSP is disabled or if the protocol
|
|
|
|
// is *not* subject to CSP.
|
|
|
|
// Please note, the correct way to opt-out of CSP using a custom
|
|
|
|
// protocolHandler is to set one of the nsIProtocolHandler flags
|
|
|
|
// that are whitelistet in subjectToCSP()
|
|
|
|
if (!sCSPEnabled || !subjectToCSP(aContentLocation)) {
|
2013-09-12 16:25:32 +00:00
|
|
|
return NS_OK;
|
2014-11-20 22:59:53 +00:00
|
|
|
}
|
2010-01-22 21:38:21 +00:00
|
|
|
|
2013-09-12 16:25:32 +00:00
|
|
|
// These content types are not subject to CSP content policy checks:
|
2014-07-30 21:37:22 +00:00
|
|
|
// TYPE_CSP_REPORT -- csp can't block csp reports
|
|
|
|
// TYPE_REFRESH -- never passed to ShouldLoad (see nsIContentPolicy.idl)
|
|
|
|
// TYPE_DOCUMENT -- used for frame-ancestors
|
2013-09-12 16:25:32 +00:00
|
|
|
if (aContentType == nsIContentPolicy::TYPE_CSP_REPORT ||
|
|
|
|
aContentType == nsIContentPolicy::TYPE_REFRESH ||
|
|
|
|
aContentType == nsIContentPolicy::TYPE_DOCUMENT) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-15 23:18:50 +00:00
|
|
|
// ----- THIS IS A TEMPORARY FAST PATH FOR CERTIFIED APPS. -----
|
|
|
|
// ----- PLEASE REMOVE ONCE bug 925004 LANDS. -----
|
|
|
|
|
|
|
|
// Cache the app status for this origin.
|
|
|
|
uint16_t status = nsIPrincipal::APP_STATUS_NOT_INSTALLED;
|
2015-03-08 13:21:00 +00:00
|
|
|
nsAutoCString sourceOrigin;
|
|
|
|
if (aRequestPrincipal && aRequestOrigin) {
|
|
|
|
aRequestOrigin->GetPrePath(sourceOrigin);
|
|
|
|
if (!mAppStatusCache.Get(sourceOrigin, &status)) {
|
|
|
|
aRequestPrincipal->GetAppStatus(&status);
|
|
|
|
mAppStatusCache.Put(sourceOrigin, status);
|
|
|
|
}
|
2014-07-15 23:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (status == nsIPrincipal::APP_STATUS_CERTIFIED) {
|
|
|
|
// The CSP for certified apps is :
|
2015-03-25 22:54:13 +00:00
|
|
|
// "default-src * data: blob:; script-src 'self'; object-src 'none'; style-src 'self' app://theme.gaiamobile.org:*"
|
2014-07-15 23:18:50 +00:00
|
|
|
// That means we can optimize for this case by:
|
2014-08-29 00:20:27 +00:00
|
|
|
// - loading same origin scripts and stylesheets, and stylesheets from the
|
|
|
|
// theme url space.
|
2014-07-15 23:18:50 +00:00
|
|
|
// - never loading objects.
|
|
|
|
// - accepting everything else.
|
|
|
|
|
|
|
|
switch (aContentType) {
|
|
|
|
case nsIContentPolicy::TYPE_SCRIPT:
|
|
|
|
case nsIContentPolicy::TYPE_STYLESHEET:
|
|
|
|
{
|
2014-08-29 00:20:27 +00:00
|
|
|
// Whitelist the theme resources.
|
|
|
|
auto themeOrigin = Preferences::GetCString("b2g.theme.origin");
|
2015-03-08 13:21:00 +00:00
|
|
|
nsAutoCString contentOrigin;
|
|
|
|
aContentLocation->GetPrePath(contentOrigin);
|
2014-08-29 00:20:27 +00:00
|
|
|
|
|
|
|
if (!(sourceOrigin.Equals(contentOrigin) ||
|
|
|
|
(themeOrigin && themeOrigin.Equals(contentOrigin)))) {
|
2014-07-15 23:18:50 +00:00
|
|
|
*aDecision = nsIContentPolicy::REJECT_SERVER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case nsIContentPolicy::TYPE_OBJECT:
|
|
|
|
*aDecision = nsIContentPolicy::REJECT_SERVER;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*aDecision = nsIContentPolicy::ACCEPT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only cache and return if we are successful. If not, we want the error
|
|
|
|
// to be reported, and thus fallback to the slow path.
|
|
|
|
if (*aDecision == nsIContentPolicy::ACCEPT) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----- END OF TEMPORARY FAST PATH FOR CERTIFIED APPS. -----
|
|
|
|
|
2013-09-12 16:25:32 +00:00
|
|
|
// find the principal of the document that initiated this request and see
|
|
|
|
// if it has a CSP policy object
|
|
|
|
nsCOMPtr<nsINode> node(do_QueryInterface(aRequestContext));
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
if (node) {
|
|
|
|
principal = node->NodePrincipal();
|
|
|
|
principal->GetCsp(getter_AddRefs(csp));
|
|
|
|
|
|
|
|
if (csp) {
|
2015-05-11 21:22:04 +00:00
|
|
|
if (PR_LOG_TEST(gCspPRLog, PR_LOG_DEBUG)) {
|
2014-04-01 16:35:22 +00:00
|
|
|
uint32_t numPolicies = 0;
|
2013-09-12 16:25:32 +00:00
|
|
|
nsresult rv = csp->GetPolicyCount(&numPolicies);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-04-01 16:35:22 +00:00
|
|
|
for (uint32_t i=0; i<numPolicies; i++) {
|
2010-01-22 21:38:21 +00:00
|
|
|
nsAutoString policy;
|
2013-09-12 16:25:32 +00:00
|
|
|
csp->GetPolicy(i, policy);
|
2010-07-03 01:15:13 +00:00
|
|
|
PR_LOG(gCspPRLog, PR_LOG_DEBUG,
|
2013-09-12 16:25:32 +00:00
|
|
|
("Document has CSP[%d]: %s", i,
|
|
|
|
NS_ConvertUTF16toUTF8(policy).get()));
|
|
|
|
}
|
2010-01-22 21:38:21 +00:00
|
|
|
}
|
2013-09-12 16:25:32 +00:00
|
|
|
}
|
|
|
|
// obtain the enforcement decision
|
|
|
|
// (don't pass aExtra, we use that slot for redirects)
|
|
|
|
csp->ShouldLoad(aContentType,
|
|
|
|
aContentLocation,
|
|
|
|
aRequestOrigin,
|
|
|
|
aRequestContext,
|
|
|
|
aMimeTypeGuess,
|
|
|
|
nullptr,
|
|
|
|
aDecision);
|
2010-01-22 21:38:21 +00:00
|
|
|
}
|
2013-09-12 16:25:32 +00:00
|
|
|
}
|
2015-05-11 21:22:04 +00:00
|
|
|
else if (PR_LOG_TEST(gCspPRLog, PR_LOG_DEBUG)) {
|
2013-09-12 16:25:32 +00:00
|
|
|
nsAutoCString uriSpec;
|
|
|
|
aContentLocation->GetSpec(uriSpec);
|
|
|
|
PR_LOG(gCspPRLog, PR_LOG_DEBUG,
|
|
|
|
("COULD NOT get nsINode for location: %s", uriSpec.get()));
|
|
|
|
}
|
2010-04-23 17:03:03 +00:00
|
|
|
|
2013-09-12 16:25:32 +00:00
|
|
|
return NS_OK;
|
2010-01-22 21:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
CSPService::ShouldProcess(uint32_t aContentType,
|
2010-01-22 21:38:21 +00:00
|
|
|
nsIURI *aContentLocation,
|
|
|
|
nsIURI *aRequestOrigin,
|
|
|
|
nsISupports *aRequestContext,
|
|
|
|
const nsACString &aMimeTypeGuess,
|
|
|
|
nsISupports *aExtra,
|
2012-07-02 23:16:11 +00:00
|
|
|
nsIPrincipal *aRequestPrincipal,
|
2012-08-22 15:56:38 +00:00
|
|
|
int16_t *aDecision)
|
2010-01-22 21:38:21 +00:00
|
|
|
{
|
2013-09-12 16:25:32 +00:00
|
|
|
if (!aContentLocation)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
*aDecision = nsIContentPolicy::ACCEPT;
|
|
|
|
return NS_OK;
|
2010-01-22 21:38:21 +00:00
|
|
|
}
|
2010-04-23 17:03:03 +00:00
|
|
|
|
|
|
|
/* nsIChannelEventSink implementation */
|
|
|
|
NS_IMETHODIMP
|
2010-08-05 02:15:55 +00:00
|
|
|
CSPService::AsyncOnChannelRedirect(nsIChannel *oldChannel,
|
|
|
|
nsIChannel *newChannel,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t flags,
|
2010-08-05 02:15:55 +00:00
|
|
|
nsIAsyncVerifyRedirectCallback *callback)
|
2010-04-23 17:03:03 +00:00
|
|
|
{
|
2010-08-05 02:15:55 +00:00
|
|
|
nsAsyncRedirectAutoCallback autoCallback(callback);
|
|
|
|
|
2014-12-17 22:19:25 +00:00
|
|
|
nsCOMPtr<nsIURI> newUri;
|
|
|
|
nsresult rv = newChannel->GetURI(getter_AddRefs(newUri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// No need to continue processing if CSP is disabled or if the protocol
|
|
|
|
// is *not* subject to CSP.
|
|
|
|
// Please note, the correct way to opt-out of CSP using a custom
|
|
|
|
// protocolHandler is to set one of the nsIProtocolHandler flags
|
|
|
|
// that are whitelistet in subjectToCSP()
|
|
|
|
if (!sCSPEnabled || !subjectToCSP(newUri)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-16 02:12:53 +00:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo;
|
2014-12-17 22:19:25 +00:00
|
|
|
rv = oldChannel->GetLoadInfo(getter_AddRefs(loadInfo));
|
2010-04-23 17:03:03 +00:00
|
|
|
|
2014-10-16 02:12:53 +00:00
|
|
|
// if no loadInfo on the channel, nothing for us to do
|
|
|
|
if (!loadInfo) {
|
2010-04-23 17:03:03 +00:00
|
|
|
return NS_OK;
|
2014-10-16 02:12:53 +00:00
|
|
|
}
|
|
|
|
|
2014-12-17 22:19:25 +00:00
|
|
|
// Get the LoadingPrincipal and CSP from the loadInfo
|
2010-04-23 17:03:03 +00:00
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
2014-12-17 22:19:25 +00:00
|
|
|
rv = loadInfo->LoadingPrincipal()->GetCsp(getter_AddRefs(csp));
|
2014-10-16 02:12:53 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-04-23 17:03:03 +00:00
|
|
|
|
2014-10-16 02:12:53 +00:00
|
|
|
// if there is no CSP, nothing for us to do
|
|
|
|
if (!csp) {
|
2010-04-23 17:03:03 +00:00
|
|
|
return NS_OK;
|
2014-10-16 02:12:53 +00:00
|
|
|
}
|
2010-04-23 17:03:03 +00:00
|
|
|
|
|
|
|
/* Since redirecting channels don't call into nsIContentPolicy, we call our
|
2014-10-16 02:12:53 +00:00
|
|
|
* Content Policy implementation directly when redirects occur using the
|
|
|
|
* information set in the LoadInfo when channels are created.
|
|
|
|
*
|
|
|
|
* We check if the CSP permits this host for this type of load, if not,
|
|
|
|
* we cancel the load now.
|
2010-04-23 17:03:03 +00:00
|
|
|
*/
|
2012-07-10 07:44:05 +00:00
|
|
|
nsCOMPtr<nsIURI> originalUri;
|
2014-10-16 02:12:53 +00:00
|
|
|
rv = oldChannel->GetOriginalURI(getter_AddRefs(originalUri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsContentPolicyType policyType = loadInfo->GetContentPolicyType();
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int16_t aDecision = nsIContentPolicy::ACCEPT;
|
2014-10-16 02:12:53 +00:00
|
|
|
csp->ShouldLoad(policyType, // load type per nsIContentPolicy (uint32_t)
|
|
|
|
newUri, // nsIURI
|
|
|
|
nullptr, // nsIURI
|
|
|
|
nullptr, // nsISupports
|
|
|
|
EmptyCString(), // ACString - MIME guess
|
|
|
|
originalUri, // aMimeTypeGuess
|
2010-04-23 17:03:03 +00:00
|
|
|
&aDecision);
|
|
|
|
|
2015-05-11 21:22:04 +00:00
|
|
|
if (newUri && PR_LOG_TEST(gCspPRLog, PR_LOG_DEBUG)) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString newUriSpec("None");
|
2010-04-23 17:03:03 +00:00
|
|
|
newUri->GetSpec(newUriSpec);
|
|
|
|
PR_LOG(gCspPRLog, PR_LOG_DEBUG,
|
2010-08-05 02:15:55 +00:00
|
|
|
("CSPService::AsyncOnChannelRedirect called for %s",
|
|
|
|
newUriSpec.get()));
|
2010-04-23 17:03:03 +00:00
|
|
|
}
|
2014-12-18 01:04:16 +00:00
|
|
|
if (aDecision == 1) {
|
2010-04-23 17:03:03 +00:00
|
|
|
PR_LOG(gCspPRLog, PR_LOG_DEBUG,
|
2010-08-05 02:15:55 +00:00
|
|
|
("CSPService::AsyncOnChannelRedirect ALLOWING request."));
|
2014-12-17 22:19:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-04-23 17:03:03 +00:00
|
|
|
PR_LOG(gCspPRLog, PR_LOG_DEBUG,
|
2010-08-05 02:15:55 +00:00
|
|
|
("CSPService::AsyncOnChannelRedirect CANCELLING request."));
|
2014-12-17 22:19:25 +00:00
|
|
|
}
|
2010-04-23 17:03:03 +00:00
|
|
|
|
|
|
|
// if ShouldLoad doesn't accept the load, cancel the request
|
2014-10-16 02:12:53 +00:00
|
|
|
if (!NS_CP_ACCEPTED(aDecision)) {
|
2010-08-05 02:15:55 +00:00
|
|
|
autoCallback.DontCallback();
|
2010-05-12 10:12:04 +00:00
|
|
|
return NS_BINDING_FAILED;
|
2010-08-05 02:15:55 +00:00
|
|
|
}
|
2014-10-16 02:12:53 +00:00
|
|
|
return NS_OK;
|
2010-04-23 17:03:03 +00:00
|
|
|
}
|