feat(artifacts): add approved native DMG traversal
This commit is contained in:
@@ -17,6 +17,7 @@ import {
|
||||
import { DirectoryArtifactReader } from "../artifacts/DirectoryArtifactReader.js";
|
||||
import { ZipArtifactReader } from "../artifacts/ZipArtifactReader.js";
|
||||
import { MachOSliceArtifactReader } from "../artifacts/MachOSliceArtifactReader.js";
|
||||
import { NativeDmgArtifactReader } from "../artifacts/NativeDmgArtifactReader.js";
|
||||
import { streamChunkToBuffer } from "../artifacts/StreamBytes.js";
|
||||
import {
|
||||
artifactInventoryResultSchema,
|
||||
@@ -49,6 +50,17 @@ interface InventoryPageInput {
|
||||
readonly edgeLimit: number;
|
||||
}
|
||||
|
||||
/** Per-request authority combined with operator-owned native mount policy. */
|
||||
export interface ArtifactNativeMountPolicy {
|
||||
readonly nativeMountApproved: boolean;
|
||||
readonly nativeMountEnabled: boolean;
|
||||
}
|
||||
|
||||
const NATIVE_MOUNT_DISABLED: ArtifactNativeMountPolicy = {
|
||||
nativeMountApproved: false,
|
||||
nativeMountEnabled: false,
|
||||
};
|
||||
|
||||
/** Immutable inventory produced by one complete artifact scan. */
|
||||
export interface ArtifactInventorySnapshot {
|
||||
readonly manifest: ArtifactInventoryResult["manifest"];
|
||||
@@ -68,8 +80,14 @@ export const inventoryArtifact = async (
|
||||
limits: ArtifactLimits,
|
||||
page: InventoryPageInput,
|
||||
signal?: AbortSignal,
|
||||
nativeMount: ArtifactNativeMountPolicy = NATIVE_MOUNT_DISABLED,
|
||||
): Promise<ArtifactInventoryResult> => {
|
||||
const snapshot = await scanArtifactInventory(inputPath, limits, signal);
|
||||
const snapshot = await scanArtifactInventory(
|
||||
inputPath,
|
||||
limits,
|
||||
signal,
|
||||
nativeMount,
|
||||
);
|
||||
return paginateArtifactInventory(snapshot, page);
|
||||
};
|
||||
|
||||
@@ -78,6 +96,7 @@ export const scanArtifactInventory = async (
|
||||
inputPath: string,
|
||||
limits: ArtifactLimits,
|
||||
signal?: AbortSignal,
|
||||
nativeMount: ArtifactNativeMountPolicy = NATIVE_MOUNT_DISABLED,
|
||||
): Promise<ArtifactInventorySnapshot> => {
|
||||
abortIfNeeded(signal);
|
||||
const path = await realpath(inputPath);
|
||||
@@ -86,7 +105,7 @@ export const scanArtifactInventory = async (
|
||||
const rootDigest = metadata.isDirectory()
|
||||
? null
|
||||
: await hashReadable(createReadStream(path), limits.maxTotalBytes, signal);
|
||||
const reader = await createReader(path, rootFormat);
|
||||
const reader = await createReader(path, rootFormat, nativeMount, signal);
|
||||
|
||||
try {
|
||||
const { nodes, occurrences } = await scanReader(reader, limits, signal);
|
||||
@@ -202,6 +221,8 @@ const inventoryLimitations = (
|
||||
const createReader = async (
|
||||
path: string,
|
||||
format: ArtifactNode["format"],
|
||||
nativeMount: ArtifactNativeMountPolicy,
|
||||
signal?: AbortSignal,
|
||||
): Promise<ArtifactReader | undefined> => {
|
||||
switch (format) {
|
||||
case "directory":
|
||||
@@ -216,6 +237,14 @@ const createReader = async (
|
||||
return process.platform === "darwin"
|
||||
? new MachOSliceArtifactReader(path)
|
||||
: undefined;
|
||||
case "dmg":
|
||||
if (!nativeMount.nativeMountApproved) return undefined;
|
||||
if (!nativeMount.nativeMountEnabled)
|
||||
throw new ArtifactReaderFailure(
|
||||
"unavailable",
|
||||
"Native DMG mounting is disabled by operator policy",
|
||||
);
|
||||
return NativeDmgArtifactReader.create(path, signal);
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
@@ -235,75 +264,102 @@ const scanReader = async (
|
||||
const occurrenceByPath = new Map<string, MutableOccurrence>();
|
||||
const registry = new ArtifactPathRegistry();
|
||||
let totalBytes = 0;
|
||||
for await (const entry of reader.entries(signal)) {
|
||||
if (occurrences.length >= limits.maxEntries)
|
||||
throw new ArtifactReaderFailure("limit", "Artifact entry limit exceeded");
|
||||
const logicalPath = normalizeArtifactPath(entry.path, limits);
|
||||
registry.add(logicalPath, entry.kind);
|
||||
preflightEntry(entry, limits);
|
||||
const parent = nearestParent(logicalPath, occurrenceByPath);
|
||||
const occurrence = createOccurrence(
|
||||
entry,
|
||||
logicalPath,
|
||||
parent?.occurrence_id ?? null,
|
||||
);
|
||||
if ((entry.kind === "file" || entry.kind === "slice") && !entry.encrypted) {
|
||||
const remainingBytes = limits.maxTotalBytes - totalBytes;
|
||||
if (remainingBytes <= 0)
|
||||
const visit = async (
|
||||
currentReader: ArtifactReader,
|
||||
prefix: string,
|
||||
): Promise<void> => {
|
||||
for await (const entry of currentReader.entries(signal)) {
|
||||
if (occurrences.length >= limits.maxEntries)
|
||||
throw new ArtifactReaderFailure(
|
||||
"limit",
|
||||
"Artifact total byte limit exceeded",
|
||||
"Artifact entry limit exceeded",
|
||||
);
|
||||
if (entry.declaredSize !== null && entry.declaredSize > remainingBytes)
|
||||
throw new ArtifactReaderFailure(
|
||||
"limit",
|
||||
"Declared artifact bytes exceed remaining cumulative limit",
|
||||
);
|
||||
const digest = await hashReadable(
|
||||
await reader.open(entry, signal),
|
||||
Math.min(limits.maxEntryBytes, remainingBytes),
|
||||
signal,
|
||||
const logicalPath = normalizeArtifactPath(
|
||||
prefix.length === 0 ? entry.path : `${prefix}/${entry.path}`,
|
||||
limits,
|
||||
);
|
||||
const expandableAsar =
|
||||
entry.kind === "file" &&
|
||||
logicalPath.toLowerCase().endsWith(".asar") &&
|
||||
entry.adapterKey.startsWith("/");
|
||||
registry.add(logicalPath, expandableAsar ? "directory" : entry.kind);
|
||||
preflightEntry(entry, limits);
|
||||
const parent = nearestParent(logicalPath, occurrenceByPath);
|
||||
const occurrence = createOccurrence(
|
||||
entry,
|
||||
logicalPath,
|
||||
parent?.occurrence_id ?? null,
|
||||
);
|
||||
totalBytes += digest.bytes;
|
||||
if (
|
||||
entry.declaredSha256 !== null &&
|
||||
entry.declaredSha256 !== digest.sha256
|
||||
)
|
||||
throw new ArtifactReaderFailure(
|
||||
"integrity",
|
||||
`Artifact integrity metadata disagrees with content: ${logicalPath}`,
|
||||
undefined,
|
||||
{
|
||||
logicalPath,
|
||||
declaredSha256: entry.declaredSha256,
|
||||
calculatedSha256: digest.sha256,
|
||||
unpacked: entry.unpacked,
|
||||
},
|
||||
(entry.kind === "file" || entry.kind === "slice") &&
|
||||
!entry.encrypted
|
||||
) {
|
||||
const remainingBytes = limits.maxTotalBytes - totalBytes;
|
||||
if (remainingBytes <= 0)
|
||||
throw new ArtifactReaderFailure(
|
||||
"limit",
|
||||
"Artifact total byte limit exceeded",
|
||||
);
|
||||
if (entry.declaredSize !== null && entry.declaredSize > remainingBytes)
|
||||
throw new ArtifactReaderFailure(
|
||||
"limit",
|
||||
"Declared artifact bytes exceed remaining cumulative limit",
|
||||
);
|
||||
const digest = await hashReadable(
|
||||
await currentReader.open(entry, signal),
|
||||
Math.min(limits.maxEntryBytes, remainingBytes),
|
||||
signal,
|
||||
);
|
||||
if (totalBytes > limits.maxTotalBytes)
|
||||
throw new ArtifactReaderFailure(
|
||||
"limit",
|
||||
"Artifact total byte limit exceeded",
|
||||
);
|
||||
const classified =
|
||||
entry.kind === "slice"
|
||||
? ({ kind: "universal-slice", format: "mach-o" } as const)
|
||||
: classifyArtifactContent(logicalPath, digest.prefix);
|
||||
const node = createArtifactNode({
|
||||
sha256: digest.sha256,
|
||||
size: digest.bytes,
|
||||
kind: classified.kind,
|
||||
format: classified.format,
|
||||
executable: entry.executable,
|
||||
contentState: "embedded",
|
||||
});
|
||||
nodes.set(node.artifact_id, node);
|
||||
occurrence.artifact_id = node.artifact_id;
|
||||
occurrence.hash_status = "verified";
|
||||
totalBytes += digest.bytes;
|
||||
if (
|
||||
entry.declaredSha256 !== null &&
|
||||
entry.declaredSha256 !== digest.sha256
|
||||
)
|
||||
throw new ArtifactReaderFailure(
|
||||
"integrity",
|
||||
`Artifact integrity metadata disagrees with content: ${logicalPath}`,
|
||||
undefined,
|
||||
{
|
||||
logicalPath,
|
||||
declaredSha256: entry.declaredSha256,
|
||||
calculatedSha256: digest.sha256,
|
||||
unpacked: entry.unpacked,
|
||||
},
|
||||
);
|
||||
if (totalBytes > limits.maxTotalBytes)
|
||||
throw new ArtifactReaderFailure(
|
||||
"limit",
|
||||
"Artifact total byte limit exceeded",
|
||||
);
|
||||
const classified =
|
||||
entry.kind === "slice"
|
||||
? ({ kind: "universal-slice", format: "mach-o" } as const)
|
||||
: classifyArtifactContent(logicalPath, digest.prefix);
|
||||
const node = createArtifactNode({
|
||||
sha256: digest.sha256,
|
||||
size: digest.bytes,
|
||||
kind: classified.kind,
|
||||
format: classified.format,
|
||||
executable: entry.executable,
|
||||
contentState: "embedded",
|
||||
});
|
||||
nodes.set(node.artifact_id, node);
|
||||
occurrence.artifact_id = node.artifact_id;
|
||||
occurrence.hash_status = "verified";
|
||||
}
|
||||
occurrences.push(occurrence);
|
||||
occurrenceByPath.set(logicalPath, occurrence);
|
||||
if (expandableAsar) {
|
||||
const nested = new AsarArtifactReader(entry.adapterKey);
|
||||
try {
|
||||
await visit(nested, logicalPath);
|
||||
} finally {
|
||||
await nested.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
occurrences.push(occurrence);
|
||||
occurrenceByPath.set(logicalPath, occurrence);
|
||||
}
|
||||
};
|
||||
await visit(reader, "");
|
||||
return { nodes, occurrences };
|
||||
};
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ export const createBinarySession = (
|
||||
): BinarySession => {
|
||||
return new BinarySession(
|
||||
new CompositeProvider([
|
||||
new ArtifactProvider(),
|
||||
new ArtifactProvider(config.artifactNativeMountEnabled),
|
||||
new NativeMacOSProvider(),
|
||||
new HopperProvider(config, logger),
|
||||
]),
|
||||
|
||||
Reference in New Issue
Block a user