fix(plugins): align package path contract validation

This commit is contained in:
ryan-wong-coder committed 2026-07-17 19:47:16 +08:00
1 parent 2a8dba4818
commit ce3bde5ee4
7 files changed
+52 -12

No files matched your search

+15 -1
View File
@@ -46,9 +46,23 @@ async function createPlugin(root: string): Promise<string> {
}
test("path validation rejects traversal, platform aliases, and duplicates", () => {
for (const unsafe of ["../escape", "/absolute", "C:/drive", "a\\b", "a/../b", "CON", "a//b"]) {
for (const unsafe of [
"../escape",
"/absolute",
"C:/drive",
"a\\b",
"a/../b",
"CON",
"assets/PRN.txt",
"file.",
"folder/file ",
"folder/file?.js",
"a//b",
"😀".repeat(129),
]) {
assert.throws(() => assertSafePackagePath(unsafe));
}
assert.equal(assertSafePackagePath("😀".repeat(128)), "😀".repeat(128));
const registry = new PackagePathRegistry();
registry.add("dist/Plugin.js");
assert.throws(() => registry.add("dist/plugin.js"), /case-colliding/);
+1
View File
@@ -4,6 +4,7 @@ export const PACKAGE_LIMITS = Object.freeze({
singleFileBytes: 50 * 1024 * 1024,
manifestBytes: 1024 * 1024,
fileCount: 5_000,
pathCharacters: 128,
pathBytes: 512,
});
+5
View File
@@ -11,6 +11,11 @@ export function assertSafePackagePath(input: string): string {
if (!input || input !== input.normalize("NFC")) {
throw new Error(`Package path must be non-empty NFC text: ${JSON.stringify(input)}`);
}
if ([...input].length > PACKAGE_LIMITS.pathCharacters) {
throw new Error(
`Package path exceeds ${PACKAGE_LIMITS.pathCharacters} Unicode characters: ${input}`,
);
}
if (Buffer.byteLength(input, "utf8") > PACKAGE_LIMITS.pathBytes) {
throw new Error(`Package path exceeds ${PACKAGE_LIMITS.pathBytes} UTF-8 bytes: ${input}`);
}