2
2
3
3
package org.utbot.instrumentation.process
4
4
5
+ import org.utbot.common.withAccessibility
5
6
import sun.security.provider.PolicyFile
7
+ import java.lang.reflect.AccessibleObject
6
8
import java.net.URI
7
9
import java.nio.file.Files
8
10
import java.nio.file.Paths
@@ -28,6 +30,15 @@ internal fun permissions(block: SimplePolicy.() -> Unit) {
28
30
}
29
31
}
30
32
33
+ /* *
34
+ * Make this [AccessibleObject] accessible and run a block inside sandbox.
35
+ */
36
+ fun <O : AccessibleObject , R > O.runSandbox (block : O .() -> R ): R {
37
+ return withAccessibility {
38
+ sandbox { block() }
39
+ }
40
+ }
41
+
31
42
/* *
32
43
* Run [block] in sandbox mode.
33
44
*
@@ -45,15 +56,17 @@ internal fun permissions(block: SimplePolicy.() -> Unit) {
45
56
* ```
46
57
* Read more [about policy file and syntax](https://docs.oracle.com/javase/7/docs/technotes/guides/security/PolicyFiles.html#Examples)
47
58
*/
48
- internal fun <T > sandbox (block : () -> T ): T {
59
+ fun <T > sandbox (block : () -> T ): T {
49
60
val policyPath = Paths .get(System .getProperty(" user.home" ), " .utbot" , " sandbox.policy" )
50
61
return sandbox(policyPath.toUri()) { block() }
51
62
}
52
63
53
- internal fun <T > sandbox (file : URI , block : () -> T ): T {
64
+ fun <T > sandbox (file : URI , block : () -> T ): T {
54
65
val path = Paths .get(file)
55
66
val perms = mutableListOf<Permission >(
56
- RuntimePermission (" accessDeclaredMembers" )
67
+ RuntimePermission (" accessDeclaredMembers" ),
68
+ RuntimePermission (" getStackWalkerWithClassReference" ),
69
+ RuntimePermission (" getClassLoader" ),
57
70
)
58
71
val allCodeSource = CodeSource (null , emptyArray<Certificate >())
59
72
if (Files .exists(path)) {
@@ -64,12 +77,12 @@ internal fun <T> sandbox(file: URI, block: () -> T): T {
64
77
return sandbox(perms, allCodeSource) { block() }
65
78
}
66
79
67
- internal fun <T > sandbox (permission : List <Permission >, cs : CodeSource , block : () -> T ): T {
80
+ fun <T > sandbox (permission : List <Permission >, cs : CodeSource , block : () -> T ): T {
68
81
val perms = permission.fold(Permissions ()) { acc, p -> acc.add(p); acc }
69
82
return sandbox(perms, cs) { block() }
70
83
}
71
84
72
- internal fun <T > sandbox (perms : PermissionCollection , cs : CodeSource , block : () -> T ): T {
85
+ fun <T > sandbox (perms : PermissionCollection , cs : CodeSource , block : () -> T ): T {
73
86
val acc = AccessControlContext (arrayOf(ProtectionDomain (cs, perms)))
74
87
return try {
75
88
AccessController .doPrivileged(PrivilegedAction { block() }, acc)
0 commit comments