import { lstat, mkdir, rename, symlink, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { expect, it } from "vitest"; import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js"; import { openRegularFile } from "../filesystem/RegularFile.js"; import { assertArtifactSelectedSource } from "./ArtifactSelectedSource.js"; import { readStableArtifact } from "../resources/readStableArtifact.js"; import { DirectoryArtifactReader } from "./DirectoryArtifactReader.js"; import { createDesktopApplicationService } from "../composition/desktop.js"; import { projectAnalysisError } from "../domain/analysisErrorProjection.js"; import { desktopApplicationSchema } from "../domain/desktopApplication.js"; import { assertArtifactFileSelectedSource } from "../resources/index.js"; it.skipIf(process.platform === "win32")( "admits a caller-selected directory symlink without changing its target identity", async () => { const root = await createTestTempDirectory("rea-directory-selection-"); const directory = join(root, "directory"); const selected = join(root, "selected"); await mkdir(directory); await symlink(directory, selected); const reader = new DirectoryArtifactReader(selected); try { const metadata = await lstat(directory, { bigint: true }); await expect(reader.rootObservation()).resolves.toMatchObject({ identity: { dev: metadata.dev, ino: metadata.ino }, }); } finally { await reader.close(); } }, ); it.skipIf(process.platform === "win32")( "rejects a directory selection that traverses through a regular file", async () => { const root = await createTestTempDirectory("rea-directory-resolution-"); await writeFile(join(root, "file"), "carrier"); const reader = new DirectoryArtifactReader(`${root}/file/..`); try { await expect(reader.rootObservation()).rejects.toMatchObject({ reason: "io", cause: { code: "ENOTDIR" }, }); } finally { await reader.close(); } }, ); it.skipIf(process.platform === "win32")( "rejects desktop inspection when the selected spelling traverses a regular file", async () => { const root = await createTestTempDirectory( "rea-desktop-enotdir-intermediate-", ); const app = join(root, "Demo.app"); await mkdir(app); await writeFile( join(app, "Info.plist"), ` CFBundleIdentifiercom.example.demo`, ); await writeFile(join(app, "marker.txt"), "marker\n"); await writeFile(join(root, "regular"), "regular file\n"); const service = createDesktopApplicationService(); try { const invalid = await service.inspect({ path: `${root}/regular/../Demo.app`, max_entries: 32, max_probe_bytes: 4096, }); if (invalid.ok) throw new Error("expected ENOTDIR refusal"); expect(projectAnalysisError(invalid.error)).toMatchObject({ code: "target_unavailable", category: "unavailable", }); const valid = await service.inspect({ path: app, max_entries: 32, max_probe_bytes: 4096, }); if (!valid.ok) throw valid.error; expect( desktopApplicationSchema .parse(valid.value.normalized_result) .files.map((file) => file.path) .sort(), ).toEqual(["Info.plist", "marker.txt"]); } finally { await service.close(); } }, ); it.skipIf(process.platform === "win32")( "reports a replaced ancestor symlink loop as a changed admitted source", async () => { const root = await createTestTempDirectory("rea-source-resolution-"); const parent = join(root, "selected"); await mkdir(parent); const path = join(parent, "carrier.bin"); await writeFile(path, "carrier"); const source = await openRegularFile(path, { symlinks: "reject" }); try { await rename(parent, join(root, "original")); await symlink("selected", parent); await expect( assertArtifactFileSelectedSource(path, source, "Carrier"), ).rejects.toMatchObject({ reason: "integrity", cause: { code: "ELOOP" }, }); } finally { await source.handle.close(); } }, ); it.skipIf(process.platform !== "linux" && process.platform !== "darwin")( "retains a name-too-long resolution failure as an invalidated selection", async () => { const path = join("/", "x".repeat(16384)); await expect( assertArtifactSelectedSource("/admitted", path), ).rejects.toMatchObject({ reason: "integrity", cause: { code: "ENAMETOOLONG" }, }); }, ); it.each(["ENOENT", "ENOTDIR", "ELOOP", "ENAMETOOLONG", "EACCES", "EIO"])( "preserves %s meaning when a stable snapshot cannot open its admitted name", async (code) => { const root = await createTestTempDirectory("rea-stable-resolution-"); const path = join(root, "carrier.bin"); await writeFile(path, "carrier"); const failure = Object.assign(new Error("injected open failure"), { code }); const result = readStableArtifact(path, 1024, undefined, { lstat: (selected) => lstat(selected, { bigint: true }), open: async () => { throw failure; }, }); if (code === "EACCES" || code === "EIO") await expect(result).rejects.toBe(failure); else await expect(result).rejects.toMatchObject({ reason: "integrity", cause: failure, }); }, ); it.skipIf(process.platform === "win32")( "keeps a rejected symlink without a hint when its resolved target lookup fails", async () => { const root = await createTestTempDirectory("rea-stable-symlink-lookup-"); const target = join(root, "target.bin"); const selected = join(root, "selected"); await writeFile(target, "carrier"); await symlink(target, selected); const selectedStats = await lstat(selected, { bigint: true }); const failure = Object.assign(new Error("target lookup failed"), { code: "EACCES", }); const rejected: unknown = await readStableArtifact( selected, 1024, undefined, { lstat: async (path) => { if (path === selected) return selectedStats; throw failure; }, realpath: async () => target, open: async () => { throw new Error("A rejected symlink must not be opened"); }, }, ).catch((cause: unknown) => cause); expect(rejected).toMatchObject({ reason: "path", resolvedPath: undefined }); if (!(rejected instanceof Error)) throw new Error("Expected path failure"); expect(rejected.message).toContain(selected); expect(rejected.message).toContain("symlink"); }, );