| 1 |
/* |
| 2 |
|
| 3 |
Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 |
contributor license agreements. See the NOTICE file distributed with |
| 5 |
this work for additional information regarding copyright ownership. |
| 6 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 |
(the "License"); you may not use this file except in compliance with |
| 8 |
the License. You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
|
| 18 |
*/ |
| 19 |
package com.untrusted.script; |
| 20 |
|
| 21 |
import java.awt.AWTPermission; |
| 22 |
import java.io.FilePermission; |
| 23 |
import java.io.SerializablePermission; |
| 24 |
import java.lang.reflect.ReflectPermission; |
| 25 |
import java.net.NetPermission; |
| 26 |
import java.net.SocketPermission; |
| 27 |
import java.net.URL; |
| 28 |
import java.security.AllPermission; |
| 29 |
import java.security.Permission; |
| 30 |
import java.sql.SQLPermission; |
| 31 |
import java.util.PropertyPermission; |
| 32 |
|
| 33 |
import javax.sound.sampled.AudioPermission; |
| 34 |
|
| 35 |
import org.w3c.dom.Document; |
| 36 |
import org.w3c.dom.Element; |
| 37 |
import org.w3c.dom.events.Event; |
| 38 |
import org.w3c.dom.events.EventListener; |
| 39 |
import org.w3c.dom.events.EventTarget; |
| 40 |
|
| 41 |
import org.apache.batik.anim.dom.SVGOMDocument; |
| 42 |
import org.apache.batik.bridge.ScriptHandler; |
| 43 |
import org.apache.batik.w3c.dom.Window; |
| 44 |
|
| 45 |
/** |
| 46 |
* This class implements the ScriptHandler interface and represents an |
| 47 |
* example of untrusted code. |
| 48 |
* |
| 49 |
* It creates a number of Java Permissions and checks that access is denied. |
| 50 |
* the tests fail if the Permissions are granted. |
| 51 |
* |
| 52 |
* The only thing that the class should be allowed to make is a connection |
| 53 |
* back to the server that served the document containing this script. |
| 54 |
* |
| 55 |
* @author <a href="mailto:[email protected]">Vincent Hardy</a> |
| 56 |
* @version $Id$ |
| 57 |
*/ |
| 58 |
public class UntrustedScriptHandler implements ScriptHandler { |
| 59 |
public static final String svgNS = "http://www.w3.org/2000/svg"; |
| 60 |
|
| 61 |
/** |
| 62 |
* Path for the file tested with FilePermission |
| 63 |
*/ |
| 64 |
public static final String testedPath = "build.sh"; |
| 65 |
|
| 66 |
/** |
| 67 |
* Host which is used for testing |
| 68 |
*/ |
| 69 |
public static final String testedHost = "nagoya.apache.org:8080"; |
| 70 |
|
| 71 |
/** |
| 72 |
* Table of Permissions which will be tested. |
| 73 |
*/ |
| 74 |
protected static Object[][] basePermissions = { |
| 75 |
{"AllPermission", new AllPermission()}, |
| 76 |
{"FilePermission read", new FilePermission(testedPath, "read")}, |
| 77 |
{"FilePermission write", new FilePermission(testedPath, "write")}, |
| 78 |
{"FilePermission execute", new FilePermission(testedPath, "execute")}, |
| 79 |
{"FilePermission delete", new FilePermission(testedPath, "delete")}, |
| 80 |
// 1.4 {"ServicePermission", new ServicePermission("krbtgt/[email protected]", "initiate")}, |
| 81 |
{"SocketPermission accept", new SocketPermission(testedHost, "accept")}, |
| 82 |
{"SocketPermission connect", new SocketPermission(testedHost, "connect")}, |
| 83 |
{"SocketPermission listen", new SocketPermission(testedHost, "listen")}, |
| 84 |
{"SocketPermission resolve", new SocketPermission(testedHost, "resolve")}, |
| 85 |
{"AudioPermission play", new AudioPermission("play")}, |
| 86 |
{"AudioPermission record", new AudioPermission("record")}, |
| 87 |
{"AWTPermission accessClipboard", new AWTPermission("accessClipboard")}, |
| 88 |
{"AWTPermission accessEventQueue", new AWTPermission("accessEventQueue")}, |
| 89 |
{"AWTPermission listenToAllAWTEvents", new AWTPermission("listenToAllAWTEvents")}, |
| 90 |
{"AWTPermission showWindowWithoutWarningBanner", new AWTPermission("showWindowWithoutWarningBanner")}, |
| 91 |
{"AWTPermission readDisplayPixels", new AWTPermission("readDisplayPixels")}, |
| 92 |
{"AWTPermission createRobot", new AWTPermission("createRobot")}, |
| 93 |
{"AWTPermission fullScreenExclusive", new AWTPermission("fullScreenExclusive")}, |
| 94 |
// 1.4 {"DelegationPermission", new DelegationPermission()}, |
| 95 |
// 1.4 {"LoggingPermission", new LoggingPermission("control")}, |
| 96 |
{"NetPermission setDefaultAuthenticator", new NetPermission("setDefaultAuthenticator")}, |
| 97 |
{"NetPermission requestPasswordAuthentication", new NetPermission("requestPasswordAuthentication")}, |
| 98 |
{"NetPermission specifyStreamHandler", new NetPermission("specifyStreamHandler")}, |
| 99 |
{"PropertyPermission java.home read", new PropertyPermission("java.home", "read")}, |
| 100 |
{"PropertyPermission java.home write", new PropertyPermission("java.home", "write")}, |
| 101 |
{"ReflectPermission", new ReflectPermission("suppressAccessChecks")}, |
| 102 |
{"RuntimePermission createClassLoader", new RuntimePermission("createClassLoader")}, |
| 103 |
{"RuntimePermission getClassLoader", new RuntimePermission("getClassLoader")}, |
| 104 |
{"RuntimePermission setContextClassLoader", new RuntimePermission("setContextClassLoader")}, |
| 105 |
{"RuntimePermission setSecurityManager", new RuntimePermission("setSecurityManager")}, |
| 106 |
{"RuntimePermission createSecurityManager", new RuntimePermission("createSecurityManager")}, |
| 107 |
{"RuntimePermission exitVM", new RuntimePermission("exitVM")}, |
| 108 |
{"RuntimePermission shutdownHooks", new RuntimePermission("shutdownHooks")}, |
| 109 |
{"RuntimePermission setFactory", new RuntimePermission("setFactory")}, |
| 110 |
{"RuntimePermission setIO", new RuntimePermission("setIO")}, |
| 111 |
{"RuntimePermission modifyThread", new RuntimePermission("modifyThread")}, |
| 112 |
{"RuntimePermission stopThread", new RuntimePermission("stopThread")}, |
| 113 |
{"RuntimePermission modifyThreadGroup", new RuntimePermission("modifyThreadGroup")}, |
| 114 |
{"RuntimePermission getProtectionDomain", new RuntimePermission("getProtectionDomain")}, |
| 115 |
{"RuntimePermission readFileDescriptor", new RuntimePermission("readFileDescriptor")}, |
| 116 |
{"RuntimePermission writeFileDescriptor", new RuntimePermission("writeFileDescriptor")}, |
| 117 |
{"RuntimePermission loadLibrary.{library name}", new RuntimePermission("loadLibrary.{library name}")}, |
| 118 |
{"RuntimePermission accessClassInPackage.java.security", new RuntimePermission("accessClassInPackage.java.security")}, |
| 119 |
{"RuntimePermission defineClassInPackage.java.lang", new RuntimePermission("defineClassInPackage.java.lang")}, |
| 120 |
{"RuntimePermission accessDeclaredMembers", new RuntimePermission("accessDeclaredMembers")}, |
| 121 |
{"RuntimePermission queuePrintJob", new RuntimePermission("queuePrintJob")}, |
| 122 |
|
| 123 |
{"SecurityPermission createAccessControlContext", new SerializablePermission("createAccessControlContext")}, |
| 124 |
{"SecurityPermission getDomainCombiner", new SerializablePermission("getDomainCombiner")}, |
| 125 |
{"SecurityPermission getPolicy", new SerializablePermission("getPolicy")}, |
| 126 |
{"SecurityPermission setPolicy", new SerializablePermission("setPolicy")}, |
| 127 |
{"SecurityPermission setSystemScope", new SerializablePermission("setSystemScope")}, |
| 128 |
{"SecurityPermission setIdentityPublicKey", new SerializablePermission("setIdentityPublicKey")}, |
| 129 |
{"SecurityPermission setIdentityInfo", new SerializablePermission("setIdentityInfo")}, |
| 130 |
{"SecurityPermission addIdentityCertificate", new SerializablePermission("addIdentityCertificate")}, |
| 131 |
{"SecurityPermission removeIdentityCertificate", new SerializablePermission("removeIdentityCertificate")}, |
| 132 |
{"SecurityPermission printIdentity", new SerializablePermission("printIdentity")}, |
| 133 |
{"SecurityPermission getSignerPrivateKey", new SerializablePermission("getSignerPrivateKey")}, |
| 134 |
{"SecurityPermission setSignerKeyPair", new SerializablePermission("setSignerKeyPair")}, |
| 135 |
|
| 136 |
{"SerializablePermission enableSubclassImplementation", new SerializablePermission("enableSubclassImplementation")}, |
| 137 |
{"SerializablePermission enableSubstitution", new SerializablePermission("enableSubstitution")}, |
| 138 |
|
| 139 |
{"SQLPermission", new SQLPermission("setLog")}, |
| 140 |
|
| 141 |
// 1.4 {"SSLPermission setHostnameVerifier", new SSLPermission("setHostnameVerifier")} |
| 142 |
// 1.4{"SSLPermission getSSLSessionContext", new SSLPermission("getSSLSessionContext")} |
| 143 |
}; |
| 144 |
|
| 145 |
/** |
| 146 |
* Set of Permissions to test. One is added if the Document is loaded from a host |
| 147 |
*/ |
| 148 |
private Object[][] permissions; |
| 149 |
|
| 150 |
/** |
| 151 |
* Reference to the rectangles which show the test status |
| 152 |
*/ |
| 153 |
private Element[] statusRects; |
| 154 |
|
| 155 |
/** |
| 156 |
* Runs this handler. This method is called by the SVG viewer |
| 157 |
* when the scripts are loaded. |
| 158 |
* @param doc The current document. |
| 159 |
* @param win An object which represents the current viewer. |
| 160 |
*/ |
| 161 |
public void run(final Document doc, final Window win){ |
| 162 |
int nGrantedTmp = 0; |
| 163 |
|
| 164 |
// |
| 165 |
// If the document is loaded over the network, check that the |
| 166 |
// class has permission to access the server |
| 167 |
// |
| 168 |
URL docURL = ((SVGOMDocument)doc).getURLObject(); |
| 169 |
if (docURL != null && docURL.getHost() != null && !"".equals(docURL.getHost())) { |
| 170 |
permissions = new Object[basePermissions.length + 3][2]; |
| 171 |
System.arraycopy(basePermissions, 0, |
| 172 |
permissions, 3, basePermissions.length); |
| 173 |
|
| 174 |
String docHost = docURL.getHost(); |
| 175 |
if (docURL.getPort() != -1) { |
| 176 |
docHost += ":" + docURL.getPort(); |
| 177 |
} |
| 178 |
|
| 179 |
permissions[0][0] = "SocketPermission accept " + docHost; |
| 180 |
permissions[0][1] = new SocketPermission(docHost, "accept"); |
| 181 |
permissions[1][0] = "SocketPermission connect " + docHost; |
| 182 |
permissions[1][1] = new SocketPermission(docHost, "connect"); |
| 183 |
permissions[2][0] = "SocketPermission resolve " + docHost; |
| 184 |
permissions[2][1] = new SocketPermission(docHost, "resolve"); |
| 185 |
nGrantedTmp = 3; |
| 186 |
} else { |
| 187 |
permissions = basePermissions; |
| 188 |
} |
| 189 |
|
| 190 |
// Captures the number of permissions which should be |
| 191 |
// granted to this code. |
| 192 |
final int nGranted = nGrantedTmp; |
| 193 |
|
| 194 |
// |
| 195 |
// Build a table in the scrollable area of the document |
| 196 |
// |
| 197 |
Element securityResults = doc.getElementById("securityResults"); |
| 198 |
statusRects = new Element[permissions.length]; |
| 199 |
|
| 200 |
for (int i=0; i<permissions.length; i++){ |
| 201 |
Element textElt = doc.createElementNS(svgNS, "text"); |
| 202 |
textElt.setAttributeNS(null, "x", "55"); |
| 203 |
textElt.setAttributeNS(null, "y", "" + (85 + i*20)); |
| 204 |
textElt.appendChild(doc.createTextNode(permissions[i][0].toString())); |
| 205 |
securityResults.appendChild(textElt); |
| 206 |
|
| 207 |
Element rectElt = doc.createElementNS(svgNS, "rect"); |
| 208 |
rectElt.setAttributeNS(null, "x", "50"); |
| 209 |
rectElt.setAttributeNS(null, "y", "" + (70 + i*20)); |
| 210 |
rectElt.setAttributeNS(null, "width", "330"); |
| 211 |
rectElt.setAttributeNS(null, "height", "20" ); |
| 212 |
rectElt.setAttributeNS(null, "class", "tableCell"); |
| 213 |
securityResults.appendChild(rectElt); |
| 214 |
|
| 215 |
rectElt = doc.createElementNS(svgNS, "rect"); |
| 216 |
rectElt.setAttributeNS(null, "x", "380"); |
| 217 |
rectElt.setAttributeNS(null, "y", "" + (70 + i*20)); |
| 218 |
rectElt.setAttributeNS(null, "width", "20"); |
| 219 |
rectElt.setAttributeNS(null, "height", "20" ); |
| 220 |
rectElt.setAttributeNS(null, "class", "tableCell"); |
| 221 |
securityResults.appendChild(rectElt); |
| 222 |
|
| 223 |
rectElt = doc.createElementNS(svgNS, "rect"); |
| 224 |
rectElt.setAttributeNS(null, "x", "383"); |
| 225 |
rectElt.setAttributeNS(null, "y", "" + (73 + i*20)); |
| 226 |
rectElt.setAttributeNS(null, "width", "14"); |
| 227 |
rectElt.setAttributeNS(null, "height", "14" ); |
| 228 |
rectElt.setAttributeNS(null, "class", "untested"); |
| 229 |
securityResults.appendChild(rectElt); |
| 230 |
|
| 231 |
statusRects[i] = rectElt; |
| 232 |
} |
| 233 |
|
| 234 |
EventTarget testButton = (EventTarget)doc.getElementById("runTest"); |
| 235 |
testButton.addEventListener("click", new EventListener() { |
| 236 |
public void handleEvent(Event evt){ |
| 237 |
SecurityManager sm = System.getSecurityManager(); |
| 238 |
int successCnt = 0; |
| 239 |
|
| 240 |
if (sm == null){ |
| 241 |
for (int i=0; i<nGranted; i++) { |
| 242 |
statusRects[i].setAttributeNS(null, "class", "passedTest"); |
| 243 |
successCnt++; |
| 244 |
} |
| 245 |
for (int i=nGranted; i<permissions.length; i++) { |
| 246 |
statusRects[i].setAttributeNS(null, "class", "failedTest"); |
| 247 |
} |
| 248 |
} |
| 249 |
else { |
| 250 |
for (int i=0; i<nGranted; i++) { |
| 251 |
Permission p = (Permission)permissions[i][1]; |
| 252 |
boolean success = true; |
| 253 |
try { |
| 254 |
sm.checkPermission(p); |
| 255 |
statusRects[i].setAttributeNS(null, "class", "passedTest"); |
| 256 |
successCnt++; |
| 257 |
} catch (SecurityException se){ |
| 258 |
statusRects[i].setAttributeNS(null, "class", "failedTest"); |
| 259 |
System.out.println("*********************************************"); |
| 260 |
se.printStackTrace(); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
for (int i=nGranted; i<permissions.length; i++) { |
| 265 |
Permission p = (Permission)permissions[i][1]; |
| 266 |
boolean success = true; |
| 267 |
try { |
| 268 |
sm.checkPermission(p); |
| 269 |
statusRects[i].setAttributeNS(null, "class", "failedTest"); |
| 270 |
} catch (SecurityException se){ |
| 271 |
statusRects[i].setAttributeNS(null, "class", "passedTest"); |
| 272 |
successCnt++; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
} |
| 277 |
|
| 278 |
// Update the global status |
| 279 |
Element globalStatus = doc.getElementById("globalStatus"); |
| 280 |
if ( successCnt == (statusRects.length) ) { |
| 281 |
globalStatus.setAttributeNS(null, "class", "passedTest"); |
| 282 |
} else { |
| 283 |
globalStatus.setAttributeNS(null, "class", "failedTest"); |
| 284 |
} |
| 285 |
|
| 286 |
String successRatioString = "Test Result: " + successCnt + " / " + statusRects.length; |
| 287 |
Element successRatio = doc.getElementById("successRatio"); |
| 288 |
successRatio.replaceChild(doc.createTextNode(successRatioString), |
| 289 |
successRatio.getFirstChild()); |
| 290 |
|
| 291 |
} |
| 292 |
}, false); |
| 293 |
|
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public void run(Document doc, org.apache.batik.bridge.Window win) { |
| 299 |
// TODO Auto-generated method stub |
| 300 |
|
| 301 |
} |
| 302 |
|
| 303 |
} |
| 304 |
|