2015-07-20 02:12:11 +00:00
|
|
|
#include "nsContentSecurityManager.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsILoadInfo.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsCORSListenerProxy.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
|
2015-12-05 15:34:47 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsContentSecurityManager, nsIContentSecurityManager)
|
2015-09-18 16:27:15 +00:00
|
|
|
|
2015-11-04 08:05:16 +00:00
|
|
|
static nsresult
|
2015-07-20 02:12:11 +00:00
|
|
|
ValidateSecurityFlags(nsILoadInfo* aLoadInfo)
|
|
|
|
{
|
|
|
|
nsSecurityFlags securityMode = aLoadInfo->GetSecurityMode();
|
|
|
|
|
|
|
|
if (securityMode != nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_INHERITS &&
|
|
|
|
securityMode != nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED &&
|
|
|
|
securityMode != nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS &&
|
|
|
|
securityMode != nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL &&
|
|
|
|
securityMode != nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS) {
|
|
|
|
MOZ_ASSERT(false, "need one securityflag from nsILoadInfo to perform security checks");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-12-05 15:34:47 +00:00
|
|
|
// make sure that cors-with-credentials is only used in combination with CORS.
|
|
|
|
if (aLoadInfo->GetRequireCorsWithCredentials() &&
|
|
|
|
securityMode != nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS) {
|
|
|
|
MOZ_ASSERT(false, "can not use cors-with-credentials without cors");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2015-07-20 02:12:11 +00:00
|
|
|
// all good, found the right security flags
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-15 01:59:35 +00:00
|
|
|
static bool SchemeIs(nsIURI* aURI, const char* aScheme)
|
2015-07-20 02:12:11 +00:00
|
|
|
{
|
2015-09-15 01:59:35 +00:00
|
|
|
nsCOMPtr<nsIURI> baseURI = NS_GetInnermostURI(aURI);
|
|
|
|
NS_ENSURE_TRUE(baseURI, false);
|
2015-07-20 02:12:11 +00:00
|
|
|
|
2015-09-15 01:59:35 +00:00
|
|
|
bool isScheme = false;
|
|
|
|
return NS_SUCCEEDED(baseURI->SchemeIs(aScheme, &isScheme)) && isScheme;
|
2015-07-20 02:12:11 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 08:05:16 +00:00
|
|
|
static nsresult
|
2015-07-20 02:12:11 +00:00
|
|
|
DoCheckLoadURIChecks(nsIURI* aURI, nsILoadInfo* aLoadInfo)
|
|
|
|
{
|
2015-11-25 21:38:05 +00:00
|
|
|
// Bug 1228117: determine the correct security policy for DTD loads
|
|
|
|
if (aLoadInfo->GetExternalContentPolicyType() == nsIContentPolicy::TYPE_DTD) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-20 02:12:11 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> loadingPrincipal = aLoadInfo->LoadingPrincipal();
|
2015-09-15 01:59:35 +00:00
|
|
|
uint32_t flags = nsIScriptSecurityManager::STANDARD;
|
|
|
|
if (aLoadInfo->GetAllowChrome()) {
|
|
|
|
flags |= nsIScriptSecurityManager::ALLOW_CHROME;
|
|
|
|
}
|
|
|
|
|
2015-07-20 02:12:11 +00:00
|
|
|
rv = nsContentUtils::GetSecurityManager()->
|
|
|
|
CheckLoadURIWithPrincipal(loadingPrincipal,
|
|
|
|
aURI,
|
2015-09-15 01:59:35 +00:00
|
|
|
flags);
|
2015-07-20 02:12:11 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// If the loadingPrincipal and the triggeringPrincipal are different, then make
|
|
|
|
// sure the triggeringPrincipal is allowed to access that URI.
|
|
|
|
nsCOMPtr<nsIPrincipal> triggeringPrincipal = aLoadInfo->TriggeringPrincipal();
|
|
|
|
if (loadingPrincipal != triggeringPrincipal) {
|
|
|
|
rv = nsContentUtils::GetSecurityManager()->
|
|
|
|
CheckLoadURIWithPrincipal(triggeringPrincipal,
|
|
|
|
aURI,
|
2015-09-15 01:59:35 +00:00
|
|
|
flags);
|
2015-07-20 02:12:11 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-04 08:05:16 +00:00
|
|
|
static bool
|
|
|
|
URIHasFlags(nsIURI* aURI, uint32_t aURIFlags)
|
|
|
|
{
|
|
|
|
bool hasFlags;
|
|
|
|
nsresult rv = NS_URIChainHasFlags(aURI, aURIFlags, &hasFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
|
|
|
|
return hasFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
2015-12-05 15:34:47 +00:00
|
|
|
DoSOPChecks(nsIURI* aURI, nsILoadInfo* aLoadInfo)
|
2015-07-20 02:12:11 +00:00
|
|
|
{
|
2015-11-04 08:05:16 +00:00
|
|
|
if (aLoadInfo->GetAllowChrome() &&
|
|
|
|
(URIHasFlags(aURI, nsIProtocolHandler::URI_IS_UI_RESOURCE) ||
|
|
|
|
SchemeIs(aURI, "moz-safe-about"))) {
|
|
|
|
// UI resources are allowed.
|
2015-09-15 01:59:35 +00:00
|
|
|
return DoCheckLoadURIChecks(aURI, aLoadInfo);
|
2015-07-20 02:12:11 +00:00
|
|
|
}
|
|
|
|
|
2015-12-05 15:34:47 +00:00
|
|
|
nsIPrincipal* loadingPrincipal = aLoadInfo->LoadingPrincipal();
|
|
|
|
bool sameOriginDataInherits =
|
|
|
|
aLoadInfo->GetSecurityMode() == nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_INHERITS;
|
2015-10-20 01:24:36 +00:00
|
|
|
|
2015-12-05 15:34:47 +00:00
|
|
|
if (sameOriginDataInherits &&
|
|
|
|
aLoadInfo->GetAboutBlankInherits() &&
|
|
|
|
NS_IsAboutBlank(aURI)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return loadingPrincipal->CheckMayLoad(aURI,
|
|
|
|
true, // report to console
|
|
|
|
sameOriginDataInherits);
|
2015-09-15 01:59:35 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 08:05:16 +00:00
|
|
|
static nsresult
|
2015-09-15 01:59:35 +00:00
|
|
|
DoCORSChecks(nsIChannel* aChannel, nsILoadInfo* aLoadInfo,
|
|
|
|
nsCOMPtr<nsIStreamListener>& aInAndOutListener)
|
|
|
|
{
|
2015-10-26 21:22:59 +00:00
|
|
|
MOZ_RELEASE_ASSERT(aInAndOutListener, "can not perform CORS checks without a listener");
|
2015-07-20 02:12:11 +00:00
|
|
|
nsIPrincipal* loadingPrincipal = aLoadInfo->LoadingPrincipal();
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<nsCORSListenerProxy> corsListener =
|
2015-07-20 02:12:11 +00:00
|
|
|
new nsCORSListenerProxy(aInAndOutListener,
|
|
|
|
loadingPrincipal,
|
2015-12-05 15:34:47 +00:00
|
|
|
aLoadInfo->GetRequireCorsWithCredentials());
|
2015-07-20 02:12:11 +00:00
|
|
|
// XXX: @arg: DataURIHandling::Allow
|
|
|
|
// lets use DataURIHandling::Allow for now and then decide on callsite basis. see also:
|
|
|
|
// http://mxr.mozilla.org/mozilla-central/source/dom/security/nsCORSListenerProxy.h#33
|
|
|
|
nsresult rv = corsListener->Init(aChannel, DataURIHandling::Allow);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
aInAndOutListener = corsListener;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-11-04 08:05:16 +00:00
|
|
|
static nsresult
|
2015-07-20 02:12:11 +00:00
|
|
|
DoContentSecurityChecks(nsIURI* aURI, nsILoadInfo* aLoadInfo)
|
|
|
|
{
|
2015-10-19 18:14:54 +00:00
|
|
|
nsContentPolicyType contentPolicyType =
|
|
|
|
aLoadInfo->GetExternalContentPolicyType();
|
2015-10-15 21:07:06 +00:00
|
|
|
nsContentPolicyType internalContentPolicyType =
|
|
|
|
aLoadInfo->InternalContentPolicyType();
|
2015-10-19 18:14:54 +00:00
|
|
|
nsCString mimeTypeGuess;
|
|
|
|
nsCOMPtr<nsINode> requestingContext = nullptr;
|
2015-07-20 02:12:11 +00:00
|
|
|
|
|
|
|
switch(contentPolicyType) {
|
2015-07-31 15:58:14 +00:00
|
|
|
case nsIContentPolicy::TYPE_OTHER: {
|
|
|
|
mimeTypeGuess = EmptyCString();
|
|
|
|
requestingContext = aLoadInfo->LoadingNode();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-10-19 02:59:18 +00:00
|
|
|
case nsIContentPolicy::TYPE_SCRIPT: {
|
|
|
|
mimeTypeGuess = NS_LITERAL_CSTRING("application/javascript");
|
|
|
|
requestingContext = aLoadInfo->LoadingNode();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-07-20 02:12:11 +00:00
|
|
|
case nsIContentPolicy::TYPE_IMAGE:
|
|
|
|
case nsIContentPolicy::TYPE_STYLESHEET:
|
|
|
|
case nsIContentPolicy::TYPE_OBJECT:
|
2015-08-10 17:25:20 +00:00
|
|
|
case nsIContentPolicy::TYPE_DOCUMENT: {
|
|
|
|
MOZ_ASSERT(false, "contentPolicyType not supported yet");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case nsIContentPolicy::TYPE_SUBDOCUMENT: {
|
|
|
|
mimeTypeGuess = NS_LITERAL_CSTRING("text/html");
|
|
|
|
requestingContext = aLoadInfo->LoadingNode();
|
|
|
|
MOZ_ASSERT(!requestingContext ||
|
|
|
|
requestingContext->NodeType() == nsIDOMNode::DOCUMENT_NODE,
|
|
|
|
"type_subdocument requires requestingContext of type Document");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-09-15 01:59:35 +00:00
|
|
|
case nsIContentPolicy::TYPE_REFRESH: {
|
2015-08-05 03:05:37 +00:00
|
|
|
MOZ_ASSERT(false, "contentPolicyType not supported yet");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-09-15 01:59:35 +00:00
|
|
|
case nsIContentPolicy::TYPE_XBL: {
|
|
|
|
mimeTypeGuess = EmptyCString();
|
|
|
|
requestingContext = aLoadInfo->LoadingNode();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-13 15:53:28 +00:00
|
|
|
case nsIContentPolicy::TYPE_PING: {
|
|
|
|
mimeTypeGuess = EmptyCString();
|
|
|
|
requestingContext = aLoadInfo->LoadingNode();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-05 03:06:19 +00:00
|
|
|
case nsIContentPolicy::TYPE_XMLHTTPREQUEST: {
|
|
|
|
// alias nsIContentPolicy::TYPE_DATAREQUEST:
|
|
|
|
requestingContext = aLoadInfo->LoadingNode();
|
2015-08-10 17:19:08 +00:00
|
|
|
MOZ_ASSERT(!requestingContext ||
|
|
|
|
requestingContext->NodeType() == nsIDOMNode::DOCUMENT_NODE,
|
|
|
|
"type_xml requires requestingContext of type Document");
|
|
|
|
|
2015-10-19 18:14:54 +00:00
|
|
|
// We're checking for the external TYPE_XMLHTTPREQUEST here in case
|
|
|
|
// an addon creates a request with that type.
|
2015-08-10 17:19:08 +00:00
|
|
|
if (internalContentPolicyType ==
|
2015-10-19 18:14:54 +00:00
|
|
|
nsIContentPolicy::TYPE_INTERNAL_XMLHTTPREQUEST ||
|
|
|
|
internalContentPolicyType ==
|
|
|
|
nsIContentPolicy::TYPE_XMLHTTPREQUEST) {
|
|
|
|
mimeTypeGuess = EmptyCString();
|
2015-08-10 17:19:08 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
MOZ_ASSERT(internalContentPolicyType ==
|
|
|
|
nsIContentPolicy::TYPE_INTERNAL_EVENTSOURCE,
|
|
|
|
"can not set mime type guess for unexpected internal type");
|
|
|
|
mimeTypeGuess = NS_LITERAL_CSTRING(TEXT_EVENT_STREAM);
|
|
|
|
}
|
2015-08-05 03:06:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-05 03:05:37 +00:00
|
|
|
case nsIContentPolicy::TYPE_OBJECT_SUBREQUEST: {
|
|
|
|
mimeTypeGuess = EmptyCString();
|
|
|
|
requestingContext = aLoadInfo->LoadingNode();
|
|
|
|
MOZ_ASSERT(!requestingContext ||
|
|
|
|
requestingContext->NodeType() == nsIDOMNode::ELEMENT_NODE,
|
|
|
|
"type_subrequest requires requestingContext of type Element");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-11-20 18:55:54 +00:00
|
|
|
case nsIContentPolicy::TYPE_DTD: {
|
|
|
|
mimeTypeGuess = EmptyCString();
|
|
|
|
requestingContext = aLoadInfo->LoadingNode();
|
|
|
|
MOZ_ASSERT(!requestingContext ||
|
|
|
|
requestingContext->NodeType() == nsIDOMNode::DOCUMENT_NODE,
|
|
|
|
"type_dtd requires requestingContext of type Document");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-07-25 17:29:22 +00:00
|
|
|
case nsIContentPolicy::TYPE_FONT: {
|
2015-07-20 02:12:11 +00:00
|
|
|
MOZ_ASSERT(false, "contentPolicyType not supported yet");
|
|
|
|
break;
|
2015-07-25 17:29:22 +00:00
|
|
|
}
|
2015-07-20 02:12:11 +00:00
|
|
|
|
2015-07-25 17:29:22 +00:00
|
|
|
case nsIContentPolicy::TYPE_MEDIA: {
|
|
|
|
if (internalContentPolicyType == nsIContentPolicy::TYPE_INTERNAL_TRACK) {
|
|
|
|
mimeTypeGuess = NS_LITERAL_CSTRING("text/vtt");
|
2015-07-20 02:12:11 +00:00
|
|
|
}
|
2015-07-25 17:29:22 +00:00
|
|
|
else {
|
|
|
|
mimeTypeGuess = EmptyCString();
|
|
|
|
}
|
|
|
|
requestingContext = aLoadInfo->LoadingNode();
|
|
|
|
MOZ_ASSERT(!requestingContext ||
|
|
|
|
requestingContext->NodeType() == nsIDOMNode::ELEMENT_NODE,
|
|
|
|
"type_media requires requestingContext of type Element");
|
2015-07-20 02:12:11 +00:00
|
|
|
break;
|
2015-07-25 17:29:22 +00:00
|
|
|
}
|
2015-07-20 02:12:11 +00:00
|
|
|
|
2015-07-27 18:57:56 +00:00
|
|
|
case nsIContentPolicy::TYPE_WEBSOCKET: {
|
2015-07-28 03:39:17 +00:00
|
|
|
MOZ_ASSERT(false, "contentPolicyType not supported yet");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-07-27 18:57:56 +00:00
|
|
|
case nsIContentPolicy::TYPE_CSP_REPORT: {
|
|
|
|
mimeTypeGuess = EmptyCString();
|
|
|
|
requestingContext = aLoadInfo->LoadingNode();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-09-28 23:34:47 +00:00
|
|
|
case nsIContentPolicy::TYPE_XSLT: {
|
|
|
|
mimeTypeGuess = NS_LITERAL_CSTRING("application/xml");
|
|
|
|
requestingContext = aLoadInfo->LoadingNode();
|
|
|
|
MOZ_ASSERT(!requestingContext ||
|
|
|
|
requestingContext->NodeType() == nsIDOMNode::DOCUMENT_NODE,
|
|
|
|
"type_xslt requires requestingContext of type Document");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-07-28 03:39:17 +00:00
|
|
|
case nsIContentPolicy::TYPE_BEACON: {
|
|
|
|
mimeTypeGuess = EmptyCString();
|
|
|
|
requestingContext = aLoadInfo->LoadingNode();
|
|
|
|
MOZ_ASSERT(!requestingContext ||
|
|
|
|
requestingContext->NodeType() == nsIDOMNode::DOCUMENT_NODE,
|
|
|
|
"type_beacon requires requestingContext of type Document");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-10-20 01:24:36 +00:00
|
|
|
case nsIContentPolicy::TYPE_FETCH: {
|
|
|
|
mimeTypeGuess = EmptyCString();
|
|
|
|
requestingContext = aLoadInfo->LoadingNode();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-07-25 17:29:22 +00:00
|
|
|
case nsIContentPolicy::TYPE_IMAGESET: {
|
2015-07-20 02:12:11 +00:00
|
|
|
MOZ_ASSERT(false, "contentPolicyType not supported yet");
|
|
|
|
break;
|
2015-07-25 17:29:22 +00:00
|
|
|
}
|
2015-07-20 02:12:11 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
// nsIContentPolicy::TYPE_INVALID
|
|
|
|
MOZ_ASSERT(false, "can not perform security check without a valid contentType");
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t shouldLoad = nsIContentPolicy::ACCEPT;
|
2015-10-20 01:33:37 +00:00
|
|
|
nsresult rv = NS_CheckContentLoadPolicy(internalContentPolicyType,
|
2015-07-20 02:12:11 +00:00
|
|
|
aURI,
|
|
|
|
aLoadInfo->LoadingPrincipal(),
|
|
|
|
requestingContext,
|
|
|
|
mimeTypeGuess,
|
|
|
|
nullptr, //extra,
|
|
|
|
&shouldLoad,
|
|
|
|
nsContentUtils::GetContentPolicy(),
|
|
|
|
nsContentUtils::GetSecurityManager());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (NS_CP_REJECTED(shouldLoad)) {
|
|
|
|
return NS_ERROR_CONTENT_BLOCKED;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Based on the security flags provided in the loadInfo of the channel,
|
|
|
|
* doContentSecurityCheck() performs the following content security checks
|
|
|
|
* before opening the channel:
|
|
|
|
*
|
|
|
|
* (1) Same Origin Policy Check (if applicable)
|
|
|
|
* (2) Allow Cross Origin but perform sanity checks whether a principal
|
|
|
|
* is allowed to access the following URL.
|
|
|
|
* (3) Perform CORS check (if applicable)
|
|
|
|
* (4) ContentPolicy checks (Content-Security-Policy, Mixed Content, ...)
|
|
|
|
*
|
|
|
|
* @param aChannel
|
|
|
|
* The channel to perform the security checks on.
|
|
|
|
* @param aInAndOutListener
|
|
|
|
* The streamListener that is passed to channel->AsyncOpen2() that is now potentially
|
|
|
|
* wrappend within nsCORSListenerProxy() and becomes the corsListener that now needs
|
|
|
|
* to be set as new streamListener on the channel.
|
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
nsContentSecurityManager::doContentSecurityCheck(nsIChannel* aChannel,
|
|
|
|
nsCOMPtr<nsIStreamListener>& aInAndOutListener)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aChannel);
|
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = aChannel->GetLoadInfo();
|
|
|
|
|
|
|
|
if (!loadInfo) {
|
|
|
|
MOZ_ASSERT(false, "channel needs to have loadInfo to perform security checks");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that only one of the five security flags is set in the loadinfo
|
|
|
|
// e.g. do not require same origin and allow cross origin at the same time
|
|
|
|
nsresult rv = ValidateSecurityFlags(loadInfo);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-12-05 15:34:47 +00:00
|
|
|
// lets store the initialSecurityCheckDone flag which indicates whether the channel
|
|
|
|
// was initialy evaluated by the contentSecurityManager. Once the inital
|
|
|
|
// asyncOpen() of the channel went through the contentSecurityManager then
|
|
|
|
// redirects do not have perform all the security checks, e.g. no reason
|
|
|
|
// to setup CORS again.
|
|
|
|
bool initialSecurityCheckDone = loadInfo->GetInitialSecurityCheckDone();
|
|
|
|
|
|
|
|
// now lets set the initalSecurityFlag for subsequent calls
|
|
|
|
rv = loadInfo->SetInitialSecurityCheckDone(true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-07-20 02:12:11 +00:00
|
|
|
// since aChannel was openend using asyncOpen2() we have to make sure
|
|
|
|
// that redirects of that channel also get openend using asyncOpen2()
|
|
|
|
// please note that some implementations of ::AsyncOpen2 might already
|
|
|
|
// have set that flag to true (e.g. nsViewSourceChannel) in which case
|
|
|
|
// we just set the flag again.
|
|
|
|
rv = loadInfo->SetEnforceSecurity(true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> finalChannelURI;
|
|
|
|
rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(finalChannelURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-09-15 01:59:35 +00:00
|
|
|
nsSecurityFlags securityMode = loadInfo->GetSecurityMode();
|
2015-12-05 09:46:20 +00:00
|
|
|
|
2015-09-15 01:59:35 +00:00
|
|
|
// if none of the REQUIRE_SAME_ORIGIN flags are set, then SOP does not apply
|
|
|
|
if ((securityMode == nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_INHERITS) ||
|
|
|
|
(securityMode == nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED)) {
|
2015-12-05 15:34:47 +00:00
|
|
|
rv = DoSOPChecks(finalChannelURI, loadInfo);
|
2015-09-15 01:59:35 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2015-07-20 02:12:11 +00:00
|
|
|
|
2015-12-05 15:34:47 +00:00
|
|
|
// if dealing with a redirected channel then we only enforce SOP
|
|
|
|
// and can return at this point.
|
|
|
|
if (initialSecurityCheckDone) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-15 01:59:35 +00:00
|
|
|
if ((securityMode == nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS) ||
|
|
|
|
(securityMode == nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL)) {
|
|
|
|
// Please note that DoCheckLoadURIChecks should only be enforced for
|
|
|
|
// cross origin requests. If the flag SEC_REQUIRE_CORS_DATA_INHERITS is set
|
|
|
|
// within the loadInfo, then then CheckLoadURIWithPrincipal is performed
|
|
|
|
// within nsCorsListenerProxy
|
2015-12-05 15:34:47 +00:00
|
|
|
rv = DoCheckLoadURIChecks(finalChannelURI, loadInfo);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (securityMode == nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS) {
|
|
|
|
rv = DoCORSChecks(aChannel, loadInfo, aInAndOutListener);
|
2015-09-15 01:59:35 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2015-07-20 02:12:11 +00:00
|
|
|
|
2015-12-05 15:34:47 +00:00
|
|
|
// Perform all ContentPolicy checks (MixedContent, CSP, ...)
|
|
|
|
rv = DoContentSecurityChecks(finalChannelURI, loadInfo);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// all security checks passed - lets allow the load
|
2015-07-20 02:12:11 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-09-18 16:27:15 +00:00
|
|
|
|
2015-12-05 15:34:47 +00:00
|
|
|
|
2015-09-18 16:27:15 +00:00
|
|
|
// ==== nsIContentSecurityManager implementation =====
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentSecurityManager::PerformSecurityCheck(nsIChannel* aChannel,
|
|
|
|
nsIStreamListener* aStreamListener,
|
|
|
|
nsIStreamListener** outStreamListener)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStreamListener> inAndOutListener = aStreamListener;
|
|
|
|
nsresult rv = doContentSecurityCheck(aChannel, inAndOutListener);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
inAndOutListener.forget(outStreamListener);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-11-06 19:10:17 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsContentSecurityManager::IsURIPotentiallyTrustworthy(nsIURI* aURI, bool* aIsTrustWorthy)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsTrustWorthy);
|
|
|
|
|
|
|
|
*aIsTrustWorthy = false;
|
|
|
|
nsAutoCString scheme;
|
|
|
|
nsresult rv = aURI->GetScheme(scheme);
|
2015-11-19 22:22:57 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-11-06 19:10:17 +00:00
|
|
|
|
|
|
|
if (scheme.EqualsLiteral("https") ||
|
|
|
|
scheme.EqualsLiteral("file") ||
|
2015-11-10 18:50:46 +00:00
|
|
|
scheme.EqualsLiteral("app") ||
|
|
|
|
scheme.EqualsLiteral("wss")) {
|
2015-11-06 19:10:17 +00:00
|
|
|
*aIsTrustWorthy = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString host;
|
|
|
|
rv = aURI->GetHost(host);
|
2015-11-19 22:22:57 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-11-06 19:10:17 +00:00
|
|
|
|
|
|
|
if (host.Equals("127.0.0.1") ||
|
|
|
|
host.Equals("localhost") ||
|
|
|
|
host.Equals("::1")) {
|
|
|
|
*aIsTrustWorthy = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|