2015-05-03 22:32:37 +03: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 15:12:37 +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/. */
|
2010-01-23 00:38:21 +03:00
|
|
|
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2010-01-23 00:38:21 +03:00
|
|
|
#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 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2010-08-05 06:15:55 +04:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
|
|
|
#include "nsAsyncRedirectVerifyHelper.h"
|
2011-05-29 03:42:57 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2011-12-05 21:42:08 +04:00
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsContentUtils.h"
|
2014-10-16 06:12:53 +04:00
|
|
|
#include "nsContentPolicyUtils.h"
|
2014-08-29 04:20:27 +04:00
|
|
|
#include "nsPrincipal.h"
|
2011-05-29 03:42:57 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2010-01-23 00:38:21 +03:00
|
|
|
|
|
|
|
/* Keeps track of whether or not CSP is enabled */
|
2011-09-29 10:19:26 +04:00
|
|
|
bool CSPService::sCSPEnabled = true;
|
2010-01-23 00:38:21 +03:00
|
|
|
|
2015-11-23 22:09:25 +03:00
|
|
|
static LazyLogModule gCspPRLog("CSP");
|
2010-01-23 00:38:21 +03:00
|
|
|
|
|
|
|
CSPService::CSPService()
|
|
|
|
{
|
2011-05-29 03:42:57 +04:00
|
|
|
Preferences::AddBoolVarCache(&sCSPEnabled, "security.csp.enable");
|
2010-01-23 00:38:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CSPService::~CSPService()
|
|
|
|
{
|
2013-10-17 21:56:12 +04:00
|
|
|
mAppStatusCache.Clear();
|
2010-01-23 00:38:21 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(CSPService, nsIContentPolicy, nsIChannelEventSink)
|
2010-01-23 00:38:21 +03:00
|
|
|
|
2016-03-25 06:09:00 +03:00
|
|
|
// Helper function to identify protocols and content types not subject to CSP.
|
2014-11-21 01:59:53 +03:00
|
|
|
bool
|
2016-03-25 06:09:00 +03:00
|
|
|
subjectToCSP(nsIURI* aURI, nsContentPolicyType aContentType) {
|
|
|
|
// These content types are not subject to CSP content policy checks:
|
|
|
|
// TYPE_CSP_REPORT -- csp can't block csp reports
|
|
|
|
// TYPE_REFRESH -- never passed to ShouldLoad (see nsIContentPolicy.idl)
|
|
|
|
// TYPE_DOCUMENT -- used for frame-ancestors
|
|
|
|
if (aContentType == nsIContentPolicy::TYPE_CSP_REPORT ||
|
|
|
|
aContentType == nsIContentPolicy::TYPE_REFRESH ||
|
|
|
|
aContentType == nsIContentPolicy::TYPE_DOCUMENT) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-21 01:59:53 +03:00
|
|
|
// 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-23 00:38:21 +03:00
|
|
|
/* nsIContentPolicy implementation */
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
CSPService::ShouldLoad(uint32_t aContentType,
|
2010-01-23 00:38:21 +03:00
|
|
|
nsIURI *aContentLocation,
|
|
|
|
nsIURI *aRequestOrigin,
|
|
|
|
nsISupports *aRequestContext,
|
|
|
|
const nsACString &aMimeTypeGuess,
|
|
|
|
nsISupports *aExtra,
|
2012-07-03 03:16:11 +04:00
|
|
|
nsIPrincipal *aRequestPrincipal,
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t *aDecision)
|
2010-01-23 00:38:21 +03:00
|
|
|
{
|
2014-11-21 01:59:53 +03:00
|
|
|
if (!aContentLocation) {
|
2013-09-12 20:25:32 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-11-21 01:59:53 +03:00
|
|
|
}
|
2010-01-23 00:38:21 +03:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
if (MOZ_LOG_TEST(gCspPRLog, LogLevel::Debug)) {
|
2013-09-12 20:25:32 +04:00
|
|
|
nsAutoCString location;
|
|
|
|
aContentLocation->GetSpec(location);
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gCspPRLog, LogLevel::Debug,
|
2013-09-12 20:25:32 +04:00
|
|
|
("CSPService::ShouldLoad called for %s", location.get()));
|
|
|
|
}
|
2012-12-15 02:53:29 +04:00
|
|
|
|
2013-09-12 20:25:32 +04:00
|
|
|
// default decision, CSP can revise it if there's a policy to enforce
|
|
|
|
*aDecision = nsIContentPolicy::ACCEPT;
|
|
|
|
|
2014-11-21 01:59:53 +03:00
|
|
|
// No need to continue processing if CSP is disabled or if the protocol
|
2016-03-25 06:09:00 +03:00
|
|
|
// or type is *not* subject to CSP.
|
2014-11-21 01:59:53 +03:00
|
|
|
// 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()
|
2016-03-25 06:09:00 +03:00
|
|
|
if (!sCSPEnabled || !subjectToCSP(aContentLocation, aContentType)) {
|
2013-09-12 20:25:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-19 05:59:18 +03:00
|
|
|
// query the principal of the document; if no document is passed, then
|
|
|
|
// fall back to using the requestPrincipal (e.g. service workers do not
|
|
|
|
// pass a document).
|
2013-09-12 20:25:32 +04:00
|
|
|
nsCOMPtr<nsINode> node(do_QueryInterface(aRequestContext));
|
2015-10-19 05:59:18 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> principal = node ? node->NodePrincipal()
|
|
|
|
: aRequestPrincipal;
|
2015-11-15 06:29:18 +03:00
|
|
|
if (!principal) {
|
|
|
|
// if we can't query a principal, then there is nothing to do.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// 1) Apply speculate CSP for preloads
|
|
|
|
bool isPreload = nsContentUtils::IsPreloadType(aContentType);
|
|
|
|
|
|
|
|
if (isPreload) {
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> preloadCsp;
|
|
|
|
rv = principal->GetPreloadCsp(getter_AddRefs(preloadCsp));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (preloadCsp) {
|
2013-09-12 20:25:32 +04:00
|
|
|
// obtain the enforcement decision
|
|
|
|
// (don't pass aExtra, we use that slot for redirects)
|
2015-11-15 06:29:18 +03:00
|
|
|
rv = preloadCsp->ShouldLoad(aContentType,
|
|
|
|
aContentLocation,
|
|
|
|
aRequestOrigin,
|
|
|
|
aRequestContext,
|
|
|
|
aMimeTypeGuess,
|
|
|
|
nullptr, // aExtra
|
|
|
|
aDecision);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// if the preload policy already denied the load, then there
|
|
|
|
// is no point in checking the real policy
|
|
|
|
if (NS_CP_REJECTED(*aDecision)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-01-23 00:38:21 +03:00
|
|
|
}
|
2013-09-12 20:25:32 +04:00
|
|
|
}
|
2010-04-23 21:03:03 +04:00
|
|
|
|
2015-11-15 06:29:18 +03:00
|
|
|
// 2) Apply actual CSP to all loads
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
rv = principal->GetCsp(getter_AddRefs(csp));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (csp) {
|
|
|
|
// obtain the enforcement decision
|
|
|
|
// (don't pass aExtra, we use that slot for redirects)
|
|
|
|
rv = csp->ShouldLoad(aContentType,
|
|
|
|
aContentLocation,
|
|
|
|
aRequestOrigin,
|
|
|
|
aRequestContext,
|
|
|
|
aMimeTypeGuess,
|
|
|
|
nullptr,
|
|
|
|
aDecision);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2013-09-12 20:25:32 +04:00
|
|
|
return NS_OK;
|
2010-01-23 00:38:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
CSPService::ShouldProcess(uint32_t aContentType,
|
2010-01-23 00:38:21 +03:00
|
|
|
nsIURI *aContentLocation,
|
|
|
|
nsIURI *aRequestOrigin,
|
|
|
|
nsISupports *aRequestContext,
|
|
|
|
const nsACString &aMimeTypeGuess,
|
|
|
|
nsISupports *aExtra,
|
2012-07-03 03:16:11 +04:00
|
|
|
nsIPrincipal *aRequestPrincipal,
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t *aDecision)
|
2010-01-23 00:38:21 +03:00
|
|
|
{
|
2014-08-05 22:47:08 +04:00
|
|
|
if (!aContentLocation) {
|
2013-09-12 20:25:32 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-08-05 22:47:08 +04:00
|
|
|
}
|
2013-09-12 20:25:32 +04:00
|
|
|
|
2014-08-05 22:47:08 +04:00
|
|
|
if (MOZ_LOG_TEST(gCspPRLog, LogLevel::Debug)) {
|
|
|
|
nsAutoCString location;
|
|
|
|
aContentLocation->GetSpec(location);
|
|
|
|
MOZ_LOG(gCspPRLog, LogLevel::Debug,
|
|
|
|
("CSPService::ShouldProcess called for %s", location.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShouldProcess is only relevant to TYPE_OBJECT, so let's convert the
|
|
|
|
// internal contentPolicyType to the mapping external one.
|
|
|
|
// If it is not TYPE_OBJECT, we can return at this point.
|
|
|
|
// Note that we should still pass the internal contentPolicyType
|
|
|
|
// (aContentType) to ShouldLoad().
|
|
|
|
uint32_t policyType =
|
|
|
|
nsContentUtils::InternalContentPolicyTypeToExternal(aContentType);
|
|
|
|
|
|
|
|
if (policyType != nsIContentPolicy::TYPE_OBJECT) {
|
|
|
|
*aDecision = nsIContentPolicy::ACCEPT;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ShouldLoad(aContentType,
|
|
|
|
aContentLocation,
|
|
|
|
aRequestOrigin,
|
|
|
|
aRequestContext,
|
|
|
|
aMimeTypeGuess,
|
|
|
|
aExtra,
|
|
|
|
aRequestPrincipal,
|
|
|
|
aDecision);
|
2010-01-23 00:38:21 +03:00
|
|
|
}
|
2010-04-23 21:03:03 +04:00
|
|
|
|
|
|
|
/* nsIChannelEventSink implementation */
|
|
|
|
NS_IMETHODIMP
|
2010-08-05 06:15:55 +04:00
|
|
|
CSPService::AsyncOnChannelRedirect(nsIChannel *oldChannel,
|
|
|
|
nsIChannel *newChannel,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flags,
|
2010-08-05 06:15:55 +04:00
|
|
|
nsIAsyncVerifyRedirectCallback *callback)
|
2010-04-23 21:03:03 +04:00
|
|
|
{
|
2010-08-05 06:15:55 +04:00
|
|
|
nsAsyncRedirectAutoCallback autoCallback(callback);
|
|
|
|
|
2014-12-18 01:19:25 +03:00
|
|
|
nsCOMPtr<nsIURI> newUri;
|
|
|
|
nsresult rv = newChannel->GetURI(getter_AddRefs(newUri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-03-25 06:09:00 +03:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = oldChannel->GetLoadInfo();
|
|
|
|
|
|
|
|
// if no loadInfo on the channel, nothing for us to do
|
|
|
|
if (!loadInfo) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-12-18 01:19:25 +03: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()
|
2016-03-25 06:09:00 +03:00
|
|
|
nsContentPolicyType policyType = loadInfo->InternalContentPolicyType();
|
|
|
|
if (!sCSPEnabled || !subjectToCSP(newUri, policyType)) {
|
2010-04-23 21:03:03 +04:00
|
|
|
return NS_OK;
|
2014-10-16 06:12:53 +04:00
|
|
|
}
|
|
|
|
|
2010-04-23 21:03:03 +04:00
|
|
|
/* Since redirecting channels don't call into nsIContentPolicy, we call our
|
2014-10-16 06:12:53 +04: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 21:03:03 +04:00
|
|
|
*/
|
2012-07-10 11:44:05 +04:00
|
|
|
nsCOMPtr<nsIURI> originalUri;
|
2014-10-16 06:12:53 +04:00
|
|
|
rv = oldChannel->GetOriginalURI(getter_AddRefs(originalUri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-11-15 06:29:18 +03:00
|
|
|
|
|
|
|
bool isPreload = nsContentUtils::IsPreloadType(policyType);
|
|
|
|
|
2015-10-29 02:32:27 +03:00
|
|
|
/* On redirect, if the content policy is a preload type, rejecting the preload
|
|
|
|
* results in the load silently failing, so we convert preloads to the actual
|
|
|
|
* type. See Bug 1219453.
|
|
|
|
*/
|
2015-11-15 06:29:18 +03:00
|
|
|
policyType =
|
|
|
|
nsContentUtils::InternalContentPolicyTypeToExternalOrWorker(policyType);
|
2014-10-16 06:12:53 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t aDecision = nsIContentPolicy::ACCEPT;
|
2015-11-15 06:29:18 +03:00
|
|
|
// 1) Apply speculative CSP for preloads
|
|
|
|
if (isPreload) {
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> preloadCsp;
|
|
|
|
loadInfo->LoadingPrincipal()->GetPreloadCsp(getter_AddRefs(preloadCsp));
|
|
|
|
|
|
|
|
if (preloadCsp) {
|
|
|
|
// Pass originalURI as aExtra to indicate the redirect
|
|
|
|
preloadCsp->ShouldLoad(policyType, // load type per nsIContentPolicy (uint32_t)
|
|
|
|
newUri, // nsIURI
|
|
|
|
nullptr, // nsIURI
|
|
|
|
nullptr, // nsISupports
|
|
|
|
EmptyCString(), // ACString - MIME guess
|
|
|
|
originalUri, // aExtra
|
|
|
|
&aDecision);
|
|
|
|
|
|
|
|
// if the preload policy already denied the load, then there
|
|
|
|
// is no point in checking the real policy
|
|
|
|
if (NS_CP_REJECTED(aDecision)) {
|
|
|
|
autoCallback.DontCallback();
|
|
|
|
return NS_BINDING_FAILED;
|
|
|
|
}
|
|
|
|
}
|
2014-12-18 01:19:25 +03:00
|
|
|
}
|
2015-11-15 06:29:18 +03:00
|
|
|
|
|
|
|
// 2) Apply actual CSP to all loads
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
loadInfo->LoadingPrincipal()->GetCsp(getter_AddRefs(csp));
|
|
|
|
|
|
|
|
if (csp) {
|
|
|
|
// Pass originalURI as aExtra to indicate the redirect
|
|
|
|
csp->ShouldLoad(policyType, // load type per nsIContentPolicy (uint32_t)
|
|
|
|
newUri, // nsIURI
|
|
|
|
nullptr, // nsIURI
|
|
|
|
nullptr, // nsISupports
|
|
|
|
EmptyCString(), // ACString - MIME guess
|
|
|
|
originalUri, // aExtra
|
|
|
|
&aDecision);
|
2014-12-18 01:19:25 +03:00
|
|
|
}
|
2010-04-23 21:03:03 +04:00
|
|
|
|
|
|
|
// if ShouldLoad doesn't accept the load, cancel the request
|
2014-10-16 06:12:53 +04:00
|
|
|
if (!NS_CP_ACCEPTED(aDecision)) {
|
2010-08-05 06:15:55 +04:00
|
|
|
autoCallback.DontCallback();
|
2010-05-12 14:12:04 +04:00
|
|
|
return NS_BINDING_FAILED;
|
2010-08-05 06:15:55 +04:00
|
|
|
}
|
2014-10-16 06:12:53 +04:00
|
|
|
return NS_OK;
|
2010-04-23 21:03:03 +04:00
|
|
|
}
|