From 97498ddae509b62c9f3c2ed444d6782b8c31dcf3 Mon Sep 17 00:00:00 2001 From: Evander Date: Thu, 26 Nov 2026 23:02:14 -0500 Subject: [PATCH 1/2] =?UTF-8?q?fix(validate):=20=E6=B8=85=E5=8D=95?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=8C=89=20UTF-8=20=E8=A7=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E8=AF=BB=E4=B8=8D=E4=BA=86=E5=B0=B1=E6=98=AF=E8=AF=BB?= =?UTF-8?q?=E4=B8=8D=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/package-contract.md | 8 +++++ scripts/lib/fs.mjs | 15 ++++++-- test/encoding.test.mjs | 78 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 test/encoding.test.mjs diff --git a/docs/package-contract.md b/docs/package-contract.md index a2370d6b..0ba7ef40 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 — the manifests, and the files they point at — must be UTF-8. +The Host decodes them 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 +same way and reports `MANIFEST_UNREADABLE`, `MCODE_UNREADABLE`, `MINIAPP_UNREADABLE`, +`MANIFEST_APP_INVALID`, or `MANIFEST_REFERENCE_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), []); +}); From 62ccf616e90ba39b21e4b6364051395fdd84f136 Mon Sep 17 00:00:00 2001 From: Evander Date: Thu, 26 Nov 2026 23:25:06 -0500 Subject: [PATCH 2/2] =?UTF-8?q?fix(validate):=20=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E5=8F=AA=E5=88=97=E5=AE=9E=E9=99=85=E4=BC=9A=E6=8A=A5=E7=9A=84?= =?UTF-8?q?=E9=82=A3=E5=87=A0=E4=B8=AA=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/package-contract.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/package-contract.md b/docs/package-contract.md index 0ba7ef40..e0dfae54 100644 --- a/docs/package-contract.md +++ b/docs/package-contract.md @@ -295,11 +295,11 @@ the other six fields, and it checks the path before it looks for the file on dis ## Text encoding -Every file the Host reads as text — the manifests, and the files they point at — must be UTF-8. -The Host decodes them 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 -same way and reports `MANIFEST_UNREADABLE`, `MCODE_UNREADABLE`, `MINIAPP_UNREADABLE`, -`MANIFEST_APP_INVALID`, or `MANIFEST_REFERENCE_INVALID`, depending on which file it was reading. +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