From f9d246fe2c6890c1b9f8964242535e7ca888a5d4 Mon Sep 17 00:00:00 2001 From: Evander Date: Thu, 26 Nov 2026 23:08:47 -0500 Subject: [PATCH] =?UTF-8?q?fix(validate):=20=E5=8F=AA=E5=B7=AE=E5=A4=A7?= =?UTF-8?q?=E5=B0=8F=E5=86=99=E7=9A=84=E4=B8=A4=E6=9D=A1=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E7=AE=97=E6=92=9E=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/package-contract.md | 4 ++++ scripts/lib/paths.mjs | 18 ++++++++++++++++-- scripts/lib/rules/files.mjs | 12 ++++++++---- test/files.test.mjs | 18 ++++++++++++++++++ 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/docs/package-contract.md b/docs/package-contract.md index a2370d6b..ad34f3c8 100644 --- a/docs/package-contract.md +++ b/docs/package-contract.md @@ -286,6 +286,10 @@ Every path inside the package, and every path written in a manifest, must be por `nul`, `com1`–`com9`, `lpt1`–`lpt9`). - No symbolic links or hard links anywhere in the package, and the package directory itself is not a symbolic link. +- No two paths in the package may differ only in letter case. The Host keeps one name per path + prefix, folded to lower case, and refuses a package where two of them land on the same name: + `README.md` and `Readme.md` collide, and so do `docs/preview.png` and `docs/Preview.png`. + `npm run check` reports the same pair as `PATH_CASE_COLLISION`. A path in `plugin.json` (`icon`, `darkIcon`, `apps`, `mcpServers`, `hooks`, `skills`, `hostBindings`, `agents`) is checked with the same rules as a path in `miniapp.json`: `sub/../icon.png` and diff --git a/scripts/lib/paths.mjs b/scripts/lib/paths.mjs index ccbf4d1f..b8c41234 100644 --- a/scripts/lib/paths.mjs +++ b/scripts/lib/paths.mjs @@ -15,8 +15,7 @@ export function normalizePluginPath(value) { } export function portablePathIssue(relativePath) { - if (!relativePath) return 'path is empty'; - if (!/^[\x00-\x7f]*$/u.test(relativePath)) return 'is not ASCII'; + if (!relativePath) return 'path is empty'; if (!/^[\x00-\x7f]*$/u.test(relativePath)) return 'is not ASCII'; if (relativePath.startsWith('/') || relativePath.startsWith('\\') || /^[A-Za-z]:[\\/]/u.test(relativePath)) { return 'is absolute'; } @@ -36,6 +35,21 @@ export function portablePathIssue(relativePath) { return undefined; } +// The Host keeps a name for every prefix of every path in the package, folded to lower case, +// and refuses two paths that land on the same name. A package that holds both `README.md` and +// `Readme.md` is refused there, so the same folding belongs here. +export function registerPortablePrefix(prefixes, relativePath) { + const segments = relativePath.split('/'); + for (let index = 1; index <= segments.length; index += 1) { + const prefix = segments.slice(0, index).join('/'); + const key = prefix.normalize('NFC').toLocaleLowerCase('en-US'); + const prior = prefixes.get(key); + if (prior !== undefined && prior !== prefix) return [prior, prefix]; + prefixes.set(key, prefix); + } + return undefined; +} + export function normalizeRoutePath(value) { if (typeof value !== 'string' || !value.trim()) return { ok: false, reason: 'must be a non-empty route path' }; const route = value.trim(); diff --git a/scripts/lib/rules/files.mjs b/scripts/lib/rules/files.mjs index 13a53815..5223e6f1 100644 --- a/scripts/lib/rules/files.mjs +++ b/scripts/lib/rules/files.mjs @@ -2,7 +2,7 @@ import { lstat, readdir } from 'node:fs/promises'; import path from 'node:path'; import { EXCLUDED_DIRECTORY, LIMITS } from '../contract.mjs'; -import { portablePathIssue } from '../paths.mjs'; +import { portablePathIssue, registerPortablePrefix } from '../paths.mjs'; export async function checkFiles(report, { packageDir }) { const rootInfo = await lstat(packageDir); @@ -11,7 +11,7 @@ export async function checkFiles(report, { packageDir }) { return; } const totals = { files: 0, bytes: 0 }; - await walk(report, packageDir, '', totals); + await walk(report, packageDir, '', totals, new Map()); if (totals.files > LIMITS.maxFiles) { report.error('PACKAGE_TOO_MANY_FILES', `${totals.files} files exceed the limit of ${LIMITS.maxFiles}`); } @@ -20,7 +20,7 @@ export async function checkFiles(report, { packageDir }) { } } -async function walk(report, absoluteDir, relativeDir, totals) { +async function walk(report, absoluteDir, relativeDir, totals, prefixes) { const entries = await readdir(absoluteDir, { withFileTypes: true }); entries.sort((a, b) => a.name.localeCompare(b.name, 'en')); for (const entry of entries) { @@ -32,13 +32,17 @@ async function walk(report, absoluteDir, relativeDir, totals) { } const issue = portablePathIssue(relativePath); if (issue) report.error('PATH_NOT_PORTABLE', `path ${issue}`, relativePath); + const collision = registerPortablePrefix(prefixes, relativePath); + if (collision) { + report.error('PATH_CASE_COLLISION', `${collision[0]} and ${collision[1]} collide`, relativePath); + } const info = await lstat(absolutePath); if (info.isSymbolicLink()) { report.error('PACKAGE_SPECIAL_FILE', 'symbolic links are not allowed', relativePath); continue; } if (info.isDirectory()) { - await walk(report, absolutePath, relativePath, totals); + await walk(report, absolutePath, relativePath, totals, prefixes); continue; } if (!info.isFile()) { diff --git a/test/files.test.mjs b/test/files.test.mjs index f2b7524e..c86458ba 100644 --- a/test/files.test.mjs +++ b/test/files.test.mjs @@ -53,6 +53,24 @@ test('files: a package root that is itself a symlink is rejected before walking' assert.deepEqual(codes(diagnostics), ['PACKAGE_ROOT_SYMLINK']); }); +test('files: two paths that differ only in letter case collide', async () => { + const diagnostics = await run(async (dir) => { + await writeFile(path.join(dir, 'Readme.md'), ''); + await mkdir(path.join(dir, 'docs'), { recursive: true }); + await writeFile(path.join(dir, 'docs', 'Preview.png'), ''); + await writeFile(path.join(dir, 'docs', 'preview.png'), ''); + }); + assert.deepEqual(codes(diagnostics).sort(), ['PATH_CASE_COLLISION', 'PATH_CASE_COLLISION']); +}); + +test('files: a directory and a file that differ only in letter case collide', async () => { + const diagnostics = await run(async (dir) => { + await mkdir(path.join(dir, 'Docs'), { recursive: true }); + await writeFile(path.join(dir, 'docs'), ''); + }); + assert.deepEqual(codes(diagnostics), ['PATH_CASE_COLLISION']); +}); + test('files: a single file over 16 MiB is an error', async () => { const diagnostics = await run(async (dir) => { await writeFile(path.join(dir, 'big.bin'), Buffer.alloc(16 * 1024 * 1024 + 1));