From 8e74c9c62fb44556b90f20bb6c12771ac05a18f2 Mon Sep 17 00:00:00 2001 From: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com> Date: Fri, 9 Oct 2026 02:21:22 +0000 Subject: [PATCH] fix(artifacts): refuse unsupported extraction formats before scanning extract_artifact on a format without an extraction reader, such as a DMG, first ran the full inventory scan (mounting the image) and then failed with an `unavailable` artifact failure: "Check artifact support and required tools". The target format, not the host, is unsupported. Check the format before any inventory or output work and raise AnalysisUnsupportedTargetError, which the provider now keeps, as it does for the other target-kind checks in this PR. Co-Authored-By: Claude Opus 5.5 --- .../extraction/ArtifactExtraction.ts | 49 +++++++++++++------ .../artifactProvider.extraction.test.ts | 21 ++++++++ 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/artifacts/extraction/ArtifactExtraction.ts b/src/artifacts/extraction/ArtifactExtraction.ts index 57e8a0066..a309d35f1 100644 --- a/src/artifacts/extraction/ArtifactExtraction.ts +++ b/src/artifacts/extraction/ArtifactExtraction.ts @@ -22,6 +22,7 @@ import { type ArtifactNode, type ArtifactOccurrence, } from "../../domain/artifactGraph.js"; +import { AnalysisUnsupportedTargetError } from "../../domain/analysisErrorCore.js"; import type { BinaryTarget } from "../../domain/binaryTarget.js"; import { scanArtifactInventory } from "../inventory/ArtifactInventory.js"; @@ -38,6 +39,7 @@ export const extractArtifact = async ( signal?: AbortSignal, ): Promise => { const sourcePath = await realpath(input.inputPath); + await requireExtractableFormat(sourcePath, input.inputFormat); const snapshot = await scanArtifactInventory(sourcePath, { signal, }); @@ -240,26 +242,45 @@ const collectNodes = ( if (selected.has(item.artifact_id)) output.set(item.artifact_id, item); }; +const ZIP_FORMATS = ["ipa", "apk", "msix", "appx", "zip"] as const; + +const isZipFormat = ( + format: BinaryTarget["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. + */ +const requireExtractableFormat = async ( + path: string, + format: BinaryTarget["format"], +): Promise => { + const directory = (await lstat(path)).isDirectory(); + if ( + !directory && + format !== "asar" && + format !== "mach-o" && + !isZipFormat(format) + ) + throw new AnalysisUnsupportedTargetError( + "extract_artifact", + path, + `Artifact format has no extraction reader: ${format}`, + ); + return directory; +}; + const createReader = async ( path: string, format: BinaryTarget["format"], ): Promise => { - if ((await lstat(path)).isDirectory()) + if (await requireExtractableFormat(path, format)) return new DirectoryArtifactReader(path); if (format === "asar") return new AsarArtifactReader(path); - if ( - format === "ipa" || - format === "apk" || - format === "msix" || - format === "appx" || - format === "zip" - ) - return new ZipArtifactReader(path, format); - if (format === "mach-o") return new MachOSliceArtifactReader(path); - throw new ArtifactReaderFailure( - "unavailable", - `Artifact format has no extraction reader: ${format}`, - ); + if (isZipFormat(format)) return new ZipArtifactReader(path, format); + return new MachOSliceArtifactReader(path); }; const preflight = (entry: ArtifactEntry): void => { diff --git a/tests/boundary/providers/native/artifactProvider.extraction.test.ts b/tests/boundary/providers/native/artifactProvider.extraction.test.ts index 534ff59d1..509a34798 100644 --- a/tests/boundary/providers/native/artifactProvider.extraction.test.ts +++ b/tests/boundary/providers/native/artifactProvider.extraction.test.ts @@ -133,6 +133,27 @@ describe("artifact extraction", () => { "source", ]); }); + it("refuses a format without an extraction reader before inventory or output", async () => { + const root = await createTestTempDirectory("rea-extract-dmg-"); + const image = join(root, "Image.dmg"); + await writeFile(image, "not mounted"); + const output = join(root, "output"); + const result = await new ArtifactProvider() + .createClient(target(image, "dmg")) + .execute( + "extract_artifact", + artifactExtractionExecutionSchema.parse({ output_root: output }), + ); + expect(result).toMatchObject({ + ok: false, + error: { + _tag: "AnalysisUnsupportedTargetError", + operation: "extract_artifact", + reason: "Artifact format has no extraction reader: dmg", + }, + }); + await expect(access(output)).rejects.toThrow(); + }); }); const inventory = async (targetValue: BinaryTarget) => { const result = await new ArtifactProvider()