From 3120045cba67785428fdbc7613d30d87725ef94b Mon Sep 17 00:00:00 2001 From: ryan-wong-coder Date: Fri, 17 Jul 2026 19:47:11 +0800 Subject: [PATCH] fix(plugins): cap manifest reads before loading --- packages/plugin-cli/src/cli.test.ts | 18 ++++++++--- packages/plugin-cli/src/manifest.ts | 49 +++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/packages/plugin-cli/src/cli.test.ts b/packages/plugin-cli/src/cli.test.ts index 565deae04..060ccfec1 100644 --- a/packages/plugin-cli/src/cli.test.ts +++ b/packages/plugin-cli/src/cli.test.ts @@ -7,6 +7,7 @@ import { readFile, rm, symlink, + truncate, writeFile, } from "node:fs/promises"; import { tmpdir } from "node:os"; @@ -526,9 +527,18 @@ test("packer rejects outputs inside the plugin source tree", async (context) => test("manifest byte limit is enforced before JSON parsing", async (context) => { const root = await mkdtemp(path.join(tmpdir(), "netcatty-plugin-limit-")); context.after(() => rm(root, { recursive: true, force: true })); - await writeFile( - path.join(root, "netcatty.plugin.json"), - `{"padding":"${"x".repeat(PACKAGE_LIMITS.manifestBytes)}"}`, - ); + const manifestPath = path.join(root, "netcatty.plugin.json"); + await writeFile(manifestPath, "{}"); + await truncate(manifestPath, PACKAGE_LIMITS.manifestBytes * 4); await assert.rejects(readAndValidateManifest(root), /manifest exceeds/); }); + +test("manifest validation refuses symlinked source manifests", async (context) => { + if (process.platform === "win32") return; + const root = await mkdtemp(path.join(tmpdir(), "netcatty-plugin-manifest-link-")); + context.after(() => rm(root, { recursive: true, force: true })); + const target = path.join(root, "target.json"); + await writeFile(target, JSON.stringify(manifest())); + await symlink(target, path.join(root, "netcatty.plugin.json")); + await assert.rejects(readAndValidateManifest(root), /must be a regular file/); +}); diff --git a/packages/plugin-cli/src/manifest.ts b/packages/plugin-cli/src/manifest.ts index 2822f5390..fb16d8a7b 100644 --- a/packages/plugin-cli/src/manifest.ts +++ b/packages/plugin-cli/src/manifest.ts @@ -1,5 +1,9 @@ import { createRequire } from "node:module"; -import { readFile } from "node:fs/promises"; +import { + lstat, + open, + readFile, +} from "node:fs/promises"; import path from "node:path"; import type { @@ -520,6 +524,47 @@ export async function readAndValidateManifest( pluginDirectory: string, ): Promise { const manifestPath = path.join(pluginDirectory, "netcatty.plugin.json"); - const contents = await readFile(manifestPath); + const initialStats = await lstat(manifestPath); + if (!initialStats.isFile()) { + throw new Error("Plugin manifest must be a regular file"); + } + if (initialStats.size > PACKAGE_LIMITS.manifestBytes) { + throw new Error(`Plugin manifest exceeds ${PACKAGE_LIMITS.manifestBytes} bytes`); + } + const handle = await open(manifestPath, "r"); + let contents: Uint8Array; + try { + const [openedStats, currentStats] = await Promise.all([ + handle.stat(), + lstat(manifestPath), + ]); + if (!openedStats.isFile() || !currentStats.isFile()) { + throw new Error("Plugin manifest must be a regular file"); + } + if (openedStats.dev !== currentStats.dev || openedStats.ino !== currentStats.ino) { + throw new Error("Plugin manifest changed while being opened"); + } + if (openedStats.size > PACKAGE_LIMITS.manifestBytes) { + throw new Error(`Plugin manifest exceeds ${PACKAGE_LIMITS.manifestBytes} bytes`); + } + const buffer = new Uint8Array(PACKAGE_LIMITS.manifestBytes + 1); + let bytesRead = 0; + while (bytesRead < buffer.byteLength) { + const result = await handle.read( + buffer, + bytesRead, + buffer.byteLength - bytesRead, + null, + ); + if (result.bytesRead === 0) break; + bytesRead += result.bytesRead; + } + if (bytesRead > PACKAGE_LIMITS.manifestBytes) { + throw new Error(`Plugin manifest exceeds ${PACKAGE_LIMITS.manifestBytes} bytes`); + } + contents = buffer.subarray(0, bytesRead); + } finally { + await handle.close(); + } return parseAndValidateManifestContents(contents); }