diff --git a/packages/plugin-cli/src/cli.test.ts b/packages/plugin-cli/src/cli.test.ts index 506d5be7f..544ee65cf 100644 --- a/packages/plugin-cli/src/cli.test.ts +++ b/packages/plugin-cli/src/cli.test.ts @@ -66,6 +66,20 @@ test("path validation rejects traversal, platform aliases, and duplicates", () = const registry = new PackagePathRegistry(); registry.add("dist/Plugin.js"); assert.throws(() => registry.add("dist/plugin.js"), /case-colliding/); + + for (const paths of [ + ["dist", "dist/index.js"], + ["dist/index.js", "dist"], + ["DIST", "dist/index.js"], + ["dist/index.js", "DIST"], + ]) { + const collisionRegistry = new PackagePathRegistry(); + collisionRegistry.add(paths[0]); + assert.throws( + () => collisionRegistry.add(paths[1]), + /File\/directory package path collision/, + ); + } }); test("manifest validation reports permission and contribution mistakes", () => { @@ -123,6 +137,11 @@ test("archive validation rejects duplicate names and CRC corruption", async (con writeFile(path.join(directory, "a.txt"), "first\n"), writeFile(path.join(directory, "b.txt"), "second\n"), ]); + await mkdir(path.join(directory, "bbbbb")); + await Promise.all([ + writeFile(path.join(directory, "aaaaa"), "parent-file\n"), + writeFile(path.join(directory, "bbbbb/file"), "child-file\n"), + ]); const validPath = path.join(root, "valid.ncpkg"); await buildPluginPackage(directory, validPath); const validBytes = await readFile(validPath); @@ -141,6 +160,26 @@ test("archive validation rejects duplicate names and CRC corruption", async (con await writeFile(duplicatePath, duplicateBytes); await assert.rejects(validatePluginPackage(duplicatePath), /Duplicate or case-colliding/); + const prefixCollisionBytes = Buffer.from(validBytes); + let prefixReplacements = 0; + for (const [source, target] of [ + [Buffer.from("aaaaa"), Buffer.from("distx")], + [Buffer.from("bbbbb/file"), Buffer.from("distx/file")], + ]) { + for (let offset = prefixCollisionBytes.indexOf(source); offset !== -1;) { + target.copy(prefixCollisionBytes, offset); + prefixReplacements += 1; + offset = prefixCollisionBytes.indexOf(source, offset + source.byteLength); + } + } + assert.equal(prefixReplacements, 4, "ZIP should contain both local and central names"); + const prefixCollisionPath = path.join(root, "prefix-collision.ncpkg"); + await writeFile(prefixCollisionPath, prefixCollisionBytes); + await assert.rejects( + validatePluginPackage(prefixCollisionPath), + /File\/directory package path collision/, + ); + const corruptedBytes = Buffer.from(validBytes); const content = Buffer.from("# Package test\n"); const contentOffset = corruptedBytes.indexOf(content); diff --git a/packages/plugin-cli/src/packagePath.ts b/packages/plugin-cli/src/packagePath.ts index dc912d07f..35e17da30 100644 --- a/packages/plugin-cli/src/packagePath.ts +++ b/packages/plugin-cli/src/packagePath.ts @@ -41,17 +41,43 @@ export function assertSafePackagePath(input: string): string { } export class PackagePathRegistry { - readonly #exact = new Set(); - readonly #portable = new Set(); + readonly #filesExact = new Set(); + readonly #filesPortable = new Set(); + readonly #directoriesExact = new Set(); + readonly #directoriesPortable = new Set(); add(input: string): string { const safePath = assertSafePackagePath(input); const portableKey = safePath.toLowerCase(); - if (this.#exact.has(safePath) || this.#portable.has(portableKey)) { + if (this.#filesExact.has(safePath) || this.#filesPortable.has(portableKey)) { throw new Error(`Duplicate or case-colliding package path: ${safePath}`); } - this.#exact.add(safePath); - this.#portable.add(portableKey); + if ( + this.#directoriesExact.has(safePath) + || this.#directoriesPortable.has(portableKey) + ) { + throw new Error(`File/directory package path collision: ${safePath}`); + } + + const segments = safePath.split("/"); + const ancestors: string[] = []; + for (let index = 1; index < segments.length; index += 1) { + const ancestor = segments.slice(0, index).join("/"); + if ( + this.#filesExact.has(ancestor) + || this.#filesPortable.has(ancestor.toLowerCase()) + ) { + throw new Error(`File/directory package path collision: ${safePath}`); + } + ancestors.push(ancestor); + } + + this.#filesExact.add(safePath); + this.#filesPortable.add(portableKey); + for (const ancestor of ancestors) { + this.#directoriesExact.add(ancestor); + this.#directoriesPortable.add(ancestor.toLowerCase()); + } return safePath; } }