2000-05-05 09:28:49 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2004-05-13 22:34:18 +04:00
|
|
|
// vim: ft=cpp tw=78 sw=4 et ts=8
|
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/. */
|
2000-05-05 09:28:49 +04:00
|
|
|
|
2015-06-17 21:12:23 +03:00
|
|
|
/*
|
2006-03-30 12:03:04 +04:00
|
|
|
* Implementation of the "@mozilla.org/layout/content-policy;1" contract.
|
|
|
|
*/
|
|
|
|
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2004-05-13 22:34:18 +04:00
|
|
|
|
2000-05-05 09:28:49 +04:00
|
|
|
#include "nsISupports.h"
|
2002-09-04 03:36:13 +04:00
|
|
|
#include "nsXPCOM.h"
|
2000-05-05 09:28:49 +04:00
|
|
|
#include "nsContentPolicyUtils.h"
|
2015-09-21 00:55:44 +03:00
|
|
|
#include "mozilla/dom/nsCSPService.h"
|
2000-05-05 09:28:49 +04:00
|
|
|
#include "nsContentPolicy.h"
|
2004-05-13 22:34:18 +04:00
|
|
|
#include "nsIURI.h"
|
2015-06-17 21:12:23 +03:00
|
|
|
#include "nsIDocShell.h"
|
2015-07-01 16:56:00 +03:00
|
|
|
#include "nsIDOMElement.h"
|
2004-07-27 21:15:53 +04:00
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2017-09-05 19:01:07 +03:00
|
|
|
#include "nsITabChild.h"
|
2004-09-01 21:12:18 +04:00
|
|
|
#include "nsIContent.h"
|
2017-02-03 06:49:07 +03:00
|
|
|
#include "nsIImageLoadingContent.h"
|
2015-06-17 21:12:23 +03:00
|
|
|
#include "nsILoadContext.h"
|
2006-03-22 18:26:35 +03:00
|
|
|
#include "nsCOMArray.h"
|
2015-07-01 16:56:00 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-09-12 01:48:43 +03:00
|
|
|
#include "mozilla/dom/nsMixedContentBlocker.h"
|
2017-03-29 05:20:32 +03:00
|
|
|
#include "nsIContentSecurityPolicy.h"
|
|
|
|
#include "mozilla/dom/TabGroup.h"
|
2017-06-21 13:14:01 +03:00
|
|
|
#include "mozilla/TaskCategory.h"
|
2000-05-05 09:28:49 +04:00
|
|
|
|
2015-06-17 21:12:23 +03:00
|
|
|
using mozilla::LogLevel;
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsContentPolicy, nsIContentPolicy)
|
2000-05-05 09:28:49 +04:00
|
|
|
|
2015-11-23 22:09:25 +03:00
|
|
|
static mozilla::LazyLogModule gConPolLog("nsContentPolicy");
|
2004-05-13 22:34:18 +04:00
|
|
|
|
2000-05-05 09:28:49 +04:00
|
|
|
nsresult
|
|
|
|
NS_NewContentPolicy(nsIContentPolicy **aResult)
|
|
|
|
{
|
|
|
|
*aResult = new nsContentPolicy;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsContentPolicy::nsContentPolicy()
|
2006-03-22 18:26:35 +03:00
|
|
|
: mPolicies(NS_CONTENTPOLICY_CATEGORY)
|
2017-04-15 23:23:37 +03:00
|
|
|
, mMixedContentBlocker(do_GetService(NS_MIXEDCONTENTBLOCKER_CONTRACTID))
|
|
|
|
, mCSPService(do_GetService(CSPSERVICE_CONTRACTID))
|
2000-05-05 09:28:49 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsContentPolicy::~nsContentPolicy()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-05-13 22:34:18 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
#define WARN_IF_URI_UNINITIALIZED(uri,name) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if ((uri)) { \
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec; \
|
2004-05-13 22:34:18 +04:00
|
|
|
(uri)->GetAsciiSpec(spec); \
|
|
|
|
if (spec.IsEmpty()) { \
|
|
|
|
NS_WARNING(name " is uninitialized, fix caller"); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
2000-05-05 09:28:49 +04:00
|
|
|
|
2004-05-13 22:34:18 +04:00
|
|
|
#else // ! defined(DEBUG)
|
|
|
|
|
|
|
|
#define WARN_IF_URI_UNINITIALIZED(uri,name)
|
|
|
|
|
|
|
|
#endif // defined(DEBUG)
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
nsContentPolicy::CheckPolicy(CPMethod policyMethod,
|
2015-06-12 23:52:07 +03:00
|
|
|
nsContentPolicyType contentType,
|
2004-05-13 22:34:18 +04:00
|
|
|
nsIURI *contentLocation,
|
|
|
|
nsIURI *requestingLocation,
|
2004-07-27 21:15:53 +04:00
|
|
|
nsISupports *requestingContext,
|
2004-05-13 22:34:18 +04:00
|
|
|
const nsACString &mimeType,
|
|
|
|
nsISupports *extra,
|
2012-07-03 03:16:11 +04:00
|
|
|
nsIPrincipal *requestPrincipal,
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t *decision)
|
2000-05-05 09:28:49 +04:00
|
|
|
{
|
2004-05-13 22:34:18 +04:00
|
|
|
//sanity-check passed-through parameters
|
|
|
|
NS_PRECONDITION(decision, "Null out pointer");
|
|
|
|
WARN_IF_URI_UNINITIALIZED(contentLocation, "Request URI");
|
|
|
|
WARN_IF_URI_UNINITIALIZED(requestingLocation, "Requesting URI");
|
|
|
|
|
2004-07-27 21:15:53 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> node(do_QueryInterface(requestingContext));
|
|
|
|
nsCOMPtr<nsIDOMWindow> window(do_QueryInterface(requestingContext));
|
2017-09-05 19:01:07 +03:00
|
|
|
nsCOMPtr<nsITabChild> tabChild(do_QueryInterface(requestingContext));
|
|
|
|
NS_ASSERTION(!requestingContext || node || window || tabChild,
|
|
|
|
"Context should be a DOM node, DOM window or a tabChild!");
|
2004-07-27 21:15:53 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-09-01 21:12:18 +04:00
|
|
|
/*
|
|
|
|
* There might not be a requestinglocation. This can happen for
|
|
|
|
* iframes with an image as src. Get the uri from the dom node.
|
|
|
|
* See bug 254510
|
|
|
|
*/
|
|
|
|
if (!requestingLocation) {
|
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
nsCOMPtr<nsIContent> node = do_QueryInterface(requestingContext);
|
|
|
|
if (node) {
|
2011-10-18 14:53:36 +04:00
|
|
|
doc = node->OwnerDoc();
|
2004-09-01 21:12:18 +04:00
|
|
|
}
|
|
|
|
if (!doc) {
|
|
|
|
doc = do_QueryInterface(requestingContext);
|
|
|
|
}
|
|
|
|
if (doc) {
|
|
|
|
requestingLocation = doc->GetDocumentURI();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-12 23:52:07 +03:00
|
|
|
nsContentPolicyType externalType =
|
|
|
|
nsContentUtils::InternalContentPolicyTypeToExternal(contentType);
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
/*
|
2000-05-05 09:28:49 +04:00
|
|
|
* Enumerate mPolicies and ask each of them, taking the logical AND of
|
|
|
|
* their permissions.
|
|
|
|
*/
|
2006-03-22 18:26:35 +03:00
|
|
|
nsresult rv;
|
2017-05-07 05:56:36 +03:00
|
|
|
const nsCOMArray<nsIContentPolicy>& entries = mPolicies.GetCachedEntries();
|
2017-03-29 05:20:32 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> window;
|
|
|
|
if (nsCOMPtr<nsINode> node = do_QueryInterface(requestingContext)) {
|
|
|
|
window = node->OwnerDoc()->GetWindow();
|
|
|
|
} else {
|
|
|
|
window = do_QueryInterface(requestingContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requestPrincipal) {
|
|
|
|
nsCOMPtr<nsIContentSecurityPolicy> csp;
|
|
|
|
requestPrincipal->GetCsp(getter_AddRefs(csp));
|
|
|
|
if (csp && window) {
|
2017-06-21 13:14:01 +03:00
|
|
|
csp->EnsureEventTarget(window->EventTargetFor(mozilla::TaskCategory::Other));
|
2017-03-29 05:20:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t count = entries.Count();
|
|
|
|
for (int32_t i = 0; i < count; i++) {
|
2004-05-13 22:34:18 +04:00
|
|
|
/* check the appropriate policy */
|
2016-01-19 20:10:50 +03:00
|
|
|
// Send internal content policy type to CSP and mixed content blocker
|
2015-09-12 01:48:43 +03:00
|
|
|
nsContentPolicyType type = externalType;
|
2017-04-15 23:23:37 +03:00
|
|
|
if (mMixedContentBlocker == entries[i] || mCSPService == entries[i]) {
|
2016-01-19 20:10:50 +03:00
|
|
|
type = contentType;
|
2015-09-21 00:55:44 +03:00
|
|
|
}
|
2015-09-12 01:48:43 +03:00
|
|
|
rv = (entries[i]->*policyMethod)(type, contentLocation,
|
2006-03-22 18:26:35 +03:00
|
|
|
requestingLocation, requestingContext,
|
2012-07-03 03:16:11 +04:00
|
|
|
mimeType, extra, requestPrincipal,
|
|
|
|
decision);
|
2004-05-13 22:34:18 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && NS_CP_REJECTED(*decision)) {
|
2017-02-03 06:49:07 +03:00
|
|
|
// If we are blocking an image, we have to let the
|
|
|
|
// ImageLoadingContent know that we blocked the load.
|
|
|
|
if (externalType == nsIContentPolicy::TYPE_IMAGE ||
|
|
|
|
externalType == nsIContentPolicy::TYPE_IMAGESET) {
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> img =
|
|
|
|
do_QueryInterface(requestingContext);
|
|
|
|
if (img) {
|
|
|
|
img->SetBlockedRequest(*decision);
|
|
|
|
}
|
|
|
|
}
|
2004-05-13 22:34:18 +04:00
|
|
|
/* policy says no, no point continuing to check */
|
|
|
|
return NS_OK;
|
2001-05-22 02:40:10 +04:00
|
|
|
}
|
2000-05-05 09:28:49 +04:00
|
|
|
}
|
|
|
|
|
2004-05-13 22:34:18 +04:00
|
|
|
// everyone returned failure, or no policies: sanitize result
|
|
|
|
*decision = nsIContentPolicy::ACCEPT;
|
2000-05-05 09:28:49 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-05-13 22:34:18 +04:00
|
|
|
//uses the parameters from ShouldXYZ to produce and log a message
|
|
|
|
//logType must be a literal string constant
|
2016-08-26 09:02:31 +03:00
|
|
|
#define LOG_CHECK(logType) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
/* skip all this nonsense if the call failed or logging is disabled */ \
|
|
|
|
if (NS_SUCCEEDED(rv) && MOZ_LOG_TEST(gConPolLog, LogLevel::Debug)) { \
|
|
|
|
const char *resultName; \
|
|
|
|
if (decision) { \
|
|
|
|
resultName = NS_CP_ResponseName(*decision); \
|
|
|
|
} else { \
|
|
|
|
resultName = "(null ptr)"; \
|
|
|
|
} \
|
|
|
|
MOZ_LOG(gConPolLog, LogLevel::Debug, \
|
|
|
|
("Content Policy: " logType ": <%s> <Ref:%s> result=%s", \
|
|
|
|
contentLocation ? contentLocation->GetSpecOrDefault().get() \
|
|
|
|
: "None", \
|
|
|
|
requestingLocation ? requestingLocation->GetSpecOrDefault().get()\
|
|
|
|
: "None", \
|
|
|
|
resultName) \
|
|
|
|
); \
|
|
|
|
} \
|
2004-05-13 22:34:18 +04:00
|
|
|
PR_END_MACRO
|
|
|
|
|
2000-05-05 09:28:49 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsContentPolicy::ShouldLoad(uint32_t contentType,
|
2004-05-13 22:34:18 +04:00
|
|
|
nsIURI *contentLocation,
|
|
|
|
nsIURI *requestingLocation,
|
2004-07-27 21:15:53 +04:00
|
|
|
nsISupports *requestingContext,
|
2004-05-13 22:34:18 +04:00
|
|
|
const nsACString &mimeType,
|
|
|
|
nsISupports *extra,
|
2012-07-03 03:16:11 +04:00
|
|
|
nsIPrincipal *requestPrincipal,
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t *decision)
|
2000-05-05 09:28:49 +04:00
|
|
|
{
|
2004-05-13 22:34:18 +04:00
|
|
|
// ShouldProcess does not need a content location, but we do
|
|
|
|
NS_PRECONDITION(contentLocation, "Must provide request location");
|
2015-02-20 19:54:12 +03:00
|
|
|
nsresult rv = CheckPolicy(&nsIContentPolicy::ShouldLoad,
|
|
|
|
contentType,
|
2004-05-13 22:34:18 +04:00
|
|
|
contentLocation, requestingLocation,
|
2012-07-03 03:16:11 +04:00
|
|
|
requestingContext, mimeType, extra,
|
|
|
|
requestPrincipal, decision);
|
2004-05-13 22:34:18 +04:00
|
|
|
LOG_CHECK("ShouldLoad");
|
|
|
|
|
|
|
|
return rv;
|
2000-05-05 09:28:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsContentPolicy::ShouldProcess(uint32_t contentType,
|
2004-05-13 22:34:18 +04:00
|
|
|
nsIURI *contentLocation,
|
|
|
|
nsIURI *requestingLocation,
|
2004-07-27 21:15:53 +04:00
|
|
|
nsISupports *requestingContext,
|
2004-05-13 22:34:18 +04:00
|
|
|
const nsACString &mimeType,
|
|
|
|
nsISupports *extra,
|
2012-07-03 03:16:11 +04:00
|
|
|
nsIPrincipal *requestPrincipal,
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t *decision)
|
2000-05-05 09:28:49 +04:00
|
|
|
{
|
2015-02-20 19:54:12 +03:00
|
|
|
nsresult rv = CheckPolicy(&nsIContentPolicy::ShouldProcess,
|
|
|
|
contentType,
|
2004-05-13 22:34:18 +04:00
|
|
|
contentLocation, requestingLocation,
|
2012-07-03 03:16:11 +04:00
|
|
|
requestingContext, mimeType, extra,
|
|
|
|
requestPrincipal, decision);
|
2004-05-13 22:34:18 +04:00
|
|
|
LOG_CHECK("ShouldProcess");
|
2000-05-05 09:28:49 +04:00
|
|
|
|
2004-05-13 22:34:18 +04:00
|
|
|
return rv;
|
|
|
|
}
|