Bug 1890277: part 2) Add `require-trusted-types-for` directive to CSP parser, guarded...
[gecko.git] / caps / nsIScriptSecurityManager.idl
blobd4941f3de339f2d7ee64900986f0480517f0029a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsISupports.idl"
7 #include "nsIPrincipal.idl"
8 interface nsIURI;
9 interface nsIChannel;
10 interface nsIClassInfo;
11 interface nsIDocShell;
12 interface nsIDomainPolicy;
13 interface nsILoadContext;
15 %{ C++
16 #include "jspubtd.h"
18 namespace mozilla {
19 namespace dom {
20 class DomainPolicyClone;
25 [ptr] native JSContextPtr(JSContext);
26 [ptr] native JSObjectPtr(JSObject);
27 [ptr] native DomainPolicyClonePtr(mozilla::dom::DomainPolicyClone);
29 [scriptable, builtinclass, uuid(51daad87-3a0c-44cc-b620-7356801c9022)]
30 interface nsIScriptSecurityManager : nsISupports
32 /**
33 * For each of these hooks returning NS_OK means 'let the action continue'.
34 * Returning an error code means 'veto the action'. XPConnect will return
35 * false to the js engine if the action is vetoed. The implementor of this
36 * interface is responsible for setting a JS exception into the JSContext
37 * if that is appropriate.
39 [noscript] void canCreateWrapper(in JSContextPtr aJSContext,
40 in nsIIDRef aIID,
41 in nsISupports aObj,
42 in nsIClassInfo aClassInfo);
44 [noscript] void canCreateInstance(in JSContextPtr aJSContext,
45 in nsCIDRef aCID);
47 [noscript] void canGetService(in JSContextPtr aJSContext,
48 in nsCIDRef aCID);
50 /**
51 * Check that the script currently running in context "cx" can load "uri".
53 * Will return error code NS_ERROR_DOM_BAD_URI if the load request
54 * should be denied.
56 * @param cx the JSContext of the script causing the load
57 * @param uri the URI that is being loaded
59 [noscript] void checkLoadURIFromScript(in JSContextPtr cx, in nsIURI uri);
61 /**
62 * Default CheckLoadURI permissions
64 // Default permissions
65 const unsigned long STANDARD = 0;
67 // Indicate that the load is a load of a new document that is not
68 // user-triggered. Here "user-triggered" could be broadly interpreted --
69 // for example, scripted sets of window.location.href might be treated as
70 // "user-triggered" in some circumstances. A typical example of a load
71 // that is not user-triggered is a <meta> refresh load. If this flag is
72 // set, the load will be denied if the originating principal's URI has the
73 // nsIProtocolHandler::URI_FORBIDS_AUTOMATIC_DOCUMENT_REPLACEMENT flag set.
74 const unsigned long LOAD_IS_AUTOMATIC_DOCUMENT_REPLACEMENT = 1 << 0;
76 // Allow the loading of chrome URLs by non-chrome URLs. Use with great
77 // care! This will actually allow the loading of any URI which has the
78 // nsIProtocolHandler::URI_IS_UI_RESOURCE protocol handler flag set. Ths
79 // probably means at least chrome: and resource:.
80 const unsigned long ALLOW_CHROME = 1 << 1;
82 // Don't allow URLs which would inherit the caller's principal (such as
83 // javascript: or data:) to load. See
84 // nsIProtocolHandler::URI_INHERITS_SECURITY_CONTEXT.
85 const unsigned long DISALLOW_INHERIT_PRINCIPAL = 1 << 2;
87 // Alias for DISALLOW_INHERIT_PRINCIPAL for backwards compat with
88 // JS-implemented extensions.
89 const unsigned long DISALLOW_SCRIPT_OR_DATA = DISALLOW_INHERIT_PRINCIPAL;
91 // Don't allow javascript: URLs to load
92 // WARNING: Support for this value was added in Mozilla 1.7.8 and
93 // Firefox 1.0.4. Use in prior versions WILL BE IGNORED.
94 // When using this, make sure that you actually want DISALLOW_SCRIPT, not
95 // DISALLOW_INHERIT_PRINCIPAL
96 const unsigned long DISALLOW_SCRIPT = 1 << 3;
98 // Do not report errors if we just want to check if a principal can load
99 // a URI to not unnecessarily spam the error console.
100 const unsigned long DONT_REPORT_ERRORS = 1 << 4;
103 * Check that content with principal aPrincipal can load "uri".
105 * Will return error code NS_ERROR_DOM_BAD_URI if the load request
106 * should be denied.
108 * @param aPrincipal the principal identifying the actor causing the load
109 * @param uri the URI that is being loaded
110 * @param flags the permission set, see above
111 * @param innerWindowID the window ID for error reporting. If this is 0
112 * (which happens automatically if it's not passed from JS), errors
113 * will only appear in the browser console, not window-associated
114 * consoles like the web console.
116 [binaryname(CheckLoadURIWithPrincipal)]
117 void checkLoadURIWithPrincipalXPCOM(in nsIPrincipal aPrincipal,
118 in nsIURI uri,
119 in unsigned long flags,
120 [optional] in unsigned long long innerWindowID);
123 * Same as the above, but when called from JS, raises exceptions with more
124 * useful messages, including both the tested URI and the principal string.
126 [implicit_jscontext, binaryname(CheckLoadURIWithPrincipalFromJS)]
127 void checkLoadURIWithPrincipal(in nsIPrincipal aPrincipal,
128 in nsIURI uri,
129 [optional] in unsigned long flags,
130 [optional] in unsigned long long innerWindowID);
133 * Similar to checkLoadURIWithPrincipal but there are two differences:
135 * 1) The URI is a string, not a URI object.
136 * 2) This function assumes that the URI may still be subject to fixup (and
137 * hence will check whether fixed-up versions of the URI are allowed to
138 * load as well); if any of the versions of this URI is not allowed, this
139 * function will return error code NS_ERROR_DOM_BAD_URI.
141 [binaryname(CheckLoadURIStrWithPrincipal)]
142 void checkLoadURIStrWithPrincipalXPCOM(in nsIPrincipal aPrincipal,
143 in AUTF8String uri,
144 in unsigned long flags);
147 * Same as the above, but when called from JS, raises exceptions with more
148 * useful messages, including both the tested URI and the principal string.
150 [implicit_jscontext, binaryname(CheckLoadURIStrWithPrincipalFromJS)]
151 void checkLoadURIStrWithPrincipal(in nsIPrincipal aPrincipal,
152 in AUTF8String uri,
153 [optional] in unsigned long flags);
156 * Returns true if the URI is from a domain that is allow-listed through
157 * prefs to be allowed to use file:// URIs.
158 * @param aUri the URI to be tested
160 boolean inFileURIAllowlist(in nsIURI aUri);
162 ///////////////// Principals ///////////////////////
165 * Return the all-powerful system principal.
167 nsIPrincipal getSystemPrincipal();
170 * Returns a principal that has the OriginAttributes of the load context.
171 * @param loadContext to get the OriginAttributes from.
173 nsIPrincipal getLoadContextContentPrincipal(in nsIURI uri,
174 in nsILoadContext loadContext);
177 * Returns a principal that has the OriginAttributes of the docshell.
178 * @param docShell to get the OriginAttributes from.
180 nsIPrincipal getDocShellContentPrincipal(in nsIURI uri,
181 in nsIDocShell docShell);
184 * If this is a content principal, return a copy with different
185 * origin attributes.
187 [implicit_jscontext]
188 nsIPrincipal principalWithOA(in nsIPrincipal principal,
189 in jsval originAttributes);
192 * Returns a principal whose origin is composed of |uri| and |originAttributes|.
193 * See nsIPrincipal.idl for a description of origin attributes, and
194 * ChromeUtils.webidl for a list of origin attributes and their defaults.
196 [implicit_jscontext]
197 nsIPrincipal createContentPrincipal(in nsIURI uri, in jsval originAttributes);
200 * Returns a principal whose origin is the one we pass in.
201 * See nsIPrincipal.idl for a description of origin attributes, and
202 * ChromeUtils.webidl for a list of origin attributes and their defaults.
204 nsIPrincipal createContentPrincipalFromOrigin(in ACString origin);
207 * Takes a principal and returns a string representation of it or a nullptr if it can't be serialized.
208 * Example output: `{"1": {"0": "https://mozilla.com", "2": "^privateBrowsingId=1"}}`
210 ACString principalToJSON(in nsIPrincipal principal);
213 * Takes a string of the following format:
214 * `{"1": {"0": "https://mozilla.com", "2": "^privateBrowsingId=1"}}`
215 * and turns it into a principal or a nullptr on error.
217 nsIPrincipal JSONToPrincipal(in ACString json);
220 * Returns a unique nonce principal with |originAttributes|.
221 * See nsIPrincipal.idl for a description of origin attributes, and
222 * ChromeUtils.webidl for a list of origin attributes and their defaults.
224 [implicit_jscontext]
225 nsIPrincipal createNullPrincipal(in jsval originAttributes);
228 * Returns OK if aSourceURI and target have the same "origin"
229 * (scheme, host, and port).
230 * ReportError flag suppresses error reports for functions that
231 * don't need reporting.
232 * FromPrivateWindow indicates whether the error occurs in a private
233 * window or not.
235 void checkSameOriginURI(in nsIURI aSourceURI,
236 in nsIURI aTargetURI,
237 in boolean reportError,
238 in boolean fromPrivateWindow);
241 * Get the principal for the given channel. This will typically be the
242 * channel owner if there is one, and the content principal for the
243 * channel's URI otherwise. aChannel must not be null.
245 nsIPrincipal getChannelResultPrincipal(in nsIChannel aChannel);
248 * Get the storage principal for the given channel. This is basically the
249 * same of getChannelResultPrincipal() execept for trackers, where we
250 * return a principal with a different OriginAttributes.
252 nsIPrincipal getChannelResultStoragePrincipal(in nsIChannel aChannel);
255 * This method returns 2 principals from a nsIChannel:
256 * - aPrincipal is the regular principal.
257 * - aPartitionedPrincipal is aPrincipal plus an isolation key in its
258 * originAttributes.
259 * See more in StoragePrincipalHelper.h
261 void getChannelResultPrincipals(in nsIChannel aChannel,
262 out nsIPrincipal aPrincipal,
263 out nsIPrincipal aPartitionedPrincipal);
266 * Temporary API until bug 1220687 is fixed.
268 * Returns the same value as getChannelResultPrincipal, but ignoring
269 * sandboxing. Specifically, if sandboxing would have prevented the
270 * channel's triggering principal from being returned by
271 * getChannelResultPrincipal, the triggering principal will be returned
272 * by this method.
274 * Note that this method only ignores sandboxing of the channel in
275 * question, it does not ignore sandboxing of any channels further up a
276 * document chain. The triggering principal itself may still be the null
277 * principal due to sandboxing further up a document chain. In that regard
278 * the ignoring of sandboxing is limited.
280 [noscript, nostdcall]
281 nsIPrincipal getChannelResultPrincipalIfNotSandboxed(in nsIChannel aChannel);
284 * Get the content principal for the channel's URI.
285 * aChannel must not be null.
287 nsIPrincipal getChannelURIPrincipal(in nsIChannel aChannel);
289 const unsigned long DEFAULT_USER_CONTEXT_ID = 0;
292 * The constant the indicates when mPrivateBrowsingId is _not_ in PBM.
293 * In other words, zero indicates Normal Browsing, and non-zero indicates PBM
295 const unsigned long DEFAULT_PRIVATE_BROWSING_ID = 0;
298 * Per-domain controls to enable and disable script. This system is designed
299 * to be used by at most one consumer, and enforces this with its semantics.
301 * Initially, domainPolicyActive is false. When activateDomainPolicy() is
302 * invoked, domainPolicyActive becomes true, and subsequent calls to
303 * activateDomainPolicy() will fail until deactivate() is invoked on the
304 * nsIDomainPolicy returned from activateDomainPolicy(). At this point,
305 * domainPolicyActive becomes false again, and a new consumer may acquire
306 * control of the system by invoking activateDomainPolicy().
308 nsIDomainPolicy activateDomainPolicy();
309 readonly attribute boolean domainPolicyActive;
312 * Only the parent process can directly access domain policies, child
313 * processes only have a read-only mirror to the one in the parent.
314 * For child processes the mirror is updated via messages
315 * and ContentChild will hold the DomainPolicy by calling
316 * ActivateDomainPolicyInternal directly. New consumer to this
317 * function should not be addded.
319 [noscript] nsIDomainPolicy activateDomainPolicyInternal();
322 * This function is for internal use only. Every time a child process is spawned, we
323 * must clone any active domain policies in the parent to the new child.
325 [noscript, notxpcom] void cloneDomainPolicy(in DomainPolicyClonePtr aClone);
328 * Query mechanism for the above policy.
330 * If domainPolicyEnabled is false, this simply returns the current value
331 * of javascript.enabled. Otherwise, it returns the same value, but taking
332 * the various blocklist/allowlist exceptions into account.
334 boolean policyAllowsScript(in nsIURI aDomain);
337 %{C++
338 #define NS_SCRIPTSECURITYMANAGER_CONTRACTID "@mozilla.org/scriptsecuritymanager;1"