1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=4 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/. */
7 #ifndef nsScriptSecurityManager_h__
8 #define nsScriptSecurityManager_h__
10 #include "nsIScriptSecurityManager.h"
12 #include "mozilla/Maybe.h"
13 #include "nsIPrincipal.h"
15 #include "nsServiceManagerUtils.h"
16 #include "nsStringFwd.h"
17 #include "js/TypeDecls.h"
22 class nsIStringBundle
;
25 class OriginAttributes
;
26 class SystemPrincipal
;
27 } // namespace mozilla
30 enum class RuntimeCode
;
33 /////////////////////////////
34 // nsScriptSecurityManager //
35 /////////////////////////////
36 #define NS_SCRIPTSECURITYMANAGER_CID \
38 0x7ee2a4c0, 0x4b93, 0x17d3, { \
39 0xba, 0x18, 0x00, 0x60, 0xb0, 0xf1, 0x99, 0xa2 \
43 class nsScriptSecurityManager final
: public nsIScriptSecurityManager
{
45 static void Shutdown();
47 NS_DEFINE_STATIC_CID_ACCESSOR(NS_SCRIPTSECURITYMANAGER_CID
)
50 NS_DECL_NSISCRIPTSECURITYMANAGER
52 static nsScriptSecurityManager
* GetScriptSecurityManager();
54 // Invoked exactly once, by XPConnect.
55 static void InitStatics();
57 void InitJSCallbacks(JSContext
* aCx
);
59 // This has to be static because it is called after gScriptSecMan is cleared.
60 static void ClearJSCallbacks(JSContext
* aCx
);
62 static already_AddRefed
<mozilla::SystemPrincipal
>
63 SystemPrincipalSingletonConstructor();
66 * Utility method for comparing two URIs. For security purposes, two URIs
67 * are equivalent if their schemes, hosts, and ports (if any) match. This
68 * method returns true if aSubjectURI and aObjectURI have the same origin,
71 static bool SecurityCompareURIs(nsIURI
* aSourceURI
, nsIURI
* aTargetURI
);
72 static uint32_t SecurityHashURI(nsIURI
* aURI
);
73 static bool IsHttpOrHttpsAndCrossOrigin(nsIURI
* aUriA
, nsIURI
* aUriB
);
75 static nsresult
ReportError(const char* aMessageTag
, nsIURI
* aSource
,
76 nsIURI
* aTarget
, bool aFromPrivateWindow
,
77 uint64_t aInnerWindowID
= 0);
78 static nsresult
ReportError(const char* aMessageTag
,
79 const nsACString
& sourceSpec
,
80 const nsACString
& targetSpec
,
81 bool aFromPrivateWindow
,
82 uint64_t aInnerWindowID
= 0);
84 static uint32_t HashPrincipalByOrigin(nsIPrincipal
* aPrincipal
);
86 static bool GetStrictFileOriginPolicy() { return sStrictFileOriginPolicy
; }
88 void DeactivateDomainPolicy();
91 // GetScriptSecurityManager is the only call that can make one
92 nsScriptSecurityManager();
93 virtual ~nsScriptSecurityManager();
95 // Decides, based on CSP, whether or not eval() and stuff can be executed.
96 static bool ContentSecurityPolicyPermitsJSAction(JSContext
* cx
,
98 JS::Handle
<JSString
*> aCode
);
100 static bool JSPrincipalsSubsume(JSPrincipals
* first
, JSPrincipals
* second
);
104 nsresult
InitPrefs();
106 static void ScriptSecurityPrefChanged(const char* aPref
, void* aSelf
);
107 void ScriptSecurityPrefChanged(const char* aPref
= nullptr);
109 inline void AddSitesToFileURIAllowlist(const nsCString
& aSiteList
);
111 nsresult
GetChannelResultPrincipal(nsIChannel
* aChannel
,
112 nsIPrincipal
** aPrincipal
,
113 bool aIgnoreSandboxing
);
115 nsresult
CheckLoadURIFlags(nsIURI
* aSourceURI
, nsIURI
* aTargetURI
,
116 nsIURI
* aSourceBaseURI
, nsIURI
* aTargetBaseURI
,
117 uint32_t aFlags
, bool aFromPrivateWindow
,
118 uint64_t aInnerWindowID
);
120 // Returns the file URI allowlist, initializing it if it has not been
122 const nsTArray
<nsCOMPtr
<nsIURI
>>& EnsureFileURIAllowlist();
124 nsCOMPtr
<nsIPrincipal
> mSystemPrincipal
;
125 bool mPrefInitialized
;
126 bool mIsJavaScriptEnabled
;
128 // List of URIs whose domains and sub-domains are allowlisted to allow
129 // access to file: URIs. Lazily initialized; isNothing() when not yet
131 mozilla::Maybe
<nsTArray
<nsCOMPtr
<nsIURI
>>> mFileURIAllowlist
;
133 // This machinery controls new-style domain policies. The old-style
134 // policy machinery will be removed soon.
135 nsCOMPtr
<nsIDomainPolicy
> mDomainPolicy
;
137 static std::atomic
<bool> sStrictFileOriginPolicy
;
139 static mozilla::StaticRefPtr
<nsIIOService
> sIOService
;
140 static nsIStringBundle
* sStrBundle
;
143 #endif // nsScriptSecurityManager_h__