2015-05-03 19:32:37 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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/. */
|
2005-04-11 00:29:36 +00:00
|
|
|
|
2006-03-30 08:03:04 +00:00
|
|
|
/*
|
|
|
|
* Content policy implementation that prevents all loads of images,
|
|
|
|
* subframes, etc from documents loaded as data (eg documents loaded
|
|
|
|
* via XMLHttpRequest).
|
|
|
|
*/
|
|
|
|
|
2015-06-16 14:39:00 +00:00
|
|
|
#include "nsContentUtils.h"
|
2005-04-11 00:29:36 +00:00
|
|
|
#include "nsDataDocumentContentPolicy.h"
|
2011-01-28 15:59:15 +00:00
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 02:17:00 +00:00
|
|
|
#include "nsIProtocolHandler.h"
|
2011-01-28 15:59:15 +00:00
|
|
|
#include "nsScriptSecurityManager.h"
|
2005-04-11 00:29:36 +00:00
|
|
|
#include "nsIDocument.h"
|
2006-01-31 04:25:22 +00:00
|
|
|
#include "nsINode.h"
|
2005-04-11 00:29:36 +00:00
|
|
|
#include "nsIDOMWindow.h"
|
2015-07-22 16:03:07 +00:00
|
|
|
#include "nsIURI.h"
|
2005-04-11 00:29:36 +00:00
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsDataDocumentContentPolicy, nsIContentPolicy)
|
2005-04-11 00:29:36 +00:00
|
|
|
|
2011-11-07 21:45:42 +00:00
|
|
|
// Helper method for ShouldLoad()
|
|
|
|
// Checks a URI for the given flags. Returns true if the URI has the flags,
|
|
|
|
// and false if not (or if we weren't able to tell).
|
|
|
|
static bool
|
2012-08-22 15:56:38 +00:00
|
|
|
HasFlags(nsIURI* aURI, uint32_t aURIFlags)
|
2011-11-07 21:45:42 +00:00
|
|
|
{
|
|
|
|
bool hasFlags;
|
|
|
|
nsresult rv = NS_URIChainHasFlags(aURI, aURIFlags, &hasFlags);
|
|
|
|
return NS_SUCCEEDED(rv) && hasFlags;
|
|
|
|
}
|
|
|
|
|
2013-01-08 17:16:28 +00:00
|
|
|
// If you change DataDocumentContentPolicy, make sure to check that
|
|
|
|
// CHECK_PRINCIPAL_AND_DATA in nsContentPolicyUtils is still valid.
|
|
|
|
// nsContentPolicyUtils may not pass all the parameters to ShouldLoad.
|
2005-04-11 00:29:36 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsDataDocumentContentPolicy::ShouldLoad(uint32_t aContentType,
|
2005-04-11 00:29:36 +00:00
|
|
|
nsIURI *aContentLocation,
|
|
|
|
nsIURI *aRequestingLocation,
|
|
|
|
nsISupports *aRequestingContext,
|
|
|
|
const nsACString &aMimeGuess,
|
|
|
|
nsISupports *aExtra,
|
2012-07-02 23:16:11 +00:00
|
|
|
nsIPrincipal *aRequestPrincipal,
|
2012-08-22 15:56:38 +00:00
|
|
|
int16_t *aDecision)
|
2005-04-11 00:29:36 +00:00
|
|
|
{
|
2015-06-12 20:52:07 +00:00
|
|
|
MOZ_ASSERT(aContentType == nsContentUtils::InternalContentPolicyTypeToExternal(aContentType),
|
|
|
|
"We should only see external content policy types here.");
|
|
|
|
|
2005-04-11 00:29:36 +00:00
|
|
|
*aDecision = nsIContentPolicy::ACCEPT;
|
2006-01-31 04:25:22 +00:00
|
|
|
// Look for the document. In most cases, aRequestingContext is a node.
|
2006-01-31 01:47:30 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc;
|
2006-01-31 04:25:22 +00:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aRequestingContext);
|
|
|
|
if (node) {
|
2011-10-18 10:53:36 +00:00
|
|
|
doc = node->OwnerDoc();
|
2006-01-31 01:47:30 +00:00
|
|
|
} else {
|
2016-01-30 17:05:36 +00:00
|
|
|
if (nsCOMPtr<nsPIDOMWindowOuter> window = do_QueryInterface(aRequestingContext)) {
|
2014-01-15 14:26:51 +00:00
|
|
|
doc = window->GetDoc();
|
2005-04-11 00:29:36 +00:00
|
|
|
}
|
|
|
|
}
|
2008-10-04 20:00:09 +00:00
|
|
|
|
|
|
|
// DTDs are always OK to load
|
|
|
|
if (!doc || aContentType == nsIContentPolicy::TYPE_DTD) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-01-28 15:52:16 +00:00
|
|
|
// Nothing else is OK to load for data documents
|
|
|
|
if (doc->IsLoadedAsData()) {
|
2012-09-04 13:29:27 +00:00
|
|
|
// ...but let static (print/print preview) documents to load fonts.
|
|
|
|
if (!doc->IsStaticDocument() || aContentType != nsIContentPolicy::TYPE_FONT) {
|
|
|
|
*aDecision = nsIContentPolicy::REJECT_TYPE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-10-04 20:00:09 +00:00
|
|
|
}
|
|
|
|
|
2011-01-28 15:59:15 +00:00
|
|
|
if (doc->IsBeingUsedAsImage()) {
|
2011-11-07 21:45:42 +00:00
|
|
|
// We only allow SVG images to load content from URIs that are local and
|
|
|
|
// also satisfy one of the following conditions:
|
|
|
|
// - URI inherits security context, e.g. data URIs
|
|
|
|
// OR
|
2012-01-12 10:36:03 +00:00
|
|
|
// - URI loadable by subsumers, e.g. blob URIs
|
2011-11-07 21:45:42 +00:00
|
|
|
// Any URI that doesn't meet these requirements will be rejected below.
|
2015-08-16 20:37:56 +00:00
|
|
|
if (!(HasFlags(aContentLocation,
|
|
|
|
nsIProtocolHandler::URI_IS_LOCAL_RESOURCE) &&
|
|
|
|
(HasFlags(aContentLocation,
|
|
|
|
nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT) ||
|
|
|
|
HasFlags(aContentLocation,
|
|
|
|
nsIProtocolHandler::URI_LOADABLE_BY_SUBSUMERS)))) {
|
2011-01-28 15:59:15 +00:00
|
|
|
*aDecision = nsIContentPolicy::REJECT_TYPE;
|
|
|
|
|
2011-11-07 21:45:42 +00:00
|
|
|
// Report error, if we can.
|
2011-01-28 15:59:15 +00:00
|
|
|
if (node) {
|
|
|
|
nsIPrincipal* requestingPrincipal = node->NodePrincipal();
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<nsIURI> principalURI;
|
2011-11-07 21:45:42 +00:00
|
|
|
nsresult rv =
|
|
|
|
requestingPrincipal->GetURI(getter_AddRefs(principalURI));
|
2011-01-28 15:59:15 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && principalURI) {
|
|
|
|
nsScriptSecurityManager::ReportError(
|
2015-03-07 09:15:23 +00:00
|
|
|
nullptr, NS_LITERAL_STRING("ExternalDataError"), principalURI,
|
2011-01-28 15:59:15 +00:00
|
|
|
aContentLocation);
|
|
|
|
}
|
|
|
|
}
|
2014-08-19 21:12:34 +00:00
|
|
|
} else if ((aContentType == nsIContentPolicy::TYPE_IMAGE ||
|
|
|
|
aContentType == nsIContentPolicy::TYPE_IMAGESET) &&
|
2011-06-23 05:21:47 +00:00
|
|
|
doc->GetDocumentURI()) {
|
|
|
|
// Check for (& disallow) recursive image-loads
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isRecursiveLoad;
|
2011-11-07 21:45:42 +00:00
|
|
|
nsresult rv = aContentLocation->EqualsExceptRef(doc->GetDocumentURI(),
|
|
|
|
&isRecursiveLoad);
|
2011-06-23 05:21:47 +00:00
|
|
|
if (NS_FAILED(rv) || isRecursiveLoad) {
|
|
|
|
NS_WARNING("Refusing to recursively load image");
|
|
|
|
*aDecision = nsIContentPolicy::REJECT_TYPE;
|
|
|
|
}
|
2011-01-28 15:59:15 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-13 22:21:41 +00:00
|
|
|
// Allow all loads for non-resource documents
|
|
|
|
if (!doc->IsResourceDoc()) {
|
2008-10-04 20:00:09 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-13 22:21:41 +00:00
|
|
|
// For resource documents, blacklist some load types
|
2008-10-04 20:00:09 +00:00
|
|
|
if (aContentType == nsIContentPolicy::TYPE_OBJECT ||
|
|
|
|
aContentType == nsIContentPolicy::TYPE_DOCUMENT ||
|
|
|
|
aContentType == nsIContentPolicy::TYPE_SUBDOCUMENT ||
|
2013-09-11 21:11:49 +00:00
|
|
|
aContentType == nsIContentPolicy::TYPE_SCRIPT ||
|
2014-10-13 22:09:58 +00:00
|
|
|
aContentType == nsIContentPolicy::TYPE_XSLT ||
|
2015-06-02 19:42:19 +00:00
|
|
|
aContentType == nsIContentPolicy::TYPE_FETCH ||
|
|
|
|
aContentType == nsIContentPolicy::TYPE_WEB_MANIFEST) {
|
2006-01-31 01:47:30 +00:00
|
|
|
*aDecision = nsIContentPolicy::REJECT_TYPE;
|
|
|
|
}
|
|
|
|
|
2013-01-08 17:16:28 +00:00
|
|
|
// If you add more restrictions here, make sure to check that
|
|
|
|
// CHECK_PRINCIPAL_AND_DATA in nsContentPolicyUtils is still valid.
|
|
|
|
// nsContentPolicyUtils may not pass all the parameters to ShouldLoad
|
|
|
|
|
2005-04-11 00:29:36 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsDataDocumentContentPolicy::ShouldProcess(uint32_t aContentType,
|
2005-04-11 00:29:36 +00:00
|
|
|
nsIURI *aContentLocation,
|
|
|
|
nsIURI *aRequestingLocation,
|
|
|
|
nsISupports *aRequestingContext,
|
|
|
|
const nsACString &aMimeGuess,
|
|
|
|
nsISupports *aExtra,
|
2012-07-02 23:16:11 +00:00
|
|
|
nsIPrincipal *aRequestPrincipal,
|
2012-08-22 15:56:38 +00:00
|
|
|
int16_t *aDecision)
|
2005-04-11 00:29:36 +00:00
|
|
|
{
|
|
|
|
return ShouldLoad(aContentType, aContentLocation, aRequestingLocation,
|
2012-07-02 23:16:11 +00:00
|
|
|
aRequestingContext, aMimeGuess, aExtra, aRequestPrincipal,
|
|
|
|
aDecision);
|
2005-04-11 00:29:36 +00:00
|
|
|
}
|