fix(artifacts)!: keep streamed reads within descriptor ownership

Use bounded positional reads for unpacked ASAR members and borrowed Mach-O slices so destroying a stream cannot bypass its descriptor owner. Retain uncertain ASAR closes and preserve borrowed sources after cancellation.

BREAKING CHANGE: Injected ASAR unpacked handles expose fd and read instead of createReadStream.
This commit is contained in:
morluto
2026-10-11 05:17:10 +08:00
parent be6272f261
commit e0e49e9dd5
4 changed files with 170 additions and 26 deletions
+20 -20
View File
@@ -30,6 +30,8 @@ import {
} from "./ArtifactReader.js";
import { closeAsarHandle, readValidatedAsarEntry } from "./AsarEntryStream.js";
import { admitAsarHeader } from "./AsarHeader.js";
import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js";
import { readFileHandleChunks } from "../filesystem/readFileHandleChunks.js";
/**
* Official Electron ASAR adapter with range-streamed member reads.
@@ -43,10 +45,12 @@ export class AsarArtifactReader implements ArtifactReader {
#archiveSize: number | undefined;
#headerSize: number | undefined;
readonly #entries = new Map<string, AsarEntryState>();
readonly #unpackedHandles = new Map<UnpackedFileHandle, string>();
readonly #unpackedHandleClosePromises = new Map<
readonly #unpackedHandles = new Map<
UnpackedFileHandle,
Promise<void>
{
readonly path: string;
readonly owner: OwnedFileHandle<UnpackedFileHandle>;
}
>();
#snapshot: { readonly path: string; readonly sha256: string } | undefined;
#snapshotRoot: string | undefined;
@@ -299,7 +303,7 @@ export class AsarArtifactReader implements ArtifactReader {
async close(): Promise<void> {
let cleanupFailure: ArtifactReaderFailure | undefined;
for (const [handle, path] of this.#unpackedHandles) {
for (const [handle, { path }] of this.#unpackedHandles) {
try {
await this.#closeUnpackedHandle(handle);
} catch (cause: unknown) {
@@ -383,7 +387,10 @@ export class AsarArtifactReader implements ArtifactReader {
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK,
);
const openedHandle = handle;
this.#unpackedHandles.set(openedHandle, canonical);
this.#unpackedHandles.set(openedHandle, {
path: canonical,
owner: new OwnedFileHandle(openedHandle),
});
const openedMetadata = await openedHandle.stat();
if (
!openedMetadata.isFile() ||
@@ -403,9 +410,8 @@ export class AsarArtifactReader implements ArtifactReader {
);
}
abortIfNeeded(signal);
const source = openedHandle.createReadStream({
const source = readFileHandleChunks(openedHandle, {
start: 0,
autoClose: false,
});
source.once("close", () => {
void this.#closeUnpackedHandle(openedHandle).catch(() => undefined);
@@ -427,7 +433,7 @@ export class AsarArtifactReader implements ArtifactReader {
failure,
ArtifactReaderFailure.cleanupObservation(
cleanupCause,
this.#unpackedHandles.get(handle) ?? entry.path,
this.#unpackedHandles.get(handle)?.path ?? entry.path,
),
);
}
@@ -437,17 +443,10 @@ export class AsarArtifactReader implements ArtifactReader {
}
async #closeUnpackedHandle(handle: UnpackedFileHandle): Promise<void> {
if (!this.#unpackedHandles.has(handle)) return;
const pending = this.#unpackedHandleClosePromises.get(handle);
if (pending !== undefined) return pending;
const closing = Promise.resolve()
.then(() => handle.close())
.then(() => {
this.#unpackedHandles.delete(handle);
})
.finally(() => this.#unpackedHandleClosePromises.delete(handle));
this.#unpackedHandleClosePromises.set(handle, closing);
return closing;
const owned = this.#unpackedHandles.get(handle);
if (owned === undefined) return;
await owned.owner.close();
this.#unpackedHandles.delete(handle);
}
#resetArchiveState(): void {
@@ -462,9 +461,10 @@ type AsarEntryState = {
readonly entry: ArtifactEntry;
};
type UnpackedFileHandle = {
readonly fd: number;
stat(): Promise<Stats>;
close(): Promise<void>;
createReadStream: FileHandle["createReadStream"];
read: FileHandle["read"];
};
type OpenAsarUnpackedFile = (
path: string,