-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsolatedScanner.java
More file actions
173 lines (145 loc) · 5.5 KB
/
Copy pathIsolatedScanner.java
File metadata and controls
173 lines (145 loc) · 5.5 KB
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
package com.cope.addonparser.scanner;
import com.cope.addonparser.model.JarScanResult;
import com.cope.addonparser.profile.MappingProfile;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* Runs addon scans in an isolated worker JVM process. Each scan forks a new JVM that loads and
* executes addon code, communicating results as JSON over stdout. The worker process can be
* independently terminated on timeout or failure, preventing untrusted addon code from affecting
* the parent process.
*/
public class IsolatedScanner implements AutoCloseable {
private static final long DEFAULT_TIMEOUT_SECONDS = 120;
private static final String WORKER_MAIN = ScanWorker.class.getName();
private final long timeoutSeconds;
private final MappingProfile profile;
public IsolatedScanner() {
this(DEFAULT_TIMEOUT_SECONDS, MappingProfile.fromSystemProperty());
}
public IsolatedScanner(MappingProfile profile) {
this(DEFAULT_TIMEOUT_SECONDS, profile);
}
public IsolatedScanner(long timeoutSeconds) {
this(timeoutSeconds, MappingProfile.fromSystemProperty());
}
public IsolatedScanner(long timeoutSeconds, MappingProfile profile) {
this.timeoutSeconds = timeoutSeconds;
this.profile = profile;
}
public JarScanResult scan(Path jarPath) {
Path absoluteJar = jarPath.toAbsolutePath().normalize();
JarScanResult result = new JarScanResult();
result.jarName = absoluteJar.getFileName().toString();
result.jarPath = absoluteJar.toString();
try {
List<String> command = buildWorkerCommand(absoluteJar, profile);
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(false);
Process process = pb.start();
StringBuilder stdout = new StringBuilder();
StringBuilder stderr = new StringBuilder();
Thread stdoutReader =
new Thread(
() -> {
try (BufferedReader reader =
new BufferedReader(
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
stdout.append(line).append('\n');
}
} catch (Exception e) {
// Stream closed - expected on timeout
}
},
"isolated-scanner-stdout");
Thread stderrReader =
new Thread(
() -> {
try (BufferedReader reader =
new BufferedReader(
new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
stderr.append(line).append('\n');
}
} catch (Exception e) {
// Stream closed - expected on timeout
}
},
"isolated-scanner-stderr");
stdoutReader.start();
stderrReader.start();
boolean finished = process.waitFor(timeoutSeconds, TimeUnit.SECONDS);
if (!finished) {
process.destroyForcibly();
result.success = false;
result.errors.add("Worker process timed out after " + timeoutSeconds + "s");
return result;
}
stdoutReader.join(5000);
stderrReader.join(5000);
int exitCode = process.exitValue();
String output = stdout.toString().trim();
if (output.isEmpty()) {
result.success = false;
result.errors.add(
"Worker produced no output (exit="
+ exitCode
+ ")"
+ (stderr.length() > 0 ? " stderr: " + stderr.toString().trim() : ""));
return result;
}
ObjectMapper mapper =
new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return mapper.readValue(output, JarScanResult.class);
} catch (Exception e) {
result.success = false;
result.errors.add("Worker launch failure: " + e.getMessage());
return result;
}
}
private static List<String> buildWorkerCommand(Path jarPath, MappingProfile profile) {
List<String> cmd = new ArrayList<>();
String javaHome = System.getProperty("java.home");
String javaBin = Path.of(javaHome, "bin", "java").toString();
cmd.add(javaBin);
cmd.add("-noverify");
cmd.add("-cp");
cmd.add(System.getProperty("java.class.path"));
// Forward relevant system properties
String[] forwardedProps = {
"addonparser.runtimeTmpDir",
"addonparser.keepTmp",
"addonparser.yarnAutoDownload",
"addonparser.yarnMappingsVersions",
"addonparser.yarnMappingsJar",
"addonparser.mappingsDir",
"addonparser.addonsSourceDir"
};
for (String prop : forwardedProps) {
String value = System.getProperty(prop);
if (value != null) {
cmd.add("-D" + prop + "=" + value);
}
}
cmd.add("-D" + MappingProfile.SYSTEM_PROPERTY + "=" + profile.cliValue());
cmd.add(WORKER_MAIN);
cmd.add(jarPath.toString());
cmd.add("--profile");
cmd.add(profile.cliValue());
return cmd;
}
@Override
public void close() {
// No persistent state to clean up
}
}