Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(artifacts): normalize ZIP current-directory prefixes at the reader

morluto6b1c36e336ee
diff --git a/src/artifacts/ZipArtifactReader.ts b/src/artifacts/ZipArtifactReader.ts
index a53e01838..05c729cb7 100644
--- a/src/artifacts/ZipArtifactReader.ts
+++ b/src/artifacts/ZipArtifactReader.ts
@@ -191,6 +191,7 @@ export class ZipArtifactReader implements ArtifactReader {
// ZIP member identity is its directory occurrence, not its filename.
// Logical path uniqueness belongs to the artifact inventory boundary.
const metadata = projectZipEntry(entry, index++);
+ if (metadata === undefined) continue;
// zip.js enforces each member's declared output size, so this sum bounds streamed work.
if (
metadata.kind === "file" &&
@@ -258,19 +259,38 @@ export class ZipArtifactReader implements ArtifactReader {
}
}
-const projectZipEntry = (entry: Entry, index: number): ArtifactEntry => {
- const symlink = isSymlink(entry);
+const projectZipEntry = (
+ entry: Entry,
+ index: number,
+): ArtifactEntry | undefined => {
+ const kind = isSymlink(entry)
+ ? "symlink"
+ : entry.directory
+ ? "directory"
+ : "file";
+ const sourcePath = zipEntryPath(entry);
+ const path = sourcePath.replace(/^(?:\.\/)+/u, "");
+ // Root directory markers carry no inventory occurrence. A file with this
+ // spelling is still passed to the shared path validator and rejected there.
+ if (kind === "directory" && (path === "" || path === ".")) return undefined;
return {
- path: zipEntryPath(entry),
- kind: entry.directory ? "directory" : symlink ? "symlink" : "file",
- declaredSize: entry.directory ? null : entry.uncompressedSize,
- compressedSize: entry.directory ? null : entry.compressedSize,
+ path,
+ kind,
+ declaredSize: kind === "directory" ? null : entry.uncompressedSize,
+ compressedSize: kind === "directory" ? null : entry.compressedSize,
executable: entry.executable,
encrypted: entry.encrypted,
byteOffset: null,
declaredSha256: null,
unpacked: false,
- limitations: symlink ? ["Archive symlink target was not read."] : [],
+ limitations: [
+ ...(kind === "symlink" ? ["Archive symlink target was not read."] : []),
+ ...(path === sourcePath
+ ? []
+ : [
+ `ZIP member name ${JSON.stringify(sourcePath)} has a leading current-directory prefix omitted from its logical path.`,
+ ]),
+ ],
adapterKey: `zip-entry:${index}`,
};
};
diff --git a/tests/acceptance/artifactZipCurrentDirectory.test.ts b/tests/acceptance/artifactZipCurrentDirectory.test.ts
new file mode 100644
index 000000000..734b639aa
--- /dev/null
+++ b/tests/acceptance/artifactZipCurrentDirectory.test.ts
@@ -0,0 +1,121 @@
+import { readFile, rm, writeFile } from "node:fs/promises";
+import { join } from "node:path";
+
+import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js";
+import { expect } from "vitest";
+
+import {
+ artifactExtractionResultSchema,
+ artifactInventoryResultSchema,
+} from "../../src/domain/artifactGraph.js";
+import { artifactInspectionResultSchema } from "../../src/domain/artifactInspection.js";
+import { parseEvidence } from "../../src/domain/evidence.js";
+import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
+import { cliTest } from "../support/cli/cliFixture.js";
+
+cliTest(
+ "inspects and extracts ZIP members with repeated current-directory prefixes",
+ async ({ cli }) => {
+ const root = await createTestTempDirectory("rea-zip-relative-paths-");
+ const archivePath = join(root, "relative.zip");
+ const writer = new ZipWriter(new Uint8ArrayWriter());
+ await writer.add("./", undefined, { directory: true });
+ await writer.add("././nested/", undefined, { directory: true });
+ await writer.add("././nested/data.txt", new TextReader("retained bytes"));
+ await writeFile(archivePath, await writer.close());
+
+ const inspected = await cli.run({
+ arguments: ["inspect-artifact", archivePath, "--json"],
+ });
+ expect(inspected.exitCode, inspected.stdout + inspected.stderr).toBe(0);
+ const inspection = artifactInspectionResultSchema.parse(
+ parseEvidence(inspected.json).normalized_result,
+ );
+ const inventoryEvidence = inspection.substeps[0]?.evidence;
+ if (inventoryEvidence === undefined)
+ throw new Error("CLI inspection did not retain ZIP inventory Evidence");
+ const inventory = artifactInventoryResultSchema.parse(
+ inventoryEvidence.normalized_result,
+ );
+ expect(
+ inventory.occurrences.map(({ logical_path }) => logical_path),
+ ).toContain("nested/data.txt");
+ expect(inventory.occurrences).toContainEqual(
+ expect.objectContaining({
+ logical_path: "nested/data.txt",
+ limitations: [
+ 'ZIP member name "././nested/data.txt" has a leading current-directory prefix omitted from its logical path.',
+ ],
+ }),
+ );
+ expect(
+ inventory.occurrences.map(({ logical_path }) => logical_path),
+ ).not.toContain("");
+
+ const extracted = await cli.run({
+ arguments: ["extract-artifact", archivePath, "--json"],
+ });
+ expect(extracted.exitCode, extracted.stdout + extracted.stderr).toBe(0);
+ const extraction = artifactExtractionResultSchema.parse(
+ parseEvidence(extracted.json).normalized_result,
+ );
+ try {
+ expect(
+ extraction.artifacts.map(({ relative_path }) => relative_path),
+ ).toEqual(["nested/data.txt"]);
+ expect(
+ await readFile(join(extraction.output_root, "nested/data.txt"), "utf8"),
+ ).toBe("retained bytes");
+ } finally {
+ await rm(extraction.output_root, { force: true, recursive: true });
+ }
+ },
+);
+
+cliTest(
+ "treats a ZIP containing only root directory markers as empty",
+ async ({ cli }) => {
+ const root = await createTestTempDirectory("rea-zip-root-only-");
+ const archivePath = join(root, "root-only.zip");
+ const writer = new ZipWriter(new Uint8ArrayWriter());
+ await writer.add("./", undefined, { directory: true });
+ await writer.add("././", undefined, { directory: true });
+ await writeFile(archivePath, await writer.close());
+
+ const inspected = await cli.run({
+ arguments: ["inspect-artifact", archivePath, "--json"],
+ });
+ expect(inspected.exitCode, inspected.stdout + inspected.stderr).toBe(0);
+ const inspection = artifactInspectionResultSchema.parse(
+ parseEvidence(inspected.json).normalized_result,
+ );
+ const inventoryEvidence = inspection.substeps[0]?.evidence;
+ if (inventoryEvidence === undefined)
+ throw new Error("CLI inspection did not retain ZIP inventory Evidence");
+ const inventory = artifactInventoryResultSchema.parse(
+ inventoryEvidence.normalized_result,
+ );
+ expect(inventory.occurrences).toMatchObject([
+ {
+ logical_path: ".",
+ artifact_id: inventory.manifest.root_artifact_id,
+ artifact_format: "zip",
+ entry_kind: "file",
+ hash_status: "verified",
+ },
+ ]);
+
+ const extracted = await cli.run({
+ arguments: ["extract-artifact", archivePath, "--json"],
+ });
+ expect(extracted.exitCode, extracted.stdout + extracted.stderr).toBe(0);
+ const extraction = artifactExtractionResultSchema.parse(
+ parseEvidence(extracted.json).normalized_result,
+ );
+ try {
+ expect(extraction.artifacts).toEqual([]);
+ } finally {
+ await rm(extraction.output_root, { force: true, recursive: true });
+ }
+ },
+);
diff --git a/tests/boundary/filesystem/artifactZipRelativePaths.test.ts b/tests/boundary/filesystem/artifactZipRelativePaths.test.ts
new file mode 100644
index 000000000..6a346ddb9
--- /dev/null
+++ b/tests/boundary/filesystem/artifactZipRelativePaths.test.ts
@@ -0,0 +1,92 @@
+import { writeFile } from "node:fs/promises";
+import { join } from "node:path";
+
+import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js";
+import { expect, it } from "vitest";
+
+import { writeOrderedZip } from "../../fixtures/artifactEntryOrder.js";
+import { inventoryArtifact } from "../../fixtures/artifactInventory.js";
+import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
+import type { ArtifactEntry } from "../../../src/artifacts/ArtifactReader.js";
+import { ZipArtifactReader } from "../../../src/artifacts/ZipArtifactReader.js";
+
+it("retains distinct occurrences when ZIP paths normalize to the same child", async () => {
+ const root = await createTestTempDirectory("rea-zip-relative-duplicates-");
+ const path = join(root, "duplicates.zip");
+ await writeOrderedZip(path, ["./data.txt", "data.txt"]);
+
+ const inventory = await inventoryArtifact(path);
+ const occurrences = inventory.occurrences.filter(
+ ({ logical_path }) => logical_path === "data.txt",
+ );
+ expect(occurrences).toHaveLength(2);
+ expect(
+ new Set(occurrences.map(({ occurrence_id }) => occurrence_id)).size,
+ ).toBe(2);
+ expect(occurrences).toContainEqual(
+ expect.objectContaining({
+ logical_path: "data.txt",
+ limitations: [
+ 'ZIP member name "./data.txt" has a leading current-directory prefix omitted from its logical path.',
+ ],
+ }),
+ );
+});
+
+it.each([".", "././."])(
+ "rejects a non-directory ZIP entry spelled %s instead of skipping it as a root marker",
+ async (name) => {
+ const root = await createTestTempDirectory("rea-zip-root-file-");
+ const path = join(root, "root-file.zip");
+ const writer = new ZipWriter(new Uint8ArrayWriter());
+ await writer.add(name, new TextReader("not a directory"));
+ await writeFile(path, await writer.close());
+
+ await expect(inventoryArtifact(path)).rejects.toMatchObject({
+ name: "ArtifactReaderFailure",
+ reason: "path",
+ });
+ },
+);
+
+it.each([
+ { sourcePath: ".", path: "." },
+ { sourcePath: "./", path: "" },
+])(
+ "does not skip a ZIP symlink spelled $sourcePath",
+ async ({ sourcePath, path: logicalPath }) => {
+ const root = await createTestTempDirectory("rea-zip-root-symlink-");
+ const path = join(root, "root-symlink.zip");
+ // Retain the Unix symlink mode even when the filename has a directory slash.
+ const writer = new ZipWriter(new Uint8ArrayWriter(), {
+ msDosCompatible: false,
+ versionMadeBy: 0x0314,
+ externalFileAttributes: 0o120777 * 0x10000,
+ });
+ await writer.add(sourcePath, new TextReader("target"));
+ await writeFile(path, await writer.close());
+
+ const reader = new ZipArtifactReader(path, "zip");
+ try {
+ const entries: ArtifactEntry[] = [];
+ for await (const entry of reader.entries()) entries.push(entry);
+ expect(entries).toMatchObject([
+ { path: logicalPath, kind: "symlink", declaredSize: 6 },
+ ]);
+ } finally {
+ await reader.close();
+ }
+ },
+);
+
+it("rejects traversal after removing harmless leading current-directory prefixes", async () => {
+ const root = await createTestTempDirectory("rea-zip-relative-traversal-");
+ const path = join(root, "traversal.zip");
+ await writeOrderedZip(path, ["./../escape.txt"]);
+
+ await expect(inventoryArtifact(path)).rejects.toMatchObject({
+ name: "ArtifactReaderFailure",
+ reason: "path",
+ message: 'Artifact path is not normalized: "../escape.txt"',
+ });
+});