Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/package-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions scripts/lib/paths.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand All @@ -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();
Expand Down
12 changes: 8 additions & 4 deletions scripts/lib/rules/files.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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}`);
}
Expand All @@ -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) {
Expand All @@ -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()) {
Expand Down
18 changes: 18 additions & 0 deletions test/files.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down