Debian Codegraph @

samples/tests/resources/script/rhinoSecurity.js

raw
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 /**
20 * This ECMA Script file represents an example of untrusted code.
21 *
22 * It creates a number of Java Permissions and checks that access is denied.
23 * the tests fail if the Permissions are granted.
24 *
25 * The only thing that the class should be allowed to make is a connection
26 * back to the server that served the document containing this script.
27 *
28 * @author <a href="mailto:[email protected]">Vincent Hardy</a>
29 * @version $Id$
30 */
31
32 importPackage(Packages.java.awt);
33 importPackage(Packages.java.io);
34 importPackage(Packages.java.lang.reflect);
35 importPackage(Packages.java.net);
36 importPackage(Packages.java.security);
37 importPackage(Packages.java.sql);
38 importPackage(Packages.java.util);
39 importPackage(Packages.javax.sound.sampled);
40
41 var svgNS = "http://www.w3.org/2000/svg";
42 var testedPath = "build.sh";
43 var testedHost = "nagoya.apache.org:8080";
44
45 var basePermissions = [
46 ["AllPermission", new AllPermission()],
47 ["FilePermission read", new FilePermission(testedPath, "read")],
48 ["FilePermission write", new FilePermission(testedPath, "write")],
49 ["FilePermission execute", new FilePermission(testedPath, "execute")],
50 ["FilePermission delete", new FilePermission(testedPath, "delete")],
51 ["SocketPermission accept", new SocketPermission(testedHost, "accept")],
52 ["SocketPermission connect", new SocketPermission(testedHost, "connect")],
53 ["SocketPermission listen", new SocketPermission(testedHost, "listen")],
54 ["SocketPermission resolve", new SocketPermission(testedHost, "resolve")],
55 ["AudioPermission play", new AudioPermission("play")],
56 ["AudioPermission record", new AudioPermission("record")],
57 ["AWTPermission accessClipboard", new AWTPermission("accessClipboard")],
58 ["AWTPermission accessEventQueue", new AWTPermission("accessEventQueue")],
59 ["AWTPermission listenToAllAWTEvents", new AWTPermission("listenToAllAWTEvents")],
60 ["AWTPermission showWindowWithoutWarningBanner", new AWTPermission("showWindowWithoutWarningBanner")],
61 ["AWTPermission readDisplayPixels", new AWTPermission("readDisplayPixels")],
62 ["AWTPermission createRobot", new AWTPermission("createRobot")],
63 ["AWTPermission fullScreenExclusive", new AWTPermission("fullScreenExclusive")],
64 ["NetPermission setDefaultAuthenticator", new NetPermission("setDefaultAuthenticator")],
65 ["NetPermission requestPasswordAuthentication", new NetPermission("requestPasswordAuthentication")],
66 ["NetPermission specifyStreamHandler", new NetPermission("specifyStreamHandler")],
67 ["PropertyPermission java.home read", new PropertyPermission("java.home", "read")],
68 ["PropertyPermission java.home write", new PropertyPermission("java.home", "write")],
69 ["ReflectPermission", new ReflectPermission("suppressAccessChecks")],
70 ["RuntimePermission createClassLoader", new RuntimePermission("createClassLoader")],
71 ["RuntimePermission getClassLoader", new RuntimePermission("getClassLoader")],
72 ["RuntimePermission setContextClassLoader", new RuntimePermission("setContextClassLoader")],
73 ["RuntimePermission setSecurityManager", new RuntimePermission("setSecurityManager")],
74 ["RuntimePermission createSecurityManager", new RuntimePermission("createSecurityManager")],
75 ["RuntimePermission exitVM", new RuntimePermission("exitVM")],
76 ["RuntimePermission shutdownHooks", new RuntimePermission("shutdownHooks")],
77 ["RuntimePermission setFactory", new RuntimePermission("setFactory")],
78 ["RuntimePermission setIO", new RuntimePermission("setIO")],
79 ["RuntimePermission modifyThread", new RuntimePermission("modifyThread")],
80 ["RuntimePermission stopThread", new RuntimePermission("stopThread")],
81 ["RuntimePermission modifyThreadGroup", new RuntimePermission("modifyThreadGroup")],
82 ["RuntimePermission getProtectionDomain", new RuntimePermission("getProtectionDomain")],
83 ["RuntimePermission readFileDescriptor", new RuntimePermission("readFileDescriptor")],
84 ["RuntimePermission writeFileDescriptor", new RuntimePermission("writeFileDescriptor")],
85 ["RuntimePermission loadLibrary.{library name}", new RuntimePermission("loadLibrary.{library name}")],
86 ["RuntimePermission accessClassInPackage.java.security", new RuntimePermission("accessClassInPackage.java.security")],
87 ["RuntimePermission defineClassInPackage.java.lang", new RuntimePermission("defineClassInPackage.java.lang")],
88 ["RuntimePermission accessDeclaredMembers", new RuntimePermission("accessDeclaredMembers")],
89 ["RuntimePermission queuePrintJob", new RuntimePermission("queuePrintJob")],
90 ["SecurityPermission createAccessControlContext", new SerializablePermission("createAccessControlContext")],
91 ["SecurityPermission getDomainCombiner", new SerializablePermission("getDomainCombiner")],
92 ["SecurityPermission getPolicy", new SerializablePermission("getPolicy")],
93 ["SecurityPermission setPolicy", new SerializablePermission("setPolicy")],
94 ["SecurityPermission setSystemScope", new SerializablePermission("setSystemScope")],
95 ["SecurityPermission setIdentityPublicKey", new SerializablePermission("setIdentityPublicKey")],
96 ["SecurityPermission setIdentityInfo", new SerializablePermission("setIdentityInfo")],
97 ["SecurityPermission addIdentityCertificate", new SerializablePermission("addIdentityCertificate")],
98 ["SecurityPermission removeIdentityCertificate", new SerializablePermission("removeIdentityCertificate")],
99 ["SecurityPermission printIdentity", new SerializablePermission("printIdentity")],
100 ["SecurityPermission getSignerPrivateKey", new SerializablePermission("getSignerPrivateKey")],
101 ["SecurityPermission setSignerKeyPair", new SerializablePermission("setSignerKeyPair")],
102 ["SerializablePermission enableSubclassImplementation", new SerializablePermission("enableSubclassImplementation")],
103 ["SerializablePermission enableSubstitution", new SerializablePermission("enableSubstitution")],
104 ["SQLPermission", new SQLPermission("setLog")],
105 ];
106
107 var permissions = null;
108 var statusRects = null;
109 var nGranted = 0;
110 function init(){
111 var docURL = document.getURLObject();
112 if (docURL != null
113 && (docURL.getHost() != null)
114 && !( "" == docURL.getHost())
115 ) {
116 permissions = new Array();
117
118 var docHost = docURL.getHost();
119 if (docURL.getPort() != -1) {
120 docHost += ":" + docURL.getPort();
121 }
122
123 permissions[0] = ["SocketPermission accept " + docHost,
124 new SocketPermission(docHost, "accept")];
125 permissions[1] = ["SocketPermission connect " + docHost,
126 new SocketPermission(docHost, "connect")];
127 permissions[2] = ["SocketPermission resolve " + docHost,
128 new SocketPermission(docHost, "resolve")];
129 // permissions.concat(basePermissions);
130
131 for (var i=0; i<basePermissions.length; i++){
132 permissions[3+i] = basePermissions[i];
133 }
134 nGranted = 3;
135 } else {
136 permissions = basePermissions;
137 }
138
139 //
140 // Build a table in the scrollable area of the document
141 //
142 var securityResults = document.getElementById("securityResults");
143 statusRects = new Array();
144
145 for (var i=0; i<permissions.length; i++){
146 var textElt = document.createElementNS(svgNS, "text");
147 textElt.setAttributeNS(null, "x", "55");
148 textElt.setAttributeNS(null, "y", "" + (85 + i*20));
149 textElt.appendChild(document.createTextNode(permissions[i][0].toString()));
150 securityResults.appendChild(textElt);
151
152 var rectElt = document.createElementNS(svgNS, "rect");
153 rectElt.setAttributeNS(null, "x", "50");
154 rectElt.setAttributeNS(null, "y", "" + (70 + i*20));
155 rectElt.setAttributeNS(null, "width", "330");
156 rectElt.setAttributeNS(null, "height", "20" );
157 rectElt.setAttributeNS(null, "class", "tableCell");
158 securityResults.appendChild(rectElt);
159
160 rectElt = document.createElementNS(svgNS, "rect");
161 rectElt.setAttributeNS(null, "x", "380");
162 rectElt.setAttributeNS(null, "y", "" + (70 + i*20));
163 rectElt.setAttributeNS(null, "width", "20");
164 rectElt.setAttributeNS(null, "height", "20" );
165 rectElt.setAttributeNS(null, "class", "tableCell");
166 securityResults.appendChild(rectElt);
167
168 rectElt = document.createElementNS(svgNS, "rect");
169 rectElt.setAttributeNS(null, "x", "383");
170 rectElt.setAttributeNS(null, "y", "" + (73 + i*20));
171 rectElt.setAttributeNS(null, "width", "14");
172 rectElt.setAttributeNS(null, "height", "14" );
173 rectElt.setAttributeNS(null, "class", "untested");
174 securityResults.appendChild(rectElt);
175
176 statusRects[i] = rectElt;
177 }
178
179 }
180
181 init();
182
183 function runEcmascriptSecurityTest(){
184 var sm = System.getSecurityManager();
185 var successCnt = 0;
186 if (sm == null){
187 for (var i=0; i<nGranted; i++) {
188 statusRects[i].setAttributeNS(null, "class", "passedTest");
189 successCnt++;
190 }
191 for (var i=nGranted; i<permissions.length; i++) {
192 statusRects[i].setAttributeNS(null, "class", "failedTest");
193 }
194 }
195 else {
196 for (var i=0; i<nGranted; i++) {
197 var p = permissions[i][1];
198 var success = true;
199 try {
200 sm.checkPermission(p);
201 statusRects[i].setAttributeNS(null, "class", "passedTest");
202 successCnt++;
203 } catch (se){
204 statusRects[i].setAttributeNS(null, "class", "failedTest");
205 se.printStackTrace();
206 }
207 }
208 for (var i=nGranted; i<permissions.length; i++) {
209 var p = permissions[i][1];
210 var success = true;
211 try {
212 sm.checkPermission(p);
213 statusRects[i].setAttributeNS(null, "class", "failedTest");
214 } catch (se){
215 statusRects[i].setAttributeNS(null, "class", "passedTest");
216 successCnt++;
217 }
218 }
219 }
220
221 // Update the global status
222 var globalStatus = document.getElementById("globalStatus");
223 if ( successCnt == (statusRects.length) ) {
224 globalStatus.setAttributeNS(null, "class", "passedTest");
225 } else {
226 globalStatus.setAttributeNS(null, "class", "failedTest");
227 }
228
229 var successRatioString = "Test Result: " + successCnt + " / " + statusRects.length;
230 var successRatio = document.getElementById("successRatio");
231 successRatio.replaceChild(document.createTextNode(successRatioString),
232 successRatio.getFirstChild());
233 }