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"
29 using mozilla::LogLevel
;
31 NS_IMPL_ISUPPORTS(nsContentPolicy
, nsIContentPolicy
)
33 static mozilla::LazyLogModule
gConPolLog("nsContentPolicy");
35 nsresult
NS_NewContentPolicy(nsIContentPolicy
** aResult
) {
36 *aResult
= new nsContentPolicy
;
41 nsContentPolicy::nsContentPolicy() : mPolicies(NS_CONTENTPOLICY_CATEGORY
) {}
43 nsContentPolicy::~nsContentPolicy() = default;
46 # define WARN_IF_URI_UNINITIALIZED(uri, name) \
50 (uri)->GetAsciiSpec(spec); \
51 if (spec.IsEmpty()) { \
52 NS_WARNING(name " is uninitialized, fix caller"); \
57 #else // ! defined(DEBUG)
59 # define WARN_IF_URI_UNINITIALIZED(uri, name)
61 #endif // defined(DEBUG)
63 inline nsresult
nsContentPolicy::CheckPolicy(CPMethod policyMethod
,
64 nsIURI
* contentLocation
,
65 nsILoadInfo
* loadInfo
,
67 nsCOMPtr
<nsISupports
> requestingContext
= loadInfo
->GetLoadingContext();
68 // sanity-check passed-through parameters
69 MOZ_ASSERT(decision
, "Null out pointer");
70 WARN_IF_URI_UNINITIALIZED(contentLocation
, "Request URI");
74 nsCOMPtr
<nsINode
> node(do_QueryInterface(requestingContext
));
75 nsCOMPtr
<nsIDOMWindow
> window(do_QueryInterface(requestingContext
));
76 nsCOMPtr
<nsIBrowserChild
> browserChild(
77 do_QueryInterface(requestingContext
));
78 NS_ASSERTION(!requestingContext
|| node
|| window
|| browserChild
,
79 "Context should be a DOM node, DOM window or a browserChild!");
83 nsCOMPtr
<mozilla::dom::Document
> doc
;
84 nsCOMPtr
<nsIContent
> node
= do_QueryInterface(requestingContext
);
86 doc
= node
->OwnerDoc();
89 doc
= do_QueryInterface(requestingContext
);
93 * Enumerate mPolicies and ask each of them, taking the logical AND of
97 const nsCOMArray
<nsIContentPolicy
>& entries
= mPolicies
.GetCachedEntries();
99 if (nsCOMPtr
<nsIContentSecurityPolicy
> csp
= doc
->GetCsp()) {
100 csp
->EnsureEventTarget(mozilla::GetMainThreadSerialEventTarget());
104 int32_t count
= entries
.Count();
105 for (int32_t i
= 0; i
< count
; i
++) {
106 /* check the appropriate policy */
107 rv
= (entries
[i
]->*policyMethod
)(contentLocation
, loadInfo
, decision
);
109 if (NS_SUCCEEDED(rv
) && NS_CP_REJECTED(*decision
)) {
110 /* policy says no, no point continuing to check */
115 // everyone returned failure, or no policies: sanitize result
116 *decision
= nsIContentPolicy::ACCEPT
;
120 // uses the parameters from ShouldXYZ to produce and log a message
121 // logType must be a literal string constant
122 #define LOG_CHECK(logType) \
124 /* skip all this nonsense if the call failed or logging is disabled */ \
125 if (NS_SUCCEEDED(rv) && MOZ_LOG_TEST(gConPolLog, LogLevel::Debug)) { \
126 const char* resultName; \
128 resultName = NS_CP_ResponseName(*decision); \
130 resultName = "(null ptr)"; \
133 gConPolLog, LogLevel::Debug, \
134 ("Content Policy: " logType ": <%s> result=%s", \
135 contentLocation ? contentLocation->GetSpecOrDefault().get() : "None", \
141 nsContentPolicy::ShouldLoad(nsIURI
* contentLocation
, nsILoadInfo
* loadInfo
,
143 // ShouldProcess does not need a content location, but we do
144 MOZ_ASSERT(contentLocation
, "Must provide request location");
145 nsresult rv
= CheckPolicy(&nsIContentPolicy::ShouldLoad
, contentLocation
,
147 LOG_CHECK("ShouldLoad");
153 nsContentPolicy::ShouldProcess(nsIURI
* contentLocation
, nsILoadInfo
* loadInfo
,
155 nsresult rv
= CheckPolicy(&nsIContentPolicy::ShouldProcess
, contentLocation
,
157 LOG_CHECK("ShouldProcess");