2000-05-05 05:28:49 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00: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 05:28:49 +00:00
|
|
|
|
|
|
|
/*
|
2004-05-13 18:34:18 +00:00
|
|
|
* Utility routines for checking content load/process policy settings,
|
|
|
|
* and routines helpful for content policy implementors.
|
2007-08-08 01:16:09 +00:00
|
|
|
*
|
|
|
|
* XXXbz it would be nice if some of this stuff could be out-of-lined in
|
|
|
|
* nsContentUtils. That would work for almost all the callers...
|
2000-05-05 05:28:49 +00:00
|
|
|
*/
|
|
|
|
|
2004-05-13 18:34:18 +00:00
|
|
|
#ifndef __nsContentPolicyUtils_h__
|
|
|
|
#define __nsContentPolicyUtils_h__
|
|
|
|
|
2015-10-02 01:51:18 +00:00
|
|
|
#include "nsContentUtils.h"
|
2000-05-05 05:28:49 +00:00
|
|
|
#include "nsIContentPolicy.h"
|
2004-10-11 16:29:42 +00:00
|
|
|
#include "nsIContent.h"
|
2007-08-08 01:16:09 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2013-08-24 02:42:40 +00:00
|
|
|
#include "nsIURI.h"
|
2013-10-21 21:23:33 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2000-05-05 05:28:49 +00:00
|
|
|
|
2004-05-13 18:34:18 +00:00
|
|
|
//XXXtw sadly, this makes consumers of nsContentPolicyUtils depend on widget
|
|
|
|
#include "nsIDocument.h"
|
2005-11-28 23:56:44 +00:00
|
|
|
#include "nsPIDOMWindow.h"
|
2000-05-05 05:28:49 +00:00
|
|
|
|
2006-03-22 18:36:36 +00:00
|
|
|
class nsACString;
|
2013-10-21 21:23:33 +00:00
|
|
|
class nsIPrincipal;
|
2006-03-22 18:36:36 +00:00
|
|
|
|
2000-09-13 23:57:52 +00:00
|
|
|
#define NS_CONTENTPOLICY_CONTRACTID "@mozilla.org/layout/content-policy;1"
|
2000-05-05 05:28:49 +00:00
|
|
|
#define NS_CONTENTPOLICY_CATEGORY "content-policy"
|
2015-02-20 16:54:12 +00:00
|
|
|
#define NS_SIMPLECONTENTPOLICY_CATEGORY "simple-content-policy"
|
2004-05-13 18:34:18 +00:00
|
|
|
#define NS_CONTENTPOLICY_CID \
|
|
|
|
{0x0e3afd3d, 0xeb60, 0x4c2b, \
|
|
|
|
{ 0x96, 0x3b, 0x56, 0xd7, 0xc4, 0x39, 0xf1, 0x24 }}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates to true if val is ACCEPT.
|
|
|
|
*
|
|
|
|
* @param val the status returned from shouldProcess/shouldLoad
|
|
|
|
*/
|
|
|
|
#define NS_CP_ACCEPTED(val) ((val) == nsIContentPolicy::ACCEPT)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates to true if val is a REJECT_* status
|
|
|
|
*
|
|
|
|
* @param val the status returned from shouldProcess/shouldLoad
|
|
|
|
*/
|
|
|
|
#define NS_CP_REJECTED(val) ((val) != nsIContentPolicy::ACCEPT)
|
|
|
|
|
|
|
|
// Offer convenient translations of constants -> const char*
|
|
|
|
|
|
|
|
// convenience macro to reduce some repetative typing...
|
|
|
|
// name is the name of a constant from this interface
|
|
|
|
#define CASE_RETURN(name) \
|
|
|
|
case nsIContentPolicy:: name : \
|
|
|
|
return #name
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a string corresponding to the name of the response constant, or
|
|
|
|
* "<Unknown Response>" if an unknown response value is given.
|
|
|
|
*
|
|
|
|
* The return value is static and must not be freed.
|
|
|
|
*
|
|
|
|
* @param response the response code
|
|
|
|
* @return the name of the given response code
|
|
|
|
*/
|
|
|
|
inline const char *
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_CP_ResponseName(int16_t response)
|
2004-05-13 18:34:18 +00:00
|
|
|
{
|
|
|
|
switch (response) {
|
|
|
|
CASE_RETURN( REJECT_REQUEST );
|
|
|
|
CASE_RETURN( REJECT_TYPE );
|
|
|
|
CASE_RETURN( REJECT_SERVER );
|
|
|
|
CASE_RETURN( REJECT_OTHER );
|
|
|
|
CASE_RETURN( ACCEPT );
|
|
|
|
default:
|
|
|
|
return "<Unknown Response>";
|
|
|
|
}
|
|
|
|
}
|
2000-05-05 05:28:49 +00:00
|
|
|
|
2004-05-13 18:34:18 +00:00
|
|
|
/**
|
|
|
|
* Returns a string corresponding to the name of the content type constant, or
|
|
|
|
* "<Unknown Type>" if an unknown content type value is given.
|
|
|
|
*
|
|
|
|
* The return value is static and must not be freed.
|
|
|
|
*
|
|
|
|
* @param contentType the content type code
|
|
|
|
* @return the name of the given content type code
|
|
|
|
*/
|
|
|
|
inline const char *
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_CP_ContentTypeName(uint32_t contentType)
|
2004-05-13 18:34:18 +00:00
|
|
|
{
|
|
|
|
switch (contentType) {
|
2015-09-20 21:55:44 +00:00
|
|
|
CASE_RETURN( TYPE_OTHER );
|
|
|
|
CASE_RETURN( TYPE_SCRIPT );
|
|
|
|
CASE_RETURN( TYPE_IMAGE );
|
|
|
|
CASE_RETURN( TYPE_STYLESHEET );
|
|
|
|
CASE_RETURN( TYPE_OBJECT );
|
|
|
|
CASE_RETURN( TYPE_DOCUMENT );
|
|
|
|
CASE_RETURN( TYPE_SUBDOCUMENT );
|
|
|
|
CASE_RETURN( TYPE_REFRESH );
|
|
|
|
CASE_RETURN( TYPE_XBL );
|
|
|
|
CASE_RETURN( TYPE_PING );
|
|
|
|
CASE_RETURN( TYPE_XMLHTTPREQUEST );
|
|
|
|
CASE_RETURN( TYPE_OBJECT_SUBREQUEST );
|
|
|
|
CASE_RETURN( TYPE_DTD );
|
|
|
|
CASE_RETURN( TYPE_FONT );
|
|
|
|
CASE_RETURN( TYPE_MEDIA );
|
|
|
|
CASE_RETURN( TYPE_WEBSOCKET );
|
|
|
|
CASE_RETURN( TYPE_CSP_REPORT );
|
|
|
|
CASE_RETURN( TYPE_XSLT );
|
|
|
|
CASE_RETURN( TYPE_BEACON );
|
|
|
|
CASE_RETURN( TYPE_FETCH );
|
|
|
|
CASE_RETURN( TYPE_IMAGESET );
|
|
|
|
CASE_RETURN( TYPE_WEB_MANIFEST );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_SCRIPT );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_WORKER );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_SHARED_WORKER );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_EMBED );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_OBJECT );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_FRAME );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_IFRAME );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_AUDIO );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_VIDEO );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_TRACK );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_XMLHTTPREQUEST );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_EVENTSOURCE );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_SERVICE_WORKER );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_SCRIPT_PRELOAD );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_IMAGE );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_IMAGE_PRELOAD );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_STYLESHEET );
|
|
|
|
CASE_RETURN( TYPE_INTERNAL_STYLESHEET_PRELOAD );
|
2007-06-17 13:50:50 +00:00
|
|
|
default:
|
2004-05-13 18:34:18 +00:00
|
|
|
return "<Unknown Type>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef CASE_RETURN
|
|
|
|
|
|
|
|
/* Passes on parameters from its "caller"'s context. */
|
|
|
|
#define CHECK_CONTENT_POLICY(action) \
|
2005-08-19 15:00:01 +00:00
|
|
|
PR_BEGIN_MACRO \
|
2001-05-21 22:40:10 +00:00
|
|
|
nsCOMPtr<nsIContentPolicy> policy = \
|
|
|
|
do_GetService(NS_CONTENTPOLICY_CONTRACTID); \
|
|
|
|
if (!policy) \
|
|
|
|
return NS_ERROR_FAILURE; \
|
2000-05-05 05:28:49 +00:00
|
|
|
\
|
2004-05-13 18:34:18 +00:00
|
|
|
return policy-> action (contentType, contentLocation, requestOrigin, \
|
2012-07-02 23:16:11 +00:00
|
|
|
context, mimeType, extra, originPrincipal, \
|
|
|
|
decision); \
|
2005-08-19 15:00:01 +00:00
|
|
|
PR_END_MACRO
|
|
|
|
|
|
|
|
/* Passes on parameters from its "caller"'s context. */
|
|
|
|
#define CHECK_CONTENT_POLICY_WITH_SERVICE(action, _policy) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
return _policy-> action (contentType, contentLocation, requestOrigin, \
|
2012-07-02 23:16:11 +00:00
|
|
|
context, mimeType, extra, originPrincipal, \
|
|
|
|
decision); \
|
2005-08-19 15:00:01 +00:00
|
|
|
PR_END_MACRO
|
2000-05-05 05:28:49 +00:00
|
|
|
|
2004-05-13 18:34:18 +00:00
|
|
|
/**
|
2007-08-08 01:16:09 +00:00
|
|
|
* Check whether we can short-circuit this check and bail out. If not, get the
|
|
|
|
* origin URI to use.
|
|
|
|
*
|
|
|
|
* Note: requestOrigin is scoped outside the PR_BEGIN_MACRO/PR_END_MACRO on
|
|
|
|
* purpose */
|
2013-01-08 17:16:28 +00:00
|
|
|
#define CHECK_PRINCIPAL_AND_DATA(action) \
|
2007-08-08 01:16:09 +00:00
|
|
|
nsCOMPtr<nsIURI> requestOrigin; \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if (originPrincipal) { \
|
|
|
|
nsCOMPtr<nsIScriptSecurityManager> secMan = aSecMan; \
|
|
|
|
if (!secMan) { \
|
|
|
|
secMan = do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID); \
|
|
|
|
} \
|
|
|
|
if (secMan) { \
|
2013-01-08 17:16:28 +00:00
|
|
|
bool isSystem; \
|
2007-08-08 01:16:09 +00:00
|
|
|
nsresult rv = secMan->IsSystemPrincipal(originPrincipal, \
|
|
|
|
&isSystem); \
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv); \
|
|
|
|
if (isSystem) { \
|
|
|
|
*decision = nsIContentPolicy::ACCEPT; \
|
2013-01-08 17:16:28 +00:00
|
|
|
nsCOMPtr<nsINode> n = do_QueryInterface(context); \
|
|
|
|
if (!n) { \
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> win = do_QueryInterface(context);\
|
2013-01-08 17:16:28 +00:00
|
|
|
n = win ? win->GetExtantDoc() : nullptr; \
|
|
|
|
} \
|
|
|
|
if (n) { \
|
|
|
|
nsIDocument* d = n->OwnerDoc(); \
|
|
|
|
if (d->IsLoadedAsData() || d->IsBeingUsedAsImage() || \
|
|
|
|
d->IsResourceDoc()) { \
|
|
|
|
nsCOMPtr<nsIContentPolicy> dataPolicy = \
|
|
|
|
do_GetService( \
|
|
|
|
"@mozilla.org/data-document-content-policy;1"); \
|
|
|
|
if (dataPolicy) { \
|
2015-10-02 01:51:18 +00:00
|
|
|
nsContentPolicyType externalType = \
|
|
|
|
nsContentUtils::InternalContentPolicyTypeToExternal(contentType);\
|
|
|
|
dataPolicy-> action (externalType, contentLocation, \
|
2013-01-08 17:16:28 +00:00
|
|
|
requestOrigin, context, \
|
|
|
|
mimeType, extra, \
|
|
|
|
originPrincipal, decision); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
2007-08-08 01:16:09 +00:00
|
|
|
return NS_OK; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
nsresult rv = originPrincipal->GetURI(getter_AddRefs(requestOrigin)); \
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv); \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for calling ShouldLoad on the content policy service. Parameters are
|
|
|
|
* the same as nsIContentPolicy::shouldLoad, except for the originPrincipal
|
|
|
|
* parameter, which should be non-null if possible, and the last two
|
|
|
|
* parameters, which can be used to pass in pointer to some useful services if
|
2007-08-10 05:35:08 +00:00
|
|
|
* the caller already has them. The origin URI to pass to shouldLoad will be
|
|
|
|
* the URI of originPrincipal, unless originPrincipal is null (in which case a
|
|
|
|
* null origin URI will be passed).
|
2004-05-13 18:34:18 +00:00
|
|
|
*/
|
2000-05-05 05:28:49 +00:00
|
|
|
inline nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_CheckContentLoadPolicy(uint32_t contentType,
|
2004-05-13 18:34:18 +00:00
|
|
|
nsIURI *contentLocation,
|
2007-08-08 01:16:09 +00:00
|
|
|
nsIPrincipal *originPrincipal,
|
2004-07-27 17:15:53 +00:00
|
|
|
nsISupports *context,
|
2004-05-13 18:34:18 +00:00
|
|
|
const nsACString &mimeType,
|
|
|
|
nsISupports *extra,
|
2012-08-22 15:56:38 +00:00
|
|
|
int16_t *decision,
|
2012-07-30 14:20:58 +00:00
|
|
|
nsIContentPolicy *policyService = nullptr,
|
|
|
|
nsIScriptSecurityManager* aSecMan = nullptr)
|
2000-05-05 05:28:49 +00:00
|
|
|
{
|
2013-01-08 17:16:28 +00:00
|
|
|
CHECK_PRINCIPAL_AND_DATA(ShouldLoad);
|
2005-08-19 15:00:01 +00:00
|
|
|
if (policyService) {
|
|
|
|
CHECK_CONTENT_POLICY_WITH_SERVICE(ShouldLoad, policyService);
|
|
|
|
}
|
2004-05-13 18:34:18 +00:00
|
|
|
CHECK_CONTENT_POLICY(ShouldLoad);
|
2000-05-05 05:28:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-13 18:34:18 +00:00
|
|
|
/**
|
2007-08-08 01:16:09 +00:00
|
|
|
* Alias for calling ShouldProcess on the content policy service. Parameters
|
|
|
|
* are the same as nsIContentPolicy::shouldLoad, except for the originPrincipal
|
|
|
|
* parameter, which should be non-null if possible, and the last two
|
|
|
|
* parameters, which can be used to pass in pointer to some useful services if
|
2007-08-10 05:35:08 +00:00
|
|
|
* the caller already has them. The origin URI to pass to shouldLoad will be
|
|
|
|
* the URI of originPrincipal, unless originPrincipal is null (in which case a
|
|
|
|
* null origin URI will be passed).
|
2004-05-13 18:34:18 +00:00
|
|
|
*/
|
2000-05-05 05:28:49 +00:00
|
|
|
inline nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_CheckContentProcessPolicy(uint32_t contentType,
|
2004-05-13 18:34:18 +00:00
|
|
|
nsIURI *contentLocation,
|
2007-08-08 01:16:09 +00:00
|
|
|
nsIPrincipal *originPrincipal,
|
2004-07-27 17:15:53 +00:00
|
|
|
nsISupports *context,
|
2004-05-13 18:34:18 +00:00
|
|
|
const nsACString &mimeType,
|
|
|
|
nsISupports *extra,
|
2012-08-22 15:56:38 +00:00
|
|
|
int16_t *decision,
|
2012-07-30 14:20:58 +00:00
|
|
|
nsIContentPolicy *policyService = nullptr,
|
|
|
|
nsIScriptSecurityManager* aSecMan = nullptr)
|
2000-05-05 05:28:49 +00:00
|
|
|
{
|
2013-01-08 17:16:28 +00:00
|
|
|
CHECK_PRINCIPAL_AND_DATA(ShouldProcess);
|
2005-08-19 15:00:01 +00:00
|
|
|
if (policyService) {
|
|
|
|
CHECK_CONTENT_POLICY_WITH_SERVICE(ShouldProcess, policyService);
|
|
|
|
}
|
2004-05-13 18:34:18 +00:00
|
|
|
CHECK_CONTENT_POLICY(ShouldProcess);
|
2000-05-05 05:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef CHECK_CONTENT_POLICY
|
2005-08-19 15:00:01 +00:00
|
|
|
#undef CHECK_CONTENT_POLICY_WITH_SERVICE
|
2000-05-05 05:28:49 +00:00
|
|
|
|
2004-05-13 18:34:18 +00:00
|
|
|
/**
|
2004-07-27 17:15:53 +00:00
|
|
|
* Helper function to get an nsIDocShell given a context.
|
|
|
|
* If the context is a document or window, the corresponding docshell will be
|
2004-05-13 18:34:18 +00:00
|
|
|
* returned.
|
2004-07-27 17:15:53 +00:00
|
|
|
* If the context is a non-document DOM node, the docshell of its ownerDocument
|
|
|
|
* will be returned.
|
2004-05-13 18:34:18 +00:00
|
|
|
*
|
2004-07-27 17:15:53 +00:00
|
|
|
* @param aContext the context to find a docshell for (can be null)
|
2009-02-18 04:32:57 +00:00
|
|
|
*
|
2012-07-30 14:20:58 +00:00
|
|
|
* @return a WEAK pointer to the docshell, or nullptr if it could
|
2004-05-13 18:34:18 +00:00
|
|
|
* not be obtained
|
2009-02-18 04:32:57 +00:00
|
|
|
*
|
|
|
|
* @note As of this writing, calls to nsIContentPolicy::Should{Load,Process}
|
|
|
|
* for TYPE_DOCUMENT and TYPE_SUBDOCUMENT pass in an aContext that either
|
|
|
|
* points to the frameElement of the window the load is happening in
|
|
|
|
* (in which case NS_CP_GetDocShellFromContext will return the parent of the
|
|
|
|
* docshell the load is happening in), or points to the window the load is
|
|
|
|
* happening in (in which case NS_CP_GetDocShellFromContext will return
|
|
|
|
* the docshell the load is happening in). It's up to callers to QI aContext
|
|
|
|
* and handle things accordingly if they want the docshell the load is
|
|
|
|
* happening in. These are somewhat odd semantics, and bug 466687 has been
|
|
|
|
* filed to consider improving them.
|
2004-05-13 18:34:18 +00:00
|
|
|
*/
|
2005-09-22 20:24:31 +00:00
|
|
|
inline nsIDocShell*
|
2004-07-27 17:15:53 +00:00
|
|
|
NS_CP_GetDocShellFromContext(nsISupports *aContext)
|
2004-05-13 18:34:18 +00:00
|
|
|
{
|
2004-07-27 17:15:53 +00:00
|
|
|
if (!aContext) {
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2004-05-13 18:34:18 +00:00
|
|
|
}
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> window = do_QueryInterface(aContext);
|
2004-05-13 18:34:18 +00:00
|
|
|
|
2005-11-28 23:56:44 +00:00
|
|
|
if (!window) {
|
2004-07-27 17:15:53 +00:00
|
|
|
// our context might be a document (which also QIs to nsIDOMNode), so
|
|
|
|
// try that first
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aContext);
|
|
|
|
if (!doc) {
|
|
|
|
// we were not a document after all, get our ownerDocument,
|
|
|
|
// hopefully
|
2004-10-11 16:29:42 +00:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aContext);
|
|
|
|
if (content) {
|
2011-10-18 10:53:36 +00:00
|
|
|
doc = content->OwnerDoc();
|
2004-07-27 17:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc) {
|
2008-11-27 04:10:28 +00:00
|
|
|
if (doc->GetDisplayDocument()) {
|
|
|
|
doc = doc->GetDisplayDocument();
|
|
|
|
}
|
|
|
|
|
2005-11-28 23:56:44 +00:00
|
|
|
window = doc->GetWindow();
|
2004-07-27 17:15:53 +00:00
|
|
|
}
|
2004-05-13 18:34:18 +00:00
|
|
|
}
|
|
|
|
|
2005-11-28 23:56:44 +00:00
|
|
|
if (!window) {
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2004-05-13 18:34:18 +00:00
|
|
|
}
|
|
|
|
|
2005-11-28 23:56:44 +00:00
|
|
|
return window->GetDocShell();
|
2004-05-13 18:34:18 +00:00
|
|
|
}
|
|
|
|
|
2000-05-05 05:28:49 +00:00
|
|
|
#endif /* __nsContentPolicyUtils_h__ */
|