2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
// vim: ft=cpp tw=78 sw=4 et ts=8
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of the "@mozilla.org/layout/content-policy;1" contract.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsContentPolicy.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsContentPolicy, nsIContentPolicy)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
static PRLogModuleInfo* gConPolLog;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewContentPolicy(nsIContentPolicy **aResult)
|
|
|
|
{
|
|
|
|
*aResult = new nsContentPolicy;
|
|
|
|
if (!*aResult)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsContentPolicy::nsContentPolicy()
|
|
|
|
: mPolicies(NS_CONTENTPOLICY_CATEGORY)
|
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (! gConPolLog) {
|
|
|
|
gConPolLog = PR_NewLogModule("nsContentPolicy");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
nsContentPolicy::~nsContentPolicy()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
#define WARN_IF_URI_UNINITIALIZED(uri,name) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if ((uri)) { \
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString spec; \
|
2007-03-22 10:30:00 -07:00
|
|
|
(uri)->GetAsciiSpec(spec); \
|
|
|
|
if (spec.IsEmpty()) { \
|
|
|
|
NS_WARNING(name " is uninitialized, fix caller"); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
|
|
|
#else // ! defined(DEBUG)
|
|
|
|
|
|
|
|
#define WARN_IF_URI_UNINITIALIZED(uri,name)
|
|
|
|
|
|
|
|
#endif // defined(DEBUG)
|
|
|
|
|
|
|
|
inline nsresult
|
|
|
|
nsContentPolicy::CheckPolicy(CPMethod policyMethod,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t contentType,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIURI *contentLocation,
|
|
|
|
nsIURI *requestingLocation,
|
|
|
|
nsISupports *requestingContext,
|
|
|
|
const nsACString &mimeType,
|
|
|
|
nsISupports *extra,
|
2012-07-02 16:16:11 -07:00
|
|
|
nsIPrincipal *requestPrincipal,
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t *decision)
|
2007-03-22 10:30:00 -07: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");
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 03:53:36 -07:00
|
|
|
doc = node->OwnerDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
if (!doc) {
|
|
|
|
doc = do_QueryInterface(requestingContext);
|
|
|
|
}
|
|
|
|
if (doc) {
|
|
|
|
requestingLocation = doc->GetDocumentURI();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enumerate mPolicies and ask each of them, taking the logical AND of
|
|
|
|
* their permissions.
|
|
|
|
*/
|
|
|
|
nsresult rv;
|
2013-10-10 05:48:03 -07:00
|
|
|
nsCOMArray<nsIContentPolicy> entries;
|
|
|
|
mPolicies.GetEntries(entries);
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = entries.Count();
|
|
|
|
for (int32_t i = 0; i < count; i++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/* check the appropriate policy */
|
|
|
|
rv = (entries[i]->*policyMethod)(contentType, contentLocation,
|
|
|
|
requestingLocation, requestingContext,
|
2012-07-02 16:16:11 -07:00
|
|
|
mimeType, extra, requestPrincipal,
|
|
|
|
decision);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && NS_CP_REJECTED(*decision)) {
|
|
|
|
/* policy says no, no point continuing to check */
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// everyone returned failure, or no policies: sanitize result
|
|
|
|
*decision = nsIContentPolicy::ACCEPT;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
|
|
|
|
//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 \
|
|
|
|
/* skip all this nonsense if the call failed */ \
|
|
|
|
if (NS_SUCCEEDED(rv)) { \
|
|
|
|
const char *resultName; \
|
|
|
|
if (decision) { \
|
|
|
|
resultName = NS_CP_ResponseName(*decision); \
|
|
|
|
} else { \
|
|
|
|
resultName = "(null ptr)"; \
|
|
|
|
} \
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString spec("None"); \
|
2007-03-22 10:30:00 -07:00
|
|
|
if (contentLocation) { \
|
|
|
|
contentLocation->GetSpec(spec); \
|
|
|
|
} \
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString refSpec("None"); \
|
2007-03-22 10:30:00 -07:00
|
|
|
if (requestingLocation) { \
|
|
|
|
requestingLocation->GetSpec(refSpec); \
|
|
|
|
} \
|
|
|
|
PR_LOG(gConPolLog, PR_LOG_DEBUG, \
|
|
|
|
("Content Policy: " logType ": <%s> <Ref:%s> result=%s", \
|
|
|
|
spec.get(), refSpec.get(), resultName) \
|
|
|
|
); \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
|
|
|
#else //!defined(PR_LOGGING)
|
|
|
|
|
|
|
|
#define LOG_CHECK(logType)
|
|
|
|
|
|
|
|
#endif //!defined(PR_LOGGING)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsContentPolicy::ShouldLoad(uint32_t contentType,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIURI *contentLocation,
|
|
|
|
nsIURI *requestingLocation,
|
|
|
|
nsISupports *requestingContext,
|
|
|
|
const nsACString &mimeType,
|
|
|
|
nsISupports *extra,
|
2012-07-02 16:16:11 -07:00
|
|
|
nsIPrincipal *requestPrincipal,
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t *decision)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// ShouldProcess does not need a content location, but we do
|
|
|
|
NS_PRECONDITION(contentLocation, "Must provide request location");
|
|
|
|
nsresult rv = CheckPolicy(&nsIContentPolicy::ShouldLoad, contentType,
|
|
|
|
contentLocation, requestingLocation,
|
2012-07-02 16:16:11 -07:00
|
|
|
requestingContext, mimeType, extra,
|
|
|
|
requestPrincipal, decision);
|
2007-03-22 10:30:00 -07:00
|
|
|
LOG_CHECK("ShouldLoad");
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsContentPolicy::ShouldProcess(uint32_t contentType,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIURI *contentLocation,
|
|
|
|
nsIURI *requestingLocation,
|
|
|
|
nsISupports *requestingContext,
|
|
|
|
const nsACString &mimeType,
|
|
|
|
nsISupports *extra,
|
2012-07-02 16:16:11 -07:00
|
|
|
nsIPrincipal *requestPrincipal,
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t *decision)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv = CheckPolicy(&nsIContentPolicy::ShouldProcess, contentType,
|
|
|
|
contentLocation, requestingLocation,
|
2012-07-02 16:16:11 -07:00
|
|
|
requestingContext, mimeType, extra,
|
|
|
|
requestPrincipal, decision);
|
2007-03-22 10:30:00 -07:00
|
|
|
LOG_CHECK("ShouldProcess");
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|