fix(artifacts): complete prefix reads and share ZIP format admission
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { readFileHandlePrefix } from "../filesystem/readFileHandlePrefix.js";
|
||||
import { constants, type Stats } from "node:fs";
|
||||
import { createHash } from "node:crypto";
|
||||
import {
|
||||
@@ -297,12 +298,13 @@ const detectArtifactFormat = async (
|
||||
> => {
|
||||
throwIfTargetResolutionCancelled(signal);
|
||||
const lower = path.toLowerCase();
|
||||
const magic = Buffer.alloc(16);
|
||||
const observed = await handle.read(magic, 0, magic.length, 0);
|
||||
const magic = await readFileHandlePrefix(handle, 16, () =>
|
||||
throwIfTargetResolutionCancelled(signal),
|
||||
);
|
||||
throwIfTargetResolutionCancelled(signal);
|
||||
await verifyTargetState(path, handle, initial, signal);
|
||||
if (
|
||||
observed.bytesRead >= 8 &&
|
||||
magic.length >= 8 &&
|
||||
magic.subarray(0, 8).equals(Buffer.from([0, 0x61, 0x73, 0x6d, 1, 0, 0, 0]))
|
||||
)
|
||||
throw new BinaryTargetError(
|
||||
@@ -310,7 +312,7 @@ const detectArtifactFormat = async (
|
||||
"WebAssembly v1 artifacts are not supported by the native opening route",
|
||||
{ constraint: "wasm_requires_static_inspection" },
|
||||
);
|
||||
const prefix = magic.subarray(0, observed.bytesRead);
|
||||
const prefix = magic;
|
||||
const nesFormat = nesContainerFormat(prefix);
|
||||
if (nesFormat !== undefined) {
|
||||
const parsed = parseInesLayout(prefix, initial.size);
|
||||
@@ -328,16 +330,13 @@ const detectArtifactFormat = async (
|
||||
return zipPackageFormatForPath(lower) ?? "zip";
|
||||
}
|
||||
// Binary plists, including keyed archives, often use other extensions.
|
||||
if (
|
||||
observed.bytesRead >= 8 &&
|
||||
magic.subarray(0, 8).equals(Buffer.from("bplist00"))
|
||||
)
|
||||
if (magic.length >= 8 && magic.subarray(0, 8).equals(Buffer.from("bplist00")))
|
||||
return "plist";
|
||||
const named = namedArtifactFormat(lower);
|
||||
if (named !== undefined) return named;
|
||||
if (
|
||||
lower.endsWith(".pkg") &&
|
||||
observed.bytesRead >= 4 &&
|
||||
magic.length >= 4 &&
|
||||
magic.subarray(0, 4).equals(Buffer.from("xar!"))
|
||||
)
|
||||
return "pkg";
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { readFileHandlePrefix } from "../filesystem/readFileHandlePrefix.js";
|
||||
import { createHash } from "node:crypto";
|
||||
import { Readable } from "node:stream";
|
||||
import {
|
||||
@@ -42,15 +43,13 @@ export class InesArtifactReader implements ArtifactReader {
|
||||
async *entries(signal?: AbortSignal): AsyncIterable<ArtifactEntry> {
|
||||
abortIfNeeded(signal);
|
||||
await this.#verifySource(signal);
|
||||
const prefix = Buffer.alloc(16);
|
||||
const { bytesRead } = await this.source.handle.read(prefix, 0, 16, 0);
|
||||
const prefix = await readFileHandlePrefix(this.source.handle, 16, () =>
|
||||
abortIfNeeded(signal),
|
||||
);
|
||||
abortIfNeeded(signal);
|
||||
// Concurrent source changes take precedence over malformed header diagnostics.
|
||||
await this.#verifySource(signal);
|
||||
const parsed = parseInesLayout(
|
||||
prefix.subarray(0, bytesRead),
|
||||
this.source.initial.size,
|
||||
);
|
||||
const parsed = parseInesLayout(prefix, this.source.initial.size);
|
||||
if (!parsed.ok) throw new ArtifactReaderFailure("format", parsed.error);
|
||||
this.#layout = parsed.value;
|
||||
this.#entries.clear();
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { isZipPackageFormat } from "../../domain/zipPackageFormat.js";
|
||||
import { lstat, realpath } from "node:fs/promises";
|
||||
|
||||
import { compareUnicodeCodePoints } from "../../domain/unicodeCodePointOrder.js";
|
||||
@@ -410,13 +411,6 @@ interface ExtractionInventory {
|
||||
readonly integrityContradictions: readonly IntegrityContradiction[];
|
||||
}
|
||||
|
||||
const ZIP_FORMATS = ["ipa", "apk", "msix", "appx", "zip"] as const;
|
||||
|
||||
const isZipFormat = (
|
||||
format: BinaryTarget["format"] | ArtifactGraphManifest["root_format"],
|
||||
): format is (typeof ZIP_FORMATS)[number] =>
|
||||
(ZIP_FORMATS as readonly string[]).includes(format);
|
||||
|
||||
/**
|
||||
* Refuse a format without an extraction reader before inventory work starts.
|
||||
* The target kind, not the host, is unsupported by extraction. The error keeps
|
||||
@@ -433,7 +427,7 @@ const requireExtractableFormat = async (
|
||||
format !== "asar" &&
|
||||
format !== "ines" &&
|
||||
!isMachOFormat(format) &&
|
||||
!isZipFormat(format)
|
||||
!isZipPackageFormat(format)
|
||||
)
|
||||
throw new AnalysisUnsupportedTargetError(
|
||||
"extract_artifact",
|
||||
@@ -498,7 +492,7 @@ const createReader = async (
|
||||
}
|
||||
if (format === "directory") return new DirectoryArtifactReader(path);
|
||||
if (format === "asar") return new AsarArtifactReader(path);
|
||||
if (isZipFormat(format)) return new ZipArtifactReader(path, format);
|
||||
if (isZipPackageFormat(format)) return new ZipArtifactReader(path, format);
|
||||
if (format === "mach-o" || format === "mach-o-universal")
|
||||
return new MachOSliceArtifactReader(path, input.environment);
|
||||
throw new AnalysisUnsupportedTargetError(
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { readFileHandlePrefix } from "../../filesystem/readFileHandlePrefix.js";
|
||||
import {
|
||||
sameRegularFileState,
|
||||
verifyRegularFileState,
|
||||
@@ -183,12 +184,14 @@ const classifyRootFormat = async (
|
||||
signal?: AbortSignal,
|
||||
): Promise<ArtifactOccurrence["artifact_format"]> => {
|
||||
abortIfNeeded(signal);
|
||||
const magic = Buffer.alloc(ARTIFACT_CLASSIFICATION_PREFIX_BYTES);
|
||||
const observed = await handle.read(magic, 0, magic.length, 0);
|
||||
const prefix = await readFileHandlePrefix(
|
||||
handle,
|
||||
ARTIFACT_CLASSIFICATION_PREFIX_BYTES,
|
||||
() => abortIfNeeded(signal),
|
||||
);
|
||||
abortIfNeeded(signal);
|
||||
await verifyRegularFileState(path, { handle, initial });
|
||||
abortIfNeeded(signal);
|
||||
const prefix = magic.subarray(0, observed.bytesRead);
|
||||
if (nesContainerFormat(prefix) !== undefined) {
|
||||
const parsed = parseInesLayout(prefix, initial.size);
|
||||
if (!parsed.ok) throw new ArtifactReaderFailure("format", parsed.error);
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import type { FileHandle } from "node:fs/promises";
|
||||
|
||||
/** Fill a positional prefix until its bound or true EOF, preserving caller cancellation errors. */
|
||||
export const readFileHandlePrefix = async (
|
||||
handle: Pick<FileHandle, "read">,
|
||||
length: number,
|
||||
checkCancellation: () => void,
|
||||
): Promise<Buffer> => {
|
||||
const prefix = Buffer.alloc(length);
|
||||
let offset = 0;
|
||||
while (offset < length) {
|
||||
checkCancellation();
|
||||
let bytesRead: number;
|
||||
try {
|
||||
({ bytesRead } = await handle.read(
|
||||
prefix,
|
||||
offset,
|
||||
length - offset,
|
||||
offset,
|
||||
));
|
||||
} catch (cause: unknown) {
|
||||
checkCancellation();
|
||||
throw cause;
|
||||
}
|
||||
checkCancellation();
|
||||
if (bytesRead === 0) break;
|
||||
offset += bytesRead;
|
||||
}
|
||||
return prefix.subarray(0, offset);
|
||||
};
|
||||
Reference in New Issue
Block a user