Debian Codegraph @

documentation-sources/content/xdocs/using/scripting/security.xml

raw
1 <?xml version="1.0"?>
2
3 <!--
4
5 Licensed to the Apache Software Foundation (ASF) under one or more
6 contributor license agreements. See the NOTICE file distributed with
7 this work for additional information regarding copyright ownership.
8 The ASF licenses this file to You under the Apache License, Version 2.0
9 (the "License"); you may not use this file except in compliance with
10 the License. You may obtain a copy of the License at
11
12 http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 -->
21
22 <!-- ========================================================================= -->
23 <!-- author [email protected] -->
24 <!-- version $Id$ -->
25 <!-- ========================================================================= -->
26
27 <!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
28 <document>
29 <header>
30 <title>Script security</title>
31 </header>
32
33 <body>
34 <p>
35 With the addition of scripting support in Batik 1.5, security features
36 have also been added to enable users of the Batik toolkit to run
37 scripts in a secure manner.
38 </p>
39 <p>
40 If you are using script, please make sure you have reviewed the
41 <a href="../../index.html#SecurityWarning">Script Security
42 Warning</a> with regards to the Batik 1.5 release.
43 </p>
44
45 <section id="sandbox">
46 <title>Running scripts securely</title>
47
48 <p>
49 The Java platform offers a lot of options for running applications
50 securely. Running an application securely requires that it runs in a
51 so-called security sand-box which controls all the access the
52 application makes to restricted resources (such as the file system).
53 </p>
54
55 <p>
56 The concept of Java security is an application-wide concept. As such,
57 it has to be applied at the application level (and not at the
58 framework level). In the Batik distribution, the sample applications
59 (such as the <a href="site:browser">Squiggle Browser</a> and the
60 <a href="site:rasterizer">SVG rasterizer</a>) apply
61 security (or disable it) but the framework does not apply it: it is
62 security-aware (meaning that it is able to handle security
63 exceptions).
64 </p>
65
66 <section id="enforcing">
67 <title>Enforcing security in a Batik application</title>
68
69 <p>
70 Enforcing security in a Batik application is done by setting a
71 <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/SecurityManager.html">java.lang.SecurityManager</a>.
72 This security manager will apply the security settings of the Java
73 platform (as defined by the
74 <em>jre-dir</em><code>/lib/security/java.policy</code> and,
75 optionally, by the policy file whose URL is defined in the
76 <code>java.security.policy</code> system property).
77 </p>
78
79 <p>
80 The
81 <a class="class" href="../../javadoc/org/apache/batik/util/ApplicationSecurityEnforcer.html">org.apache.batik.util.ApplicationSecurityEnforcer</a>
82 helper class makes it easier for Batik application
83 developers to add security support in their applications. That
84 helper class is used by the sample Batik applications.
85 </p>
86 </section>
87
88 <section id="squiggle">
89 <title>Squiggle security</title>
90
91 <p>
92 The Squiggle browser lets the user decide whether or not scripts
93 should be run securely (see the “Browser Options” in the
94 preference dialog box). When scripts are run securely, Squiggle
95 will enforce the security settings as follows:
96 </p>
97 <ul>
98 <li>
99 The default policy is defined by the policy file found
100 in the distribution: <code>org/apache/batik/apps/svgbrowser/svgbrowser.policy</code>.
101 In the binary distribution, that file would be in the
102 <code>batik-squiggle.jar</code> file. In the source
103 distribution, that file would be in the
104 <code>resources</code> directory. The default policy
105 file gives appropriate permissions to the Batik code,
106 the XML parser and the Rhino scripting engine and very
107 limited permissions to scripts.
108 </li>
109 <li>
110 At startup time, and whenever the preference settings are
111 modified, Squiggle makes a copy of the default policy
112 and appends any additional permissions granted to
113 scripts by the user through the preference
114 settings. This policy file can be found in the
115 <code>[user.home]/.batik</code> directory, and is
116 called <code>__svgbrowser.policy</code>. Note that
117 this file is automatically generated and should not be
118 modified manually (as any edits would be lost).
119 </li>
120 <li>
121 The policy defined as described above is enforced
122 unless the <code>java.security.policy</code> system
123 property is defined. In that case, the policy defined
124 by the system property takes precedence and the policy
125 file generated from the Squiggle preferences is
126 ignored.
127 </li>
128 </ul>
129
130 <p>
131 <strong>Important note:</strong>
132 The default policy files assume that the applications use the
133 Xerces parser and give appropriate permissions to its
134 <code>lib/xerces-2_5_0.jar</code> jar file. If you are using
135 a different XML parser, you need to modify the policy files to
136 grant the proper permissions to your XML parser instead of
137 Xerces. You will have to replace:
138 </p>
139 <source>grant codeBase "${app.dev.base}/lib/xerces_2_5_0.jar" {
140 permission java.security.AllPermission;
141 };</source>
142 <p>with:</p>
143 <source>grant codeBase "${app.dev.base}/lib/myXMLParser.jar" {
144 permission java.security.AllPermission;
145 };</source>
146 <p>
147 in the <code>resources/org/apache/batik/apps/svgbrowser/resources/svgbrowser.policy</code>
148 file (for the source distribution) and do the same in
149 <code>resources/org/apache/batik/apps/svgbrowser/resources/svgbrowser.bin.policy</code> (for
150 the binary distribution which will then need to be rebuilt with the <code>build dist-zip</code>
151 command.
152 </p>
153 <p>
154 Alternatively, you can write your own policy file and specify its
155 URL through the <code>java.security.policy</code> system property (which you can
156 specify through the <code>-Djava.security.policy=</code><em>url</em> command line
157 option).
158 </p>
159 </section>
160 </section>
161
162 <section id="externalResources">
163 <title>Controlling access to external resources</title>
164
165 <p>
166 SVG makes a very powerful use of external resources in many elements
167 such as <code>image</code>, <code>use</code>, <code>font</code>,
168 <code>script</code> and <code>radialGradient</code>. There are over
169 fifteen SVG elements that may reference external resources that way.
170 </p>
171 <p>
172 In some environments, and typically for security reasons, it is
173 important to control the resources referenced by an SVG document
174 and be able to accept or reject these resources.
175 </p>
176 <p>
177 In the Batik toolkit, this flexibility is provided by the
178 <a class="class" href="../../javadoc/org/apache/batik/bridge/UserAgent.html">org.apache.batik.bridge.UserAgent</a>
179 interface which can define various strategies with regards to external
180 resources. By providing a new implementation of the
181 <code>UserAgent</code> interface, it is possible to apply the desired
182 security strategy for scripts and external resources.
183 </p>
184 <p>
185 The following <code>UserAgent</code> methods are provided for
186 that purpose:
187 </p>
188 <ul>
189 <li>
190 <code>getScriptSecurity(scriptType, scriptURL, docURL)</code>
191 should return the
192 <a class="class" href="../../javadoc/org/apache/batik/bridge/ScriptSecurity.html">ScriptSecurity</a>
193 strategy for a script of type <code>scriptType</code> (e.g.,
194 <code>text/ecmascript</code>) coming from <code>scriptURL</code>,
195 when referenced from the document whose URL is <code>docURL</code>.
196 </li>
197 <li>
198 <code>getExternalResourceSecurity(resourceURL, docURL)</code>
199 should return the
200 <a class="class" href="../../javadoc/org/apache/batik/bridge/ExternalResourceSecurity.html">ExternalResourceSecurity</a>
201 strategry for a resource coming from <code>resourceURL</code>
202 referenced from the document at URL <code>docURL</code>.
203 </li>
204 </ul>
205
206 <p>
207 The <code>ScriptSecurity</code> and <code>ExternalResourceSecurity</code>
208 interfaces have methods (<code>checkLoadScript</code> and
209 <code>checkLoadExternalResource</code> respectively) which should
210 throw a
211 <a class="class" href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/SecurityException.html">SecurityException</a>
212 if accessing the script or resource is considered a security violation.
213 </p>
214 <note>
215 The <code>UserAgent</code> interface has two additional methods
216 (<code>checkLoadScript</code> and <code>checkLoadExternalResource</code>
217 which are meant to provide a short hand for getting a security strategy
218 object and calling the <code>checkLoad</code>* method on that object.
219 This is how the
220 <a class="class" href="../../javadoc/org/apache/batik/bridge/UserAgentAdapter.html">org.apache.batik.bridge.UserAgentAdapter</a>
221 class implements this method.
222 </note>
223 <p>
224 Batik provides the following set of <code>ScriptSecurity</code>
225 implementations:
226 </p>
227
228 <dl>
229 <dt>NoLoadScriptSecurity</dt>
230 <dd>
231 <p>The script resource should not be loaded.</p>
232 </dd>
233 <dt>EmbededScriptSecurity</dt>
234 <dd>
235 <p>
236 The script resource will only be loaded if it is embeded in the SVG
237 document referencing it. This means that script attributes (such as
238 <code>onclick</code> on a <code>rect</code> element),
239 inline <code>script</code> elements and <code>script</code> elements
240 using a <code>data:</code> URL as its <code>xlink:href</code> attribute
241 value will be allowed. All other script resources should not be
242 loaded.
243 </p>
244 </dd>
245 <dt>DefaultScriptSecurity</dt>
246 <dd>
247 <p>
248 The script resource will only
249 be loaded if it is embeded in the SVG document (see the description
250 of <code>EmbededScriptSecurity</code>) or if it is coming from the same
251 location as the document referencing the script. If the document comes
252 from a network server, then any script coming from that server will
253 be allowed. If the document comes from the file system, then only
254 scripts under the same directory root as the SVG document will be allowed.
255 </p>
256 </dd>
257 <dt>RelaxedScriptSecurity</dt>
258 <dd>
259 <p>
260 Scripts from any location can be loaded.
261 </p>
262 </dd>
263 </dl>
264
265 <p>
266 In addition, Batik provides the following set of
267 <code>ExternalResourceSecurity</code> implementations:
268 </p>
269
270 <dl>
271 <dt>NoLoadExternalResourceSecurity</dt>
272 <dd>
273 <p>No external references are allowed.</p>
274 </dd>
275 <dt>EmbededExternalResourceSecurity</dt>
276 <dd>
277 <p>
278 Only resources embeded into the file are allowed (i.e., references
279 through the <code>data:</code> protocol).
280 </p>
281 </dd>
282 <dt>DefaultExternalResourceSecurity</dt>
283 <dd>
284 <p>
285 Embeded external resources (see above) and resources coming from
286 the same location as the document referencing them are allowed.
287 </p>
288 </dd>
289 <dt>RelaxedExternalResourceSecurity</dt>
290 <dd>
291 <p>Resources from any location can be loaded.</p>
292 </dd>
293 </dl>
294
295 </section>
296
297 </body>
298 </document>