Fix PklBugException when reading local package assets (#642)

This commit is contained in:
Daniel Chao
2024-09-10 05:51:34 -07:00
committed by GitHub
parent 7868d9d9c8
commit 47f2143e0d
4 changed files with 113 additions and 24 deletions

View File

@@ -488,7 +488,16 @@ public final class ResourceReaders {
var dependency = getProjectDepsResolver().getResolvedDependency(assetUri.getPackageUri());
var local = getLocalUri(dependency, assetUri);
if (local != null) {
return VmContext.get(null).getResourceManager().read(local, null);
var resourceManager = VmContext.get(null).getResourceManager();
var securityManager = VmContext.get(null).getSecurityManager();
securityManager.checkReadResource(local);
var reader = resourceManager.getResourceReader(local);
if (reader == null) {
throw new VmExceptionBuilder()
.evalError("noResourceReaderRegistered", uri.getScheme())
.build();
}
return resourceManager.doRead(reader, local, null);
}
var remoteDep = (Dependency.RemoteDependency) dependency;
var bytes = getPackageResolver().getBytes(assetUri, true, remoteDep.getChecksums());

View File

@@ -67,43 +67,46 @@ public final class ResourceManager {
return reader;
}
public Optional<Object> doRead(ResourceReader reader, URI uri, @Nullable Node readNode) {
Optional<Object> resource;
try {
resource = reader.read(uri);
} catch (IOException e) {
throw new VmExceptionBuilder()
.evalError("ioErrorReadingResource", uri)
.withCause(e)
.withOptionalLocation(readNode)
.build();
} catch (URISyntaxException e) {
throw new VmExceptionBuilder()
.evalError("invalidResourceUri", uri)
.withHint(e.getReason())
.withOptionalLocation(readNode)
.build();
} catch (SecurityManagerException | PackageLoadError | HttpClientInitException e) {
throw new VmExceptionBuilder().withCause(e).withOptionalLocation(readNode).build();
}
return resource;
}
@TruffleBoundary
public Optional<Object> read(URI resourceUri, @Nullable Node readNode) {
return resources.computeIfAbsent(
resourceUri.normalize(),
uri -> {
(uri) -> {
try {
securityManager.checkReadResource(uri);
} catch (SecurityManagerException e) {
throw new VmExceptionBuilder().withCause(e).withOptionalLocation(readNode).build();
}
var reader = resourceReaders.get(uri.getScheme());
var reader = getResourceReader(uri);
if (reader == null) {
throw new VmExceptionBuilder()
.withOptionalLocation(readNode)
.evalError("noResourceReaderRegistered", resourceUri.getScheme())
.evalError("noResourceReaderRegistered", uri.getScheme())
.build();
}
Optional<Object> resource;
try {
resource = reader.read(uri);
} catch (IOException e) {
throw new VmExceptionBuilder()
.evalError("ioErrorReadingResource", uri)
.withCause(e)
.withOptionalLocation(readNode)
.build();
} catch (URISyntaxException e) {
throw new VmExceptionBuilder()
.evalError("invalidResourceUri", resourceUri)
.withHint(e.getReason())
.withOptionalLocation(readNode)
.build();
} catch (SecurityManagerException | PackageLoadError | HttpClientInitException e) {
throw new VmExceptionBuilder().withCause(e).withOptionalLocation(readNode).build();
}
var resource = doRead(reader, uri, readNode);
if (resource.isEmpty()) return resource;
var res = resource.get();