From 79656ad97a345968ca171dae2631e108b771ff37 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:15:44 -0500 Subject: [PATCH] fix(nuget): distinguish malformed archives from caller cancellation (#12597) * fix(nuget): preserve ZIP parser failure provenance under cancellation * fix(nuget): observe cancellation across ZIP directory enumeration --- src/nuget/NugetNupkgParser.test.ts | 80 +++++++++++++++++++++++++++++- src/nuget/NugetNupkgParser.ts | 39 +++++++++------ 2 files changed, 101 insertions(+), 18 deletions(-) diff --git a/src/nuget/NugetNupkgParser.test.ts b/src/nuget/NugetNupkgParser.test.ts index 67afda3be1..46b3b4bc0c 100644 --- a/src/nuget/NugetNupkgParser.test.ts +++ b/src/nuget/NugetNupkgParser.test.ts @@ -320,8 +320,16 @@ it("reports cancellation during a member read with the caller's reason", async ( const enumeration = vi .spyOn(ZipReader.prototype, "getEntriesGenerator") .mockImplementation(async function* (this: ZipReader, options) { - yield* original.call(this, options); - controller.abort(reason); + for await (const entry of original.call(this, options)) { + if (!entry.directory && entry.filename === "_rels/.rels") { + const getData = entry.getData.bind(entry); + entry.getData = (writer, options) => { + controller.abort(reason); + return getData(writer, options); + }; + } + yield entry; + } return true; }); try { @@ -341,6 +349,74 @@ it("reports cancellation during a member read with the caller's reason", async ( } }); +it.each(["before a visible row", "at enumeration completion"] as const)( + "reports cancellation %s without a metadata member read", + async (phase) => { + const bytes = await nugetNupkgMissingNuspecFixture(); + const controller = new AbortController(); + const reason = { caller: "stop ZIP inventory" }; + const original = ZipReader.prototype.getEntriesGenerator; + const enumeration = vi + .spyOn(ZipReader.prototype, "getEntriesGenerator") + .mockImplementation(async function* (this: ZipReader, options) { + for await (const entry of original.call(this, options)) { + if (phase === "before a visible row") controller.abort(reason); + yield entry; + } + if (phase === "at enumeration completion") controller.abort(reason); + return true; + }); + try { + const failure: unknown = await parseNugetNupkg( + bytes, + artifactOf(bytes), + defaults, + controller.signal, + ).catch((cause: unknown) => cause); + if (!(failure instanceof ArtifactReaderFailure)) + throw new Error("expected inventory cancellation"); + expect(failure.reason).toBe("cancelled"); + expect(failure.cancellation?.reason).toBe(reason); + } finally { + enumeration.mockRestore(); + } + }, +); + +it("keeps the ZIP directory failure when cancellation reuses that error", async () => { + // A local header signature without a central directory is not a valid ZIP. + const bytes = Buffer.from([0x50, 0x4b, 0x03, 0x04]); + const controller = new AbortController(); + const original = ZipReader.prototype.getEntriesGenerator; + const enumeration = vi + .spyOn(ZipReader.prototype, "getEntriesGenerator") + .mockImplementation(async function* (this: ZipReader, options) { + try { + return yield* original.call(this, options); + } catch (cause: unknown) { + controller.abort(cause); + throw cause; + } + }); + try { + const failure: unknown = await parseNugetNupkg( + bytes, + artifactOf(bytes), + defaults, + controller.signal, + ).catch((cause: unknown) => cause); + if (!(failure instanceof ParseFailureError)) + throw new Error("expected malformed ZIP failure", { cause: failure }); + expect(failure.failure.kind).toBe("malformed"); + expect(failure.cause).toBeInstanceOf(ArtifactReaderFailure); + expect((failure.cause as ArtifactReaderFailure).cause).toBe( + controller.signal.reason, + ); + } finally { + enumeration.mockRestore(); + } +}); + const largeRelsFixture = () => nugetNupkgFixture({ includeRels: false, diff --git a/src/nuget/NugetNupkgParser.ts b/src/nuget/NugetNupkgParser.ts index 69539615b0..b9b348d562 100644 --- a/src/nuget/NugetNupkgParser.ts +++ b/src/nuget/NugetNupkgParser.ts @@ -15,6 +15,7 @@ import { import type { Element, Node } from "@xmldom/xmldom"; import { + assertArtifactReadActive, ArtifactReaderFailure, decodeZipEntryText, zipEntryNameLimitations, @@ -116,17 +117,7 @@ const zipSize = (value: number | undefined): number | null => const asFileEntry = (entry: Entry): entry is FileEntry => !entry.directory && "getData" in entry; -const wrapZipFailure = ( - cause: unknown, - signal: AbortSignal | undefined, - compressionMethod?: number, -): never => { - // zip.js rejects with the caller's abort reason; keep it as cancellation - // rather than reporting the package as damaged. - if (signal?.aborted === true && Object.is(cause, signal.reason)) - throw new ArtifactReaderFailure("cancelled", "Artifact read cancelled", { - cause: signal.reason, - }); +const wrapZipFailure = (cause: unknown, compressionMethod?: number): never => { if ( cause instanceof ArtifactReaderFailure || cause instanceof ParseFailureError @@ -198,7 +189,7 @@ const readMemberBytes = async ( ...(signal === undefined ? {} : { signal }), }); } catch (cause: unknown) { - return wrapZipFailure(cause, signal, entry.compressionMethod); + return wrapZipFailure(cause, entry.compressionMethod); } }; @@ -541,15 +532,29 @@ export const parseNugetNupkg = async ( ); const reader = new ZipReader(new Uint8ArrayReader(bytes), ZIP_READER_OPTIONS); const limitations: string[] = [...BASE_LIMITATIONS]; + const zipAbort = signal === undefined ? undefined : new AbortController(); + const zipSignal = zipAbort?.signal; + // zip.js throws its signal's reason. Give it an owned cancellation record + // so an independent ZIP failure cannot be confused with the caller's reason. + const onAbort = () => + zipAbort?.abort( + new ArtifactReaderFailure("cancelled", "Artifact read cancelled", { + cause: signal?.reason, + }), + ); + if (signal?.aborted) onAbort(); + else signal?.addEventListener("abort", onAbort, { once: true }); try { + assertArtifactReadActive(signal); // Count while enumerating so an oversized directory stops at the budget // instead of materializing and decoding every member first. const entries: Entry[] = []; try { for await (const entry of reader.getEntriesGenerator({ decodeText: decodeZipEntryText, - ...(signal === undefined ? {} : { signal }), })) { + // zip.js observes cancellation for member reads, but not for directory enumeration. + assertArtifactReadActive(signal); if (entries.length >= input.max_entries) throw decodeLimit( NUGET_NUPKG_FORMAT, @@ -563,8 +568,9 @@ export const parseNugetNupkg = async ( entries.push(entry); } } catch (cause: unknown) { - return wrapZipFailure(cause, signal); + return wrapZipFailure(cause); } + assertArtifactReadActive(signal); // The inventory comes from the central directory; local entries it omits // are only visible as bytes no listed record accounts for. const directoryOffset = reader.directoryOffset; @@ -626,7 +632,7 @@ export const parseNugetNupkg = async ( else { const relsBytes = await readMemberBytes( rels.entry, - signal, + zipSignal, input.max_nuspec_bytes, "OPC relationships member", ); @@ -691,7 +697,7 @@ export const parseNugetNupkg = async ( } const nuspecBytes = await readMemberBytes( selected.member.entry, - signal, + zipSignal, input.max_nuspec_bytes, "Selected .nuspec", ); @@ -758,6 +764,7 @@ export const parseNugetNupkg = async ( limitations, }; } finally { + signal?.removeEventListener("abort", onAbort); await reader.close().catch(() => undefined); } };