-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJar.java
More file actions
453 lines (392 loc) · 16.8 KB
/
Jar.java
File metadata and controls
453 lines (392 loc) · 16.8 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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* Copyright (c) Forge Development LLC
* SPDX-License-Identifier: LGPL-2.1-only
*/
package cpw.mods.jarhandling.impl;
import cpw.mods.jarhandling.JarMetadata;
import cpw.mods.jarhandling.SecureJar;
import cpw.mods.util.LambdaExceptionUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.module.ModuleDescriptor;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemAlreadyExistsException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.spi.FileSystemProvider;
import java.security.CodeSigner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
public class Jar implements SecureJar {
private static final CodeSigner[] EMPTY_CODESIGNERS = new CodeSigner[0];
private static final FileSystemProvider UFSP = FileSystemProvider.installedProviders().stream()
.filter(p -> "union".equalsIgnoreCase(p.getScheme()))
.findFirst()
.orElseThrow(() -> new IllegalStateException("Couldn't find UnionFileSystemProvider"));
private final Manifest manifest;
private final Hashtable<String, CodeSigner[]> pendingSigners = new Hashtable<>();
private final Hashtable<String, CodeSigner[]> verifiedSigners = new Hashtable<>();
private final Map<String, StatusData> statusData = new HashMap<>();
private final JarMetadata metadata;
private final Path filesystemRoot;
private final Path filesystemPrimary;
private final Map<String, String> nameOverrides;
private final JarModuleDataProvider moduleDataProvider;
private final Set<String> packages;
private final List<Provider> providers;
@Override
public ModuleDataProvider moduleDataProvider() {
return moduleDataProvider;
}
@Override
public Path getPrimaryPath() {
return filesystemPrimary;
}
public Jar(Function<SecureJar, JarMetadata> metadataFunction, BiPredicate<String, String> pathfilter, Path... paths) {
this(null, metadataFunction, pathfilter, paths);
}
/** Supplying a manifest is stupid. */
@Deprecated(forRemoval = true, since = "2.2")
public Jar(Supplier<Manifest> defaultManifest, Function<SecureJar, JarMetadata> metadataFunction, BiPredicate<String, String> pathfilter, Path... paths) {
var validPaths = Arrays.stream(paths)
.map(Path::toAbsolutePath)
.map(Path::normalize)
.filter(Files::exists)
.toArray(Path[]::new);
if (validPaths.length == 0)
throw new UncheckedIOException(new IOException("Invalid paths argument, contained no existing paths: " + Arrays.toString(paths)));
this.moduleDataProvider = new JarModuleDataProvider(this);
this.filesystemRoot = newFileSystem(pathfilter, validPaths);
this.filesystemPrimary = validPaths[validPaths.length - 1];
this.manifest = findManifest(validPaths, defaultManifest);
this.nameOverrides = gatherVersionedFiles();
this.providers = gatherProviders(pathfilter);
this.packages = gatherPackages();
this.metadata = metadataFunction.apply(this);
}
@Override
public CodeSigner[] getManifestSigners() {
return getData(JarFile.MANIFEST_NAME).map(r->r.signers).orElse(null);
}
@Override
public Status verifyPath(Path path) {
if (path.getFileSystem() != this.filesystemRoot.getFileSystem())
throw new IllegalArgumentException("Wrong filesystem");
final var pathname = path.toString();
if (statusData.containsKey(pathname))
return getFileStatus(pathname);
try {
var bytes = Files.readAllBytes(path);
verifyAndGetSigners(pathname, bytes);
return getFileStatus(pathname);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
@Override
public Status getFileStatus(final String name) {
return hasSecurityData() ? getData(name).map(StatusData::status).orElse(Status.NONE) : Status.UNVERIFIED;
}
@Override
public Attributes getTrustedManifestEntries(final String name) {
var manattrs = manifest.getAttributes(name);
var mansigners = getManifestSigners();
var objsigners = getData(name).map(sd->sd.signers).orElse(EMPTY_CODESIGNERS);
if (mansigners == null || (mansigners.length == objsigners.length)) {
return manattrs;
} else {
return null;
}
}
@Override
public boolean hasSecurityData() {
return !pendingSigners.isEmpty() || !this.verifiedSigners.isEmpty();
}
@Override
public String name() {
return metadata.name();
}
@Override
public Set<String> getPackages() {
return this.packages;
}
@Override
public List<Provider> getProviders() {
return this.providers;
}
@Override
public Path getPath(String first, String... rest) {
var rel = this.filesystemRoot.getFileSystem().getPath(first, rest);
return this.filesystemRoot.resolve(rel);
}
@Override
public Path getRootPath() {
return this.filesystemRoot;
}
@Override
public String toString() {
return "Jar[" + getURI() + "]";
}
/*==============================================================================================*
* INTERNAL SHIT *
*==============================================================================================*/
private Path newFileSystem(BiPredicate<String, String> filter, Path[] paths) {
if (paths == null || paths.length == 0)
throw new IllegalArgumentException("Must contain atleast one path");
FileSystem fs = null;
try {
if (filter == null && paths.length == 1) {
if (Files.isDirectory(paths[0]))
return paths[0];
var uri = paths[0].toUri();
if ("file".equals(uri.getScheme())) {
// We have to manually open the jar files up via a URI instead of a Path
// because the ZipFileSystem implementation only caches the FileSystems
// when accessed that way. But we can only open it once or else it throws
// a FileSystemAlreadyExistsException. So, exceptions as codeflow, yay!
uri = new URI("jar:" + uri);
try {
fs = FileSystems.newFileSystem(uri, Map.of(), null);
} catch (FileSystemAlreadyExistsException e) {
fs = FileSystems.getFileSystem(uri);
}
} else {
// JarInJar is fucking stupid and breaks horribly if it knows about files directly.
// So because I don't want to go into the rabbit hole that is digging into that
// Any non-standard file system will be wrapped in a Union File System
// This still gets the performance benefit of having 90% of everything bypass the UFS
// TODO: [SM] Remove JarInJar file system in favor of a simpler dependency management system.
fs = UFSP.newFileSystem(paths[0], Map.of("filter", (BiPredicate<String, String>)(a, b) -> true));
}
} else {
var lst = new ArrayList<>(Arrays.asList(paths));
var base = lst.remove(0);
if (filter == null)
fs = UFSP.newFileSystem(base, Map.of("additional", lst));
else
fs = UFSP.newFileSystem(base, Map.of("filter", filter, "additional", lst));
}
} catch (IOException | URISyntaxException e) {
return sneak(e);
}
return fs.getRootDirectories().iterator().next();
}
/** Public for API compat, will break soon-ish */
public synchronized CodeSigner[] verifyAndGetSigners(String name, byte[] bytes) {
if (!hasSecurityData())
return null;
// If we're a multi-release jar we need to be sure to check the correct entry.
name = this.nameOverrides.getOrDefault(name, name);
var data = statusData.get(name);
if (data != null)
return data.signers();
var signers = ManifestVerifier.verify(this.manifest, pendingSigners, verifiedSigners, name, bytes);
if (signers == null) {
this.statusData.put(name, new StatusData(Status.INVALID, null));
return null;
} else {
var ret = signers.orElse(null);
this.statusData.put(name, new StatusData(Status.VERIFIED, ret));
return ret;
}
}
@SuppressWarnings("unchecked")
private static <E extends Throwable, R> R sneak(Throwable e) throws E {
throw (E)e;
}
private Optional<StatusData> getData(final String name) {
return Optional.ofNullable(statusData.get(name));
}
public Manifest getManifest() {
return manifest;
}
public URI getURI() {
return this.filesystemRoot.toUri();
}
public ModuleDescriptor computeDescriptor() {
return metadata.descriptor();
}
public Optional<URI> findFile(String name) {
name = nameOverrides.getOrDefault(name, name);
var resolved = filesystemRoot.resolve(name);
if (Files.exists(resolved))
return Optional.of(resolved.toUri());
return Optional.empty();
}
private record StatusData(Status status, CodeSigner[] signers) {}
private List<Provider> gatherProviders(BiPredicate<String, String> filter) {
var services = this.filesystemRoot.resolve("META-INF/services/");
if (!Files.exists(services))
return List.of();
try (var servicesDirStream = Files.list(services)) {
return servicesDirStream
.filter(Files::isRegularFile)
.map(path -> getProvider(path, filter))
.toList();
} catch (IOException e) {
return sneak(e);
}
}
/* Public for SecureJar only */
public static Provider getProvider(Path path, BiPredicate<String, String> filter) {
var sname = path.getFileName().toString();
try {
var entries = new ArrayList<String>();
for (var line : Files.readAllLines(path)) {
int idx = line.indexOf('#');
if (idx != -1)
line = line.substring(0, idx);
line = line.trim();
if (line.isEmpty())
continue;
if (filter != null && !filter.test(line.replace('.', '/'), ""))
continue;
entries.add(line);
}
return new Provider(sname, entries);
} catch (IOException e) {
return sneak(e);
}
}
private Map<String, String> gatherVersionedFiles() {
var versionsDir = this.filesystemRoot.resolve("META-INF/versions");
if (!Boolean.parseBoolean(getManifest().getMainAttributes().getValue("Multi-Release")) || !Files.exists(versionsDir))
return Map.of();
var ret = new HashMap<String, String>();
var versions = new HashMap<String, Integer>(8);
try (var versionsDirStream = Files.walk(versionsDir)) {
versionsDirStream
.filter(Files::isRegularFile)
.map(filesystemRoot::relativize)
.forEach(path -> {
var ver = Integer.parseInt(path.getName(2).toString());
var key = path.subpath(3, path.getNameCount()).toString().replace('\\', '/');
if (ver <= Runtime.version().feature() && versions.getOrDefault(key, 0) < ver) {
versions.put(key, ver);
ret.put(key, path.toString());
}
});
} catch (IOException e) {
sneak(e);
}
return Map.copyOf(ret);
}
private Set<String> gatherPackages() {
var files = new HashSet<String>(this.nameOverrides.keySet());
try (var filesStream = Files.walk(this.filesystemRoot)) {
filesStream
.filter(p -> Files.isRegularFile(p) && !"META-INF".equals(p.getName(0).toString()))
.map(p -> this.filesystemRoot.relativize(p).toString().replace('\\', '/'))
.forEach(files::add);
} catch (IOException e) {
return sneak(e);
}
var ret = new HashSet<String>();
for (var file : files) {
int idx = file.lastIndexOf('/');
if (idx == -1 || !file.endsWith(".class"))
continue;
ret.add(file.substring(0, idx).replace('/', '.'));
}
return ret;
}
private Manifest findManifest(Path[] paths, Supplier<Manifest> defaultManifest) {
try {
for (int x = paths.length - 1; x >= 0; x--) { // Walk backwards because this is what cpw wanted?
var path = paths[x];
if (Files.isDirectory(path)) {
var manfile = path.resolve(JarFile.MANIFEST_NAME);
if (Files.exists(manfile)) {
try (var is = Files.newInputStream(manfile)) {
return new Manifest(is);
}
}
} else {
try (var jis = new JarInputStream(Files.newInputStream(path))) {
var jv = SecureJarVerifier.getJarVerifier(jis);
if (jv != null) {
// Read till we find the manifest, or run out of entries.
while (SecureJarVerifier.isParsingMeta(jv)) {
if (jis.getNextJarEntry() == null)
break;
}
if (SecureJarVerifier.hasSignatures(jv)) {
pendingSigners.putAll(SecureJarVerifier.getPendingSigners(jv));
var manifestSigners = SecureJarVerifier.getVerifiedSigners(jv).get(JarFile.MANIFEST_NAME);
if (manifestSigners != null)
verifiedSigners.put(JarFile.MANIFEST_NAME, manifestSigners);
this.statusData.put(JarFile.MANIFEST_NAME, new StatusData(Status.VERIFIED, manifestSigners));
}
}
if (jis.getManifest() != null)
return new Manifest(jis.getManifest());
}
}
}
return defaultManifest != null ? defaultManifest.get() : new Manifest();
} catch (IOException e) {
return sneak(e);
}
}
private record JarModuleDataProvider(Jar jar) implements ModuleDataProvider {
@Override
public String name() {
return jar.name();
}
@Override
public ModuleDescriptor descriptor() {
return jar.computeDescriptor();
}
@Override
public URI uri() {
return jar.getURI();
}
@Override
public Optional<URI> findFile(final String name) {
return jar.findFile(name);
}
@Override
public Optional<InputStream> open(String name) {
// Path.toURI() can sometimes return URIs that are invalid syntax/can't be passed to Paths.get
// Specifically ZipPath and jars with []'s. https://github.com/MinecraftForge/MinecraftForge/issues/9842
// So bypass all of that and get the InputStream from the path itself.
name = jar.nameOverrides.getOrDefault(name, name);
var resolved = jar.filesystemRoot.resolve(name);
if (Files.exists(resolved)) {
try {
return Optional.of(Files.newInputStream(resolved));
} catch (IOException e) {
return sneak(e);
}
}
return Optional.empty();
}
@Override
public Manifest getManifest() {
return jar.getManifest();
}
@Override
public CodeSigner[] verifyAndGetSigners(final String cname, final byte[] bytes) {
return jar.verifyAndGetSigners(cname, bytes);
}
}
}