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 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-09 02:21:22 +00:00
co-authored by Claude Opus 5.5
parent 44d5b4b25e
commit 8e74c9c62f
2 changed files with 56 additions and 14 deletions
+35 -14
View File
@@ -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<ArtifactExtractionResult> => {
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<boolean> => {
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<ArtifactReader> => {
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 => {
@@ -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()