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"
18 #include "js/TypeDecls.h"
23 class nsIStringBundle
;
26 class OriginAttributes
;
27 class SystemPrincipal
;
28 } // namespace mozilla
31 enum class RuntimeCode
;
34 /////////////////////////////
35 // nsScriptSecurityManager //
36 /////////////////////////////
37 #define NS_SCRIPTSECURITYMANAGER_CID \
39 0x7ee2a4c0, 0x4b93, 0x17d3, { \
40 0xba, 0x18, 0x00, 0x60, 0xb0, 0xf1, 0x99, 0xa2 \
44 class nsScriptSecurityManager final
: public nsIScriptSecurityManager
{
46 static void Shutdown();
48 NS_DEFINE_STATIC_CID_ACCESSOR(NS_SCRIPTSECURITYMANAGER_CID
)
51 NS_DECL_NSISCRIPTSECURITYMANAGER
53 static nsScriptSecurityManager
* GetScriptSecurityManager();
55 // Invoked exactly once, by XPConnect.
56 static void InitStatics();
58 void InitJSCallbacks(JSContext
* aCx
);
60 // This has to be static because it is called after gScriptSecMan is cleared.
61 static void ClearJSCallbacks(JSContext
* aCx
);
63 static already_AddRefed
<mozilla::SystemPrincipal
>
64 SystemPrincipalSingletonConstructor();
67 * Utility method for comparing two URIs. For security purposes, two URIs
68 * are equivalent if their schemes, hosts, and ports (if any) match. This
69 * method returns true if aSubjectURI and aObjectURI have the same origin,
72 static bool SecurityCompareURIs(nsIURI
* aSourceURI
, nsIURI
* aTargetURI
);
73 static uint32_t SecurityHashURI(nsIURI
* aURI
);
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 nsIIOService
* sIOService
;
140 static nsIStringBundle
* sStrBundle
;
143 #endif // nsScriptSecurityManager_h__