diff --git a/packages/plugin-cli/src/archive.ts b/packages/plugin-cli/src/archive.ts index 037a6dbbf..9382f7a3f 100644 --- a/packages/plugin-cli/src/archive.ts +++ b/packages/plugin-cli/src/archive.ts @@ -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 { + 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); diff --git a/packages/plugin-cli/src/cli.test.ts b/packages/plugin-cli/src/cli.test.ts index afcc89bbc..7e55cca81 100644 --- a/packages/plugin-cli/src/cli.test.ts +++ b/packages/plugin-cli/src/cli.test.ts @@ -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 }));