fix(plugins): cap manifest reads before loading

This commit is contained in:
ryan-wong-coder committed 2026-07-17 19:47:17 +08:00
1 parent 5c1d8e4f41
commit 3120045cba
2 files changed
+61 -6

No files matched your search

+14 -4
View File
@@ -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/);
});
+47 -2
View File
@@ -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<PluginManifest> {
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);
}