63
63
*/
64
64
public class SecureSM extends SecurityManager {
65
65
66
- private final String [] packagesThatCanExit ;
66
+ private final String [] classesThatCanExit ;
67
67
68
68
/**
69
69
* Creates a new security manager where no packages can exit nor halt the virtual machine.
@@ -73,38 +73,40 @@ public SecureSM() {
73
73
}
74
74
75
75
/**
76
- * Creates a new security manager with the specified list of packages being the only packages
77
- * that can exit or halt the virtual machine.
76
+ * Creates a new security manager with the specified list of regular expressions as the those that class names will be tested against to
77
+ * check whether or not a class can exit or halt the virtual machine.
78
78
*
79
- * @param packagesThatCanExit the list of packages that can exit or halt the virtual machine
79
+ * @param classesThatCanExit the list of classes that can exit or halt the virtual machine
80
80
*/
81
- public SecureSM (final String [] packagesThatCanExit ) {
82
- this .packagesThatCanExit = packagesThatCanExit ;
81
+ public SecureSM (final String [] classesThatCanExit ) {
82
+ this .classesThatCanExit = classesThatCanExit ;
83
83
}
84
84
85
85
/**
86
- * Creates a new security manager with a standard set of test packages being the only packages
87
- * that can exit or halt the virtual machine. The packages that can exit are
86
+ * Creates a new security manager with a standard set of test packages being the only packages that can exit or halt the virtual machine.
87
+ * The packages that can exit are:
88
+ * <ul>
88
89
* <li><code>org.apache.maven.surefire.booter.</code></li>
89
90
* <li><code>com.carrotsearch.ant.tasks.junit4.</code></li>
90
91
* <li><code>org.eclipse.internal.junit.runner.</code></li>
91
92
* <li><code>com.intellij.rt.execution.junit.</code></li>
93
+ * </ul>
92
94
*
93
95
* @return an instance of SecureSM where test packages can halt or exit the virtual machine
94
96
*/
95
97
public static SecureSM createTestSecureSM () {
96
98
return new SecureSM (TEST_RUNNER_PACKAGES );
97
99
}
98
100
99
- private static final String [] TEST_RUNNER_PACKAGES = new String [] {
101
+ static final String [] TEST_RUNNER_PACKAGES = new String [] {
100
102
// surefire test runner
101
- "org.apache.maven.surefire.booter. " ,
103
+ "org\\ .apache\\ .maven\\ .surefire\\ .booter\\ ..* " ,
102
104
// junit4 test runner
103
- "com.carrotsearch.ant.tasks.junit4. " ,
105
+ "com\\ .carrotsearch\\ .ant\\ .tasks\\ .junit4\\ .slave \\ ..* " ,
104
106
// eclipse test runner
105
- "org.eclipse.jdt.internal.junit.runner. " ,
107
+ "org\\ .eclipse.jdt\\ .internal\\ .junit\\ .runner\\ ..* " ,
106
108
// intellij test runner
107
- "com.intellij.rt.execution.junit. "
109
+ "com\\ .intellij\\ .rt\\ .execution\\ .junit\\ ..* "
108
110
};
109
111
110
112
// java.security.debug support
@@ -203,6 +205,8 @@ public void checkExit(int status) {
203
205
204
206
/**
205
207
* The "Uwe Schindler" algorithm.
208
+ *
209
+ * @param status the exit status
206
210
*/
207
211
protected void innerCheckExit (final int status ) {
208
212
AccessController .doPrivileged (new PrivilegedAction <Void >() {
@@ -222,14 +226,12 @@ public Void run() {
222
226
}
223
227
224
228
if (exitMethodHit != null ) {
225
- if (packagesThatCanExit == null ) {
229
+ if (classesThatCanExit == null ) {
226
230
break ;
227
231
}
228
- for (String packageThatCanExit : packagesThatCanExit ) {
229
- if (className .startsWith (packageThatCanExit )) {
230
- // this exit point is allowed, we return normally from closure:
231
- return null ;
232
- }
232
+ if (classCanExit (className , classesThatCanExit )) {
233
+ // this exit point is allowed, we return normally from closure:
234
+ return null ;
233
235
}
234
236
// anything else in stack trace is not allowed, break and throw SecurityException below:
235
237
break ;
@@ -248,4 +250,13 @@ public Void run() {
248
250
super .checkExit (status );
249
251
}
250
252
253
+ static boolean classCanExit (final String className , final String [] classesThatCanExit ) {
254
+ for (final String classThatCanExit : classesThatCanExit ) {
255
+ if (className .matches (classThatCanExit )) {
256
+ return true ;
257
+ }
258
+ }
259
+ return false ;
260
+ }
261
+
251
262
}
0 commit comments