1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 // vim: ft=cpp tw=80 sw=2 et ts=8
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 * Implementation of the "@mozilla.org/layout/content-policy;1" contract.
11 #include "mozilla/Logging.h"
13 #include "nsISupports.h"
15 #include "nsContentPolicyUtils.h"
16 #include "mozilla/dom/nsCSPService.h"
17 #include "nsContentPolicy.h"
19 #include "nsIBrowserChild.h"
20 #include "nsIContent.h"
21 #include "nsIImageLoadingContent.h"
22 #include "nsCOMArray.h"
23 #include "nsContentUtils.h"
24 #include "mozilla/dom/nsMixedContentBlocker.h"
25 #include "nsIContentSecurityPolicy.h"
26 #include "mozilla/TaskCategory.h"
30 using mozilla::LogLevel
;
32 NS_IMPL_ISUPPORTS(nsContentPolicy
, nsIContentPolicy
)
34 static mozilla::LazyLogModule
gConPolLog("nsContentPolicy");
36 nsresult
NS_NewContentPolicy(nsIContentPolicy
** aResult
) {
37 *aResult
= new nsContentPolicy
;
42 nsContentPolicy::nsContentPolicy() : mPolicies(NS_CONTENTPOLICY_CATEGORY
) {}
44 nsContentPolicy::~nsContentPolicy() = default;
47 # define WARN_IF_URI_UNINITIALIZED(uri, name) \
51 (uri)->GetAsciiSpec(spec); \
52 if (spec.IsEmpty()) { \
53 NS_WARNING(name " is uninitialized, fix caller"); \
58 #else // ! defined(DEBUG)
60 # define WARN_IF_URI_UNINITIALIZED(uri, name)
62 #endif // defined(DEBUG)
64 inline nsresult
nsContentPolicy::CheckPolicy(CPMethod policyMethod
,
65 nsIURI
* contentLocation
,
66 nsILoadInfo
* loadInfo
,
67 const nsACString
& mimeType
,
69 nsCOMPtr
<nsISupports
> requestingContext
= loadInfo
->GetLoadingContext();
70 // sanity-check passed-through parameters
71 MOZ_ASSERT(decision
, "Null out pointer");
72 WARN_IF_URI_UNINITIALIZED(contentLocation
, "Request URI");
76 nsCOMPtr
<nsINode
> node(do_QueryInterface(requestingContext
));
77 nsCOMPtr
<nsIDOMWindow
> window(do_QueryInterface(requestingContext
));
78 nsCOMPtr
<nsIBrowserChild
> browserChild(
79 do_QueryInterface(requestingContext
));
80 NS_ASSERTION(!requestingContext
|| node
|| window
|| browserChild
,
81 "Context should be a DOM node, DOM window or a browserChild!");
85 nsCOMPtr
<mozilla::dom::Document
> doc
;
86 nsCOMPtr
<nsIContent
> node
= do_QueryInterface(requestingContext
);
88 doc
= node
->OwnerDoc();
91 doc
= do_QueryInterface(requestingContext
);
95 * Enumerate mPolicies and ask each of them, taking the logical AND of
99 const nsCOMArray
<nsIContentPolicy
>& entries
= mPolicies
.GetCachedEntries();
101 nsCOMPtr
<nsPIDOMWindowOuter
> window
;
102 if (nsCOMPtr
<nsINode
> node
= do_QueryInterface(requestingContext
)) {
103 window
= node
->OwnerDoc()->GetWindow();
105 window
= do_QueryInterface(requestingContext
);
109 nsCOMPtr
<nsIContentSecurityPolicy
> csp
= doc
->GetCsp();
111 csp
->EnsureEventTarget(
112 window
->EventTargetFor(mozilla::TaskCategory::Other
));
116 int32_t count
= entries
.Count();
117 for (int32_t i
= 0; i
< count
; i
++) {
118 /* check the appropriate policy */
119 rv
= (entries
[i
]->*policyMethod
)(contentLocation
, loadInfo
, mimeType
,
122 if (NS_SUCCEEDED(rv
) && NS_CP_REJECTED(*decision
)) {
123 /* policy says no, no point continuing to check */
128 // everyone returned failure, or no policies: sanitize result
129 *decision
= nsIContentPolicy::ACCEPT
;
133 // uses the parameters from ShouldXYZ to produce and log a message
134 // logType must be a literal string constant
135 #define LOG_CHECK(logType) \
137 /* skip all this nonsense if the call failed or logging is disabled */ \
138 if (NS_SUCCEEDED(rv) && MOZ_LOG_TEST(gConPolLog, LogLevel::Debug)) { \
139 const char* resultName; \
141 resultName = NS_CP_ResponseName(*decision); \
143 resultName = "(null ptr)"; \
146 gConPolLog, LogLevel::Debug, \
147 ("Content Policy: " logType ": <%s> result=%s", \
148 contentLocation ? contentLocation->GetSpecOrDefault().get() : "None", \
154 nsContentPolicy::ShouldLoad(nsIURI
* contentLocation
, nsILoadInfo
* loadInfo
,
155 const nsACString
& mimeType
, int16_t* decision
) {
156 // ShouldProcess does not need a content location, but we do
157 MOZ_ASSERT(contentLocation
, "Must provide request location");
158 nsresult rv
= CheckPolicy(&nsIContentPolicy::ShouldLoad
, contentLocation
,
159 loadInfo
, mimeType
, decision
);
160 LOG_CHECK("ShouldLoad");
166 nsContentPolicy::ShouldProcess(nsIURI
* contentLocation
, nsILoadInfo
* loadInfo
,
167 const nsACString
& mimeType
, int16_t* decision
) {
168 nsresult rv
= CheckPolicy(&nsIContentPolicy::ShouldProcess
, contentLocation
,
169 loadInfo
, mimeType
, decision
);
170 LOG_CHECK("ShouldProcess");