Bug 1750871 - run mochitest-remote on fission everywhere. r=releng-reviewers,aki
[gecko.git] / dom / base / nsContentPolicy.cpp
blob003bffb01b82ee0af0a2bd79a2041f97220fecc0
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/. */
7 /*
8 * Implementation of the "@mozilla.org/layout/content-policy;1" contract.
9 */
11 #include "mozilla/Logging.h"
13 #include "nsISupports.h"
14 #include "nsXPCOM.h"
15 #include "nsContentPolicyUtils.h"
16 #include "mozilla/dom/nsCSPService.h"
17 #include "nsContentPolicy.h"
18 #include "nsIURI.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"
28 class nsIDOMWindow;
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;
38 NS_ADDREF(*aResult);
39 return NS_OK;
42 nsContentPolicy::nsContentPolicy() : mPolicies(NS_CONTENTPOLICY_CATEGORY) {}
44 nsContentPolicy::~nsContentPolicy() = default;
46 #ifdef DEBUG
47 # define WARN_IF_URI_UNINITIALIZED(uri, name) \
48 PR_BEGIN_MACRO \
49 if ((uri)) { \
50 nsAutoCString spec; \
51 (uri)->GetAsciiSpec(spec); \
52 if (spec.IsEmpty()) { \
53 NS_WARNING(name " is uninitialized, fix caller"); \
54 } \
55 } \
56 PR_END_MACRO
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,
68 int16_t* decision) {
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");
74 #ifdef DEBUG
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!");
83 #endif
85 nsCOMPtr<mozilla::dom::Document> doc;
86 nsCOMPtr<nsIContent> node = do_QueryInterface(requestingContext);
87 if (node) {
88 doc = node->OwnerDoc();
90 if (!doc) {
91 doc = do_QueryInterface(requestingContext);
95 * Enumerate mPolicies and ask each of them, taking the logical AND of
96 * their permissions.
98 nsresult rv;
99 const nsCOMArray<nsIContentPolicy>& entries = mPolicies.GetCachedEntries();
101 nsCOMPtr<nsPIDOMWindowOuter> window;
102 if (nsCOMPtr<nsINode> node = do_QueryInterface(requestingContext)) {
103 window = node->OwnerDoc()->GetWindow();
104 } else {
105 window = do_QueryInterface(requestingContext);
108 if (doc) {
109 nsCOMPtr<nsIContentSecurityPolicy> csp = doc->GetCsp();
110 if (csp && window) {
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,
120 decision);
122 if (NS_SUCCEEDED(rv) && NS_CP_REJECTED(*decision)) {
123 /* policy says no, no point continuing to check */
124 return NS_OK;
128 // everyone returned failure, or no policies: sanitize result
129 *decision = nsIContentPolicy::ACCEPT;
130 return NS_OK;
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) \
136 PR_BEGIN_MACRO \
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; \
140 if (decision) { \
141 resultName = NS_CP_ResponseName(*decision); \
142 } else { \
143 resultName = "(null ptr)"; \
145 MOZ_LOG( \
146 gConPolLog, LogLevel::Debug, \
147 ("Content Policy: " logType ": <%s> result=%s", \
148 contentLocation ? contentLocation->GetSpecOrDefault().get() : "None", \
149 resultName)); \
151 PR_END_MACRO
153 NS_IMETHODIMP
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");
162 return rv;
165 NS_IMETHODIMP
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");
172 return rv;