2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsDataDocumentContentPolicy.h"
|
2011-01-28 07:59:15 -08:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsScriptSecurityManager.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsDataDocumentContentPolicy, nsIContentPolicy)
|
|
|
|
|
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
|
|
|
|
HasFlags(nsIURI* aURI, PRUint32 aURIFlags)
|
|
|
|
{
|
|
|
|
bool hasFlags;
|
|
|
|
nsresult rv = NS_URIChainHasFlags(aURI, aURIFlags, &hasFlags);
|
|
|
|
return NS_SUCCEEDED(rv) && hasFlags;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDataDocumentContentPolicy::ShouldLoad(PRUint32 aContentType,
|
|
|
|
nsIURI *aContentLocation,
|
|
|
|
nsIURI *aRequestingLocation,
|
|
|
|
nsISupports *aRequestingContext,
|
|
|
|
const nsACString &aMimeGuess,
|
|
|
|
nsISupports *aExtra,
|
2012-07-02 16:16:11 -07:00
|
|
|
nsIPrincipal *aRequestPrincipal,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt16 *aDecision)
|
|
|
|
{
|
|
|
|
*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 {
|
|
|
|
nsCOMPtr<nsIDOMWindow> window = do_QueryInterface(aRequestingContext);
|
|
|
|
if (window) {
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc;
|
|
|
|
window->GetDocument(getter_AddRefs(domDoc));
|
|
|
|
doc = do_QueryInterface(domDoc);
|
|
|
|
}
|
|
|
|
}
|
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()) {
|
2008-10-04 13:00:09 -07:00
|
|
|
*aDecision = nsIContentPolicy::REJECT_TYPE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
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(
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr, NS_LITERAL_STRING("CheckSameOriginError"), principalURI,
|
2011-01-28 07:59:15 -08:00
|
|
|
aContentLocation);
|
|
|
|
}
|
|
|
|
}
|
2011-06-22 22:21:47 -07:00
|
|
|
} else if (aContentType == nsIContentPolicy::TYPE_IMAGE &&
|
|
|
|
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 ||
|
|
|
|
aContentType == nsIContentPolicy::TYPE_SCRIPT) {
|
2007-03-22 10:30:00 -07:00
|
|
|
*aDecision = nsIContentPolicy::REJECT_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDataDocumentContentPolicy::ShouldProcess(PRUint32 aContentType,
|
|
|
|
nsIURI *aContentLocation,
|
|
|
|
nsIURI *aRequestingLocation,
|
|
|
|
nsISupports *aRequestingContext,
|
|
|
|
const nsACString &aMimeGuess,
|
|
|
|
nsISupports *aExtra,
|
2012-07-02 16:16:11 -07:00
|
|
|
nsIPrincipal *aRequestPrincipal,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt16 *aDecision)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|