2015-05-03 12:32:37 -07: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 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
|
|
|
|
|
|
|
/*
|
|
|
|
* Content policy implementation that prevents all loads of images,
|
|
|
|
* subframes, etc from documents loaded as data (eg documents loaded
|
|
|
|
* via XMLHttpRequest).
|
|
|
|
*/
|
|
|
|
|
2015-06-16 07:39:00 -07:00
|
|
|
#include "nsContentUtils.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsDataDocumentContentPolicy.h"
|
2011-01-28 07:59:15 -08:00
|
|
|
#include "nsNetUtil.h"
|
2015-07-06 19:17:00 -07:00
|
|
|
#include "nsIProtocolHandler.h"
|
2011-01-28 07:59:15 -08:00
|
|
|
#include "nsScriptSecurityManager.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2015-07-22 09:03:07 -07:00
|
|
|
#include "nsIURI.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsDataDocumentContentPolicy, nsIContentPolicy)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-11-07 13:45:42 -08: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 08:56:38 -07:00
|
|
|
HasFlags(nsIURI* aURI, uint32_t aURIFlags)
|
2011-11-07 13:45:42 -08:00
|
|
|
{
|
|
|
|
bool hasFlags;
|
|
|
|
nsresult rv = NS_URIChainHasFlags(aURI, aURIFlags, &hasFlags);
|
|
|
|
return NS_SUCCEEDED(rv) && hasFlags;
|
|
|
|
}
|
|
|
|
|
2013-01-08 09:16:28 -08: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.
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDataDocumentContentPolicy::ShouldLoad(uint32_t aContentType,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIURI *aContentLocation,
|
|
|
|
nsIURI *aRequestingLocation,
|
|
|
|
nsISupports *aRequestingContext,
|
|
|
|
const nsACString &aMimeGuess,
|
|
|
|
nsISupports *aExtra,
|
2012-07-02 16:16:11 -07:00
|
|
|
nsIPrincipal *aRequestPrincipal,
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t *aDecision)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2015-06-12 13:52:07 -07:00
|
|
|
MOZ_ASSERT(aContentType == nsContentUtils::InternalContentPolicyTypeToExternal(aContentType),
|
|
|
|
"We should only see external content policy types here.");
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
*aDecision = nsIContentPolicy::ACCEPT;
|
|
|
|
// Look for the document. In most cases, aRequestingContext is a node.
|
|
|
|
nsCOMPtr<nsIDocument> doc;
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aRequestingContext);
|
|
|
|
if (node) {
|
2011-10-18 03:53:36 -07:00
|
|
|
doc = node->OwnerDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2014-01-15 06:26:51 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aRequestingContext);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (window) {
|
2014-01-15 06:26:51 -08:00
|
|
|
doc = window->GetDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2008-10-04 13:00:09 -07:00
|
|
|
|
|
|
|
// DTDs are always OK to load
|
|
|
|
if (!doc || aContentType == nsIContentPolicy::TYPE_DTD) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-01-28 07:52:16 -08:00
|
|
|
// Nothing else is OK to load for data documents
|
|
|
|
if (doc->IsLoadedAsData()) {
|
2012-09-04 06:29:27 -07: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 13:00:09 -07:00
|
|
|
}
|
|
|
|
|
2011-01-28 07:59:15 -08:00
|
|
|
if (doc->IsBeingUsedAsImage()) {
|
2011-11-07 13:45:42 -08: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 02:36:03 -08:00
|
|
|
// - URI loadable by subsumers, e.g. blob URIs
|
2011-11-07 13:45:42 -08:00
|
|
|
// Any URI that doesn't meet these requirements will be rejected below.
|
2015-08-16 13:37:56 -07: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 07:59:15 -08:00
|
|
|
*aDecision = nsIContentPolicy::REJECT_TYPE;
|
|
|
|
|
2011-11-07 13:45:42 -08:00
|
|
|
// Report error, if we can.
|
2011-01-28 07:59:15 -08:00
|
|
|
if (node) {
|
|
|
|
nsIPrincipal* requestingPrincipal = node->NodePrincipal();
|
|
|
|
nsRefPtr<nsIURI> principalURI;
|
2011-11-07 13:45:42 -08:00
|
|
|
nsresult rv =
|
|
|
|
requestingPrincipal->GetURI(getter_AddRefs(principalURI));
|
2011-01-28 07:59:15 -08:00
|
|
|
if (NS_SUCCEEDED(rv) && principalURI) {
|
|
|
|
nsScriptSecurityManager::ReportError(
|
2015-03-07 01:15:23 -08:00
|
|
|
nullptr, NS_LITERAL_STRING("ExternalDataError"), principalURI,
|
2011-01-28 07:59:15 -08:00
|
|
|
aContentLocation);
|
|
|
|
}
|
|
|
|
}
|
2014-08-19 14:12:34 -07:00
|
|
|
} else if ((aContentType == nsIContentPolicy::TYPE_IMAGE ||
|
|
|
|
aContentType == nsIContentPolicy::TYPE_IMAGESET) &&
|
2011-06-22 22:21:47 -07:00
|
|
|
doc->GetDocumentURI()) {
|
|
|
|
// Check for (& disallow) recursive image-loads
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isRecursiveLoad;
|
2011-11-07 13:45:42 -08:00
|
|
|
nsresult rv = aContentLocation->EqualsExceptRef(doc->GetDocumentURI(),
|
|
|
|
&isRecursiveLoad);
|
2011-06-22 22:21:47 -07:00
|
|
|
if (NS_FAILED(rv) || isRecursiveLoad) {
|
|
|
|
NS_WARNING("Refusing to recursively load image");
|
|
|
|
*aDecision = nsIContentPolicy::REJECT_TYPE;
|
|
|
|
}
|
2011-01-28 07:59:15 -08:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-13 14:21:41 -08:00
|
|
|
// Allow all loads for non-resource documents
|
|
|
|
if (!doc->IsResourceDoc()) {
|
2008-10-04 13:00:09 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-13 14:21:41 -08:00
|
|
|
// For resource documents, blacklist some load types
|
2008-10-04 13:00:09 -07:00
|
|
|
if (aContentType == nsIContentPolicy::TYPE_OBJECT ||
|
|
|
|
aContentType == nsIContentPolicy::TYPE_DOCUMENT ||
|
|
|
|
aContentType == nsIContentPolicy::TYPE_SUBDOCUMENT ||
|
2013-09-11 14:11:49 -07:00
|
|
|
aContentType == nsIContentPolicy::TYPE_SCRIPT ||
|
2014-10-13 15:09:58 -07:00
|
|
|
aContentType == nsIContentPolicy::TYPE_XSLT ||
|
2015-06-02 12:42:19 -07:00
|
|
|
aContentType == nsIContentPolicy::TYPE_FETCH ||
|
|
|
|
aContentType == nsIContentPolicy::TYPE_WEB_MANIFEST) {
|
2007-03-22 10:30:00 -07:00
|
|
|
*aDecision = nsIContentPolicy::REJECT_TYPE;
|
|
|
|
}
|
|
|
|
|
2013-01-08 09:16:28 -08: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
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDataDocumentContentPolicy::ShouldProcess(uint32_t aContentType,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIURI *aContentLocation,
|
|
|
|
nsIURI *aRequestingLocation,
|
|
|
|
nsISupports *aRequestingContext,
|
|
|
|
const nsACString &aMimeGuess,
|
|
|
|
nsISupports *aExtra,
|
2012-07-02 16:16:11 -07:00
|
|
|
nsIPrincipal *aRequestPrincipal,
|
2012-08-22 08:56:38 -07:00
|
|
|
int16_t *aDecision)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return ShouldLoad(aContentType, aContentLocation, aRequestingLocation,
|
2012-07-02 16:16:11 -07:00
|
|
|
aRequestingContext, aMimeGuess, aExtra, aRequestPrincipal,
|
|
|
|
aDecision);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|