Files
reverse-engineering-anything/src/artifacts/apple/KeyedArchiveRawRefusal.test.ts
T
吴佳浩Alben eab61d02a1 fix(apple): report a read-denied selected archive as host access denial (#6479)
* fix(apple): report a read-denied selected archive as host access denial

inspect-keyed-archive reported a host read refusal on the caller-selected bundle entry as artifact_operation_failed/execution_failure with reason io, leaving EACCES or EPERM only inside message text. The caller selected that entry, so the refusal is a selection outcome with its own typed classification, system code and read-access recovery.

DirectoryArtifactReader now marks a refused entry open with DirectoryArtifactEntryAccessDenied, which retains the host system code and the selected entry path. KeyedArchiveReader, which owns the caller's selection, maps it to the shared AnalysisAccessDeniedError, and artifact failure translation keeps that typed error instead of replacing it with a generic io failure. Other readers and other reader failures are unchanged; every existing reason keeps its current projection.

Adds the access_denied projection coverage plus a real host refusal through the production reader and a non-permission control.

* fix(apple): carry cleanup ownership and cover raw refusals for a denied selection

Addresses the two review findings on the denied-selection classification:

- The typed host access denial now carries the reader's cleanup observation, so a denied open that left a resource owned does not project as fully cleaned.
- Adds coverage that starts from raw host EACCES/EPERM/EIO refusals with the real DirectoryArtifactReader open path, so the marker itself is exercised rather than constructed by the test. EIO stays an artifact failure.
2026-10-23 23:41:16 +09:00

114 lines
4.0 KiB
TypeScript

import { writeFile, mkdir } from "node:fs/promises";
import type { FileHandle } from "node:fs/promises";
import { join } from "node:path";
import { build } from "plist";
import { expect, it, vi } from "vitest";
import { AnalysisAccessDeniedError } from "../../domain/analysisErrorCore.js";
import { projectAnalysisError } from "../../domain/analysisErrorProjection.js";
import { ArtifactReaderFailure } from "../ArtifactReader.js";
import { translateArtifactFailure } from "../ArtifactOperationFailure.js";
import { DirectoryArtifactEntryAccessDenied } from "../DirectoryArtifactReader.js";
import { inspectBundleKeyedArchive } from "./KeyedArchiveReader.js";
import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js";
/**
* The reader still opens real descriptors; only the host result for the
* selected entry is replaced, so the raw-error classification runs for real.
*/
const { mockOpen } = vi.hoisted(() => ({ mockOpen: vi.fn() }));
vi.mock("node:fs/promises", async (importOriginal) => ({
...(await importOriginal<typeof import("node:fs/promises")>()),
open: mockOpen,
}));
const archive = build({
$archiver: "NSKeyedArchiver",
$version: 100000,
$objects: ["$null", "leaf"],
$top: { root: { CF$UID: 1 } },
});
const selectedPath = "Contents/Resources/Model.plist";
const bundle = async () => {
const root = await createTestTempDirectory("rea-keyed-raw-denial-");
await mkdir(join(root, "Contents", "Resources"), { recursive: true });
const path = join(root, "Contents", "Resources", "Model.plist");
await writeFile(path, Buffer.from(archive));
return { root, path };
};
const inspect = (bundlePath: string) =>
inspectBundleKeyedArchive({
bundlePath,
targetSha256: "a".repeat(64),
parameters: { path: selectedPath },
platform: "darwin",
});
/** Reject the selected entry's open with the host error under test. */
const hostOpenRefusal = async (systemCode: string) => {
const actual =
await vi.importActual<typeof import("node:fs/promises")>(
"node:fs/promises",
);
const { root, path } = await bundle();
mockOpen.mockImplementation(
async (file: Parameters<typeof actual.open>[0], ...rest: never[]) => {
if (String(file) === path) {
throw Object.assign(new Error(`${systemCode}: permission denied`), {
code: systemCode,
});
}
return actual.open(file, ...rest) as Promise<FileHandle>;
},
);
return root;
};
it.each(["EACCES", "EPERM"] as const)(
"classifies the reader's raw %s refusal as a typed host access denial",
async (systemCode) => {
const root = await hostOpenRefusal(systemCode);
try {
const rejected = await inspect(root).catch((cause: unknown) => cause);
expect(rejected).toBeInstanceOf(AnalysisAccessDeniedError);
if (!(rejected instanceof AnalysisAccessDeniedError))
throw new Error("expected a host access denial");
expect(rejected.systemCode).toBe(systemCode);
expect(rejected.path).toBe(join(root, selectedPath));
expect(projectAnalysisError(rejected)).toMatchObject({
code: "access_denied",
category: "unavailable",
details: { system_code: systemCode, boundary: "filesystem-read" },
});
} finally {
mockOpen.mockReset();
}
},
);
it("keeps the reader's raw EIO refusal an artifact failure", async () => {
const root = await hostOpenRefusal("EIO");
try {
const rejected = await inspect(root).catch((cause: unknown) => cause);
expect(rejected).not.toBeInstanceOf(AnalysisAccessDeniedError);
expect(rejected).toBeInstanceOf(ArtifactReaderFailure);
if (!(rejected instanceof ArtifactReaderFailure))
throw new Error("expected a reader failure");
expect(rejected.reason).toBe("io");
expect(
projectAnalysisError(
translateArtifactFailure("inspect_keyed_archive", rejected),
),
).toMatchObject({
code: "artifact_operation_failed",
details: { reason: "io" },
});
} finally {
mockOpen.mockReset();
}
});