test(zip): consolidate filename provenance checks at consumer boundaries (#11864)

This commit is contained in:
morluto authored and GitHub committed 2026-11-18 06:23:27 +08:00
1 parent c78e6e3aa5
commit 6f0f9a122d
6 files changed
+16 -91

No files matched your search

-13
View File
@@ -1,13 +0,0 @@
import { expect, it } from "vitest";
import { decodeZipCp437 } from "./zipEntryText.js";
it("decodes legacy ZIP names as ASCII below 0x80 and Unicode CP437 above", () => {
// macOS folder icon file: the trailing CR stays a control character.
expect(decodeZipCp437(Uint8Array.of(0x49, 0x63, 0x6f, 0x6e, 0x0d))).toBe(
"Icon\r",
);
expect(decodeZipCp437(Uint8Array.of(0x80, 0x9e, 0xe1, 0xff))).toBe(
`Ç₧ß${String.fromCharCode(0xa0)}`,
);
});
+1 -51
View File
@@ -1,9 +1,6 @@
import { ParseFailureError } from "../domain/parseFailure.js";
import { expect, it, vi } from "vitest";
import { createHash } from "node:crypto";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
import {
TextReader,
Uint8ArrayWriter,
@@ -21,13 +18,8 @@ import {
nugetNupkgMalformedNuspecFixture,
nugetNupkgMissingNuspecFixture,
ungroupedNuspec,
NUPKG_NAME_ENCODING_CASES,
nugetNupkgNameEncodingFixture,
} from "./NugetNupkg.fixture.js";
import {
ArtifactReaderFailure,
ZipArtifactReader,
} from "../resources/index.js";
import { ArtifactReaderFailure } from "../resources/index.js";
import {
NUPKG_LIMITATIONS,
distinctTargetMatches,
@@ -44,48 +36,6 @@ const artifactOf = (bytes: Uint8Array) => ({
bytes: bytes.length,
});
it.each(NUPKG_NAME_ENCODING_CASES)(
"preserves $encoding filename provenance consistently with the shared ZIP reader",
async (input) => {
const bytes = await nugetNupkgNameEncodingFixture(input);
const result = await parseNugetNupkg(bytes, artifactOf(bytes), defaults);
expect(result.contents).toContainEqual(
expect.objectContaining({
name: input.name,
path: input.name,
role: "lib",
}),
);
const root = await createTestTempDirectory("rea-nupkg-name-encoding-");
const path = join(root, "package.nupkg");
await writeFile(path, bytes);
const reader = new ZipArtifactReader(path, "zip");
try {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const shared = entries.find((entry) => entry.path === input.name);
expect(shared).toBeDefined();
const warnings = result.limitations.filter((value) =>
value.includes("CP437"),
);
if (input.uncertain) {
expect(shared?.limitations).toEqual([
expect.stringContaining(input.rawName.toString("hex")),
]);
expect(warnings).toEqual([
expect.stringContaining(shared!.limitations[0]!),
]);
expect(warnings[0]).toContain(JSON.stringify(input.name));
} else {
expect(shared?.limitations).toEqual([]);
expect(warnings).toEqual([]);
}
} finally {
await reader.close();
}
},
);
it("redacts an unfollowed authority manifest target before package-path normalization", async () => {
const bytes = await nugetNupkgFixture({
relsTarget: "//user:password@feed.invalid/missing.nuspec?token=visible",
+8 -2
View File
@@ -43,14 +43,20 @@ cliTest(
);
expect(evidence.normalized_result).toEqual(cliEvidence.normalized_result);
expect(evidence.normalized_result.contents).toContainEqual(
expect.objectContaining({ name: input.name, path: input.name }),
expect.objectContaining({
name: input.name,
path: input.name,
role: "lib",
}),
);
const warnings = evidence.normalized_result.limitations.filter((value) =>
value.includes("CP437"),
);
if (input.uncertain) {
expect(warnings).toEqual([
expect.stringContaining(input.rawName.toString("hex")),
expect.stringContaining(
`CP437 fallback; source encoding unverified; raw hex: ${input.rawName.toString("hex")}`,
),
]);
expect(warnings[0]).toContain(JSON.stringify(input.name));
expect(evidence.limitations).toContain(warnings[0]);
@@ -160,7 +160,12 @@ const legacyControlNames = [
] as const;
it.each([
["legacy CP437", Buffer.from([0x82, 0x2e, 0x74, 0x78, 0x74]), false, "é.txt"],
[
"legacy CP437 mapping",
Buffer.from([0x82, 0x80, 0x9e, 0xe1, 0xff, 0x2e, 0x74, 0x78, 0x74]),
false,
"éÇ₧ß\u00a0.txt",
],
["unflagged ASCII", Buffer.from("plain.txt"), false, "plain.txt"],
...legacyControlNames,
[
@@ -297,29 +302,6 @@ it("preserves valid Info-ZIP Unicode Path overrides", async () => {
);
});
it("reports unflagged high-byte ZIP names as CP437 readings with their source bytes", async () => {
const root = await createTestTempDirectory("rea-zip-legacy-name-limitation-");
const path = join(root, "legacy-name.zip");
const rawName = Buffer.from([0xc7, 0xd1, 0xb1, 0xdb, 0x2e, 0x74, 0x78, 0x74]);
await writeSingleEntryZip(path, "name.txt");
const archive = await readFile(path);
await writeFile(
path,
rewriteSingleEntryZipName(archive, "name.txt", rawName, false),
);
const inventory = await inventoryArtifact(path);
expect(inventory.occurrences).toContainEqual(
expect.objectContaining({
logical_path: "╟╤▒█.txt",
hash_status: "verified",
limitations: [
expect.stringMatching(/CP437.*unverified.*c7d1b1db2e747874/iu),
],
}),
);
});
it("falls back to the stored name and reports bytes when a Unicode Path field CRC is invalid", async () => {
const root = await createTestTempDirectory(
"rea-zip-invalid-unicode-extra-crc-",
+1
View File
@@ -294,6 +294,7 @@ it("preserves raw legacy ZIP name bytes and CP437 path interpretation through MC
expect(inventory.occurrences).toContainEqual(
expect.objectContaining({
logical_path: "╟╤▒█.txt",
hash_status: "verified",
limitations: [
expect.stringMatching(/CP437.*unverified.*c7d1b1db2e747874/iu),
],
@@ -88,7 +88,6 @@ it("gives the UTF-8 flag precedence over a Unicode Path for names and formats",
});
it.each([
"file",
"crc",
"version",
"short",