/src/mozilla-central/caps/nsScriptSecurityManager.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
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/. */ |
6 | | |
7 | | #include "nsScriptSecurityManager.h" |
8 | | |
9 | | #include "mozilla/ArrayUtils.h" |
10 | | |
11 | | #include "xpcpublic.h" |
12 | | #include "XPCWrapper.h" |
13 | | #include "nsIInputStreamChannel.h" |
14 | | #include "nsILoadContext.h" |
15 | | #include "nsIServiceManager.h" |
16 | | #include "nsIScriptObjectPrincipal.h" |
17 | | #include "nsIScriptContext.h" |
18 | | #include "nsIScriptError.h" |
19 | | #include "nsIURL.h" |
20 | | #include "nsIURIMutator.h" |
21 | | #include "nsINestedURI.h" |
22 | | #include "nspr.h" |
23 | | #include "nsJSPrincipals.h" |
24 | | #include "mozilla/BasePrincipal.h" |
25 | | #include "ExpandedPrincipal.h" |
26 | | #include "SystemPrincipal.h" |
27 | | #include "DomainPolicy.h" |
28 | | #include "nsString.h" |
29 | | #include "nsCRT.h" |
30 | | #include "nsCRTGlue.h" |
31 | | #include "nsDocShell.h" |
32 | | #include "nsError.h" |
33 | | #include "nsGlobalWindowInner.h" |
34 | | #include "nsDOMCID.h" |
35 | | #include "nsTextFormatter.h" |
36 | | #include "nsIStringBundle.h" |
37 | | #include "nsNetUtil.h" |
38 | | #include "nsIEffectiveTLDService.h" |
39 | | #include "nsIProperties.h" |
40 | | #include "nsDirectoryServiceDefs.h" |
41 | | #include "nsIFile.h" |
42 | | #include "nsIFileURL.h" |
43 | | #include "nsIZipReader.h" |
44 | | #include "nsIScriptGlobalObject.h" |
45 | | #include "nsPIDOMWindow.h" |
46 | | #include "nsIDocShell.h" |
47 | | #include "nsIPrompt.h" |
48 | | #include "nsIWindowWatcher.h" |
49 | | #include "nsIConsoleService.h" |
50 | | #include "nsIOService.h" |
51 | | #include "nsIContent.h" |
52 | | #include "nsDOMJSUtils.h" |
53 | | #include "nsAboutProtocolUtils.h" |
54 | | #include "nsIClassInfo.h" |
55 | | #include "nsIURIFixup.h" |
56 | | #include "nsCDefaultURIFixup.h" |
57 | | #include "nsIChromeRegistry.h" |
58 | | #include "nsIResProtocolHandler.h" |
59 | | #include "nsIContentSecurityPolicy.h" |
60 | | #include "nsIAsyncVerifyRedirectCallback.h" |
61 | | #include "mozilla/Preferences.h" |
62 | | #include "mozilla/dom/BindingUtils.h" |
63 | | #include "mozilla/NullPrincipal.h" |
64 | | #include <stdint.h> |
65 | | #include "mozilla/dom/ScriptSettings.h" |
66 | | #include "mozilla/ClearOnShutdown.h" |
67 | | #include "mozilla/StaticPtr.h" |
68 | | #include "nsContentUtils.h" |
69 | | #include "nsJSUtils.h" |
70 | | #include "nsILoadInfo.h" |
71 | | #include "nsIDOMXULCommandDispatcher.h" |
72 | | #include "nsITreeSelection.h" |
73 | | |
74 | | // This should be probably defined on some other place... but I couldn't find it |
75 | | #define WEBAPPS_PERM_NAME "webapps-manage" |
76 | | |
77 | | using namespace mozilla; |
78 | | using namespace mozilla::dom; |
79 | | |
80 | | nsIIOService *nsScriptSecurityManager::sIOService = nullptr; |
81 | | JSContext *nsScriptSecurityManager::sContext = nullptr; |
82 | | bool nsScriptSecurityManager::sStrictFileOriginPolicy = true; |
83 | | |
84 | | namespace { |
85 | | |
86 | | class BundleHelper |
87 | | { |
88 | | public: |
89 | | NS_INLINE_DECL_REFCOUNTING(BundleHelper) |
90 | | |
91 | | static nsIStringBundle* |
92 | | GetOrCreate() |
93 | 0 | { |
94 | 0 | MOZ_ASSERT(!sShutdown); |
95 | 0 |
|
96 | 0 | // Already shutting down. Nothing should require the use of the string |
97 | 0 | // bundle when shutting down. |
98 | 0 | if (sShutdown) { |
99 | 0 | return nullptr; |
100 | 0 | } |
101 | 0 | |
102 | 0 | if (!sSelf) { |
103 | 0 | sSelf = new BundleHelper(); |
104 | 0 | } |
105 | 0 |
|
106 | 0 | return sSelf->GetOrCreateInternal(); |
107 | 0 | } |
108 | | |
109 | | static void |
110 | | Shutdown() |
111 | 0 | { |
112 | 0 | sSelf = nullptr; |
113 | 0 | sShutdown = true; |
114 | 0 | } |
115 | | |
116 | | private: |
117 | 0 | ~BundleHelper() = default; |
118 | | |
119 | | nsIStringBundle* |
120 | | GetOrCreateInternal() |
121 | 0 | { |
122 | 0 | if (!mBundle) { |
123 | 0 | nsCOMPtr<nsIStringBundleService> bundleService = |
124 | 0 | mozilla::services::GetStringBundleService(); |
125 | 0 | if (NS_WARN_IF(!bundleService)) { |
126 | 0 | return nullptr; |
127 | 0 | } |
128 | 0 | |
129 | 0 | nsresult rv = |
130 | 0 | bundleService->CreateBundle("chrome://global/locale/security/caps.properties", |
131 | 0 | getter_AddRefs(mBundle)); |
132 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
133 | 0 | return nullptr; |
134 | 0 | } |
135 | 0 | } |
136 | 0 | |
137 | 0 | return mBundle; |
138 | 0 | } |
139 | | |
140 | | nsCOMPtr<nsIStringBundle> mBundle; |
141 | | |
142 | | static StaticRefPtr<BundleHelper> sSelf; |
143 | | static bool sShutdown; |
144 | | }; |
145 | | |
146 | | StaticRefPtr<BundleHelper> BundleHelper::sSelf; |
147 | | bool BundleHelper::sShutdown = false; |
148 | | |
149 | | } // anonymous |
150 | | |
151 | | /////////////////////////// |
152 | | // Convenience Functions // |
153 | | /////////////////////////// |
154 | | |
155 | | class nsAutoInPrincipalDomainOriginSetter { |
156 | | public: |
157 | 0 | nsAutoInPrincipalDomainOriginSetter() { |
158 | 0 | ++sInPrincipalDomainOrigin; |
159 | 0 | } |
160 | 0 | ~nsAutoInPrincipalDomainOriginSetter() { |
161 | 0 | --sInPrincipalDomainOrigin; |
162 | 0 | } |
163 | | static uint32_t sInPrincipalDomainOrigin; |
164 | | }; |
165 | | uint32_t nsAutoInPrincipalDomainOriginSetter::sInPrincipalDomainOrigin; |
166 | | |
167 | | static |
168 | | nsresult |
169 | | GetOriginFromURI(nsIURI* aURI, nsACString& aOrigin) |
170 | 0 | { |
171 | 0 | if (nsAutoInPrincipalDomainOriginSetter::sInPrincipalDomainOrigin > 1) { |
172 | 0 | // Allow a single recursive call to GetPrincipalDomainOrigin, since that |
173 | 0 | // might be happening on a different principal from the first call. But |
174 | 0 | // after that, cut off the recursion; it just indicates that something |
175 | 0 | // we're doing in this method causes us to reenter a security check here. |
176 | 0 | return NS_ERROR_NOT_AVAILABLE; |
177 | 0 | } |
178 | 0 | |
179 | 0 | nsAutoInPrincipalDomainOriginSetter autoSetter; |
180 | 0 |
|
181 | 0 | nsCOMPtr<nsIURI> uri = NS_GetInnermostURI(aURI); |
182 | 0 | NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED); |
183 | 0 |
|
184 | 0 | nsAutoCString hostPort; |
185 | 0 |
|
186 | 0 | nsresult rv = uri->GetHostPort(hostPort); |
187 | 0 | if (NS_SUCCEEDED(rv)) { |
188 | 0 | nsAutoCString scheme; |
189 | 0 | rv = uri->GetScheme(scheme); |
190 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
191 | 0 | aOrigin = scheme + NS_LITERAL_CSTRING("://") + hostPort; |
192 | 0 | } |
193 | 0 | else { |
194 | 0 | // Some URIs (e.g., nsSimpleURI) don't support host. Just |
195 | 0 | // get the full spec. |
196 | 0 | rv = uri->GetSpec(aOrigin); |
197 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
198 | 0 | } |
199 | 0 |
|
200 | 0 | return NS_OK; |
201 | 0 | } |
202 | | |
203 | | static |
204 | | nsresult |
205 | | GetPrincipalDomainOrigin(nsIPrincipal* aPrincipal, |
206 | | nsACString& aOrigin) |
207 | 0 | { |
208 | 0 |
|
209 | 0 | nsCOMPtr<nsIURI> uri; |
210 | 0 | aPrincipal->GetDomain(getter_AddRefs(uri)); |
211 | 0 | if (!uri) { |
212 | 0 | aPrincipal->GetURI(getter_AddRefs(uri)); |
213 | 0 | } |
214 | 0 | NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED); |
215 | 0 |
|
216 | 0 | return GetOriginFromURI(uri, aOrigin); |
217 | 0 | } |
218 | | |
219 | | inline void SetPendingExceptionASCII(JSContext *cx, const char *aMsg) |
220 | 0 | { |
221 | 0 | JS_ReportErrorASCII(cx, "%s", aMsg); |
222 | 0 | } |
223 | | |
224 | | inline void SetPendingException(JSContext *cx, const char16_t *aMsg) |
225 | 0 | { |
226 | 0 | NS_ConvertUTF16toUTF8 msg(aMsg); |
227 | 0 | JS_ReportErrorUTF8(cx, "%s", msg.get()); |
228 | 0 | } |
229 | | |
230 | | /* static */ |
231 | | bool |
232 | | nsScriptSecurityManager::SecurityCompareURIs(nsIURI* aSourceURI, |
233 | | nsIURI* aTargetURI) |
234 | 0 | { |
235 | 0 | return NS_SecurityCompareURIs(aSourceURI, aTargetURI, sStrictFileOriginPolicy); |
236 | 0 | } |
237 | | |
238 | | // SecurityHashURI is consistent with SecurityCompareURIs because NS_SecurityHashURI |
239 | | // is consistent with NS_SecurityCompareURIs. See nsNetUtil.h. |
240 | | uint32_t |
241 | | nsScriptSecurityManager::SecurityHashURI(nsIURI* aURI) |
242 | 0 | { |
243 | 0 | return NS_SecurityHashURI(aURI); |
244 | 0 | } |
245 | | |
246 | | /* |
247 | | * GetChannelResultPrincipal will return the principal that the resource |
248 | | * returned by this channel will use. For example, if the resource is in |
249 | | * a sandbox, it will return the nullprincipal. If the resource is forced |
250 | | * to inherit principal, it will return the principal of its parent. If |
251 | | * the load doesn't require sandboxing or inheriting, it will return the same |
252 | | * principal as GetChannelURIPrincipal. Namely the principal of the URI |
253 | | * that is being loaded. |
254 | | */ |
255 | | NS_IMETHODIMP |
256 | | nsScriptSecurityManager::GetChannelResultPrincipal(nsIChannel* aChannel, |
257 | | nsIPrincipal** aPrincipal) |
258 | 0 | { |
259 | 0 | return GetChannelResultPrincipal(aChannel, aPrincipal, |
260 | 0 | /*aIgnoreSandboxing*/ false); |
261 | 0 | } |
262 | | |
263 | | nsresult |
264 | | nsScriptSecurityManager::GetChannelResultPrincipalIfNotSandboxed(nsIChannel* aChannel, |
265 | | nsIPrincipal** aPrincipal) |
266 | 0 | { |
267 | 0 | return GetChannelResultPrincipal(aChannel, aPrincipal, |
268 | 0 | /*aIgnoreSandboxing*/ true); |
269 | 0 | } |
270 | | |
271 | | static void |
272 | | InheritAndSetCSPOnPrincipalIfNeeded(nsIChannel* aChannel, nsIPrincipal* aPrincipal) |
273 | 0 | { |
274 | 0 | // loading a data: URI into an iframe, or loading frame[srcdoc] need |
275 | 0 | // to inherit the CSP (see Bug 1073952, 1381761). |
276 | 0 | MOZ_ASSERT(aChannel && aPrincipal, "need a valid channel and principal"); |
277 | 0 | if (!aChannel) { |
278 | 0 | return; |
279 | 0 | } |
280 | 0 | |
281 | 0 | nsCOMPtr<nsILoadInfo> loadInfo = aChannel->GetLoadInfo(); |
282 | 0 | if (!loadInfo || |
283 | 0 | loadInfo->GetExternalContentPolicyType() != nsIContentPolicy::TYPE_SUBDOCUMENT) { |
284 | 0 | return; |
285 | 0 | } |
286 | 0 | |
287 | 0 | nsCOMPtr<nsIURI> uri; |
288 | 0 | nsresult rv = aChannel->GetURI(getter_AddRefs(uri)); |
289 | 0 | NS_ENSURE_SUCCESS_VOID(rv); |
290 | 0 | nsAutoCString URISpec; |
291 | 0 | rv = uri->GetSpec(URISpec); |
292 | 0 | NS_ENSURE_SUCCESS_VOID(rv); |
293 | 0 |
|
294 | 0 | bool isSrcDoc = URISpec.EqualsLiteral("about:srcdoc"); |
295 | 0 | bool isData = (NS_SUCCEEDED(uri->SchemeIs("data", &isData)) && isData); |
296 | 0 |
|
297 | 0 | if (!isSrcDoc && !isData) { |
298 | 0 | return; |
299 | 0 | } |
300 | 0 | |
301 | 0 | nsCOMPtr<nsIPrincipal> principalToInherit = |
302 | 0 | loadInfo->FindPrincipalToInherit(aChannel); |
303 | 0 |
|
304 | 0 | nsCOMPtr<nsIContentSecurityPolicy> originalCSP; |
305 | 0 | principalToInherit->GetCsp(getter_AddRefs(originalCSP)); |
306 | 0 | if (!originalCSP) { |
307 | 0 | return; |
308 | 0 | } |
309 | 0 | |
310 | 0 | // if the principalToInherit had a CSP, add it to the before |
311 | 0 | // created NullPrincipal (unless it already has one) |
312 | 0 | MOZ_ASSERT(aPrincipal->GetIsNullPrincipal(), |
313 | 0 | "inheriting the CSP only valid for NullPrincipal"); |
314 | 0 | nsCOMPtr<nsIContentSecurityPolicy> nullPrincipalCSP; |
315 | 0 | aPrincipal->GetCsp(getter_AddRefs(nullPrincipalCSP)); |
316 | 0 | if (nullPrincipalCSP) { |
317 | 0 | MOZ_ASSERT(nullPrincipalCSP == originalCSP, |
318 | 0 | "There should be no other CSP here."); |
319 | 0 | // CSPs are equal, no need to set it again. |
320 | 0 | return; |
321 | 0 | } |
322 | 0 | aPrincipal->SetCsp(originalCSP); |
323 | 0 | } |
324 | | |
325 | | nsresult |
326 | | nsScriptSecurityManager::GetChannelResultPrincipal(nsIChannel* aChannel, |
327 | | nsIPrincipal** aPrincipal, |
328 | | bool aIgnoreSandboxing) |
329 | 0 | { |
330 | 0 | MOZ_ASSERT(aChannel, "Must have channel!"); |
331 | 0 |
|
332 | 0 | // Check whether we have an nsILoadInfo that says what we should do. |
333 | 0 | nsCOMPtr<nsILoadInfo> loadInfo = aChannel->GetLoadInfo(); |
334 | 0 | if (loadInfo && loadInfo->GetForceInheritPrincipalOverruleOwner()) { |
335 | 0 | nsCOMPtr<nsIPrincipal> principalToInherit = |
336 | 0 | loadInfo->FindPrincipalToInherit(aChannel); |
337 | 0 | principalToInherit.forget(aPrincipal); |
338 | 0 | return NS_OK; |
339 | 0 | } |
340 | 0 | |
341 | 0 | nsCOMPtr<nsISupports> owner; |
342 | 0 | aChannel->GetOwner(getter_AddRefs(owner)); |
343 | 0 | if (owner) { |
344 | 0 | CallQueryInterface(owner, aPrincipal); |
345 | 0 | if (*aPrincipal) { |
346 | 0 | return NS_OK; |
347 | 0 | } |
348 | 0 | } |
349 | 0 | |
350 | 0 | if (loadInfo) { |
351 | 0 | if (!aIgnoreSandboxing && loadInfo->GetLoadingSandboxed()) { |
352 | 0 | MOZ_ALWAYS_TRUE(NS_SUCCEEDED(loadInfo->GetSandboxedLoadingPrincipal(aPrincipal))); |
353 | 0 | MOZ_ASSERT(*aPrincipal); |
354 | 0 | InheritAndSetCSPOnPrincipalIfNeeded(aChannel, *aPrincipal); |
355 | 0 | return NS_OK; |
356 | 0 | } |
357 | 0 |
|
358 | 0 | bool forceInherit = loadInfo->GetForceInheritPrincipal(); |
359 | 0 | if (aIgnoreSandboxing && !forceInherit) { |
360 | 0 | // Check if SEC_FORCE_INHERIT_PRINCIPAL was dropped because of |
361 | 0 | // sandboxing: |
362 | 0 | if (loadInfo->GetLoadingSandboxed() && |
363 | 0 | loadInfo->GetForceInheritPrincipalDropped()) { |
364 | 0 | forceInherit = true; |
365 | 0 | } |
366 | 0 | } |
367 | 0 | if (forceInherit) { |
368 | 0 | nsCOMPtr<nsIPrincipal> principalToInherit = |
369 | 0 | loadInfo->FindPrincipalToInherit(aChannel); |
370 | 0 | principalToInherit.forget(aPrincipal); |
371 | 0 | return NS_OK; |
372 | 0 | } |
373 | 0 | |
374 | 0 | auto securityMode = loadInfo->GetSecurityMode(); |
375 | 0 | // The data: inheritance flags should only apply to the initial load, |
376 | 0 | // not to loads that it might have redirected to. |
377 | 0 | if (loadInfo->RedirectChain().IsEmpty() && |
378 | 0 | (securityMode == nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_INHERITS || |
379 | 0 | securityMode == nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS || |
380 | 0 | securityMode == nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS)) { |
381 | 0 |
|
382 | 0 | nsCOMPtr<nsIURI> uri; |
383 | 0 | nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(uri)); |
384 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
385 | 0 |
|
386 | 0 | nsCOMPtr<nsIPrincipal> principalToInherit = |
387 | 0 | loadInfo->FindPrincipalToInherit(aChannel); |
388 | 0 | bool inheritForAboutBlank = loadInfo->GetAboutBlankInherits(); |
389 | 0 |
|
390 | 0 | if (nsContentUtils::ChannelShouldInheritPrincipal(principalToInherit, |
391 | 0 | uri, |
392 | 0 | inheritForAboutBlank, |
393 | 0 | false)) { |
394 | 0 | principalToInherit.forget(aPrincipal); |
395 | 0 | return NS_OK; |
396 | 0 | } |
397 | 0 | } |
398 | 0 | } |
399 | 0 | nsresult rv = GetChannelURIPrincipal(aChannel, aPrincipal); |
400 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
401 | 0 | InheritAndSetCSPOnPrincipalIfNeeded(aChannel, *aPrincipal); |
402 | 0 | return NS_OK; |
403 | 0 | } |
404 | | |
405 | | /* The principal of the URI that this channel is loading. This is never |
406 | | * affected by things like sandboxed loads, or loads where we forcefully |
407 | | * inherit the principal. Think of this as the principal of the server |
408 | | * which this channel is loading from. Most callers should use |
409 | | * GetChannelResultPrincipal instead of GetChannelURIPrincipal. Only |
410 | | * call GetChannelURIPrincipal if you are sure that you want the |
411 | | * principal that matches the uri, even in cases when the load is |
412 | | * sandboxed or when the load could be a blob or data uri (i.e even when |
413 | | * you encounter loads that may or may not be sandboxed and loads |
414 | | * that may or may not inherit)." |
415 | | */ |
416 | | NS_IMETHODIMP |
417 | | nsScriptSecurityManager::GetChannelURIPrincipal(nsIChannel* aChannel, |
418 | | nsIPrincipal** aPrincipal) |
419 | 0 | { |
420 | 0 | MOZ_ASSERT(aChannel, "Must have channel!"); |
421 | 0 |
|
422 | 0 | // Get the principal from the URI. Make sure this does the same thing |
423 | 0 | // as nsDocument::Reset and XULDocument::StartDocumentLoad. |
424 | 0 | nsCOMPtr<nsIURI> uri; |
425 | 0 | nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(uri)); |
426 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
427 | 0 |
|
428 | 0 | nsCOMPtr<nsILoadInfo> loadInfo; |
429 | 0 | aChannel->GetLoadInfo(getter_AddRefs(loadInfo)); |
430 | 0 |
|
431 | 0 | // Inherit the origin attributes from loadInfo. |
432 | 0 | // If this is a top-level document load, the origin attributes of the |
433 | 0 | // loadInfo will be set from nsDocShell::DoURILoad. |
434 | 0 | // For subresource loading, the origin attributes of the loadInfo is from |
435 | 0 | // its loadingPrincipal. |
436 | 0 | OriginAttributes attrs; |
437 | 0 |
|
438 | 0 | // For addons loadInfo might be null. |
439 | 0 | if (loadInfo) { |
440 | 0 | attrs = loadInfo->GetOriginAttributes(); |
441 | 0 | } |
442 | 0 |
|
443 | 0 | nsCOMPtr<nsIPrincipal> prin = |
444 | 0 | BasePrincipal::CreateCodebasePrincipal(uri, attrs); |
445 | 0 | prin.forget(aPrincipal); |
446 | 0 | return *aPrincipal ? NS_OK : NS_ERROR_FAILURE; |
447 | 0 | } |
448 | | |
449 | | NS_IMETHODIMP |
450 | | nsScriptSecurityManager::IsSystemPrincipal(nsIPrincipal* aPrincipal, |
451 | | bool* aIsSystem) |
452 | 9 | { |
453 | 9 | *aIsSystem = (aPrincipal == mSystemPrincipal); |
454 | 9 | return NS_OK; |
455 | 9 | } |
456 | | |
457 | | ///////////////////////////// |
458 | | // nsScriptSecurityManager // |
459 | | ///////////////////////////// |
460 | | |
461 | | //////////////////////////////////// |
462 | | // Methods implementing ISupports // |
463 | | //////////////////////////////////// |
464 | | NS_IMPL_ISUPPORTS(nsScriptSecurityManager, |
465 | | nsIScriptSecurityManager) |
466 | | |
467 | | /////////////////////////////////////////////////// |
468 | | // Methods implementing nsIScriptSecurityManager // |
469 | | /////////////////////////////////////////////////// |
470 | | |
471 | | ///////////////// Security Checks ///////////////// |
472 | | |
473 | | bool |
474 | | nsScriptSecurityManager::ContentSecurityPolicyPermitsJSAction(JSContext *cx, |
475 | | JS::HandleValue aValue) |
476 | 0 | { |
477 | 0 | MOZ_ASSERT(cx == nsContentUtils::GetCurrentJSContext()); |
478 | 0 | nsCOMPtr<nsIPrincipal> subjectPrincipal = nsContentUtils::SubjectPrincipal(); |
479 | 0 | nsCOMPtr<nsIContentSecurityPolicy> csp; |
480 | 0 | nsresult rv = subjectPrincipal->GetCsp(getter_AddRefs(csp)); |
481 | 0 | NS_ASSERTION(NS_SUCCEEDED(rv), "CSP: Failed to get CSP from principal."); |
482 | 0 |
|
483 | 0 | // don't do anything unless there's a CSP |
484 | 0 | if (!csp) |
485 | 0 | return true; |
486 | 0 | |
487 | 0 | bool evalOK = true; |
488 | 0 | bool reportViolation = false; |
489 | 0 | rv = csp->GetAllowsEval(&reportViolation, &evalOK); |
490 | 0 |
|
491 | 0 | if (NS_FAILED(rv)) |
492 | 0 | { |
493 | 0 | NS_WARNING("CSP: failed to get allowsEval"); |
494 | 0 | return true; // fail open to not break sites. |
495 | 0 | } |
496 | 0 |
|
497 | 0 | if (reportViolation) { |
498 | 0 | JS::Rooted<JSString*> jsString(cx, JS::ToString(cx, aValue)); |
499 | 0 | if (NS_WARN_IF(!jsString)) { |
500 | 0 | JS_ClearPendingException(cx); |
501 | 0 | return false; |
502 | 0 | } |
503 | 0 | |
504 | 0 | nsAutoJSString scriptSample; |
505 | 0 | if (NS_WARN_IF(!scriptSample.init(cx, jsString))) { |
506 | 0 | JS_ClearPendingException(cx); |
507 | 0 | return false; |
508 | 0 | } |
509 | 0 | |
510 | 0 | JS::AutoFilename scriptFilename; |
511 | 0 | nsAutoString fileName; |
512 | 0 | unsigned lineNum = 0; |
513 | 0 | unsigned columnNum = 0; |
514 | 0 | if (JS::DescribeScriptedCaller(cx, &scriptFilename, &lineNum, |
515 | 0 | &columnNum)) { |
516 | 0 | if (const char *file = scriptFilename.get()) { |
517 | 0 | CopyUTF8toUTF16(nsDependentCString(file), fileName); |
518 | 0 | } |
519 | 0 | } else { |
520 | 0 | MOZ_ASSERT(!JS_IsExceptionPending(cx)); |
521 | 0 | } |
522 | 0 | csp->LogViolationDetails(nsIContentSecurityPolicy::VIOLATION_TYPE_EVAL, |
523 | 0 | nullptr, // triggering element |
524 | 0 | fileName, |
525 | 0 | scriptSample, |
526 | 0 | lineNum, |
527 | 0 | columnNum, |
528 | 0 | EmptyString(), |
529 | 0 | EmptyString()); |
530 | 0 | } |
531 | 0 |
|
532 | 0 | return evalOK; |
533 | 0 | } |
534 | | |
535 | | // static |
536 | | bool |
537 | | nsScriptSecurityManager::JSPrincipalsSubsume(JSPrincipals *first, |
538 | | JSPrincipals *second) |
539 | 0 | { |
540 | 0 | return nsJSPrincipals::get(first)->Subsumes(nsJSPrincipals::get(second)); |
541 | 0 | } |
542 | | |
543 | | NS_IMETHODIMP |
544 | | nsScriptSecurityManager::CheckSameOriginURI(nsIURI* aSourceURI, |
545 | | nsIURI* aTargetURI, |
546 | | bool reportError, |
547 | | bool aFromPrivateWindow) |
548 | 0 | { |
549 | 0 | // Please note that aFromPrivateWindow is only 100% accurate if |
550 | 0 | // reportError is true. |
551 | 0 | if (!SecurityCompareURIs(aSourceURI, aTargetURI)) |
552 | 0 | { |
553 | 0 | if (reportError) { |
554 | 0 | ReportError("CheckSameOriginError", |
555 | 0 | aSourceURI, aTargetURI, aFromPrivateWindow); |
556 | 0 | } |
557 | 0 | return NS_ERROR_DOM_BAD_URI; |
558 | 0 | } |
559 | 0 | return NS_OK; |
560 | 0 | } |
561 | | |
562 | | /*static*/ uint32_t |
563 | | nsScriptSecurityManager::HashPrincipalByOrigin(nsIPrincipal* aPrincipal) |
564 | 0 | { |
565 | 0 | nsCOMPtr<nsIURI> uri; |
566 | 0 | aPrincipal->GetDomain(getter_AddRefs(uri)); |
567 | 0 | if (!uri) |
568 | 0 | aPrincipal->GetURI(getter_AddRefs(uri)); |
569 | 0 | return SecurityHashURI(uri); |
570 | 0 | } |
571 | | |
572 | | NS_IMETHODIMP |
573 | | nsScriptSecurityManager::CheckLoadURIFromScript(JSContext *cx, nsIURI *aURI) |
574 | 0 | { |
575 | 0 | // Get principal of currently executing script. |
576 | 0 | MOZ_ASSERT(cx == nsContentUtils::GetCurrentJSContext()); |
577 | 0 | nsIPrincipal* principal = nsContentUtils::SubjectPrincipal(); |
578 | 0 | nsresult rv = CheckLoadURIWithPrincipal(principal, aURI, |
579 | 0 | nsIScriptSecurityManager::STANDARD); |
580 | 0 | if (NS_SUCCEEDED(rv)) { |
581 | 0 | // OK to load |
582 | 0 | return NS_OK; |
583 | 0 | } |
584 | 0 | |
585 | 0 | // Report error. |
586 | 0 | nsAutoCString spec; |
587 | 0 | if (NS_FAILED(aURI->GetAsciiSpec(spec))) |
588 | 0 | return NS_ERROR_FAILURE; |
589 | 0 | nsAutoCString msg("Access to '"); |
590 | 0 | msg.Append(spec); |
591 | 0 | msg.AppendLiteral("' from script denied"); |
592 | 0 | SetPendingExceptionASCII(cx, msg.get()); |
593 | 0 | return NS_ERROR_DOM_BAD_URI; |
594 | 0 | } |
595 | | |
596 | | /** |
597 | | * Helper method to handle cases where a flag passed to |
598 | | * CheckLoadURIWithPrincipal means denying loading if the given URI has certain |
599 | | * nsIProtocolHandler flags set. |
600 | | * @return if success, access is allowed. Otherwise, deny access |
601 | | */ |
602 | | static nsresult |
603 | | DenyAccessIfURIHasFlags(nsIURI* aURI, uint32_t aURIFlags) |
604 | 0 | { |
605 | 0 | MOZ_ASSERT(aURI, "Must have URI!"); |
606 | 0 |
|
607 | 0 | bool uriHasFlags; |
608 | 0 | nsresult rv = |
609 | 0 | NS_URIChainHasFlags(aURI, aURIFlags, &uriHasFlags); |
610 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
611 | 0 |
|
612 | 0 | if (uriHasFlags) { |
613 | 0 | return NS_ERROR_DOM_BAD_URI; |
614 | 0 | } |
615 | 0 | |
616 | 0 | return NS_OK; |
617 | 0 | } |
618 | | |
619 | | static bool |
620 | | EqualOrSubdomain(nsIURI* aProbeArg, nsIURI* aBase) |
621 | 0 | { |
622 | 0 | nsresult rv; |
623 | 0 | nsCOMPtr<nsIURI> probe = aProbeArg; |
624 | 0 |
|
625 | 0 | nsCOMPtr<nsIEffectiveTLDService> tldService = do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID); |
626 | 0 | NS_ENSURE_TRUE(tldService, false); |
627 | 0 | while (true) { |
628 | 0 | if (nsScriptSecurityManager::SecurityCompareURIs(probe, aBase)) { |
629 | 0 | return true; |
630 | 0 | } |
631 | 0 | |
632 | 0 | nsAutoCString host, newHost; |
633 | 0 | rv = probe->GetHost(host); |
634 | 0 | NS_ENSURE_SUCCESS(rv, false); |
635 | 0 |
|
636 | 0 | rv = tldService->GetNextSubDomain(host, newHost); |
637 | 0 | if (rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS) { |
638 | 0 | return false; |
639 | 0 | } |
640 | 0 | NS_ENSURE_SUCCESS(rv, false); |
641 | 0 | rv = NS_MutateURI(probe) |
642 | 0 | .SetHost(newHost) |
643 | 0 | .Finalize(probe); |
644 | 0 | NS_ENSURE_SUCCESS(rv, false); |
645 | 0 | } |
646 | 0 | } |
647 | | |
648 | | NS_IMETHODIMP |
649 | | nsScriptSecurityManager::CheckLoadURIWithPrincipal(nsIPrincipal* aPrincipal, |
650 | | nsIURI *aTargetURI, |
651 | | uint32_t aFlags) |
652 | 0 | { |
653 | 0 | MOZ_ASSERT(aPrincipal, "CheckLoadURIWithPrincipal must have a principal"); |
654 | 0 |
|
655 | 0 | // If someone passes a flag that we don't understand, we should |
656 | 0 | // fail, because they may need a security check that we don't |
657 | 0 | // provide. |
658 | 0 | NS_ENSURE_FALSE(aFlags & ~(nsIScriptSecurityManager::LOAD_IS_AUTOMATIC_DOCUMENT_REPLACEMENT | |
659 | 0 | nsIScriptSecurityManager::ALLOW_CHROME | |
660 | 0 | nsIScriptSecurityManager::DISALLOW_SCRIPT | |
661 | 0 | nsIScriptSecurityManager::DISALLOW_INHERIT_PRINCIPAL | |
662 | 0 | nsIScriptSecurityManager::DONT_REPORT_ERRORS), |
663 | 0 | NS_ERROR_UNEXPECTED); |
664 | 0 | NS_ENSURE_ARG_POINTER(aPrincipal); |
665 | 0 | NS_ENSURE_ARG_POINTER(aTargetURI); |
666 | 0 |
|
667 | 0 | // If DISALLOW_INHERIT_PRINCIPAL is set, we prevent loading of URIs which |
668 | 0 | // would do such inheriting. That would be URIs that do not have their own |
669 | 0 | // security context. We do this even for the system principal. |
670 | 0 | if (aFlags & nsIScriptSecurityManager::DISALLOW_INHERIT_PRINCIPAL) { |
671 | 0 | nsresult rv = |
672 | 0 | DenyAccessIfURIHasFlags(aTargetURI, |
673 | 0 | nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT); |
674 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
675 | 0 | } |
676 | 0 |
|
677 | 0 | if (aPrincipal == mSystemPrincipal) { |
678 | 0 | // Allow access |
679 | 0 | return NS_OK; |
680 | 0 | } |
681 | 0 | |
682 | 0 | nsCOMPtr<nsIURI> sourceURI; |
683 | 0 | aPrincipal->GetURI(getter_AddRefs(sourceURI)); |
684 | 0 | if (!sourceURI) { |
685 | 0 | auto* basePrin = BasePrincipal::Cast(aPrincipal); |
686 | 0 | if (basePrin->Is<ExpandedPrincipal>()) { |
687 | 0 | auto expanded = basePrin->As<ExpandedPrincipal>(); |
688 | 0 | for (auto& prin : expanded->WhiteList()) { |
689 | 0 | nsresult rv = CheckLoadURIWithPrincipal(prin, |
690 | 0 | aTargetURI, |
691 | 0 | aFlags); |
692 | 0 | if (NS_SUCCEEDED(rv)) { |
693 | 0 | // Allow access if it succeeded with one of the white listed principals |
694 | 0 | return NS_OK; |
695 | 0 | } |
696 | 0 | } |
697 | 0 | // None of our whitelisted principals worked. |
698 | 0 | return NS_ERROR_DOM_BAD_URI; |
699 | 0 | } |
700 | 0 | NS_ERROR("Non-system principals or expanded principal passed to CheckLoadURIWithPrincipal " |
701 | 0 | "must have a URI!"); |
702 | 0 | return NS_ERROR_UNEXPECTED; |
703 | 0 | } |
704 | 0 | |
705 | 0 | // Automatic loads are not allowed from certain protocols. |
706 | 0 | if (aFlags & nsIScriptSecurityManager::LOAD_IS_AUTOMATIC_DOCUMENT_REPLACEMENT) { |
707 | 0 | nsresult rv = |
708 | 0 | DenyAccessIfURIHasFlags(sourceURI, |
709 | 0 | nsIProtocolHandler::URI_FORBIDS_AUTOMATIC_DOCUMENT_REPLACEMENT); |
710 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
711 | 0 | } |
712 | 0 |
|
713 | 0 | // If either URI is a nested URI, get the base URI |
714 | 0 | nsCOMPtr<nsIURI> sourceBaseURI = NS_GetInnermostURI(sourceURI); |
715 | 0 | nsCOMPtr<nsIURI> targetBaseURI = NS_GetInnermostURI(aTargetURI); |
716 | 0 |
|
717 | 0 | //-- get the target scheme |
718 | 0 | nsAutoCString targetScheme; |
719 | 0 | nsresult rv = targetBaseURI->GetScheme(targetScheme); |
720 | 0 | if (NS_FAILED(rv)) return rv; |
721 | 0 | |
722 | 0 | //-- Some callers do not allow loading javascript: |
723 | 0 | if ((aFlags & nsIScriptSecurityManager::DISALLOW_SCRIPT) && |
724 | 0 | targetScheme.EqualsLiteral("javascript")) |
725 | 0 | { |
726 | 0 | return NS_ERROR_DOM_BAD_URI; |
727 | 0 | } |
728 | 0 | |
729 | 0 | // Check for uris that are only loadable by principals that subsume them |
730 | 0 | bool hasFlags; |
731 | 0 | rv = NS_URIChainHasFlags(targetBaseURI, |
732 | 0 | nsIProtocolHandler::URI_LOADABLE_BY_SUBSUMERS, |
733 | 0 | &hasFlags); |
734 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
735 | 0 |
|
736 | 0 | if (hasFlags) { |
737 | 0 | // check nothing else in the URI chain has flags that prevent |
738 | 0 | // access: |
739 | 0 | rv = CheckLoadURIFlags(sourceURI, aTargetURI, sourceBaseURI, |
740 | 0 | targetBaseURI, aFlags, |
741 | 0 | aPrincipal->OriginAttributesRef().mPrivateBrowsingId > 0); |
742 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
743 | 0 | // Check the principal is allowed to load the target. |
744 | 0 | return aPrincipal->CheckMayLoad(targetBaseURI, true, false); |
745 | 0 | } |
746 | 0 | |
747 | 0 | //-- get the source scheme |
748 | 0 | nsAutoCString sourceScheme; |
749 | 0 | rv = sourceBaseURI->GetScheme(sourceScheme); |
750 | 0 | if (NS_FAILED(rv)) return rv; |
751 | 0 | |
752 | 0 | // When comparing schemes, if the relevant pref is set, view-source URIs |
753 | 0 | // are reachable from same-protocol (so e.g. file: can link to |
754 | 0 | // view-source:file). This is required for reftests. |
755 | 0 | static bool sViewSourceReachableFromInner = false; |
756 | 0 | static bool sCachedViewSourcePref = false; |
757 | 0 | if (!sCachedViewSourcePref) { |
758 | 0 | sCachedViewSourcePref = true; |
759 | 0 | mozilla::Preferences::AddBoolVarCache(&sViewSourceReachableFromInner, |
760 | 0 | "security.view-source.reachable-from-inner-protocol"); |
761 | 0 | } |
762 | 0 |
|
763 | 0 | bool targetIsViewSource = false; |
764 | 0 |
|
765 | 0 | if (sourceScheme.LowerCaseEqualsLiteral(NS_NULLPRINCIPAL_SCHEME)) { |
766 | 0 | // A null principal can target its own URI. |
767 | 0 | if (sourceURI == aTargetURI) { |
768 | 0 | return NS_OK; |
769 | 0 | } |
770 | 0 | } |
771 | 0 | else if (sViewSourceReachableFromInner && |
772 | 0 | sourceScheme.EqualsIgnoreCase(targetScheme.get()) && |
773 | 0 | NS_SUCCEEDED(aTargetURI->SchemeIs("view-source", &targetIsViewSource)) && |
774 | 0 | targetIsViewSource) |
775 | 0 | { |
776 | 0 | // exception for foo: linking to view-source:foo for reftests... |
777 | 0 | return NS_OK; |
778 | 0 | } |
779 | 0 | else if (sourceScheme.EqualsIgnoreCase("file") && |
780 | 0 | targetScheme.EqualsIgnoreCase("moz-icon")) |
781 | 0 | { |
782 | 0 | // exception for file: linking to moz-icon://.ext?size=... |
783 | 0 | // Note that because targetScheme is the base (innermost) URI scheme, |
784 | 0 | // this does NOT allow file -> moz-icon:file:///... links. |
785 | 0 | // This is intentional. |
786 | 0 | return NS_OK; |
787 | 0 | } |
788 | 0 | |
789 | 0 | // Check for webextension |
790 | 0 | rv = NS_URIChainHasFlags(aTargetURI, |
791 | 0 | nsIProtocolHandler::URI_LOADABLE_BY_EXTENSIONS, |
792 | 0 | &hasFlags); |
793 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
794 | 0 |
|
795 | 0 | if (hasFlags && BasePrincipal::Cast(aPrincipal)->AddonPolicy()) { |
796 | 0 | return NS_OK; |
797 | 0 | } |
798 | 0 | |
799 | 0 | // If we get here, check all the schemes can link to each other, from the top down: |
800 | 0 | nsCaseInsensitiveCStringComparator stringComparator; |
801 | 0 | nsCOMPtr<nsIURI> currentURI = sourceURI; |
802 | 0 | nsCOMPtr<nsIURI> currentOtherURI = aTargetURI; |
803 | 0 |
|
804 | 0 | bool denySameSchemeLinks = false; |
805 | 0 | rv = NS_URIChainHasFlags(aTargetURI, nsIProtocolHandler::URI_SCHEME_NOT_SELF_LINKABLE, |
806 | 0 | &denySameSchemeLinks); |
807 | 0 | if (NS_FAILED(rv)) return rv; |
808 | 0 | |
809 | 0 | while (currentURI && currentOtherURI) { |
810 | 0 | nsAutoCString scheme, otherScheme; |
811 | 0 | currentURI->GetScheme(scheme); |
812 | 0 | currentOtherURI->GetScheme(otherScheme); |
813 | 0 |
|
814 | 0 | bool schemesMatch = scheme.Equals(otherScheme, stringComparator); |
815 | 0 | bool isSamePage = false; |
816 | 0 | // about: URIs are special snowflakes. |
817 | 0 | if (scheme.EqualsLiteral("about") && schemesMatch) { |
818 | 0 | nsAutoCString moduleName, otherModuleName; |
819 | 0 | // about: pages can always link to themselves: |
820 | 0 | isSamePage = |
821 | 0 | NS_SUCCEEDED(NS_GetAboutModuleName(currentURI, moduleName)) && |
822 | 0 | NS_SUCCEEDED(NS_GetAboutModuleName(currentOtherURI, otherModuleName)) && |
823 | 0 | moduleName.Equals(otherModuleName); |
824 | 0 | if (!isSamePage) { |
825 | 0 | // We will have allowed the load earlier if the source page has |
826 | 0 | // system principal. So we know the source has a content |
827 | 0 | // principal, and it's trying to link to something else. |
828 | 0 | // Linkable about: pages are always reachable, even if we hit |
829 | 0 | // the CheckLoadURIFlags call below. |
830 | 0 | // We punch only 1 other hole: iff the source is unlinkable, |
831 | 0 | // we let them link to other pages explicitly marked SAFE |
832 | 0 | // for content. This avoids world-linkable about: pages linking |
833 | 0 | // to non-world-linkable about: pages. |
834 | 0 | nsCOMPtr<nsIAboutModule> module, otherModule; |
835 | 0 | bool knowBothModules = |
836 | 0 | NS_SUCCEEDED(NS_GetAboutModule(currentURI, getter_AddRefs(module))) && |
837 | 0 | NS_SUCCEEDED(NS_GetAboutModule(currentOtherURI, getter_AddRefs(otherModule))); |
838 | 0 | uint32_t aboutModuleFlags = 0; |
839 | 0 | uint32_t otherAboutModuleFlags = 0; |
840 | 0 | knowBothModules = knowBothModules && |
841 | 0 | NS_SUCCEEDED(module->GetURIFlags(currentURI, &aboutModuleFlags)) && |
842 | 0 | NS_SUCCEEDED(otherModule->GetURIFlags(currentOtherURI, &otherAboutModuleFlags)); |
843 | 0 | if (knowBothModules) { |
844 | 0 | isSamePage = |
845 | 0 | !(aboutModuleFlags & nsIAboutModule::MAKE_LINKABLE) && |
846 | 0 | (otherAboutModuleFlags & nsIAboutModule::URI_SAFE_FOR_UNTRUSTED_CONTENT); |
847 | 0 | if (isSamePage && otherAboutModuleFlags & nsIAboutModule::MAKE_LINKABLE) { |
848 | 0 | //XXXgijs: this is a hack. The target will be nested |
849 | 0 | // (with innerURI of moz-safe-about:whatever), and |
850 | 0 | // the source isn't, so we won't pass if we finish |
851 | 0 | // the loop. We *should* pass, though, so return here. |
852 | 0 | // This hack can go away when bug 1228118 is fixed. |
853 | 0 | return NS_OK; |
854 | 0 | } |
855 | 0 | } |
856 | 0 | } |
857 | 0 | } else { |
858 | 0 | bool equalExceptRef = false; |
859 | 0 | rv = currentURI->EqualsExceptRef(currentOtherURI, &equalExceptRef); |
860 | 0 | isSamePage = NS_SUCCEEDED(rv) && equalExceptRef; |
861 | 0 | } |
862 | 0 |
|
863 | 0 | // If schemes are not equal, or they're equal but the target URI |
864 | 0 | // is different from the source URI and doesn't always allow linking |
865 | 0 | // from the same scheme, check if the URI flags of the current target |
866 | 0 | // URI allow the current source URI to link to it. |
867 | 0 | // The policy is specified by the protocol flags on both URIs. |
868 | 0 | if (!schemesMatch || (denySameSchemeLinks && !isSamePage)) { |
869 | 0 | return CheckLoadURIFlags(currentURI, currentOtherURI, |
870 | 0 | sourceBaseURI, targetBaseURI, aFlags, |
871 | 0 | aPrincipal->OriginAttributesRef().mPrivateBrowsingId > 0); |
872 | 0 | } |
873 | 0 | // Otherwise... check if we can nest another level: |
874 | 0 | nsCOMPtr<nsINestedURI> nestedURI = do_QueryInterface(currentURI); |
875 | 0 | nsCOMPtr<nsINestedURI> nestedOtherURI = do_QueryInterface(currentOtherURI); |
876 | 0 |
|
877 | 0 | // If schemes match and neither URI is nested further, we're OK. |
878 | 0 | if (!nestedURI && !nestedOtherURI) { |
879 | 0 | return NS_OK; |
880 | 0 | } |
881 | 0 | // If one is nested and the other isn't, something is wrong. |
882 | 0 | if (!nestedURI != !nestedOtherURI) { |
883 | 0 | return NS_ERROR_DOM_BAD_URI; |
884 | 0 | } |
885 | 0 | // Otherwise, both should be nested and we'll go through the loop again. |
886 | 0 | nestedURI->GetInnerURI(getter_AddRefs(currentURI)); |
887 | 0 | nestedOtherURI->GetInnerURI(getter_AddRefs(currentOtherURI)); |
888 | 0 | } |
889 | 0 |
|
890 | 0 | // We should never get here. We should always return from inside the loop. |
891 | 0 | return NS_ERROR_DOM_BAD_URI; |
892 | 0 | } |
893 | | |
894 | | /** |
895 | | * Helper method to check whether the target URI and its innermost ("base") URI |
896 | | * has protocol flags that should stop it from being loaded by the source URI |
897 | | * (and/or the source URI's innermost ("base") URI), taking into account any |
898 | | * nsIScriptSecurityManager flags originally passed to |
899 | | * CheckLoadURIWithPrincipal and friends. |
900 | | * |
901 | | * @return if success, access is allowed. Otherwise, deny access |
902 | | */ |
903 | | nsresult |
904 | | nsScriptSecurityManager::CheckLoadURIFlags(nsIURI *aSourceURI, |
905 | | nsIURI *aTargetURI, |
906 | | nsIURI *aSourceBaseURI, |
907 | | nsIURI *aTargetBaseURI, |
908 | | uint32_t aFlags, |
909 | | bool aFromPrivateWindow) |
910 | 0 | { |
911 | 0 | // Note that the order of policy checks here is very important! |
912 | 0 | // We start from most restrictive and work our way down. |
913 | 0 | bool reportErrors = !(aFlags & nsIScriptSecurityManager::DONT_REPORT_ERRORS); |
914 | 0 | const char* errorTag = "CheckLoadURIError"; |
915 | 0 |
|
916 | 0 | nsAutoCString targetScheme; |
917 | 0 | nsresult rv = aTargetBaseURI->GetScheme(targetScheme); |
918 | 0 | if (NS_FAILED(rv)) return rv; |
919 | 0 | |
920 | 0 | // Check for system target URI |
921 | 0 | rv = DenyAccessIfURIHasFlags(aTargetURI, |
922 | 0 | nsIProtocolHandler::URI_DANGEROUS_TO_LOAD); |
923 | 0 | if (NS_FAILED(rv)) { |
924 | 0 | // Deny access, since the origin principal is not system |
925 | 0 | if (reportErrors) { |
926 | 0 | ReportError(errorTag, aSourceURI, aTargetURI, aFromPrivateWindow); |
927 | 0 | } |
928 | 0 | return rv; |
929 | 0 | } |
930 | 0 |
|
931 | 0 | // Check for chrome target URI |
932 | 0 | bool hasFlags = false; |
933 | 0 | rv = NS_URIChainHasFlags(aTargetURI, |
934 | 0 | nsIProtocolHandler::URI_IS_UI_RESOURCE, |
935 | 0 | &hasFlags); |
936 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
937 | 0 | if (hasFlags) { |
938 | 0 | if (aFlags & nsIScriptSecurityManager::ALLOW_CHROME) { |
939 | 0 | // Allow a URI_IS_UI_RESOURCE source to link to a URI_IS_UI_RESOURCE |
940 | 0 | // target if ALLOW_CHROME is set. |
941 | 0 | // |
942 | 0 | // ALLOW_CHROME is a flag that we pass on all loads _except_ docshell |
943 | 0 | // loads (since docshell loads run the loaded content with its origin |
944 | 0 | // principal). So we're effectively allowing resource://, chrome://, |
945 | 0 | // and moz-icon:// source URIs to load resource://, chrome://, and |
946 | 0 | // moz-icon:// files, so long as they're not loading it as a document. |
947 | 0 | bool sourceIsUIResource; |
948 | 0 | rv = NS_URIChainHasFlags(aSourceBaseURI, |
949 | 0 | nsIProtocolHandler::URI_IS_UI_RESOURCE, |
950 | 0 | &sourceIsUIResource); |
951 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
952 | 0 | if (sourceIsUIResource) { |
953 | 0 | return NS_OK; |
954 | 0 | } |
955 | 0 | |
956 | 0 | if (targetScheme.EqualsLiteral("resource")) { |
957 | 0 | // Mochitests that need to load resource:// URIs not declared |
958 | 0 | // content-accessible in manifests should set the preference |
959 | 0 | // "security.all_resource_uri_content_accessible" true. |
960 | 0 | static bool sSecurityPrefCached = false; |
961 | 0 | static bool sAllResourceUriContentAccessible = false; |
962 | 0 | if (!sSecurityPrefCached) { |
963 | 0 | sSecurityPrefCached = true; |
964 | 0 | Preferences::AddBoolVarCache( |
965 | 0 | &sAllResourceUriContentAccessible, |
966 | 0 | "security.all_resource_uri_content_accessible", |
967 | 0 | false); |
968 | 0 | } |
969 | 0 | if (sAllResourceUriContentAccessible) { |
970 | 0 | return NS_OK; |
971 | 0 | } |
972 | 0 | |
973 | 0 | nsCOMPtr<nsIProtocolHandler> ph; |
974 | 0 | rv = sIOService->GetProtocolHandler("resource", getter_AddRefs(ph)); |
975 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
976 | 0 | if (!ph) { |
977 | 0 | return NS_ERROR_DOM_BAD_URI; |
978 | 0 | } |
979 | 0 | |
980 | 0 | nsCOMPtr<nsIResProtocolHandler> rph = do_QueryInterface(ph); |
981 | 0 | if (!rph) { |
982 | 0 | return NS_ERROR_DOM_BAD_URI; |
983 | 0 | } |
984 | 0 | |
985 | 0 | bool accessAllowed = false; |
986 | 0 | rph->AllowContentToAccess(aTargetBaseURI, &accessAllowed); |
987 | 0 | if (accessAllowed) { |
988 | 0 | return NS_OK; |
989 | 0 | } |
990 | 0 | } else if (targetScheme.EqualsLiteral("chrome")) { |
991 | 0 | // Allow the load only if the chrome package is whitelisted. |
992 | 0 | nsCOMPtr<nsIXULChromeRegistry> reg( |
993 | 0 | do_GetService(NS_CHROMEREGISTRY_CONTRACTID)); |
994 | 0 | if (reg) { |
995 | 0 | bool accessAllowed = false; |
996 | 0 | reg->AllowContentToAccess(aTargetBaseURI, &accessAllowed); |
997 | 0 | if (accessAllowed) { |
998 | 0 | return NS_OK; |
999 | 0 | } |
1000 | 0 | } |
1001 | 0 | } |
1002 | 0 | } |
1003 | 0 | |
1004 | 0 | if (reportErrors) { |
1005 | 0 | ReportError(errorTag, aSourceURI, aTargetURI, aFromPrivateWindow); |
1006 | 0 | } |
1007 | 0 | return NS_ERROR_DOM_BAD_URI; |
1008 | 0 | } |
1009 | 0 |
|
1010 | 0 | // Check for target URI pointing to a file |
1011 | 0 | rv = NS_URIChainHasFlags(aTargetURI, |
1012 | 0 | nsIProtocolHandler::URI_IS_LOCAL_FILE, |
1013 | 0 | &hasFlags); |
1014 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1015 | 0 | if (hasFlags) { |
1016 | 0 | // Allow domains that were whitelisted in the prefs. In 99.9% of cases, |
1017 | 0 | // this array is empty. |
1018 | 0 | bool isWhitelisted; |
1019 | 0 | MOZ_ALWAYS_SUCCEEDS(InFileURIWhitelist(aSourceURI, &isWhitelisted)); |
1020 | 0 | if (isWhitelisted) { |
1021 | 0 | return NS_OK; |
1022 | 0 | } |
1023 | 0 | |
1024 | 0 | // Allow chrome:// |
1025 | 0 | bool isChrome = false; |
1026 | 0 | if (NS_SUCCEEDED(aSourceBaseURI->SchemeIs("chrome", &isChrome)) && isChrome) { |
1027 | 0 | return NS_OK; |
1028 | 0 | } |
1029 | 0 | |
1030 | 0 | // Nothing else. |
1031 | 0 | if (reportErrors) { |
1032 | 0 | ReportError(errorTag, aSourceURI, aTargetURI, aFromPrivateWindow); |
1033 | 0 | } |
1034 | 0 | return NS_ERROR_DOM_BAD_URI; |
1035 | 0 | } |
1036 | 0 |
|
1037 | 0 | // OK, everyone is allowed to load this, since unflagged handlers are |
1038 | 0 | // deprecated but treated as URI_LOADABLE_BY_ANYONE. But check whether we |
1039 | 0 | // need to warn. At some point we'll want to make this warning into an |
1040 | 0 | // error and treat unflagged handlers as URI_DANGEROUS_TO_LOAD. |
1041 | 0 | rv = NS_URIChainHasFlags(aTargetBaseURI, |
1042 | 0 | nsIProtocolHandler::URI_LOADABLE_BY_ANYONE, |
1043 | 0 | &hasFlags); |
1044 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1045 | 0 | // NB: we also get here if the base URI is URI_LOADABLE_BY_SUBSUMERS, |
1046 | 0 | // and none of the rest of the nested chain of URIs for aTargetURI |
1047 | 0 | // prohibits the load, so avoid warning in that case: |
1048 | 0 | bool hasSubsumersFlag = false; |
1049 | 0 | rv = NS_URIChainHasFlags(aTargetBaseURI, |
1050 | 0 | nsIProtocolHandler::URI_LOADABLE_BY_SUBSUMERS, |
1051 | 0 | &hasSubsumersFlag); |
1052 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1053 | 0 | if (!hasFlags && !hasSubsumersFlag) { |
1054 | 0 | nsCOMPtr<nsIStringBundle> bundle = BundleHelper::GetOrCreate(); |
1055 | 0 | if (bundle) { |
1056 | 0 | nsAutoString message; |
1057 | 0 | NS_ConvertASCIItoUTF16 ucsTargetScheme(targetScheme); |
1058 | 0 | const char16_t* formatStrings[] = { ucsTargetScheme.get() }; |
1059 | 0 | rv = bundle->FormatStringFromName("ProtocolFlagError", |
1060 | 0 | formatStrings, |
1061 | 0 | ArrayLength(formatStrings), |
1062 | 0 | message); |
1063 | 0 | if (NS_SUCCEEDED(rv)) { |
1064 | 0 | nsCOMPtr<nsIConsoleService> console( |
1065 | 0 | do_GetService("@mozilla.org/consoleservice;1")); |
1066 | 0 | NS_ENSURE_TRUE(console, NS_ERROR_FAILURE); |
1067 | 0 |
|
1068 | 0 | console->LogStringMessage(message.get()); |
1069 | 0 | } |
1070 | 0 | } |
1071 | 0 | } |
1072 | 0 |
|
1073 | 0 | return NS_OK; |
1074 | 0 | } |
1075 | | |
1076 | | nsresult |
1077 | | nsScriptSecurityManager::ReportError(const char* aMessageTag, nsIURI* aSource, |
1078 | | nsIURI* aTarget, bool aFromPrivateWindow) |
1079 | 0 | { |
1080 | 0 | nsresult rv; |
1081 | 0 | NS_ENSURE_TRUE(aSource && aTarget, NS_ERROR_NULL_POINTER); |
1082 | 0 |
|
1083 | 0 | // Get the source URL spec |
1084 | 0 | nsAutoCString sourceSpec; |
1085 | 0 | rv = aSource->GetAsciiSpec(sourceSpec); |
1086 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1087 | 0 |
|
1088 | 0 | // Get the target URL spec |
1089 | 0 | nsAutoCString targetSpec; |
1090 | 0 | rv = aTarget->GetAsciiSpec(targetSpec); |
1091 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1092 | 0 |
|
1093 | 0 | nsCOMPtr<nsIStringBundle> bundle = BundleHelper::GetOrCreate(); |
1094 | 0 | if (NS_WARN_IF(!bundle)) { |
1095 | 0 | return NS_OK; |
1096 | 0 | } |
1097 | 0 | |
1098 | 0 | // Localize the error message |
1099 | 0 | nsAutoString message; |
1100 | 0 | NS_ConvertASCIItoUTF16 ucsSourceSpec(sourceSpec); |
1101 | 0 | NS_ConvertASCIItoUTF16 ucsTargetSpec(targetSpec); |
1102 | 0 | const char16_t *formatStrings[] = { ucsSourceSpec.get(), ucsTargetSpec.get() }; |
1103 | 0 | rv = bundle->FormatStringFromName(aMessageTag, |
1104 | 0 | formatStrings, |
1105 | 0 | ArrayLength(formatStrings), |
1106 | 0 | message); |
1107 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1108 | 0 |
|
1109 | 0 | nsCOMPtr<nsIConsoleService> console(do_GetService(NS_CONSOLESERVICE_CONTRACTID)); |
1110 | 0 | NS_ENSURE_TRUE(console, NS_ERROR_FAILURE); |
1111 | 0 | nsCOMPtr<nsIScriptError> error(do_CreateInstance(NS_SCRIPTERROR_CONTRACTID)); |
1112 | 0 | NS_ENSURE_TRUE(error, NS_ERROR_FAILURE); |
1113 | 0 |
|
1114 | 0 | // using category of "SOP" so we can link to MDN |
1115 | 0 | rv = error->Init(message, EmptyString(), |
1116 | 0 | EmptyString(), 0, 0, |
1117 | 0 | nsIScriptError::errorFlag, |
1118 | 0 | "SOP", aFromPrivateWindow); |
1119 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1120 | 0 | console->LogMessage(error); |
1121 | 0 | return NS_OK; |
1122 | 0 | } |
1123 | | |
1124 | | NS_IMETHODIMP |
1125 | | nsScriptSecurityManager::CheckLoadURIStrWithPrincipal(nsIPrincipal* aPrincipal, |
1126 | | const nsACString& aTargetURIStr, |
1127 | | uint32_t aFlags) |
1128 | 0 | { |
1129 | 0 | nsresult rv; |
1130 | 0 | nsCOMPtr<nsIURI> target; |
1131 | 0 | rv = NS_NewURI(getter_AddRefs(target), aTargetURIStr, |
1132 | 0 | nullptr, nullptr, sIOService); |
1133 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1134 | 0 |
|
1135 | 0 | rv = CheckLoadURIWithPrincipal(aPrincipal, target, aFlags); |
1136 | 0 | if (rv == NS_ERROR_DOM_BAD_URI) { |
1137 | 0 | // Don't warn because NS_ERROR_DOM_BAD_URI is one of the expected |
1138 | 0 | // return values. |
1139 | 0 | return rv; |
1140 | 0 | } |
1141 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1142 | 0 |
|
1143 | 0 | // Now start testing fixup -- since aTargetURIStr is a string, not |
1144 | 0 | // an nsIURI, we may well end up fixing it up before loading. |
1145 | 0 | // Note: This needs to stay in sync with the nsIURIFixup api. |
1146 | 0 | nsCOMPtr<nsIURIFixup> fixup = do_GetService(NS_URIFIXUP_CONTRACTID); |
1147 | 0 | if (!fixup) { |
1148 | 0 | return rv; |
1149 | 0 | } |
1150 | 0 | |
1151 | 0 | uint32_t flags[] = { |
1152 | 0 | nsIURIFixup::FIXUP_FLAG_NONE, |
1153 | 0 | nsIURIFixup::FIXUP_FLAG_FIX_SCHEME_TYPOS, |
1154 | 0 | nsIURIFixup::FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP, |
1155 | 0 | nsIURIFixup::FIXUP_FLAGS_MAKE_ALTERNATE_URI, |
1156 | 0 | nsIURIFixup::FIXUP_FLAG_ALLOW_KEYWORD_LOOKUP | |
1157 | 0 | nsIURIFixup::FIXUP_FLAGS_MAKE_ALTERNATE_URI |
1158 | 0 | }; |
1159 | 0 |
|
1160 | 0 | for (uint32_t i = 0; i < ArrayLength(flags); ++i) { |
1161 | 0 | rv = fixup->CreateFixupURI(aTargetURIStr, flags[i], nullptr, |
1162 | 0 | getter_AddRefs(target)); |
1163 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1164 | 0 |
|
1165 | 0 | rv = CheckLoadURIWithPrincipal(aPrincipal, target, aFlags); |
1166 | 0 | if (rv == NS_ERROR_DOM_BAD_URI) { |
1167 | 0 | // Don't warn because NS_ERROR_DOM_BAD_URI is one of the expected |
1168 | 0 | // return values. |
1169 | 0 | return rv; |
1170 | 0 | } |
1171 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1172 | 0 | } |
1173 | 0 |
|
1174 | 0 | return rv; |
1175 | 0 | } |
1176 | | |
1177 | | NS_IMETHODIMP |
1178 | | nsScriptSecurityManager::InFileURIWhitelist(nsIURI* aUri, bool* aResult) |
1179 | 0 | { |
1180 | 0 | MOZ_ASSERT(aUri); |
1181 | 0 | MOZ_ASSERT(aResult); |
1182 | 0 |
|
1183 | 0 | *aResult = false; |
1184 | 0 | for (nsIURI* uri : EnsureFileURIWhitelist()) { |
1185 | 0 | if (EqualOrSubdomain(aUri, uri)) { |
1186 | 0 | *aResult = true; |
1187 | 0 | return NS_OK; |
1188 | 0 | } |
1189 | 0 | } |
1190 | 0 |
|
1191 | 0 | return NS_OK; |
1192 | 0 | } |
1193 | | |
1194 | | ///////////////// Principals /////////////////////// |
1195 | | |
1196 | | NS_IMETHODIMP |
1197 | | nsScriptSecurityManager::GetSystemPrincipal(nsIPrincipal **result) |
1198 | 6 | { |
1199 | 6 | NS_ADDREF(*result = mSystemPrincipal); |
1200 | 6 | |
1201 | 6 | return NS_OK; |
1202 | 6 | } |
1203 | | |
1204 | | NS_IMETHODIMP |
1205 | | nsScriptSecurityManager::CreateCodebasePrincipal(nsIURI* aURI, JS::Handle<JS::Value> aOriginAttributes, |
1206 | | JSContext* aCx, nsIPrincipal** aPrincipal) |
1207 | 0 | { |
1208 | 0 | OriginAttributes attrs; |
1209 | 0 | if (!aOriginAttributes.isObject() || !attrs.Init(aCx, aOriginAttributes)) { |
1210 | 0 | return NS_ERROR_INVALID_ARG; |
1211 | 0 | } |
1212 | 0 | nsCOMPtr<nsIPrincipal> prin = BasePrincipal::CreateCodebasePrincipal(aURI, attrs); |
1213 | 0 | prin.forget(aPrincipal); |
1214 | 0 | return *aPrincipal ? NS_OK : NS_ERROR_FAILURE; |
1215 | 0 | } |
1216 | | |
1217 | | NS_IMETHODIMP |
1218 | | nsScriptSecurityManager::CreateCodebasePrincipalFromOrigin(const nsACString& aOrigin, |
1219 | | nsIPrincipal** aPrincipal) |
1220 | 0 | { |
1221 | 0 | if (StringBeginsWith(aOrigin, NS_LITERAL_CSTRING("["))) { |
1222 | 0 | return NS_ERROR_INVALID_ARG; |
1223 | 0 | } |
1224 | 0 | |
1225 | 0 | if (StringBeginsWith(aOrigin, NS_LITERAL_CSTRING(NS_NULLPRINCIPAL_SCHEME ":"))) { |
1226 | 0 | return NS_ERROR_INVALID_ARG; |
1227 | 0 | } |
1228 | 0 | |
1229 | 0 | nsCOMPtr<nsIPrincipal> prin = BasePrincipal::CreateCodebasePrincipal(aOrigin); |
1230 | 0 | prin.forget(aPrincipal); |
1231 | 0 | return *aPrincipal ? NS_OK : NS_ERROR_FAILURE; |
1232 | 0 | } |
1233 | | |
1234 | | NS_IMETHODIMP |
1235 | | nsScriptSecurityManager::CreateNullPrincipal(JS::Handle<JS::Value> aOriginAttributes, |
1236 | | JSContext* aCx, nsIPrincipal** aPrincipal) |
1237 | 0 | { |
1238 | 0 | OriginAttributes attrs; |
1239 | 0 | if (!aOriginAttributes.isObject() || !attrs.Init(aCx, aOriginAttributes)) { |
1240 | 0 | return NS_ERROR_INVALID_ARG; |
1241 | 0 | } |
1242 | 0 | nsCOMPtr<nsIPrincipal> prin = NullPrincipal::Create(attrs); |
1243 | 0 | prin.forget(aPrincipal); |
1244 | 0 | return NS_OK; |
1245 | 0 | } |
1246 | | |
1247 | | NS_IMETHODIMP |
1248 | | nsScriptSecurityManager:: |
1249 | | GetLoadContextCodebasePrincipal(nsIURI* aURI, |
1250 | | nsILoadContext* aLoadContext, |
1251 | | nsIPrincipal** aPrincipal) |
1252 | 0 | { |
1253 | 0 | NS_ENSURE_STATE(aLoadContext); |
1254 | 0 | OriginAttributes docShellAttrs; |
1255 | 0 | aLoadContext->GetOriginAttributes(docShellAttrs); |
1256 | 0 |
|
1257 | 0 | nsCOMPtr<nsIPrincipal> prin = |
1258 | 0 | BasePrincipal::CreateCodebasePrincipal(aURI, docShellAttrs); |
1259 | 0 | prin.forget(aPrincipal); |
1260 | 0 | return *aPrincipal ? NS_OK : NS_ERROR_FAILURE; |
1261 | 0 | } |
1262 | | |
1263 | | NS_IMETHODIMP |
1264 | | nsScriptSecurityManager::GetDocShellCodebasePrincipal(nsIURI* aURI, |
1265 | | nsIDocShell* aDocShell, |
1266 | | nsIPrincipal** aPrincipal) |
1267 | 0 | { |
1268 | 0 | nsCOMPtr<nsIPrincipal> prin = |
1269 | 0 | BasePrincipal::CreateCodebasePrincipal(aURI, nsDocShell::Cast(aDocShell)->GetOriginAttributes()); |
1270 | 0 | prin.forget(aPrincipal); |
1271 | 0 | return *aPrincipal ? NS_OK : NS_ERROR_FAILURE; |
1272 | 0 | } |
1273 | | |
1274 | | NS_IMETHODIMP |
1275 | | nsScriptSecurityManager::CanCreateWrapper(JSContext *cx, |
1276 | | const nsIID &aIID, |
1277 | | nsISupports *aObj, |
1278 | | nsIClassInfo *aClassInfo) |
1279 | 3.24M | { |
1280 | 3.24M | // XXX Special case for Exception ? |
1281 | 3.24M | |
1282 | 3.24M | // We give remote-XUL whitelisted domains a free pass here. See bug 932906. |
1283 | 3.24M | JS::Rooted<JS::Realm*> contextRealm(cx, JS::GetCurrentRealmOrNull(cx)); |
1284 | 3.24M | MOZ_RELEASE_ASSERT(contextRealm); |
1285 | 3.24M | if (!xpc::AllowContentXBLScope(contextRealm)) { |
1286 | 0 | return NS_OK; |
1287 | 0 | } |
1288 | 3.24M | |
1289 | 3.24M | if (nsContentUtils::IsCallerChrome()) { |
1290 | 3.24M | return NS_OK; |
1291 | 3.24M | } |
1292 | 0 | |
1293 | 0 | //-- Access denied, report an error |
1294 | 0 | nsAutoCString originUTF8; |
1295 | 0 | nsIPrincipal* subjectPrincipal = nsContentUtils::SubjectPrincipal(); |
1296 | 0 | GetPrincipalDomainOrigin(subjectPrincipal, originUTF8); |
1297 | 0 | NS_ConvertUTF8toUTF16 originUTF16(originUTF8); |
1298 | 0 | nsAutoCString classInfoNameUTF8; |
1299 | 0 | if (aClassInfo) { |
1300 | 0 | aClassInfo->GetClassDescription(classInfoNameUTF8); |
1301 | 0 | } |
1302 | 0 | if (classInfoNameUTF8.IsEmpty()) { |
1303 | 0 | classInfoNameUTF8.AssignLiteral("UnnamedClass"); |
1304 | 0 | } |
1305 | 0 |
|
1306 | 0 | nsCOMPtr<nsIStringBundle> bundle = BundleHelper::GetOrCreate(); |
1307 | 0 | if (NS_WARN_IF(!bundle)) { |
1308 | 0 | return NS_OK; |
1309 | 0 | } |
1310 | 0 | |
1311 | 0 | NS_ConvertUTF8toUTF16 classInfoUTF16(classInfoNameUTF8); |
1312 | 0 | nsresult rv; |
1313 | 0 | nsAutoString errorMsg; |
1314 | 0 | if (originUTF16.IsEmpty()) { |
1315 | 0 | const char16_t* formatStrings[] = { classInfoUTF16.get() }; |
1316 | 0 | rv = bundle->FormatStringFromName("CreateWrapperDenied", |
1317 | 0 | formatStrings, |
1318 | 0 | 1, |
1319 | 0 | errorMsg); |
1320 | 0 | } else { |
1321 | 0 | const char16_t* formatStrings[] = { classInfoUTF16.get(), |
1322 | 0 | originUTF16.get() }; |
1323 | 0 | rv = bundle->FormatStringFromName("CreateWrapperDeniedForOrigin", |
1324 | 0 | formatStrings, |
1325 | 0 | 2, |
1326 | 0 | errorMsg); |
1327 | 0 | } |
1328 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1329 | 0 |
|
1330 | 0 | SetPendingException(cx, errorMsg.get()); |
1331 | 0 | return NS_ERROR_DOM_XPCONNECT_ACCESS_DENIED; |
1332 | 0 | } |
1333 | | |
1334 | | NS_IMETHODIMP |
1335 | | nsScriptSecurityManager::CanCreateInstance(JSContext *cx, |
1336 | | const nsCID &aCID) |
1337 | 1.62M | { |
1338 | 1.62M | if (nsContentUtils::IsCallerChrome()) { |
1339 | 1.62M | return NS_OK; |
1340 | 1.62M | } |
1341 | 0 | |
1342 | 0 | //-- Access denied, report an error |
1343 | 0 | nsAutoCString errorMsg("Permission denied to create instance of class. CID="); |
1344 | 0 | char cidStr[NSID_LENGTH]; |
1345 | 0 | aCID.ToProvidedString(cidStr); |
1346 | 0 | errorMsg.Append(cidStr); |
1347 | 0 | SetPendingExceptionASCII(cx, errorMsg.get()); |
1348 | 0 | return NS_ERROR_DOM_XPCONNECT_ACCESS_DENIED; |
1349 | 0 | } |
1350 | | |
1351 | | NS_IMETHODIMP |
1352 | | nsScriptSecurityManager::CanGetService(JSContext *cx, |
1353 | | const nsCID &aCID) |
1354 | 0 | { |
1355 | 0 | if (nsContentUtils::IsCallerChrome()) { |
1356 | 0 | return NS_OK; |
1357 | 0 | } |
1358 | 0 | |
1359 | 0 | //-- Access denied, report an error |
1360 | 0 | nsAutoCString errorMsg("Permission denied to get service. CID="); |
1361 | 0 | char cidStr[NSID_LENGTH]; |
1362 | 0 | aCID.ToProvidedString(cidStr); |
1363 | 0 | errorMsg.Append(cidStr); |
1364 | 0 | SetPendingExceptionASCII(cx, errorMsg.get()); |
1365 | 0 | return NS_ERROR_DOM_XPCONNECT_ACCESS_DENIED; |
1366 | 0 | } |
1367 | | |
1368 | | const char sJSEnabledPrefName[] = "javascript.enabled"; |
1369 | | const char sFileOriginPolicyPrefName[] = |
1370 | | "security.fileuri.strict_origin_policy"; |
1371 | | |
1372 | | static const char* kObservedPrefs[] = { |
1373 | | sJSEnabledPrefName, |
1374 | | sFileOriginPolicyPrefName, |
1375 | | "capability.policy.", |
1376 | | nullptr |
1377 | | }; |
1378 | | |
1379 | | |
1380 | | ///////////////////////////////////////////// |
1381 | | // Constructor, Destructor, Initialization // |
1382 | | ///////////////////////////////////////////// |
1383 | | nsScriptSecurityManager::nsScriptSecurityManager(void) |
1384 | | : mPrefInitialized(false) |
1385 | | , mIsJavaScriptEnabled(false) |
1386 | 3 | { |
1387 | 3 | static_assert(sizeof(intptr_t) == sizeof(void*), |
1388 | 3 | "intptr_t and void* have different lengths on this platform. " |
1389 | 3 | "This may cause a security failure with the SecurityLevel union."); |
1390 | 3 | } |
1391 | | |
1392 | | nsresult nsScriptSecurityManager::Init() |
1393 | 3 | { |
1394 | 3 | nsresult rv = CallGetService(NS_IOSERVICE_CONTRACTID, &sIOService); |
1395 | 3 | NS_ENSURE_SUCCESS(rv, rv); |
1396 | 3 | |
1397 | 3 | InitPrefs(); |
1398 | 3 | |
1399 | 3 | // Create our system principal singleton |
1400 | 3 | RefPtr<SystemPrincipal> system = SystemPrincipal::Create(); |
1401 | 3 | |
1402 | 3 | mSystemPrincipal = system; |
1403 | 3 | |
1404 | 3 | //-- Register security check callback in the JS engine |
1405 | 3 | // Currently this is used to control access to function.caller |
1406 | 3 | sContext = danger::GetJSContext(); |
1407 | 3 | |
1408 | 3 | static const JSSecurityCallbacks securityCallbacks = { |
1409 | 3 | ContentSecurityPolicyPermitsJSAction, |
1410 | 3 | JSPrincipalsSubsume, |
1411 | 3 | }; |
1412 | 3 | |
1413 | 3 | MOZ_ASSERT(!JS_GetSecurityCallbacks(sContext)); |
1414 | 3 | JS_SetSecurityCallbacks(sContext, &securityCallbacks); |
1415 | 3 | JS_InitDestroyPrincipalsCallback(sContext, nsJSPrincipals::Destroy); |
1416 | 3 | |
1417 | 3 | JS_SetTrustedPrincipals(sContext, system); |
1418 | 3 | |
1419 | 3 | return NS_OK; |
1420 | 3 | } |
1421 | | |
1422 | | static StaticRefPtr<nsScriptSecurityManager> gScriptSecMan; |
1423 | | |
1424 | | nsScriptSecurityManager::~nsScriptSecurityManager(void) |
1425 | 0 | { |
1426 | 0 | Preferences::UnregisterPrefixCallbacks( |
1427 | 0 | PREF_CHANGE_METHOD(nsScriptSecurityManager::ScriptSecurityPrefChanged), |
1428 | 0 | kObservedPrefs, |
1429 | 0 | this); |
1430 | 0 | if (mDomainPolicy) { |
1431 | 0 | mDomainPolicy->Deactivate(); |
1432 | 0 | } |
1433 | 0 | // ContentChild might hold a reference to the domain policy, |
1434 | 0 | // and it might release it only after the security manager is |
1435 | 0 | // gone. But we can still assert this for the main process. |
1436 | 0 | MOZ_ASSERT_IF(XRE_IsParentProcess(), |
1437 | 0 | !mDomainPolicy); |
1438 | 0 | } |
1439 | | |
1440 | | void |
1441 | | nsScriptSecurityManager::Shutdown() |
1442 | 0 | { |
1443 | 0 | if (sContext) { |
1444 | 0 | JS_SetSecurityCallbacks(sContext, nullptr); |
1445 | 0 | JS_SetTrustedPrincipals(sContext, nullptr); |
1446 | 0 | sContext = nullptr; |
1447 | 0 | } |
1448 | 0 |
|
1449 | 0 | NS_IF_RELEASE(sIOService); |
1450 | 0 | BundleHelper::Shutdown(); |
1451 | 0 | } |
1452 | | |
1453 | | nsScriptSecurityManager * |
1454 | | nsScriptSecurityManager::GetScriptSecurityManager() |
1455 | 6 | { |
1456 | 6 | return gScriptSecMan; |
1457 | 6 | } |
1458 | | |
1459 | | /* static */ void |
1460 | | nsScriptSecurityManager::InitStatics() |
1461 | 3 | { |
1462 | 3 | RefPtr<nsScriptSecurityManager> ssManager = new nsScriptSecurityManager(); |
1463 | 3 | nsresult rv = ssManager->Init(); |
1464 | 3 | if (NS_FAILED(rv)) { |
1465 | 0 | MOZ_CRASH("ssManager->Init() failed"); |
1466 | 0 | } |
1467 | 3 | |
1468 | 3 | ClearOnShutdown(&gScriptSecMan); |
1469 | 3 | gScriptSecMan = ssManager; |
1470 | 3 | } |
1471 | | |
1472 | | // Currently this nsGenericFactory constructor is used only from FastLoad |
1473 | | // (XPCOM object deserialization) code, when "creating" the system principal |
1474 | | // singleton. |
1475 | | already_AddRefed<SystemPrincipal> |
1476 | | nsScriptSecurityManager::SystemPrincipalSingletonConstructor() |
1477 | 0 | { |
1478 | 0 | if (gScriptSecMan) |
1479 | 0 | return do_AddRef(gScriptSecMan->mSystemPrincipal).downcast<SystemPrincipal>(); |
1480 | 0 | return nullptr; |
1481 | 0 | } |
1482 | | |
1483 | | struct IsWhitespace { |
1484 | 0 | static bool Test(char aChar) { return NS_IsAsciiWhitespace(aChar); }; |
1485 | | }; |
1486 | | struct IsWhitespaceOrComma { |
1487 | 0 | static bool Test(char aChar) { return aChar == ',' || NS_IsAsciiWhitespace(aChar); }; |
1488 | | }; |
1489 | | |
1490 | | template <typename Predicate> |
1491 | | uint32_t SkipPast(const nsCString& str, uint32_t base) |
1492 | 0 | { |
1493 | 0 | while (base < str.Length() && Predicate::Test(str[base])) { |
1494 | 0 | ++base; |
1495 | 0 | } |
1496 | 0 | return base; |
1497 | 0 | } Unexecuted instantiation: unsigned int SkipPast<IsWhitespace>(nsTString<char> const&, unsigned int) Unexecuted instantiation: unsigned int SkipPast<IsWhitespaceOrComma>(nsTString<char> const&, unsigned int) |
1498 | | |
1499 | | template <typename Predicate> |
1500 | | uint32_t SkipUntil(const nsCString& str, uint32_t base) |
1501 | 0 | { |
1502 | 0 | while (base < str.Length() && !Predicate::Test(str[base])) { |
1503 | 0 | ++base; |
1504 | 0 | } |
1505 | 0 | return base; |
1506 | 0 | } Unexecuted instantiation: unsigned int SkipUntil<IsWhitespace>(nsTString<char> const&, unsigned int) Unexecuted instantiation: unsigned int SkipUntil<IsWhitespaceOrComma>(nsTString<char> const&, unsigned int) |
1507 | | |
1508 | | inline void |
1509 | | nsScriptSecurityManager::ScriptSecurityPrefChanged(const char* aPref) |
1510 | 3 | { |
1511 | 3 | MOZ_ASSERT(mPrefInitialized); |
1512 | 3 | mIsJavaScriptEnabled = |
1513 | 3 | Preferences::GetBool(sJSEnabledPrefName, mIsJavaScriptEnabled); |
1514 | 3 | sStrictFileOriginPolicy = |
1515 | 3 | Preferences::GetBool(sFileOriginPolicyPrefName, false); |
1516 | 3 | mFileURIWhitelist.reset(); |
1517 | 3 | } |
1518 | | |
1519 | | void |
1520 | | nsScriptSecurityManager::AddSitesToFileURIWhitelist(const nsCString& aSiteList) |
1521 | 0 | { |
1522 | 0 | for (uint32_t base = SkipPast<IsWhitespace>(aSiteList, 0), bound = 0; |
1523 | 0 | base < aSiteList.Length(); |
1524 | 0 | base = SkipPast<IsWhitespace>(aSiteList, bound)) |
1525 | 0 | { |
1526 | 0 | // Grab the current site. |
1527 | 0 | bound = SkipUntil<IsWhitespace>(aSiteList, base); |
1528 | 0 | nsAutoCString site(Substring(aSiteList, base, bound - base)); |
1529 | 0 |
|
1530 | 0 | // Check if the URI is schemeless. If so, add both http and https. |
1531 | 0 | nsAutoCString unused; |
1532 | 0 | if (NS_FAILED(sIOService->ExtractScheme(site, unused))) { |
1533 | 0 | AddSitesToFileURIWhitelist(NS_LITERAL_CSTRING("http://") + site); |
1534 | 0 | AddSitesToFileURIWhitelist(NS_LITERAL_CSTRING("https://") + site); |
1535 | 0 | continue; |
1536 | 0 | } |
1537 | 0 |
|
1538 | 0 | // Convert it to a URI and add it to our list. |
1539 | 0 | nsCOMPtr<nsIURI> uri; |
1540 | 0 | nsresult rv = NS_NewURI(getter_AddRefs(uri), site, nullptr, nullptr, sIOService); |
1541 | 0 | if (NS_SUCCEEDED(rv)) { |
1542 | 0 | mFileURIWhitelist.ref().AppendElement(uri); |
1543 | 0 | } else { |
1544 | 0 | nsCOMPtr<nsIConsoleService> console(do_GetService("@mozilla.org/consoleservice;1")); |
1545 | 0 | if (console) { |
1546 | 0 | nsAutoString msg = NS_LITERAL_STRING("Unable to to add site to file:// URI whitelist: ") + |
1547 | 0 | NS_ConvertASCIItoUTF16(site); |
1548 | 0 | console->LogStringMessage(msg.get()); |
1549 | 0 | } |
1550 | 0 | } |
1551 | 0 | } |
1552 | 0 | } |
1553 | | |
1554 | | nsresult |
1555 | | nsScriptSecurityManager::InitPrefs() |
1556 | 3 | { |
1557 | 3 | nsIPrefBranch* branch = Preferences::GetRootBranch(); |
1558 | 3 | NS_ENSURE_TRUE(branch, NS_ERROR_FAILURE); |
1559 | 3 | |
1560 | 3 | mPrefInitialized = true; |
1561 | 3 | |
1562 | 3 | // Set the initial value of the "javascript.enabled" prefs |
1563 | 3 | ScriptSecurityPrefChanged(); |
1564 | 3 | |
1565 | 3 | // set observer callbacks in case the value of the prefs change |
1566 | 3 | Preferences::RegisterPrefixCallbacks( |
1567 | 3 | PREF_CHANGE_METHOD(nsScriptSecurityManager::ScriptSecurityPrefChanged), |
1568 | 3 | kObservedPrefs, |
1569 | 3 | this); |
1570 | 3 | |
1571 | 3 | OriginAttributes::InitPrefs(); |
1572 | 3 | |
1573 | 3 | return NS_OK; |
1574 | 3 | } |
1575 | | |
1576 | | NS_IMETHODIMP |
1577 | | nsScriptSecurityManager::GetDomainPolicyActive(bool *aRv) |
1578 | 0 | { |
1579 | 0 | *aRv = !!mDomainPolicy; |
1580 | 0 | return NS_OK; |
1581 | 0 | } |
1582 | | |
1583 | | NS_IMETHODIMP |
1584 | | nsScriptSecurityManager::ActivateDomainPolicy(nsIDomainPolicy** aRv) |
1585 | 0 | { |
1586 | 0 | if (!XRE_IsParentProcess()) { |
1587 | 0 | return NS_ERROR_SERVICE_NOT_AVAILABLE; |
1588 | 0 | } |
1589 | 0 | |
1590 | 0 | return ActivateDomainPolicyInternal(aRv); |
1591 | 0 | } |
1592 | | |
1593 | | NS_IMETHODIMP |
1594 | | nsScriptSecurityManager::ActivateDomainPolicyInternal(nsIDomainPolicy** aRv) |
1595 | 0 | { |
1596 | 0 | // We only allow one domain policy at a time. The holder of the previous |
1597 | 0 | // policy must explicitly deactivate it first. |
1598 | 0 | if (mDomainPolicy) { |
1599 | 0 | return NS_ERROR_SERVICE_NOT_AVAILABLE; |
1600 | 0 | } |
1601 | 0 | |
1602 | 0 | mDomainPolicy = new DomainPolicy(); |
1603 | 0 | nsCOMPtr<nsIDomainPolicy> ptr = mDomainPolicy; |
1604 | 0 | ptr.forget(aRv); |
1605 | 0 | return NS_OK; |
1606 | 0 | } |
1607 | | |
1608 | | // Intentionally non-scriptable. Script must have a reference to the |
1609 | | // nsIDomainPolicy to deactivate it. |
1610 | | void |
1611 | | nsScriptSecurityManager::DeactivateDomainPolicy() |
1612 | 0 | { |
1613 | 0 | mDomainPolicy = nullptr; |
1614 | 0 | } |
1615 | | |
1616 | | void |
1617 | | nsScriptSecurityManager::CloneDomainPolicy(DomainPolicyClone* aClone) |
1618 | 0 | { |
1619 | 0 | MOZ_ASSERT(aClone); |
1620 | 0 | if (mDomainPolicy) { |
1621 | 0 | mDomainPolicy->CloneDomainPolicy(aClone); |
1622 | 0 | } else { |
1623 | 0 | aClone->active() = false; |
1624 | 0 | } |
1625 | 0 | } |
1626 | | |
1627 | | NS_IMETHODIMP |
1628 | | nsScriptSecurityManager::PolicyAllowsScript(nsIURI* aURI, bool *aRv) |
1629 | 3 | { |
1630 | 3 | nsresult rv; |
1631 | 3 | |
1632 | 3 | // Compute our rule. If we don't have any domain policy set up that might |
1633 | 3 | // provide exceptions to this rule, we're done. |
1634 | 3 | *aRv = mIsJavaScriptEnabled; |
1635 | 3 | if (!mDomainPolicy) { |
1636 | 3 | return NS_OK; |
1637 | 3 | } |
1638 | 0 | |
1639 | 0 | // We have a domain policy. Grab the appropriate set of exceptions to the |
1640 | 0 | // rule (either the blacklist or the whitelist, depending on whether script |
1641 | 0 | // is enabled or disabled by default). |
1642 | 0 | nsCOMPtr<nsIDomainSet> exceptions; |
1643 | 0 | nsCOMPtr<nsIDomainSet> superExceptions; |
1644 | 0 | if (*aRv) { |
1645 | 0 | mDomainPolicy->GetBlacklist(getter_AddRefs(exceptions)); |
1646 | 0 | mDomainPolicy->GetSuperBlacklist(getter_AddRefs(superExceptions)); |
1647 | 0 | } else { |
1648 | 0 | mDomainPolicy->GetWhitelist(getter_AddRefs(exceptions)); |
1649 | 0 | mDomainPolicy->GetSuperWhitelist(getter_AddRefs(superExceptions)); |
1650 | 0 | } |
1651 | 0 |
|
1652 | 0 | bool contains; |
1653 | 0 | rv = exceptions->Contains(aURI, &contains); |
1654 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1655 | 0 | if (contains) { |
1656 | 0 | *aRv = !*aRv; |
1657 | 0 | return NS_OK; |
1658 | 0 | } |
1659 | 0 | rv = superExceptions->ContainsSuperDomain(aURI, &contains); |
1660 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
1661 | 0 | if (contains) { |
1662 | 0 | *aRv = !*aRv; |
1663 | 0 | } |
1664 | 0 |
|
1665 | 0 | return NS_OK; |
1666 | 0 | } |
1667 | | |
1668 | | const nsTArray<nsCOMPtr<nsIURI>>& |
1669 | | nsScriptSecurityManager::EnsureFileURIWhitelist() |
1670 | 0 | { |
1671 | 0 | if (mFileURIWhitelist.isSome()) { |
1672 | 0 | return mFileURIWhitelist.ref(); |
1673 | 0 | } |
1674 | 0 | |
1675 | 0 | // |
1676 | 0 | // Rebuild the set of principals for which we allow file:// URI loads. This |
1677 | 0 | // implements a small subset of an old pref-based CAPS people that people |
1678 | 0 | // have come to depend on. See bug 995943. |
1679 | 0 | // |
1680 | 0 | |
1681 | 0 | mFileURIWhitelist.emplace(); |
1682 | 0 | nsAutoCString policies; |
1683 | 0 | mozilla::Preferences::GetCString("capability.policy.policynames", policies); |
1684 | 0 | for (uint32_t base = SkipPast<IsWhitespaceOrComma>(policies, 0), bound = 0; |
1685 | 0 | base < policies.Length(); |
1686 | 0 | base = SkipPast<IsWhitespaceOrComma>(policies, bound)) |
1687 | 0 | { |
1688 | 0 | // Grab the current policy name. |
1689 | 0 | bound = SkipUntil<IsWhitespaceOrComma>(policies, base); |
1690 | 0 | auto policyName = Substring(policies, base, bound - base); |
1691 | 0 |
|
1692 | 0 | // Figure out if this policy allows loading file:// URIs. If not, we can skip it. |
1693 | 0 | nsCString checkLoadURIPrefName = NS_LITERAL_CSTRING("capability.policy.") + |
1694 | 0 | policyName + |
1695 | 0 | NS_LITERAL_CSTRING(".checkloaduri.enabled"); |
1696 | 0 | nsAutoString value; |
1697 | 0 | nsresult rv = Preferences::GetString(checkLoadURIPrefName.get(), value); |
1698 | 0 | if (NS_FAILED(rv) || !value.LowerCaseEqualsLiteral("allaccess")) { |
1699 | 0 | continue; |
1700 | 0 | } |
1701 | 0 | |
1702 | 0 | // Grab the list of domains associated with this policy. |
1703 | 0 | nsCString domainPrefName = NS_LITERAL_CSTRING("capability.policy.") + |
1704 | 0 | policyName + |
1705 | 0 | NS_LITERAL_CSTRING(".sites"); |
1706 | 0 | nsAutoCString siteList; |
1707 | 0 | Preferences::GetCString(domainPrefName.get(), siteList); |
1708 | 0 | AddSitesToFileURIWhitelist(siteList); |
1709 | 0 | } |
1710 | 0 |
|
1711 | 0 | return mFileURIWhitelist.ref(); |
1712 | 0 | } |