1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * Content policy implementation that prevents all loads of images,
9 * subframes, etc from documents loaded as data (eg documents loaded
10 * via XMLHttpRequest).
13 #include "nsContentPolicyUtils.h"
14 #include "nsContentUtils.h"
15 #include "nsDataDocumentContentPolicy.h"
16 #include "nsNetUtil.h"
17 #include "nsIProtocolHandler.h"
18 #include "nsScriptSecurityManager.h"
19 #include "mozilla/dom/Document.h"
20 #include "mozilla/ScopeExit.h"
24 using namespace mozilla
;
26 NS_IMPL_ISUPPORTS(nsDataDocumentContentPolicy
, nsIContentPolicy
)
28 // Helper method for ShouldLoad()
29 // Checks a URI for the given flags. Returns true if the URI has the flags,
30 // and false if not (or if we weren't able to tell).
31 static bool HasFlags(nsIURI
* aURI
, uint32_t aURIFlags
) {
33 nsresult rv
= NS_URIChainHasFlags(aURI
, aURIFlags
, &hasFlags
);
34 return NS_SUCCEEDED(rv
) && hasFlags
;
37 // If you change DataDocumentContentPolicy, make sure to check that
38 // CHECK_PRINCIPAL_AND_DATA in nsContentPolicyUtils is still valid.
39 // nsContentPolicyUtils may not pass all the parameters to ShouldLoad.
41 nsDataDocumentContentPolicy::ShouldLoad(nsIURI
* aContentLocation
,
42 nsILoadInfo
* aLoadInfo
,
44 auto setBlockingReason
= mozilla::MakeScopeExit([&]() {
45 if (NS_CP_REJECTED(*aDecision
)) {
46 NS_SetRequestBlockingReason(
47 aLoadInfo
, nsILoadInfo::BLOCKING_REASON_CONTENT_POLICY_DATA_DOCUMENT
);
51 ExtContentPolicyType contentType
= aLoadInfo
->GetExternalContentPolicyType();
52 nsCOMPtr
<nsISupports
> requestingContext
= aLoadInfo
->GetLoadingContext();
54 *aDecision
= nsIContentPolicy::ACCEPT
;
55 // Look for the document. In most cases, requestingContext is a node.
56 nsCOMPtr
<mozilla::dom::Document
> doc
;
57 nsCOMPtr
<nsINode
> node
= do_QueryInterface(requestingContext
);
59 doc
= node
->OwnerDoc();
61 if (nsCOMPtr
<nsPIDOMWindowOuter
> window
=
62 do_QueryInterface(requestingContext
)) {
63 doc
= window
->GetDoc();
67 // DTDs are always OK to load
68 if (!doc
|| contentType
== ExtContentPolicy::TYPE_DTD
) {
72 if (doc
->IsLoadedAsData()) {
74 if (!doc
->IsStaticDocument()) {
75 // If not a print/print preview doc, then nothing else is allowed for
79 // Let static (print/print preview) documents to load fonts and
81 switch (contentType
) {
82 case ExtContentPolicy::TYPE_IMAGE
:
83 case ExtContentPolicy::TYPE_IMAGESET
:
84 case ExtContentPolicy::TYPE_FONT
:
85 case ExtContentPolicy::TYPE_UA_FONT
:
86 // This one is a bit sketchy, but nsObjectLoadingContent takes care of
87 // only getting here if it is an image.
88 case ExtContentPolicy::TYPE_OBJECT
:
96 *aDecision
= nsIContentPolicy::REJECT_TYPE
;
101 mozilla::dom::Document
* docToCheckForImage
= doc
->GetDisplayDocument();
102 if (!docToCheckForImage
) {
103 docToCheckForImage
= doc
;
106 if (docToCheckForImage
->IsBeingUsedAsImage()) {
107 // We only allow SVG images to load content from URIs that are local and
108 // also satisfy one of the following conditions:
109 // - URI inherits security context, e.g. data URIs
111 // - URI loadable by subsumers, e.g. blob URIs
112 // Any URI that doesn't meet these requirements will be rejected below.
113 if (!(HasFlags(aContentLocation
,
114 nsIProtocolHandler::URI_IS_LOCAL_RESOURCE
) &&
115 (HasFlags(aContentLocation
,
116 nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT
) ||
117 HasFlags(aContentLocation
,
118 nsIProtocolHandler::URI_LOADABLE_BY_SUBSUMERS
)))) {
119 *aDecision
= nsIContentPolicy::REJECT_TYPE
;
121 // Report error, if we can.
123 nsIPrincipal
* requestingPrincipal
= node
->NodePrincipal();
124 nsAutoCString sourceSpec
;
125 requestingPrincipal
->GetAsciiSpec(sourceSpec
);
126 nsAutoCString targetSpec
;
127 aContentLocation
->GetAsciiSpec(targetSpec
);
128 nsScriptSecurityManager::ReportError(
129 "ExternalDataError", sourceSpec
, targetSpec
,
130 requestingPrincipal
->OriginAttributesRef().IsPrivateBrowsing());
132 } else if ((contentType
== ExtContentPolicy::TYPE_IMAGE
||
133 contentType
== ExtContentPolicy::TYPE_IMAGESET
) &&
134 doc
->GetDocumentURI()) {
135 // Check for (& disallow) recursive image-loads
136 bool isRecursiveLoad
;
137 nsresult rv
= aContentLocation
->EqualsExceptRef(doc
->GetDocumentURI(),
139 if (NS_FAILED(rv
) || isRecursiveLoad
) {
140 NS_WARNING("Refusing to recursively load image");
141 *aDecision
= nsIContentPolicy::REJECT_TYPE
;
147 // Allow all loads for non-resource documents
148 if (!doc
->IsResourceDoc()) {
152 // For resource documents, blacklist some load types
153 if (contentType
== ExtContentPolicy::TYPE_OBJECT
||
154 contentType
== ExtContentPolicy::TYPE_DOCUMENT
||
155 contentType
== ExtContentPolicy::TYPE_SUBDOCUMENT
||
156 contentType
== ExtContentPolicy::TYPE_SCRIPT
||
157 contentType
== ExtContentPolicy::TYPE_XSLT
||
158 contentType
== ExtContentPolicy::TYPE_FETCH
||
159 contentType
== ExtContentPolicy::TYPE_WEB_MANIFEST
) {
160 *aDecision
= nsIContentPolicy::REJECT_TYPE
;
163 // If you add more restrictions here, make sure to check that
164 // CHECK_PRINCIPAL_AND_DATA in nsContentPolicyUtils is still valid.
165 // nsContentPolicyUtils may not pass all the parameters to ShouldLoad
171 nsDataDocumentContentPolicy::ShouldProcess(nsIURI
* aContentLocation
,
172 nsILoadInfo
* aLoadInfo
,
173 int16_t* aDecision
) {
174 return ShouldLoad(aContentLocation
, aLoadInfo
, aDecision
);