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
8 changes: 8 additions & 0 deletions docs/package-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ A path in `plugin.json` (`icon`, `darkIcon`, `apps`, `mcpServers`, `hooks`, `ski
them as `MANIFEST_FIELD_INVALID` for `icon` and `darkIcon`, and as `MANIFEST_REFERENCE_INVALID` for
the other six fields, and it checks the path before it looks for the file on disk.

## Text encoding

Every file the Host reads as text must be UTF-8. The Host decodes those with a decoder that
refuses anything else, so a manifest saved in another encoding is not repaired on the way in: the
package does not install. `npm run check` decodes the manifests, and the app files they point at,
the same way — `MANIFEST_UNREADABLE`, `MCODE_UNREADABLE`, `MINIAPP_UNREADABLE`, or
`MANIFEST_APP_INVALID`, depending on which file it was reading.

## Package limits

At most 1024 files, 16 MiB per file, and 64 MiB in total.
Expand Down
15 changes: 13 additions & 2 deletions scripts/lib/fs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@ import { readdir, readFile, stat } from 'node:fs/promises';
import path from 'node:path';

const BOM = '\uFEFF';
// The Host reads the manifests with a decoder that refuses bytes that are not UTF-8, so a file
// saved in another encoding stops the package there. `ignoreBOM` keeps the byte order mark in
// the output, which is what the check below looks for; the default decoder would have taken it
// out and the check would never fire.
const UTF8_DECODER = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });

export async function readJsonFile(absolutePath) {
let text;
let bytes;
try {
text = await readFile(absolutePath, 'utf8');
bytes = await readFile(absolutePath);
} catch (error) {
if (error && error.code === 'ENOENT') return { ok: false, reason: 'missing', detail: 'file not found' };
throw error;
}
let text;
try {
text = UTF8_DECODER.decode(bytes);
} catch {
return { ok: false, reason: 'encoding', detail: 'is not valid UTF-8' };
}
if (text.startsWith(BOM)) {
return { ok: false, reason: 'bom', detail: 'starts with a UTF-8 BOM; save the file without a BOM' };
}
Expand Down
78 changes: 78 additions & 0 deletions test/encoding.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { validatePackage } from '../scripts/lib/package.mjs';
import { codes, copyExample, makeTmpRoot } from './helpers.mjs';

// The bytes of a few Chinese characters in GBK, which is what a file saved on a Chinese Windows
// machine holds. Node has no GBK codec, so the bytes are written out directly.
const GBK = new Map([
['中', [0xd6, 0xd0]],
['文', [0xce, 0xc4]],
['字', [0xd7, 0xd6]],
['节', [0xbd, 0xda]],
]);

function asGbk(text) {
const bytes = [];
for (const character of text) {
const gbk = GBK.get(character);
if (gbk) bytes.push(...gbk);
else bytes.push(...Buffer.from(character, 'utf8'));
}
return Buffer.from(bytes);
}

async function writeGbkJson(file, value) {
await writeFile(file, asGbk(`${JSON.stringify(value, null, 2)}\n`));
}

test('package: a manifest saved in another encoding is refused', async () => {
const root = await makeTmpRoot();
const { dir } = await copyExample(root);
const file = path.join(dir, '.minimax-plugin', 'plugin.json');
const manifest = JSON.parse(await readFile(file, 'utf8'));
await writeGbkJson(file, { ...manifest, description: '中文说明' });

// The bytes are not UTF-8, but they are still JSON once they are read the lenient way: the
// Chinese characters become replacement characters and the rest of the file is untouched. That
// is how the file used to pass the check while the Host refused to read it at all.
const lenient = (await readFile(file)).toString('utf8');
assert.doesNotThrow(() => JSON.parse(lenient));
const diagnostics = await validatePackage(dir);
assert.ok(codes(diagnostics).includes('MANIFEST_UNREADABLE'), `expected a refusal, got ${codes(diagnostics).join(', ')}`);
assert.match(diagnostics.find((d) => d.code === 'MANIFEST_UNREADABLE').message, /UTF-8/u);
});

test('package: a package.json saved in another encoding is refused', async () => {
const root = await makeTmpRoot();
const { dir } = await copyExample(root);
const file = path.join(dir, 'package.json');
const packageJson = JSON.parse(await readFile(file, 'utf8'));
await writeGbkJson(file, { ...packageJson, description: '中文说明' });

const diagnostics = await validatePackage(dir);
assert.ok(codes(diagnostics).includes('MCODE_UNREADABLE'), `expected a refusal, got ${codes(diagnostics).join(', ')}`);
});

test('package: a miniapp.json saved in another encoding is refused', async () => {
const root = await makeTmpRoot();
const { dir } = await copyExample(root);
const file = path.join(dir, 'miniapp', 'miniapp.json');
const miniapp = JSON.parse(await readFile(file, 'utf8'));
await writeGbkJson(file, { ...miniapp, description: '中文说明' });

const diagnostics = await validatePackage(dir);
assert.ok(codes(diagnostics).includes('MINIAPP_UNREADABLE'), `expected a refusal, got ${codes(diagnostics).join(', ')}`);
});

test('package: a manifest in UTF-8 with Chinese text still passes', async () => {
const root = await makeTmpRoot();
const { dir } = await copyExample(root);
const file = path.join(dir, '.minimax-plugin', 'plugin.json');
const manifest = JSON.parse(await readFile(file, 'utf8'));
await writeFile(file, `${JSON.stringify({ ...manifest, description: '中文说明' }, null, 2)}\n`);

assert.deepEqual(await validatePackage(dir), []);
});