mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 08:31:49 +00:00
fix(plugins): reject package path prefix collisions
This commit is contained in:
1 parent
ce3bde5ee4
commit
e958a464ad
2 files changed
+70
-5
No files matched your search
@@ -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);
|
||||
|
||||
@@ -41,17 +41,43 @@ export function assertSafePackagePath(input: string): string {
|
||||
}
|
||||
|
||||
export class PackagePathRegistry {
|
||||
readonly #exact = new Set<string>();
|
||||
readonly #portable = new Set<string>();
|
||||
readonly #filesExact = new Set<string>();
|
||||
readonly #filesPortable = new Set<string>();
|
||||
readonly #directoriesExact = new Set<string>();
|
||||
readonly #directoriesPortable = new Set<string>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user