2014-05-14 18:03:43 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "nsCSPUtils.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsICryptoHash.h"
|
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
static PRLogModuleInfo*
|
|
|
|
GetCspUtilsLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo* gCspUtilsPRLog;
|
|
|
|
if (!gCspUtilsPRLog)
|
|
|
|
gCspUtilsPRLog = PR_NewLogModule("CSPUtils");
|
|
|
|
return gCspUtilsPRLog;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define CSPUTILSLOG(args) PR_LOG(GetCspUtilsLog(), 4, args)
|
|
|
|
|
|
|
|
void
|
|
|
|
CSP_GetLocalizedStr(const char16_t* aName,
|
|
|
|
const char16_t** aParams,
|
|
|
|
uint32_t aLength,
|
|
|
|
char16_t** outResult)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStringBundle> keyStringBundle;
|
|
|
|
nsCOMPtr<nsIStringBundleService> stringBundleService =
|
|
|
|
mozilla::services::GetStringBundleService();
|
|
|
|
|
|
|
|
NS_ASSERTION(stringBundleService, "String bundle service must be present!");
|
|
|
|
stringBundleService->CreateBundle("chrome://global/locale/security/csp.properties",
|
|
|
|
getter_AddRefs(keyStringBundle));
|
|
|
|
|
|
|
|
NS_ASSERTION(keyStringBundle, "Key string bundle must be available!");
|
|
|
|
|
|
|
|
if (!keyStringBundle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
keyStringBundle->FormatStringFromName(aName, aParams, aLength, outResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CSP_LogStrMessage(const nsAString& aMsg)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIConsoleService> console(do_GetService("@mozilla.org/consoleservice;1"));
|
|
|
|
|
|
|
|
if (!console) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsString msg = PromiseFlatString(aMsg);
|
|
|
|
console->LogStringMessage(msg.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CSP_LogMessage(const nsAString& aMessage,
|
|
|
|
const nsAString& aSourceName,
|
|
|
|
const nsAString& aSourceLine,
|
|
|
|
uint32_t aLineNumber,
|
|
|
|
uint32_t aColumnNumber,
|
|
|
|
uint32_t aFlags,
|
|
|
|
const char *aCategory,
|
|
|
|
uint32_t aInnerWindowID)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIConsoleService> console(do_GetService(NS_CONSOLESERVICE_CONTRACTID));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptError> error(do_CreateInstance(NS_SCRIPTERROR_CONTRACTID));
|
|
|
|
|
|
|
|
if (!console || !error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepending CSP to the outgoing console message
|
|
|
|
nsString cspMsg;
|
|
|
|
cspMsg.Append(NS_LITERAL_STRING("Content Security Policy: "));
|
|
|
|
cspMsg.Append(aMessage);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
if (aInnerWindowID > 0) {
|
|
|
|
nsCString catStr;
|
|
|
|
catStr.AssignASCII(aCategory);
|
|
|
|
rv = error->InitWithWindowID(cspMsg, aSourceName,
|
|
|
|
aSourceLine, aLineNumber,
|
|
|
|
aColumnNumber, aFlags,
|
|
|
|
catStr, aInnerWindowID);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = error->Init(cspMsg, aSourceName,
|
|
|
|
aSourceLine, aLineNumber,
|
|
|
|
aColumnNumber, aFlags,
|
|
|
|
aCategory);
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console->LogMessage(error);
|
|
|
|
}
|
|
|
|
|
2014-05-20 14:20:00 +00:00
|
|
|
/**
|
|
|
|
* Combines CSP_LogMessage and CSP_GetLocalizedStr into one call.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
CSP_LogLocalizedStr(const char16_t* aName,
|
|
|
|
const char16_t** aParams,
|
|
|
|
uint32_t aLength,
|
|
|
|
const nsAString& aSourceName,
|
|
|
|
const nsAString& aSourceLine,
|
|
|
|
uint32_t aLineNumber,
|
|
|
|
uint32_t aColumnNumber,
|
|
|
|
uint32_t aFlags,
|
|
|
|
const char* aCategory,
|
|
|
|
uint32_t aInnerWindowID)
|
|
|
|
{
|
|
|
|
nsXPIDLString logMsg;
|
|
|
|
CSP_GetLocalizedStr(aName, aParams, aLength, getter_Copies(logMsg));
|
|
|
|
CSP_LogMessage(logMsg, aSourceName, aSourceLine,
|
|
|
|
aLineNumber, aColumnNumber, aFlags,
|
|
|
|
aCategory, aInnerWindowID);
|
|
|
|
}
|
|
|
|
|
2014-05-14 18:03:43 +00:00
|
|
|
/* ===== Helpers ============================ */
|
2014-12-10 12:54:00 +00:00
|
|
|
CSPDirective
|
|
|
|
CSP_ContentTypeToDirective(nsContentPolicyType aType)
|
|
|
|
{
|
|
|
|
switch (aType) {
|
|
|
|
case nsIContentPolicy::TYPE_IMAGE:
|
|
|
|
case nsIContentPolicy::TYPE_IMAGESET:
|
|
|
|
return nsIContentSecurityPolicy::IMG_SRC_DIRECTIVE;
|
|
|
|
|
|
|
|
// BLock XSLT as script, see bug 910139
|
|
|
|
case nsIContentPolicy::TYPE_XSLT:
|
|
|
|
case nsIContentPolicy::TYPE_SCRIPT:
|
|
|
|
return nsIContentSecurityPolicy::SCRIPT_SRC_DIRECTIVE;
|
|
|
|
|
|
|
|
case nsIContentPolicy::TYPE_STYLESHEET:
|
|
|
|
return nsIContentSecurityPolicy::STYLE_SRC_DIRECTIVE;
|
|
|
|
|
|
|
|
case nsIContentPolicy::TYPE_FONT:
|
|
|
|
return nsIContentSecurityPolicy::FONT_SRC_DIRECTIVE;
|
|
|
|
|
|
|
|
case nsIContentPolicy::TYPE_MEDIA:
|
|
|
|
return nsIContentSecurityPolicy::MEDIA_SRC_DIRECTIVE;
|
|
|
|
|
|
|
|
case nsIContentPolicy::TYPE_SUBDOCUMENT:
|
|
|
|
return nsIContentSecurityPolicy::FRAME_SRC_DIRECTIVE;
|
|
|
|
|
|
|
|
case nsIContentPolicy::TYPE_WEBSOCKET:
|
|
|
|
case nsIContentPolicy::TYPE_XMLHTTPREQUEST:
|
|
|
|
case nsIContentPolicy::TYPE_BEACON:
|
|
|
|
case nsIContentPolicy::TYPE_FETCH:
|
|
|
|
return nsIContentSecurityPolicy::CONNECT_SRC_DIRECTIVE;
|
|
|
|
|
|
|
|
case nsIContentPolicy::TYPE_OBJECT:
|
|
|
|
case nsIContentPolicy::TYPE_OBJECT_SUBREQUEST:
|
|
|
|
return nsIContentSecurityPolicy::OBJECT_SRC_DIRECTIVE;
|
|
|
|
|
|
|
|
case nsIContentPolicy::TYPE_XBL:
|
|
|
|
case nsIContentPolicy::TYPE_PING:
|
|
|
|
case nsIContentPolicy::TYPE_DTD:
|
|
|
|
case nsIContentPolicy::TYPE_OTHER:
|
|
|
|
return nsIContentSecurityPolicy::DEFAULT_SRC_DIRECTIVE;
|
|
|
|
|
2014-12-17 22:19:25 +00:00
|
|
|
// csp shold not block top level loads, e.g. in case
|
|
|
|
// of a redirect.
|
|
|
|
case nsIContentPolicy::TYPE_DOCUMENT:
|
|
|
|
// CSP can not block csp reports
|
2014-12-10 12:54:00 +00:00
|
|
|
case nsIContentPolicy::TYPE_CSP_REPORT:
|
2014-12-17 22:19:25 +00:00
|
|
|
return nsIContentSecurityPolicy::NO_DIRECTIVE;
|
|
|
|
|
2014-12-10 12:54:00 +00:00
|
|
|
// Fall through to error for all other directives
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Can not map nsContentPolicyType to CSPDirective");
|
|
|
|
}
|
|
|
|
return nsIContentSecurityPolicy::DEFAULT_SRC_DIRECTIVE;
|
|
|
|
}
|
2014-05-14 18:03:43 +00:00
|
|
|
|
|
|
|
nsCSPHostSrc*
|
|
|
|
CSP_CreateHostSrcFromURI(nsIURI* aURI)
|
|
|
|
{
|
|
|
|
// Create the host first
|
|
|
|
nsCString host;
|
|
|
|
aURI->GetHost(host);
|
|
|
|
nsCSPHostSrc *hostsrc = new nsCSPHostSrc(NS_ConvertUTF8toUTF16(host));
|
|
|
|
|
|
|
|
// Add the scheme.
|
|
|
|
nsCString scheme;
|
|
|
|
aURI->GetScheme(scheme);
|
|
|
|
hostsrc->setScheme(NS_ConvertUTF8toUTF16(scheme));
|
|
|
|
|
|
|
|
int32_t port;
|
|
|
|
aURI->GetPort(&port);
|
|
|
|
// Only add port if it's not default port.
|
|
|
|
if (port > 0) {
|
|
|
|
nsAutoString portStr;
|
|
|
|
portStr.AppendInt(port);
|
|
|
|
hostsrc->setPort(portStr);
|
|
|
|
}
|
|
|
|
return hostsrc;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CSP_IsValidDirective(const nsAString& aDir)
|
|
|
|
{
|
2014-12-10 12:54:00 +00:00
|
|
|
uint32_t numDirs = (sizeof(CSPStrDirectives) / sizeof(CSPStrDirectives[0]));
|
2014-05-14 18:03:43 +00:00
|
|
|
|
2014-12-10 12:54:00 +00:00
|
|
|
for (uint32_t i = 0; i < numDirs; i++) {
|
2014-05-14 18:03:43 +00:00
|
|
|
if (aDir.LowerCaseEqualsASCII(CSPStrDirectives[i])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool
|
2014-12-10 12:54:00 +00:00
|
|
|
CSP_IsDirective(const nsAString& aValue, CSPDirective aDir)
|
2014-05-14 18:03:43 +00:00
|
|
|
{
|
2014-12-10 12:54:00 +00:00
|
|
|
return aValue.LowerCaseEqualsASCII(CSP_CSPDirectiveToString(aDir));
|
2014-05-14 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CSP_IsKeyword(const nsAString& aValue, enum CSPKeyword aKey)
|
|
|
|
{
|
|
|
|
return aValue.LowerCaseEqualsASCII(CSP_EnumToKeyword(aKey));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CSP_IsQuotelessKeyword(const nsAString& aKey)
|
|
|
|
{
|
|
|
|
nsString lowerKey = PromiseFlatString(aKey);
|
|
|
|
ToLowerCase(lowerKey);
|
|
|
|
|
|
|
|
static_assert(CSP_LAST_KEYWORD_VALUE ==
|
|
|
|
(sizeof(CSPStrKeywords) / sizeof(CSPStrKeywords[0])),
|
|
|
|
"CSP_LAST_KEYWORD_VALUE does not match length of CSPStrKeywords");
|
|
|
|
|
|
|
|
nsAutoString keyword;
|
|
|
|
for (uint32_t i = 0; i < CSP_LAST_KEYWORD_VALUE; i++) {
|
|
|
|
// skipping the leading ' and trimming the trailing '
|
|
|
|
keyword.AssignASCII(CSPStrKeywords[i] + 1);
|
|
|
|
keyword.Trim("'", false, true);
|
|
|
|
if (lowerKey.Equals(keyword)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ===== nsCSPSrc ============================ */
|
|
|
|
|
|
|
|
nsCSPBaseSrc::nsCSPBaseSrc()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSPBaseSrc::~nsCSPBaseSrc()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// ::permits is only called for external load requests, therefore:
|
|
|
|
// nsCSPKeywordSrc and nsCSPHashSource fall back to this base class
|
|
|
|
// implementation which will never allow the load.
|
|
|
|
bool
|
2014-08-12 19:55:08 +00:00
|
|
|
nsCSPBaseSrc::permits(nsIURI* aUri, const nsAString& aNonce, bool aWasRedirected) const
|
2014-05-14 18:03:43 +00:00
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
{
|
|
|
|
nsAutoCString spec;
|
|
|
|
aUri->GetSpec(spec);
|
|
|
|
CSPUTILSLOG(("nsCSPBaseSrc::permits, aUri: %s", spec.get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ::allows is only called for inlined loads, therefore:
|
|
|
|
// nsCSPSchemeSrc, nsCSPHostSrc fall back
|
|
|
|
// to this base class implementation which will never allow the load.
|
|
|
|
bool
|
|
|
|
nsCSPBaseSrc::allows(enum CSPKeyword aKeyword, const nsAString& aHashOrNonce) const
|
|
|
|
{
|
|
|
|
CSPUTILSLOG(("nsCSPBaseSrc::allows, aKeyWord: %s, a HashOrNonce: %s",
|
|
|
|
aKeyword == CSP_HASH ? "hash" : CSP_EnumToKeyword(aKeyword),
|
|
|
|
NS_ConvertUTF16toUTF8(aHashOrNonce).get()));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ====== nsCSPSchemeSrc ===================== */
|
|
|
|
|
|
|
|
nsCSPSchemeSrc::nsCSPSchemeSrc(const nsAString& aScheme)
|
|
|
|
: mScheme(aScheme)
|
|
|
|
{
|
|
|
|
ToLowerCase(mScheme);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSPSchemeSrc::~nsCSPSchemeSrc()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-08-12 19:55:08 +00:00
|
|
|
nsCSPSchemeSrc::permits(nsIURI* aUri, const nsAString& aNonce, bool aWasRedirected) const
|
2014-05-14 18:03:43 +00:00
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
{
|
|
|
|
nsAutoCString spec;
|
|
|
|
aUri->GetSpec(spec);
|
|
|
|
CSPUTILSLOG(("nsCSPSchemeSrc::permits, aUri: %s", spec.get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_ASSERTION((!mScheme.EqualsASCII("")), "scheme can not be the empty string");
|
|
|
|
nsAutoCString scheme;
|
|
|
|
nsresult rv = aUri->GetScheme(scheme);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
return mScheme.EqualsASCII(scheme.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSPSchemeSrc::toString(nsAString& outStr) const
|
|
|
|
{
|
|
|
|
outStr.Append(mScheme);
|
|
|
|
outStr.AppendASCII(":");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ===== nsCSPHostSrc ======================== */
|
|
|
|
|
|
|
|
nsCSPHostSrc::nsCSPHostSrc(const nsAString& aHost)
|
|
|
|
: mHost(aHost)
|
2014-11-06 01:45:54 +00:00
|
|
|
, mAllowHttps(false)
|
2014-05-14 18:03:43 +00:00
|
|
|
{
|
|
|
|
ToLowerCase(mHost);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSPHostSrc::~nsCSPHostSrc()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-08-12 19:55:08 +00:00
|
|
|
nsCSPHostSrc::permits(nsIURI* aUri, const nsAString& aNonce, bool aWasRedirected) const
|
2014-05-14 18:03:43 +00:00
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
{
|
|
|
|
nsAutoCString spec;
|
|
|
|
aUri->GetSpec(spec);
|
|
|
|
CSPUTILSLOG(("nsCSPHostSrc::permits, aUri: %s", spec.get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-01 11:59:05 +00:00
|
|
|
// we are following the enforcement rules from the spec, see:
|
|
|
|
// http://www.w3.org/TR/CSP11/#match-source-expression
|
2014-05-14 18:03:43 +00:00
|
|
|
|
2014-10-01 11:59:05 +00:00
|
|
|
// 4.3) scheme matching: Check if the scheme matches.
|
2014-05-14 18:03:43 +00:00
|
|
|
nsAutoCString scheme;
|
|
|
|
nsresult rv = aUri->GetScheme(scheme);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2014-10-01 11:59:05 +00:00
|
|
|
if (!mScheme.IsEmpty() &&
|
|
|
|
!mScheme.EqualsASCII(scheme.get())) {
|
2014-11-06 01:45:54 +00:00
|
|
|
|
|
|
|
// We should not return false for scheme-less sources where the protected resource
|
|
|
|
// is http and the load is https, see:
|
|
|
|
// http://www.w3.org/TR/CSP2/#match-source-expression
|
|
|
|
bool isHttpsScheme =
|
|
|
|
(NS_SUCCEEDED(aUri->SchemeIs("https", &isHttpsScheme)) && isHttpsScheme);
|
|
|
|
|
|
|
|
if (!(isHttpsScheme && mAllowHttps)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-14 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The host in nsCSpHostSrc should never be empty. In case we are enforcing
|
|
|
|
// just a specific scheme, the parser should generate a nsCSPSchemeSource.
|
|
|
|
NS_ASSERTION((!mHost.IsEmpty()), "host can not be the empty string");
|
|
|
|
|
2014-10-01 11:59:05 +00:00
|
|
|
// 2) host matching: Enforce a single *
|
|
|
|
if (mHost.EqualsASCII("*")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before we can check if the host matches, we have to
|
|
|
|
// extract the host part from aUri.
|
2014-05-14 18:03:43 +00:00
|
|
|
nsAutoCString uriHost;
|
|
|
|
rv = aUri->GetHost(uriHost);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
2014-10-01 11:59:05 +00:00
|
|
|
// 4.5) host matching: Check if the allowed host starts with a wilcard.
|
2014-05-14 18:03:43 +00:00
|
|
|
if (mHost.First() == '*') {
|
|
|
|
NS_ASSERTION(mHost[1] == '.', "Second character needs to be '.' whenever host starts with '*'");
|
|
|
|
|
2014-06-30 17:53:17 +00:00
|
|
|
// Eliminate leading "*", but keeping the FULL STOP (.) thereafter before checking
|
2014-10-01 11:59:05 +00:00
|
|
|
// if the remaining characters match
|
2014-05-14 18:03:43 +00:00
|
|
|
nsString wildCardHost = mHost;
|
2014-06-30 17:53:17 +00:00
|
|
|
wildCardHost = Substring(wildCardHost, 1, wildCardHost.Length() - 1);
|
2014-05-14 18:03:43 +00:00
|
|
|
if (!StringEndsWith(NS_ConvertUTF8toUTF16(uriHost), wildCardHost)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-10-01 11:59:05 +00:00
|
|
|
// 4.6) host matching: Check if hosts match.
|
2014-05-14 18:03:43 +00:00
|
|
|
else if (!mHost.Equals(NS_ConvertUTF8toUTF16(uriHost))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-01 11:59:05 +00:00
|
|
|
// 4.9) Path matching: If there is a path, we have to enforce
|
|
|
|
// path-level matching, unless the channel got redirected, see:
|
2014-08-12 19:55:08 +00:00
|
|
|
// http://www.w3.org/TR/CSP11/#source-list-paths-and-redirects
|
|
|
|
if (!aWasRedirected && !mPath.IsEmpty()) {
|
|
|
|
// cloning uri so we can ignore the ref
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
aUri->CloneIgnoringRef(getter_AddRefs(uri));
|
|
|
|
|
|
|
|
nsAutoCString uriPath;
|
|
|
|
rv = uri->GetPath(uriPath);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
// check if the last character of mPath is '/'; if so
|
|
|
|
// we just have to check loading resource is within
|
|
|
|
// the allowed path.
|
|
|
|
if (mPath.Last() == '/') {
|
|
|
|
if (!StringBeginsWith(NS_ConvertUTF8toUTF16(uriPath), mPath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// otherwise mPath whitelists a specific file, and we have to
|
|
|
|
// check if the loading resource matches that whitelisted file.
|
|
|
|
else {
|
|
|
|
if (!mPath.Equals(NS_ConvertUTF8toUTF16(uriPath))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 11:59:05 +00:00
|
|
|
// 4.8) Port matching: If port uses wildcard, allow the load.
|
2014-05-14 18:03:43 +00:00
|
|
|
if (mPort.EqualsASCII("*")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-01 11:59:05 +00:00
|
|
|
// Before we can check if the port matches, we have to
|
|
|
|
// query the port from aUri.
|
2014-05-14 18:03:43 +00:00
|
|
|
int32_t uriPort;
|
|
|
|
rv = aUri->GetPort(&uriPort);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
uriPort = (uriPort > 0) ? uriPort : NS_GetDefaultPort(scheme.get());
|
|
|
|
|
2014-10-01 11:59:05 +00:00
|
|
|
// 4.7) Default port matching: If mPort is empty, we have to compare default ports.
|
2014-05-14 18:03:43 +00:00
|
|
|
if (mPort.IsEmpty()) {
|
|
|
|
int32_t port = NS_GetDefaultPort(NS_ConvertUTF16toUTF8(mScheme).get());
|
|
|
|
if (port != uriPort) {
|
2014-11-06 01:45:54 +00:00
|
|
|
// We should not return false for scheme-less sources where the protected resource
|
|
|
|
// is http and the load is https, see: http://www.w3.org/TR/CSP2/#match-source-expression
|
|
|
|
// BUT, we only allow scheme-less sources to be upgraded from http to https if CSP
|
|
|
|
// does not explicitly define a port.
|
|
|
|
if (!(uriPort == NS_GetDefaultPort("https") && mAllowHttps)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-14 18:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-01 11:59:05 +00:00
|
|
|
// 4.7) Port matching: Compare the ports.
|
2014-05-14 18:03:43 +00:00
|
|
|
else {
|
|
|
|
nsString portStr;
|
|
|
|
portStr.AppendInt(uriPort);
|
|
|
|
if (!mPort.Equals(portStr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-12 19:55:08 +00:00
|
|
|
// At the end: scheme, host, path, and port match -> allow the load.
|
2014-05-14 18:03:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSPHostSrc::toString(nsAString& outStr) const
|
|
|
|
{
|
|
|
|
// If mHost is a single "*", we append the wildcard and return.
|
|
|
|
if (mHost.EqualsASCII("*") &&
|
|
|
|
mScheme.IsEmpty() &&
|
|
|
|
mPort.IsEmpty()) {
|
|
|
|
outStr.Append(mHost);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// append scheme
|
|
|
|
outStr.Append(mScheme);
|
|
|
|
|
|
|
|
// append host
|
|
|
|
outStr.AppendASCII("://");
|
|
|
|
outStr.Append(mHost);
|
|
|
|
|
|
|
|
// append port
|
|
|
|
if (!mPort.IsEmpty()) {
|
|
|
|
outStr.AppendASCII(":");
|
|
|
|
outStr.Append(mPort);
|
|
|
|
}
|
|
|
|
|
2014-08-12 19:55:08 +00:00
|
|
|
// append path
|
|
|
|
outStr.Append(mPath);
|
2014-05-14 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-06 01:45:54 +00:00
|
|
|
nsCSPHostSrc::setScheme(const nsAString& aScheme, bool aAllowHttps)
|
2014-05-14 18:03:43 +00:00
|
|
|
{
|
|
|
|
mScheme = aScheme;
|
|
|
|
ToLowerCase(mScheme);
|
2014-11-06 01:45:54 +00:00
|
|
|
mAllowHttps = aAllowHttps;
|
2014-05-14 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSPHostSrc::setPort(const nsAString& aPort)
|
|
|
|
{
|
|
|
|
mPort = aPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSPHostSrc::appendPath(const nsAString& aPath)
|
|
|
|
{
|
|
|
|
mPath.Append(aPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ===== nsCSPKeywordSrc ===================== */
|
|
|
|
|
2014-12-10 12:54:00 +00:00
|
|
|
nsCSPKeywordSrc::nsCSPKeywordSrc(enum CSPKeyword aKeyword)
|
2014-05-14 18:03:43 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION((aKeyword != CSP_SELF),
|
|
|
|
"'self' should have been replaced in the parser");
|
|
|
|
mKeyword = aKeyword;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSPKeywordSrc::~nsCSPKeywordSrc()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsCSPKeywordSrc::allows(enum CSPKeyword aKeyword, const nsAString& aHashOrNonce) const
|
|
|
|
{
|
|
|
|
CSPUTILSLOG(("nsCSPKeywordSrc::allows, aKeyWord: %s, a HashOrNonce: %s",
|
|
|
|
CSP_EnumToKeyword(aKeyword), NS_ConvertUTF16toUTF8(aHashOrNonce).get()));
|
|
|
|
return mKeyword == aKeyword;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSPKeywordSrc::toString(nsAString& outStr) const
|
|
|
|
{
|
|
|
|
outStr.AppendASCII(CSP_EnumToKeyword(mKeyword));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ===== nsCSPNonceSrc ==================== */
|
|
|
|
|
|
|
|
nsCSPNonceSrc::nsCSPNonceSrc(const nsAString& aNonce)
|
|
|
|
: mNonce(aNonce)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSPNonceSrc::~nsCSPNonceSrc()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-08-12 19:55:08 +00:00
|
|
|
nsCSPNonceSrc::permits(nsIURI* aUri, const nsAString& aNonce, bool aWasRedirected) const
|
2014-05-14 18:03:43 +00:00
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
{
|
|
|
|
nsAutoCString spec;
|
|
|
|
aUri->GetSpec(spec);
|
|
|
|
CSPUTILSLOG(("nsCSPNonceSrc::permits, aUri: %s, aNonce: %s",
|
|
|
|
spec.get(), NS_ConvertUTF16toUTF8(aNonce).get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return mNonce.Equals(aNonce);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsCSPNonceSrc::allows(enum CSPKeyword aKeyword, const nsAString& aHashOrNonce) const
|
|
|
|
{
|
|
|
|
CSPUTILSLOG(("nsCSPNonceSrc::allows, aKeyWord: %s, a HashOrNonce: %s",
|
|
|
|
CSP_EnumToKeyword(aKeyword), NS_ConvertUTF16toUTF8(aHashOrNonce).get()));
|
|
|
|
|
|
|
|
if (aKeyword != CSP_NONCE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return mNonce.Equals(aHashOrNonce);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSPNonceSrc::toString(nsAString& outStr) const
|
|
|
|
{
|
|
|
|
outStr.AppendASCII(CSP_EnumToKeyword(CSP_NONCE));
|
|
|
|
outStr.Append(mNonce);
|
|
|
|
outStr.AppendASCII("'");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ===== nsCSPHashSrc ===================== */
|
|
|
|
|
|
|
|
nsCSPHashSrc::nsCSPHashSrc(const nsAString& aAlgo, const nsAString& aHash)
|
|
|
|
: mAlgorithm(aAlgo)
|
|
|
|
, mHash(aHash)
|
|
|
|
{
|
|
|
|
// Only the algo should be rewritten to lowercase, the hash must remain the same.
|
|
|
|
ToLowerCase(mAlgorithm);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSPHashSrc::~nsCSPHashSrc()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsCSPHashSrc::allows(enum CSPKeyword aKeyword, const nsAString& aHashOrNonce) const
|
|
|
|
{
|
|
|
|
CSPUTILSLOG(("nsCSPHashSrc::allows, aKeyWord: %s, a HashOrNonce: %s",
|
|
|
|
CSP_EnumToKeyword(aKeyword), NS_ConvertUTF16toUTF8(aHashOrNonce).get()));
|
|
|
|
|
|
|
|
if (aKeyword != CSP_HASH) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert aHashOrNonce to UTF-8
|
|
|
|
NS_ConvertUTF16toUTF8 utf8_hash(aHashOrNonce);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsICryptoHash> hasher;
|
|
|
|
hasher = do_CreateInstance("@mozilla.org/security/hash;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
rv = hasher->InitWithString(NS_ConvertUTF16toUTF8(mAlgorithm));
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
rv = hasher->Update((uint8_t *)utf8_hash.get(), utf8_hash.Length());
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
nsAutoCString hash;
|
|
|
|
rv = hasher->Finish(true, hash);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
// The NSS Base64 encoder automatically adds linebreaks "\r\n" every 64
|
|
|
|
// characters. We need to remove these so we can properly validate longer
|
|
|
|
// (SHA-512) base64-encoded hashes
|
|
|
|
hash.StripChars("\r\n");
|
|
|
|
return NS_ConvertUTF16toUTF8(mHash).Equals(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSPHashSrc::toString(nsAString& outStr) const
|
|
|
|
{
|
|
|
|
outStr.AppendASCII("'");
|
|
|
|
outStr.Append(mAlgorithm);
|
|
|
|
outStr.AppendASCII("-");
|
|
|
|
outStr.Append(mHash);
|
|
|
|
outStr.AppendASCII("'");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ===== nsCSPReportURI ===================== */
|
|
|
|
|
|
|
|
nsCSPReportURI::nsCSPReportURI(nsIURI *aURI)
|
|
|
|
:mReportURI(aURI)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSPReportURI::~nsCSPReportURI()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSPReportURI::toString(nsAString& outStr) const
|
|
|
|
{
|
|
|
|
nsAutoCString spec;
|
|
|
|
nsresult rv = mReportURI->GetSpec(spec);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
outStr.AppendASCII(spec.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ===== nsCSPDirective ====================== */
|
|
|
|
|
2014-12-10 12:54:00 +00:00
|
|
|
nsCSPDirective::nsCSPDirective(CSPDirective aDirective)
|
2014-05-14 18:03:43 +00:00
|
|
|
{
|
|
|
|
mDirective = aDirective;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSPDirective::~nsCSPDirective()
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mSrcs.Length(); i++) {
|
|
|
|
delete mSrcs[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-08-12 19:55:08 +00:00
|
|
|
nsCSPDirective::permits(nsIURI* aUri, const nsAString& aNonce, bool aWasRedirected) const
|
2014-05-14 18:03:43 +00:00
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
{
|
|
|
|
nsAutoCString spec;
|
|
|
|
aUri->GetSpec(spec);
|
|
|
|
CSPUTILSLOG(("nsCSPDirective::permits, aUri: %s", spec.get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mSrcs.Length(); i++) {
|
2014-08-12 19:55:08 +00:00
|
|
|
if (mSrcs[i]->permits(aUri, aNonce, aWasRedirected)) {
|
2014-05-14 18:03:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-15 23:26:59 +00:00
|
|
|
bool
|
|
|
|
nsCSPDirective::permits(nsIURI* aUri) const
|
|
|
|
{
|
|
|
|
nsString dummyNonce;
|
2014-08-12 19:55:08 +00:00
|
|
|
return permits(aUri, dummyNonce, false);
|
2014-08-15 23:26:59 +00:00
|
|
|
}
|
|
|
|
|
2014-05-14 18:03:43 +00:00
|
|
|
bool
|
|
|
|
nsCSPDirective::allows(enum CSPKeyword aKeyword, const nsAString& aHashOrNonce) const
|
|
|
|
{
|
|
|
|
CSPUTILSLOG(("nsCSPDirective::allows, aKeyWord: %s, a HashOrNonce: %s",
|
|
|
|
CSP_EnumToKeyword(aKeyword), NS_ConvertUTF16toUTF8(aHashOrNonce).get()));
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mSrcs.Length(); i++) {
|
|
|
|
if (mSrcs[i]->allows(aKeyword, aHashOrNonce)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSPDirective::toString(nsAString& outStr) const
|
|
|
|
{
|
|
|
|
// Append directive name
|
2014-12-10 12:54:00 +00:00
|
|
|
outStr.AppendASCII(CSP_CSPDirectiveToString(mDirective));
|
2014-05-14 18:03:43 +00:00
|
|
|
outStr.AppendASCII(" ");
|
|
|
|
|
|
|
|
// Append srcs
|
|
|
|
uint32_t length = mSrcs.Length();
|
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
|
|
|
mSrcs[i]->toString(outStr);
|
|
|
|
if (i != (length - 1)) {
|
|
|
|
outStr.AppendASCII(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-06-27 23:30:03 +00:00
|
|
|
nsCSPDirective::restrictsContentType(nsContentPolicyType aContentType) const
|
2014-05-14 18:03:43 +00:00
|
|
|
{
|
|
|
|
// make sure we do not check for the default src before any other sources
|
|
|
|
if (isDefaultDirective()) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-27 23:30:03 +00:00
|
|
|
return mDirective == CSP_ContentTypeToDirective(aContentType);
|
2014-05-14 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
2014-05-20 14:20:00 +00:00
|
|
|
void
|
|
|
|
nsCSPDirective::getReportURIs(nsTArray<nsString> &outReportURIs) const
|
|
|
|
{
|
2014-12-10 12:54:00 +00:00
|
|
|
NS_ASSERTION((mDirective == nsIContentSecurityPolicy::REPORT_URI_DIRECTIVE), "not a report-uri directive");
|
2014-05-20 14:20:00 +00:00
|
|
|
|
|
|
|
// append uris
|
|
|
|
nsString tmpReportURI;
|
|
|
|
for (uint32_t i = 0; i < mSrcs.Length(); i++) {
|
|
|
|
tmpReportURI.Truncate();
|
|
|
|
mSrcs[i]->toString(tmpReportURI);
|
|
|
|
outReportURIs.AppendElement(tmpReportURI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 18:03:43 +00:00
|
|
|
/* ===== nsCSPPolicy ========================= */
|
|
|
|
|
|
|
|
nsCSPPolicy::nsCSPPolicy()
|
|
|
|
: mReportOnly(false)
|
|
|
|
{
|
|
|
|
CSPUTILSLOG(("nsCSPPolicy::nsCSPPolicy"));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSPPolicy::~nsCSPPolicy()
|
|
|
|
{
|
|
|
|
CSPUTILSLOG(("nsCSPPolicy::~nsCSPPolicy"));
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mDirectives.Length(); i++) {
|
|
|
|
delete mDirectives[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-12-10 12:54:00 +00:00
|
|
|
nsCSPPolicy::permits(CSPDirective aDir,
|
|
|
|
nsIURI* aUri,
|
|
|
|
bool aSpecific) const
|
|
|
|
{
|
|
|
|
nsString outp;
|
|
|
|
return this->permits(aDir, aUri, EmptyString(), false, aSpecific, outp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsCSPPolicy::permits(CSPDirective aDir,
|
2014-05-14 18:03:43 +00:00
|
|
|
nsIURI* aUri,
|
|
|
|
const nsAString& aNonce,
|
2014-08-12 19:55:08 +00:00
|
|
|
bool aWasRedirected,
|
2014-12-10 12:54:00 +00:00
|
|
|
bool aSpecific,
|
2014-05-14 18:03:43 +00:00
|
|
|
nsAString& outViolatedDirective) const
|
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
{
|
|
|
|
nsAutoCString spec;
|
|
|
|
aUri->GetSpec(spec);
|
2014-12-10 12:54:00 +00:00
|
|
|
CSPUTILSLOG(("nsCSPPolicy::permits, aUri: %s, aDir: %d, aSpecific: %s",
|
|
|
|
spec.get(), aDir, aSpecific ? "true" : "false"));
|
2014-05-14 18:03:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_ASSERTION(aUri, "permits needs an uri to perform the check!");
|
|
|
|
|
|
|
|
nsCSPDirective* defaultDir = nullptr;
|
|
|
|
|
2014-12-10 12:54:00 +00:00
|
|
|
// Try to find a relevant directive
|
2014-05-14 18:03:43 +00:00
|
|
|
// These directive arrays are short (1-5 elements), not worth using a hashtable.
|
|
|
|
for (uint32_t i = 0; i < mDirectives.Length(); i++) {
|
2014-12-10 12:54:00 +00:00
|
|
|
if (mDirectives[i]->equals(aDir)) {
|
2014-08-12 19:55:08 +00:00
|
|
|
if (!mDirectives[i]->permits(aUri, aNonce, aWasRedirected)) {
|
2014-05-14 18:03:43 +00:00
|
|
|
mDirectives[i]->toString(outViolatedDirective);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (mDirectives[i]->isDefaultDirective()) {
|
|
|
|
defaultDir = mDirectives[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the above loop runs through, we haven't found a matching directive.
|
|
|
|
// Avoid relooping, just store the result of default-src while looping.
|
2014-12-10 12:54:00 +00:00
|
|
|
if (!aSpecific && defaultDir) {
|
2014-08-12 19:55:08 +00:00
|
|
|
if (!defaultDir->permits(aUri, aNonce, aWasRedirected)) {
|
2014-05-14 18:03:43 +00:00
|
|
|
defaultDir->toString(outViolatedDirective);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-10 12:54:00 +00:00
|
|
|
// Nothing restricts this, so we're allowing the load
|
|
|
|
// See bug 764937
|
2014-11-18 00:12:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-14 18:03:43 +00:00
|
|
|
bool
|
|
|
|
nsCSPPolicy::allows(nsContentPolicyType aContentType,
|
|
|
|
enum CSPKeyword aKeyword,
|
|
|
|
const nsAString& aHashOrNonce) const
|
|
|
|
{
|
|
|
|
CSPUTILSLOG(("nsCSPPolicy::allows, aKeyWord: %s, a HashOrNonce: %s",
|
|
|
|
CSP_EnumToKeyword(aKeyword), NS_ConvertUTF16toUTF8(aHashOrNonce).get()));
|
|
|
|
|
|
|
|
nsCSPDirective* defaultDir = nullptr;
|
|
|
|
|
|
|
|
// Try to find a matching directive
|
|
|
|
for (uint32_t i = 0; i < mDirectives.Length(); i++) {
|
2014-06-27 23:30:03 +00:00
|
|
|
if (mDirectives[i]->restrictsContentType(aContentType)) {
|
2014-05-14 18:03:43 +00:00
|
|
|
if (mDirectives[i]->allows(aKeyword, aHashOrNonce)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (mDirectives[i]->isDefaultDirective()) {
|
|
|
|
defaultDir = mDirectives[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only match {nonce,hash}-source on specific directives (not default-src)
|
|
|
|
if (aKeyword == CSP_NONCE || aKeyword == CSP_HASH) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the above loop runs through, we haven't found a matching directive.
|
|
|
|
// Avoid relooping, just store the result of default-src while looping.
|
|
|
|
if (defaultDir) {
|
|
|
|
return defaultDir->allows(aKeyword, aHashOrNonce);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allowing the load; see Bug 885433
|
|
|
|
// a) inline scripts (also unsafe eval) should only be blocked
|
|
|
|
// if there is a [script-src] or [default-src]
|
|
|
|
// b) inline styles should only be blocked
|
|
|
|
// if there is a [style-src] or [default-src]
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsCSPPolicy::allows(nsContentPolicyType aContentType,
|
|
|
|
enum CSPKeyword aKeyword) const
|
|
|
|
{
|
|
|
|
return allows(aContentType, aKeyword, NS_LITERAL_STRING(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsCSPPolicy::toString(nsAString& outStr) const
|
|
|
|
{
|
|
|
|
uint32_t length = mDirectives.Length();
|
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
2014-12-17 19:14:04 +00:00
|
|
|
|
|
|
|
if (mDirectives[i]->equals(nsIContentSecurityPolicy::REFERRER_DIRECTIVE)) {
|
|
|
|
outStr.AppendASCII(CSP_CSPDirectiveToString(nsIContentSecurityPolicy::REFERRER_DIRECTIVE));
|
|
|
|
outStr.AppendASCII(" ");
|
|
|
|
outStr.Append(mReferrerPolicy);
|
|
|
|
} else {
|
|
|
|
mDirectives[i]->toString(outStr);
|
|
|
|
}
|
2014-05-14 18:03:43 +00:00
|
|
|
if (i != (length - 1)) {
|
|
|
|
outStr.AppendASCII("; ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-12-10 12:54:00 +00:00
|
|
|
nsCSPPolicy::hasDirective(CSPDirective aDir) const
|
2014-05-14 18:03:43 +00:00
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mDirectives.Length(); i++) {
|
|
|
|
if (mDirectives[i]->equals(aDir)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-20 14:20:00 +00:00
|
|
|
|
2014-10-17 23:33:02 +00:00
|
|
|
/*
|
|
|
|
* Use this function only after ::allows() returned 'false'. Most and
|
|
|
|
* foremost it's used to get the violated directive before sending reports.
|
|
|
|
* The parameter outDirective is the equivalent of 'outViolatedDirective'
|
|
|
|
* for the ::permits() function family.
|
|
|
|
*/
|
2014-05-20 14:20:00 +00:00
|
|
|
void
|
|
|
|
nsCSPPolicy::getDirectiveStringForContentType(nsContentPolicyType aContentType,
|
|
|
|
nsAString& outDirective) const
|
|
|
|
{
|
2014-10-17 23:33:02 +00:00
|
|
|
nsCSPDirective* defaultDir = nullptr;
|
2014-05-20 14:20:00 +00:00
|
|
|
for (uint32_t i = 0; i < mDirectives.Length(); i++) {
|
2014-06-27 23:30:03 +00:00
|
|
|
if (mDirectives[i]->restrictsContentType(aContentType)) {
|
2014-05-20 14:20:00 +00:00
|
|
|
mDirectives[i]->toString(outDirective);
|
|
|
|
return;
|
|
|
|
}
|
2014-10-17 23:33:02 +00:00
|
|
|
if (mDirectives[i]->isDefaultDirective()) {
|
|
|
|
defaultDir = mDirectives[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we haven't found a matching directive yet,
|
|
|
|
// the contentType must be restricted by the default directive
|
|
|
|
if (defaultDir) {
|
|
|
|
defaultDir->toString(outDirective);
|
|
|
|
return;
|
2014-05-20 14:20:00 +00:00
|
|
|
}
|
2014-10-17 23:33:02 +00:00
|
|
|
NS_ASSERTION(false, "Can not query directive string for contentType!");
|
|
|
|
outDirective.AppendASCII("couldNotQueryViolatedDirective");
|
2014-05-20 14:20:00 +00:00
|
|
|
}
|
|
|
|
|
2014-08-15 23:26:59 +00:00
|
|
|
void
|
2014-12-10 12:54:00 +00:00
|
|
|
nsCSPPolicy::getDirectiveAsString(CSPDirective aDir, nsAString& outDirective) const
|
2014-08-15 23:26:59 +00:00
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mDirectives.Length(); i++) {
|
2014-11-18 00:12:00 +00:00
|
|
|
if (mDirectives[i]->equals(aDir)) {
|
2014-08-15 23:26:59 +00:00
|
|
|
mDirectives[i]->toString(outDirective);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 14:20:00 +00:00
|
|
|
void
|
|
|
|
nsCSPPolicy::getReportURIs(nsTArray<nsString>& outReportURIs) const
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mDirectives.Length(); i++) {
|
2014-12-10 12:54:00 +00:00
|
|
|
if (mDirectives[i]->equals(nsIContentSecurityPolicy::REPORT_URI_DIRECTIVE)) {
|
2014-05-20 14:20:00 +00:00
|
|
|
mDirectives[i]->getReportURIs(outReportURIs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|