fix(plugins): reject in-tree package outputs

This commit is contained in:
ryan-wong-coder committed 2026-07-17 19:47:16 +08:00
1 parent 4ca3e22aa7
commit fc535b95b1
2 files changed
+56

No files matched your search

+34
View File
@@ -5,6 +5,7 @@ import {
mkdir,
open,
opendir,
realpath,
rename,
rm,
stat,
@@ -110,6 +111,31 @@ function isExecutablePackageFile(packagePath: string, mode: number): boolean {
|| EXECUTABLE_EXTENSIONS.has(path.posix.extname(packagePath).toLowerCase());
}
function isSameOrDescendantPath(parentPath: string, candidatePath: string): boolean {
const relativePath = path.relative(parentPath, candidatePath);
return relativePath === ""
|| (!path.isAbsolute(relativePath)
&& relativePath !== ".."
&& !relativePath.startsWith(`..${path.sep}`));
}
async function resolveThroughExistingAncestor(targetPath: string): Promise<string> {
let currentPath = targetPath;
const missingSegments: string[] = [];
while (true) {
try {
const resolvedAncestor = await realpath(currentPath);
return path.join(resolvedAncestor, ...missingSegments.reverse());
} catch (error) {
if (!(error instanceof Error && "code" in error && error.code === "ENOENT")) throw error;
const parentPath = path.dirname(currentPath);
if (parentPath === currentPath) throw error;
missingSegments.push(path.basename(currentPath));
currentPath = parentPath;
}
}
}
async function scanPackageDirectory(
pluginDirectory: string,
manifest: PluginManifest,
@@ -342,6 +368,14 @@ export async function buildPluginPackage(
if (!resolvedOutputPath.endsWith(".ncpkg")) {
throw new Error("Plugin package output must use the .ncpkg extension");
}
const canonicalSourceDirectory = await realpath(sourceDirectory);
const canonicalOutputPath = await resolveThroughExistingAncestor(resolvedOutputPath);
if (
isSameOrDescendantPath(sourceDirectory, resolvedOutputPath)
|| isSameOrDescendantPath(canonicalSourceDirectory, canonicalOutputPath)
) {
throw new Error("Plugin package output must be outside the plugin source directory");
}
const manifest = await readAndValidateManifest(sourceDirectory);
const files = await scanPackageDirectory(sourceDirectory, manifest);
await writeArchive(files, resolvedOutputPath);
+22
View File
@@ -253,6 +253,28 @@ test("packer ignores root dev artifacts without dropping nested runtime dependen
assert.equal(validation.fileCount, 4);
});
test("packer rejects outputs inside the plugin source tree", async (context) => {
const root = await mkdtemp(path.join(tmpdir(), "netcatty-plugin-output-containment-"));
context.after(() => rm(root, { recursive: true, force: true }));
const directory = await createPlugin(root);
const nestedOutput = path.join(directory, "dist/plugin.ncpkg");
await writeFile(nestedOutput, "previous package output\n");
await assert.rejects(
buildPluginPackage(directory, nestedOutput),
/output must be outside the plugin source directory/,
);
if (process.platform !== "win32") {
const outputAlias = path.join(root, "output-alias");
await symlink(path.join(directory, "dist"), outputAlias);
await assert.rejects(
buildPluginPackage(directory, path.join(outputAlias, "plugin.ncpkg")),
/output must be outside the plugin source directory/,
);
}
});
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 }));