My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* jnlp-loader
* Copyright (C) 2009 John Pritchard, jdp@syntelos.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package loader.sandbox;

import lxl.io.Caller;
import lxl.net.Find;

import loader.Extension;
import loader.Jar;
import loader.Jnlp;
import loader.Nativelib;

import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.StringTokenizer;

import org.xml.sax.SAXException;

/**
* Delete unpacked class files found to perform reflection including
* dynamic name binding (Class.forName), or access to java.io.File,
* java.lang.Runtime and java.lang.ClassLoader.
*
*
* @see http://code.google.com/p/jnlp-loader/issues/detail?id=6
* @author jdp
*/
public class WebLoader
extends loader.Main
implements Runnable
{

/**
* Reference to application class loader used by {@link Jnlp}
*/
static WebLoader Instance;

public static void main(String[] argv){
try {
WebLoader main = new WebLoader(argv);
main.run();
}
catch (Exception exc){
exc.printStackTrace();
System.exit(1);
}
}

/**
* Code access policy
*
* @return OK
*/
public static boolean CodeAccessPolicy(String classname){
if (null != classname){
Boolean screen = DisallowedClasses.get(classname);
if (null != screen)
return screen.booleanValue();
else {
for (int cc = 0; cc < DisallowedPackagesCount; cc++){
if (classname.startsWith(DisallowedPackages[cc]))
return false;
}
return true;
}
}
else
return false;
}
public final static boolean CallerIsIn(){
if (Caller.IsIn(WebLoader.Instance))
return true;
else
throw new IllegalStateException();
}
public final static boolean CallerIsNotIn(){
if (Caller.IsNotIn(WebLoader.Instance))
return true;
else
throw new IllegalStateException();
}

static URL GetCodebase(){
return WebLoader.Instance.getCodebase();
}


/**
* @param argv Main function arguments.
*/
public WebLoader(String[] argv)
throws IOException, SAXException
{
super(argv);

Instance = this;

ServiceManager.Create();

UrlHandler.Create();
}
/**
* @param dir Provide a static directory name or default to jnlp href name.
* @param argv Main function arguments.
*/
public WebLoader(String dir, String[] argv)
throws IOException, SAXException
{
super(dir,argv);

Instance = this;

ServiceManager.Create();

UrlHandler.Create();
}

protected boolean runInitJnlpAccept(Jnlp jnlp, Extension ext){
return true;
}
protected boolean runInitJnlpAccept(Jnlp jnlp, Jar ext){
return true;
}
protected final boolean runInitJnlpAccept(Jnlp jnlp, Nativelib ext){
return false;
}
protected final void runJnlpCheck(){
Find find = new Find();

while (find.hasNext()){
File file = find.next();
if (file.getName().endsWith(".class")){
try {
ClassFile classfile = new ClassFile(file);

if (AllowSuper(classfile)){
boolean dropped = false;
/*
* Scan field references from class
*/
ClassFile.CPInfo[] fieldReferences = classfile.cpFieldRefs();
if (null != fieldReferences){
for (ClassFile.CPInfo fieldRef : fieldReferences){
if (AllowFieldRef(fieldRef,classfile)){

if (WebLoader.Debug){
String bclass = fieldRef.getClass(classfile);
String bname = fieldRef.getName(classfile);
String btype = fieldRef.getType(classfile);

System.err.println(String.format("%s: allowed class '%s' with field-ref cl '%s' na '%s' ty '%s'.",Thread.currentThread().getName(),classfile.getName(),bclass,bname,btype));
}

continue;
}
else {
dropped = file.delete();

if (WebLoader.Test){
String bclass = fieldRef.getClass(classfile);
String bname = fieldRef.getName(classfile);
String btype = fieldRef.getType(classfile);

System.err.println(String.format("%s: dropped class '%s' with field-ref cl '%s' na '%s' ty '%s'.",Thread.currentThread().getName(),classfile.getName(),bclass,bname,btype));
}
}
}
}
if (Debug || (!dropped)){
/*
* Scan method references from class
*/
ClassFile.CPInfo[] methodReferences = classfile.cpMethodRefs();
if (null != methodReferences){
for (ClassFile.CPInfo methodRef : methodReferences){
if (AllowMethodRef(methodRef,classfile)){

if (WebLoader.Debug){
String bclass = methodRef.getClass(classfile);
String bname = methodRef.getName(classfile);
String btype = methodRef.getType(classfile);

System.err.println(String.format("%s: allowed class '%s' with method-ref cl '%s' na '%s' ty '%s'.",Thread.currentThread().getName(),classfile.getName(),bclass,bname,btype));
}

continue;
}
else {
dropped = file.delete();
if (WebLoader.Test){
String bclass = methodRef.getClass(classfile);
String bname = methodRef.getName(classfile);
String btype = methodRef.getType(classfile);

System.err.println(String.format("%s: dropped class '%s' with method-ref cl '%s' na '%s' ty '%s'.",Thread.currentThread().getName(),classfile.getName(),bclass,bname,btype));
}
}
}
}
if (Debug || (!dropped)){
/*
* Scan fields of class
*/
ClassFile.FieldInfo[] fields = classfile.cpFields();
if (null != fields){
for (ClassFile.FieldInfo field: fields){

if (AllowField(field,classfile)){

if (WebLoader.Debug){
String name = field.getName(classfile);
String signature = CleanBClass(field.getSignature(classfile));


System.err.println(String.format("%s: allowed class '%s' with field name '%s' type '%s'.",Thread.currentThread().getName(),classfile.getName(),name,signature));
}

continue;
}
else {
dropped = file.delete();

if (WebLoader.Test){
String name = field.getName(classfile);
String signature = CleanBClass(field.getSignature(classfile));

System.err.println(String.format("%s: dropped class '%s' for field name '%s' type '%s'.",Thread.currentThread().getName(),classfile.getName(),name,signature));
}
}
}
}

if (Debug || (!dropped)){
/*
* Scan methods of class
*/
ClassFile.MethodInfo[] methods = classfile.cpMethods();
if (null != methods){
for (ClassFile.MethodInfo method: methods){

if (AllowMethod(method,classfile)){

if (WebLoader.Debug){

String name = method.getName(classfile);
String signature = CleanMClass(method.getSignature(classfile));

System.err.println(String.format("%s: allowed class '%s' with method name '%s' type '%s'.",Thread.currentThread().getName(),classfile.getName(),name,signature));
}

continue;
}
else {
dropped = file.delete();
if (WebLoader.Test){

String name = method.getName(classfile);
String signature = CleanMClass(method.getSignature(classfile));

System.err.println(String.format("%s: dropped class '%s' for method name '%s' type '%s'.",Thread.currentThread().getName(),classfile.getName(),name,signature));
}
}
}
}
}
}
}
}
else {
file.delete();

if (WebLoader.Test){
System.err.println(String.format("%s: dropped file '%s' for superclass.",Thread.currentThread().getName(),file.getPath()));
}
}
}
catch (Exception any){

file.delete();

if (WebLoader.Test){
System.err.println(String.format("%s: dropped file '%s' for error '%s'.",Thread.currentThread().getName(),file.getPath(),any.toString()));

if (WebLoader.Debug)
any.printStackTrace();
}
}
}
}
}

/*
* access policy actors
*/
protected static boolean AllowSuper(ClassFile classfile){
String superclass = classfile.getNameSuper();

return CodeAccessPolicy(superclass);
}
protected static boolean AllowFieldRef(ClassFile.CPInfo fieldRef, ClassFile classfile){

String bclass = fieldRef.getClass(classfile);

return CodeAccessPolicy(bclass);
}
protected static boolean AllowMethodRef(ClassFile.CPInfo methodRef, ClassFile classfile){

String bclass = methodRef.getClass(classfile);

return CodeAccessPolicy(bclass);
}

protected static boolean AllowField(ClassFile.FieldInfo field, ClassFile classfile){

String signature = field.getSignature(classfile);

signature = CleanBClass(signature);

if (null != signature)
return CodeAccessPolicy(signature);
else
return true;
}
protected static boolean AllowMethod(ClassFile.MethodInfo method, ClassFile classfile){

String signature = method.getSignature(classfile);

signature = CleanMClass(signature);

if (null != signature){
StringTokenizer strtok = new StringTokenizer(signature,",");
while (strtok.hasMoreTokens()){
if (!CodeAccessPolicy(strtok.nextToken())){
return false;
}
}
}
return true;
}

/*
* string trimmers
*/
protected static String CleanBClass(String bc){
if (null != bc && 0 != bc.length()){
while ('[' == bc.charAt(0))
bc = bc.substring(1);

return (bc.substring(1,bc.length()-1));
}
else
return null;
}
protected static String CleanMClass(String bc){
if (null != bc && 0 != bc.length()){
while ('(' == bc.charAt(0))
bc = bc.substring(1);
int idx = bc.lastIndexOf(')');
return CleanBClass(bc.substring(0,idx));
}
else
return null;
}

/*
* access policy data
*/
private final static String[] DisallowedPackages = {
"com.sun.java.",
"com.sun.net.",
"sun.net."
};
private final static int DisallowedPackagesCount = DisallowedPackages.length;

private final static java.util.Map<String,Boolean> DisallowedClasses = new java.util.HashMap<String,Boolean>();
static {
DisallowedClasses.put("java.lang.Class",Boolean.FALSE);
DisallowedClasses.put("java.lang.ClassLoader",Boolean.FALSE);
DisallowedClasses.put("java.lang.reflect.Field",Boolean.FALSE);
DisallowedClasses.put("java.lang.reflect.Method",Boolean.FALSE);
DisallowedClasses.put("java.net.URLClassLoader",Boolean.FALSE);
DisallowedClasses.put("java.security.SecureClassLoader",Boolean.FALSE);
DisallowedClasses.put("java.lang.Runtime",Boolean.FALSE);
DisallowedClasses.put("java.lang.System",Boolean.FALSE);
DisallowedClasses.put("java.io.File",Boolean.FALSE);
DisallowedClasses.put("java.io.RandomAccessFile",Boolean.FALSE);
DisallowedClasses.put("java.io.FileInputStream",Boolean.FALSE);
DisallowedClasses.put("java.io.FileOutputStream",Boolean.FALSE);
DisallowedClasses.put("java.net.DatagramSocket",Boolean.FALSE);
DisallowedClasses.put("java.net.DatagramSocketImpl",Boolean.FALSE);
DisallowedClasses.put("java.net.DatagramSocketImplFactory",Boolean.FALSE);
DisallowedClasses.put("java.net.MulticastSocket",Boolean.FALSE);
DisallowedClasses.put("java.net.PlainDatagramSocketImpl",Boolean.FALSE);
DisallowedClasses.put("java.net.PlainDatagramSocketImplFactory",Boolean.FALSE);
DisallowedClasses.put("java.net.ServerSocket",Boolean.FALSE);
DisallowedClasses.put("java.net.Socket",Boolean.FALSE);
DisallowedClasses.put("java.net.SocketImpl",Boolean.FALSE);
DisallowedClasses.put("java.net.SocketFactory",Boolean.FALSE);
DisallowedClasses.put("java.net.URLStreamHandler",Boolean.FALSE);
DisallowedClasses.put("java.net.URLStreamHandlerFactory",Boolean.FALSE);
DisallowedClasses.put("loader.Main",Boolean.FALSE);
DisallowedClasses.put("loader.Loader",Boolean.FALSE);
DisallowedClasses.put("loader.Jar",Boolean.FALSE);
DisallowedClasses.put("loader.Nativelib",Boolean.FALSE);
DisallowedClasses.put("loader.Extension",Boolean.FALSE);
DisallowedClasses.put("loader.Clean",Boolean.FALSE);
DisallowedClasses.put("loader.Find",Boolean.FALSE);
}

}

Change log

r110 by john.douglas.pritchard on Dec 8, 2009   Diff
upd 1.0.6
Go to: 
Project members, sign in to write a code review

Older revisions

r109 by john.douglas.pritchard on Dec 7, 2009   Diff
int 1.0.6
r97 by john.douglas.pritchard on Nov 30, 2009   Diff
upd 1.0.6
r92 by john.douglas.pritchard on Nov 30, 2009   Diff
int 1.0.6
All revisions of this file

File info

Size: 17285 bytes, 428 lines
Powered by Google Project Hosting