Debian Codegraph @

rhino/src/org/mozilla/javascript/SecurityController.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
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 // API class
8
9 package org.mozilla.javascript;
10
11 /**
12 * This class describes the support needed to implement security.
13 * <p>
14 * Three main pieces of functionality are required to implement
15 * security for JavaScript. First, it must be possible to define
16 * classes with an associated security domain. (This security
17 * domain may be any object incorporating notion of access
18 * restrictions that has meaning to an embedding; for a client-side
19 * JavaScript embedding this would typically be
20 * java.security.ProtectionDomain or similar object depending on an
21 * origin URL and/or a digital certificate.)
22 * Next it must be possible to get a security domain object that
23 * allows a particular action only if all security domains
24 * associated with code on the current Java stack allows it. And
25 * finally, it must be possible to execute script code with
26 * associated security domain injected into Java stack.
27 * <p>
28 * These three pieces of functionality are encapsulated in the
29 * SecurityController class.
30 *
31 * @see org.mozilla.javascript.Context#setSecurityController(SecurityController)
32 * @see java.lang.ClassLoader
33 * @since 1.5 Release 4
34 */
35 public abstract class SecurityController
36 {
37 private static SecurityController global;
38
39 // The method must NOT be public or protected
40 static SecurityController global()
41 {
42 return global;
43 }
44
45 /**
46 * Check if global {@link SecurityController} was already installed.
47 * @see #initGlobal(SecurityController controller)
48 */
49 public static boolean hasGlobal()
50 {
51 return global != null;
52 }
53
54 /**
55 * Initialize global controller that will be used for all
56 * security-related operations. The global controller takes precedence
57 * over already installed {@link Context}-specific controllers and cause
58 * any subsequent call to
59 * {@link Context#setSecurityController(SecurityController)}
60 * to throw an exception.
61 * <p>
62 * The method can only be called once.
63 *
64 * @see #hasGlobal()
65 */
66 public static void initGlobal(SecurityController controller)
67 {
68 if (controller == null) throw new IllegalArgumentException();
69 if (global != null) {
70 throw new SecurityException("Cannot overwrite already installed global SecurityController");
71 }
72 global = controller;
73 }
74
75 /**
76 * Get class loader-like object that can be used
77 * to define classes with the given security context.
78 * @param parentLoader parent class loader to delegate search for classes
79 * not defined by the class loader itself
80 * @param securityDomain some object specifying the security
81 * context of the code that is defined by the returned class loader.
82 */
83 public abstract GeneratedClassLoader createClassLoader(
84 ClassLoader parentLoader, Object securityDomain);
85
86 /**
87 * Create {@link GeneratedClassLoader} with restrictions imposed by
88 * staticDomain and all current stack frames.
89 * The method uses the SecurityController instance associated with the
90 * current {@link Context} to construct proper dynamic domain and create
91 * corresponding class loader.
92 * <par>
93 * If no SecurityController is associated with the current {@link Context} ,
94 * the method calls {@link Context#createClassLoader(ClassLoader parent)}.
95 *
96 * @param parent parent class loader. If null,
97 * {@link Context#getApplicationClassLoader()} will be used.
98 * @param staticDomain static security domain.
99 */
100 public static GeneratedClassLoader createLoader(
101 ClassLoader parent, Object staticDomain)
102 {
103 Context cx = Context.getContext();
104 if (parent == null) {
105 parent = cx.getApplicationClassLoader();
106 }
107 SecurityController sc = cx.getSecurityController();
108 GeneratedClassLoader loader;
109 if (sc == null) {
110 loader = cx.createClassLoader(parent);
111 } else {
112 Object dynamicDomain = sc.getDynamicSecurityDomain(staticDomain);
113 loader = sc.createClassLoader(parent, dynamicDomain);
114 }
115 return loader;
116 }
117
118 public static Class<?> getStaticSecurityDomainClass() {
119 SecurityController sc = Context.getContext().getSecurityController();
120 return sc == null ? null : sc.getStaticSecurityDomainClassInternal();
121 }
122
123 public Class<?> getStaticSecurityDomainClassInternal()
124 {
125 return null;
126 }
127
128 /**
129 * Get dynamic security domain that allows an action only if it is allowed
130 * by the current Java stack and <i>securityDomain</i>. If
131 * <i>securityDomain</i> is null, return domain representing permissions
132 * allowed by the current stack.
133 */
134 public abstract Object getDynamicSecurityDomain(Object securityDomain);
135
136 /**
137 * Call {@link
138 * Callable#call(Context cx, Scriptable scope, Scriptable thisObj,
139 * Object[] args)}
140 * of <i>callable</i> under restricted security domain where an action is
141 * allowed only if it is allowed according to the Java stack on the
142 * moment of the <i>execWithDomain</i> call and <i>securityDomain</i>.
143 * Any call to {@link #getDynamicSecurityDomain(Object)} during
144 * execution of <tt>callable.call(cx, scope, thisObj, args)</tt>
145 * should return a domain incorporate restrictions imposed by
146 * <i>securityDomain</i> and Java stack on the moment of callWithDomain
147 * invocation.
148 * <p>
149 * The method should always be overridden, it is not declared abstract
150 * for compatibility reasons.
151 */
152 public Object callWithDomain(Object securityDomain, Context cx,
153 final Callable callable, Scriptable scope,
154 final Scriptable thisObj, final Object[] args)
155 {
156 return execWithDomain(cx, scope, new Script()
157 {
158 public Object exec(Context cx, Scriptable scope)
159 {
160 return callable.call(cx, scope, thisObj, args);
161 }
162
163 }, securityDomain);
164 }
165
166 /**
167 * @deprecated The application should not override this method and instead
168 * override
169 * {@link #callWithDomain(Object securityDomain, Context cx, Callable callable, Scriptable scope, Scriptable thisObj, Object[] args)}.
170 */
171 @Deprecated
172 public Object execWithDomain(Context cx, Scriptable scope,
173 Script script, Object securityDomain)
174 {
175 throw new IllegalStateException("callWithDomain should be overridden");
176 }
177
178 }