implements packer PR-10 production trust and gates
This commit is contained in:
@@ -6,6 +6,8 @@ import p.packer.api.building.PackerBuildRequest;
|
||||
import p.packer.api.building.PackerBuildResult;
|
||||
import p.packer.api.building.PackerBuildService;
|
||||
import p.packer.api.diagnostics.PackerDiagnostic;
|
||||
import p.packer.api.diagnostics.PackerDiagnosticCategory;
|
||||
import p.packer.api.diagnostics.PackerDiagnosticSeverity;
|
||||
import p.packer.api.events.PackerEventKind;
|
||||
import p.packer.api.events.PackerEventSink;
|
||||
import p.packer.api.events.PackerProgress;
|
||||
@@ -60,6 +62,17 @@ public final class FileSystemPackerBuildService implements PackerBuildService {
|
||||
final Path assetTableJson = buildDirectory.resolve("asset_table.json").toAbsolutePath().normalize();
|
||||
final Path preloadJson = buildDirectory.resolve("preload.json").toAbsolutePath().normalize();
|
||||
final Path metadataJson = buildDirectory.resolve("asset_table_metadata.json").toAbsolutePath().normalize();
|
||||
final List<PackerDiagnostic> trustDiagnostics = new ArrayList<>(validateExistingOutputs(buildRequest, assetsArchive, metadataJson));
|
||||
if (!trustDiagnostics.isEmpty()) {
|
||||
final String summary = trustDiagnostics.getFirst().message();
|
||||
events.emit(PackerEventKind.BUILD_FINISHED, summary, new PackerProgress(1.0d, false), List.of());
|
||||
return new PackerBuildResult(
|
||||
PackerOperationStatus.FAILED,
|
||||
summary,
|
||||
assetsArchive,
|
||||
Map.of(),
|
||||
trustDiagnostics);
|
||||
}
|
||||
events.emit(PackerEventKind.BUILD_STARTED, "Build started.", new PackerProgress(0.0d, false), List.of());
|
||||
final PackerBuildPlanResult planResult = buildPlanner.plan(buildRequest.project());
|
||||
if (planResult.plan() == null) {
|
||||
@@ -175,14 +188,76 @@ public final class FileSystemPackerBuildService implements PackerBuildService {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return ((Map<String, Object>) new com.fasterxml.jackson.databind.ObjectMapper().readValue(Files.readString(metadataJson), Map.class))
|
||||
.getOrDefault("cache_key", "")
|
||||
.toString();
|
||||
final Map<String, Object> metadata = new com.fasterxml.jackson.databind.ObjectMapper().readValue(Files.readString(metadataJson), Map.class);
|
||||
final Object schema = metadata.get("schema_version");
|
||||
if (!(schema instanceof Number) || ((Number) schema).intValue() != SCHEMA_VERSION) {
|
||||
throw new IllegalArgumentException("Unsupported build metadata schema_version: " + schema);
|
||||
}
|
||||
return metadata.getOrDefault("cache_key", "").toString();
|
||||
} catch (IOException exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private List<PackerDiagnostic> validateExistingOutputs(PackerBuildRequest request, Path assetsArchive, Path metadataJson) {
|
||||
final List<PackerDiagnostic> diagnostics = new ArrayList<>();
|
||||
if (Files.isRegularFile(metadataJson)) {
|
||||
try {
|
||||
final Map<?, ?> metadata = new com.fasterxml.jackson.databind.ObjectMapper().readValue(Files.readString(metadataJson), Map.class);
|
||||
final Object schemaVersion = metadata.get("schema_version");
|
||||
if (!(schemaVersion instanceof Number) || ((Number) schemaVersion).intValue() != SCHEMA_VERSION) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.VERSIONING,
|
||||
"Unsupported build metadata schema_version: " + schemaVersion,
|
||||
metadataJson,
|
||||
true));
|
||||
}
|
||||
} catch (IOException exception) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.VERSIONING,
|
||||
"Unable to read build metadata for version validation.",
|
||||
metadataJson,
|
||||
true));
|
||||
}
|
||||
}
|
||||
if (request.incremental() && Files.isRegularFile(assetsArchive)) {
|
||||
try {
|
||||
final byte[] prelude = Files.readAllBytes(assetsArchive);
|
||||
if (prelude.length < PRELUDE_SIZE) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.VERSIONING,
|
||||
"Existing assets.pa prelude is truncated.",
|
||||
assetsArchive,
|
||||
true));
|
||||
} else {
|
||||
final ByteBuffer buffer = ByteBuffer.wrap(prelude, 0, PRELUDE_SIZE).order(ByteOrder.BIG_ENDIAN);
|
||||
final byte[] magic = new byte[4];
|
||||
buffer.get(magic);
|
||||
final int schemaVersion = buffer.getInt();
|
||||
if (!java.util.Arrays.equals(magic, MAGIC) || schemaVersion != SCHEMA_VERSION) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.VERSIONING,
|
||||
"Existing assets.pa uses an unsupported writer-side schema surface.",
|
||||
assetsArchive,
|
||||
true));
|
||||
}
|
||||
}
|
||||
} catch (IOException exception) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.VERSIONING,
|
||||
"Unable to inspect existing assets.pa for incremental trust validation.",
|
||||
assetsArchive,
|
||||
true));
|
||||
}
|
||||
}
|
||||
return diagnostics;
|
||||
}
|
||||
|
||||
private record EmittedArchive(
|
||||
byte[] bytes,
|
||||
String assetTableJson,
|
||||
|
||||
+16
-1
@@ -122,13 +122,28 @@ public final class PackerAssetDeclarationParser {
|
||||
true));
|
||||
return;
|
||||
}
|
||||
values.add(value.asText().trim());
|
||||
final String relativePath = value.asText().trim();
|
||||
if (!isTrustedRelativePath(relativePath)) {
|
||||
diagnostics.add(new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
PackerDiagnosticCategory.STRUCTURAL,
|
||||
"Input role '" + entry.getKey() + "' contains an untrusted path outside the asset root.",
|
||||
manifestPath,
|
||||
true));
|
||||
return;
|
||||
}
|
||||
values.add(relativePath);
|
||||
});
|
||||
result.put(entry.getKey(), List.copyOf(values));
|
||||
});
|
||||
return Map.copyOf(result);
|
||||
}
|
||||
|
||||
private boolean isTrustedRelativePath(String value) {
|
||||
final Path path = Path.of(value).normalize();
|
||||
return !path.isAbsolute() && !path.startsWith("..");
|
||||
}
|
||||
|
||||
private PackerDiagnostic missingOrInvalid(String fieldName, String expected, Path manifestPath) {
|
||||
return new PackerDiagnostic(
|
||||
PackerDiagnosticSeverity.ERROR,
|
||||
|
||||
+9
-1
@@ -28,6 +28,9 @@ public final class FileSystemPackerRegistryRepository implements PackerRegistryR
|
||||
try {
|
||||
final RegistryDocument document = MAPPER.readValue(registryPath.toFile(), RegistryDocument.class);
|
||||
final int schemaVersion = document.schemaVersion <= 0 ? REGISTRY_SCHEMA_VERSION : document.schemaVersion;
|
||||
if (schemaVersion != REGISTRY_SCHEMA_VERSION) {
|
||||
throw new PackerRegistryException("Unsupported registry schema_version: " + schemaVersion);
|
||||
}
|
||||
final List<PackerRegistryEntry> entries = new ArrayList<>();
|
||||
if (document.assets != null) {
|
||||
for (RegistryAssetDocument asset : document.assets) {
|
||||
@@ -94,7 +97,12 @@ public final class FileSystemPackerRegistryRepository implements PackerRegistryR
|
||||
if (root == null || root.isBlank()) {
|
||||
throw new PackerRegistryException("Registry asset root must not be blank");
|
||||
}
|
||||
return root.trim().replace('\\', '/');
|
||||
final String normalized = root.trim().replace('\\', '/');
|
||||
final Path normalizedPath = Path.of(normalized).normalize();
|
||||
if (normalizedPath.isAbsolute() || normalizedPath.startsWith("..")) {
|
||||
throw new PackerRegistryException("Registry asset root is outside the trusted assets boundary: " + normalized);
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
|
||||
+6
-1
@@ -369,7 +369,12 @@ public final class FileSystemPackerMutationService implements PackerMutationServ
|
||||
if (preview.targetAssetRoot() == null) {
|
||||
throw new PackerMutationException("Mutation preview does not define a target asset root");
|
||||
}
|
||||
return preview.targetAssetRoot();
|
||||
final Path targetRoot = preview.targetAssetRoot();
|
||||
final Path projectRoot = preview.request().project().rootPath().toAbsolutePath().normalize();
|
||||
if (!targetRoot.toAbsolutePath().normalize().startsWith(projectRoot.resolve("assets").toAbsolutePath().normalize())) {
|
||||
throw new PackerMutationException("Mutation target root is outside the trusted assets boundary");
|
||||
}
|
||||
return targetRoot;
|
||||
}
|
||||
|
||||
private void emit(
|
||||
|
||||
Reference in New Issue
Block a user