diff --git a/docs/package-contract.md b/docs/package-contract.md index a2370d6b..e0dfae54 100644 --- a/docs/package-contract.md +++ b/docs/package-contract.md @@ -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. diff --git a/scripts/lib/fs.mjs b/scripts/lib/fs.mjs index 9af4f4d0..c27b4fd6 100644 --- a/scripts/lib/fs.mjs +++ b/scripts/lib/fs.mjs @@ -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' }; } diff --git a/test/encoding.test.mjs b/test/encoding.test.mjs new file mode 100644 index 00000000..2998e8c7 --- /dev/null +++ b/test/encoding.test.mjs @@ -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), []); +});