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"
|
|
|
|
#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"
|
2004-09-01 21:12:18 +04:00
|
|
|
#include "nsIContent.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"
|
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
|
|
|
|
2004-05-13 22:34:18 +04:00
|
|
|
static PRLogModuleInfo* gConPolLog;
|
|
|
|
|
2000-05-05 09:28:49 +04:00
|
|
|
nsresult
|
|
|
|
NS_NewContentPolicy(nsIContentPolicy **aResult)
|
|
|
|
{
|
|
|
|
*aResult = new nsContentPolicy;
|
|
|
|
if (!*aResult)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsContentPolicy::nsContentPolicy()
|
2006-03-22 18:26:35 +03:00
|
|
|
: mPolicies(NS_CONTENTPOLICY_CATEGORY)
|
2015-02-20 19:54:12 +03:00
|
|
|
, mSimplePolicies(NS_SIMPLECONTENTPOLICY_CATEGORY)
|
2000-05-05 09:28:49 +04:00
|
|
|
{
|
2004-05-13 22:34:18 +04:00
|
|
|
if (! gConPolLog) {
|
|
|
|
gConPolLog = PR_NewLogModule("nsContentPolicy");
|
|
|
|
}
|
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-02-20 19:54:12 +03:00
|
|
|
SCPMethod simplePolicyMethod,
|
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));
|
|
|
|
NS_ASSERTION(!requestingContext || node || window,
|
|
|
|
"Context should be a DOM node or a DOM window!");
|
|
|
|
}
|
|
|
|
#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);
|
|
|
|
|
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;
|
2013-10-10 16:48:03 +04:00
|
|
|
nsCOMArray<nsIContentPolicy> entries;
|
|
|
|
mPolicies.GetEntries(entries);
|
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 */
|
2015-06-12 23:52:07 +03:00
|
|
|
rv = (entries[i]->*policyMethod)(externalType, 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)) {
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
2015-02-20 19:54:12 +03:00
|
|
|
nsCOMPtr<nsIDOMElement> topFrameElement;
|
|
|
|
bool isTopLevel = true;
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window;
|
|
|
|
if (nsCOMPtr<nsINode> node = do_QueryInterface(requestingContext)) {
|
|
|
|
window = node->OwnerDoc()->GetWindow();
|
|
|
|
} else {
|
|
|
|
window = do_QueryInterface(requestingContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window) {
|
|
|
|
nsCOMPtr<nsIDocShell> docShell = window->GetDocShell();
|
|
|
|
nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(docShell);
|
|
|
|
loadContext->GetTopFrameElement(getter_AddRefs(topFrameElement));
|
|
|
|
|
|
|
|
MOZ_ASSERT(window->IsOuterWindow());
|
|
|
|
|
|
|
|
if (topFrameElement) {
|
|
|
|
nsCOMPtr<nsIDOMWindow> topWindow;
|
|
|
|
window->GetScriptableTop(getter_AddRefs(topWindow));
|
|
|
|
isTopLevel = topWindow == static_cast<nsIDOMWindow*>(window);
|
|
|
|
} else {
|
|
|
|
// If we don't have a top frame element, then requestingContext is
|
|
|
|
// part of the top-level XUL document. Presumably it's the <browser>
|
|
|
|
// element that content is being loaded into, so we call it the
|
|
|
|
// topFrameElement.
|
|
|
|
topFrameElement = do_QueryInterface(requestingContext);
|
|
|
|
isTopLevel = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMArray<nsISimpleContentPolicy> simpleEntries;
|
|
|
|
mSimplePolicies.GetEntries(simpleEntries);
|
|
|
|
count = simpleEntries.Count();
|
|
|
|
for (int32_t i = 0; i < count; i++) {
|
|
|
|
/* check the appropriate policy */
|
2015-06-12 23:52:07 +03:00
|
|
|
rv = (simpleEntries[i]->*simplePolicyMethod)(externalType, contentLocation,
|
2015-02-20 19:54:12 +03:00
|
|
|
requestingLocation,
|
|
|
|
topFrameElement, isTopLevel,
|
|
|
|
mimeType, extra, requestPrincipal,
|
|
|
|
decision);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && NS_CP_REJECTED(*decision)) {
|
|
|
|
/* policy says no, no point continuing to check */
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
#define LOG_CHECK(logType) \
|
|
|
|
PR_BEGIN_MACRO \
|
2015-05-14 20:13:23 +03:00
|
|
|
/* skip all this nonsense if the call failed or logging is disabled */ \
|
2015-06-04 01:25:57 +03:00
|
|
|
if (NS_SUCCEEDED(rv) && MOZ_LOG_TEST(gConPolLog, LogLevel::Debug)) { \
|
2004-05-13 22:34:18 +04:00
|
|
|
const char *resultName; \
|
|
|
|
if (decision) { \
|
|
|
|
resultName = NS_CP_ResponseName(*decision); \
|
|
|
|
} else { \
|
|
|
|
resultName = "(null ptr)"; \
|
|
|
|
} \
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec("None"); \
|
2004-05-13 22:34:18 +04:00
|
|
|
if (contentLocation) { \
|
|
|
|
contentLocation->GetSpec(spec); \
|
|
|
|
} \
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString refSpec("None"); \
|
2004-05-13 22:34:18 +04:00
|
|
|
if (requestingLocation) { \
|
|
|
|
requestingLocation->GetSpec(refSpec); \
|
|
|
|
} \
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(gConPolLog, LogLevel::Debug, \
|
2004-05-13 22:34:18 +04:00
|
|
|
("Content Policy: " logType ": <%s> <Ref:%s> result=%s", \
|
|
|
|
spec.get(), refSpec.get(), resultName) \
|
|
|
|
); \
|
|
|
|
} \
|
|
|
|
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,
|
|
|
|
&nsISimpleContentPolicy::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,
|
|
|
|
&nsISimpleContentPolicy::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;
|
|
|
|
}
|