diff --git a/packages/cli/src/commands/plugin.ts b/packages/cli/src/commands/plugin.ts index e4161134..6c95f402 100644 --- a/packages/cli/src/commands/plugin.ts +++ b/packages/cli/src/commands/plugin.ts @@ -5,7 +5,7 @@ */ import { Command, Option } from 'clipanion'; -import { join, isAbsolute } from 'node:path'; +import { join, isAbsolute, resolve, sep } from 'node:path'; import { homedir } from 'node:os'; import { existsSync, mkdirSync, copyFileSync, cpSync, rmSync } from 'node:fs'; import chalk from 'chalk'; @@ -121,15 +121,40 @@ export class PluginCommand extends Command { return 0; } + /** + * Validate plugin name to prevent path traversal attacks + */ + private isValidPluginName(name: string): boolean { + // Reject empty, path separators, or path traversal sequences + return Boolean(name) && + !name.includes('/') && + !name.includes('\\') && + !name.includes('..') && + name !== '.' && + !name.startsWith('.'); + } + private async installPlugin(pluginManager: ReturnType): Promise { if (!this.source) { this.context.stderr.write(chalk.red('--source is required for install\n')); return 1; } + // Expand tilde to home directory for all operations + const resolvedSource = this.source.startsWith('~') + ? join(homedir(), this.source.slice(1)) + : this.source; + this.context.stdout.write(`Installing plugin from ${this.source}...\n`); - const plugin = await loadPlugin(this.source); + const plugin = await loadPlugin(resolvedSource); + + // Validate plugin name from metadata + const pluginName = plugin.metadata.name; + if (!this.isValidPluginName(pluginName)) { + this.context.stderr.write(chalk.red(`Invalid plugin name: ${pluginName}\n`)); + return 1; + } // Determine plugins directory const projectPath = this.global @@ -148,15 +173,17 @@ export class PluginCommand extends Command { this.source.includes('\\') || isAbsolute(this.source); - // Expand tilde to home directory for file operations - const resolvedSource = this.source.startsWith('~') - ? join(homedir(), this.source.slice(1)) - : this.source; - if (isLocalPath && existsSync(resolvedSource)) { - const pluginName = plugin.metadata.name; const targetDir = join(pluginsDir, pluginName); + // Verify targetDir is within pluginsDir (defense in depth) + const resolvedTarget = resolve(targetDir); + const resolvedPluginsDir = resolve(pluginsDir); + if (!resolvedTarget.startsWith(resolvedPluginsDir + sep)) { + this.context.stderr.write(chalk.red('Invalid plugin name\n')); + return 1; + } + // Create plugins directory if needed if (!existsSync(pluginsDir)) { mkdirSync(pluginsDir, { recursive: true }); @@ -174,8 +201,15 @@ export class PluginCommand extends Command { if (!existsSync(targetDir)) { mkdirSync(targetDir, { recursive: true }); } - const ext = resolvedSource.endsWith('.json') ? '.json' : '.js'; - const destFileName = ext === '.json' ? 'plugin.json' : 'index.js'; + // Preserve .mjs for ESM plugins, use .js for others + let destFileName: string; + if (resolvedSource.endsWith('.json')) { + destFileName = 'plugin.json'; + } else if (resolvedSource.endsWith('.mjs')) { + destFileName = 'index.mjs'; + } else { + destFileName = 'index.js'; + } copyFileSync(resolvedSource, join(targetDir, destFileName)); } @@ -210,6 +244,12 @@ export class PluginCommand extends Command { return 1; } + // Validate plugin name to prevent path traversal attacks + if (!this.isValidPluginName(this.name)) { + this.context.stderr.write(chalk.red('Invalid plugin name\n')); + return 1; + } + await pluginManager.unregister(this.name); // Remove plugin files from disk @@ -221,6 +261,14 @@ export class PluginCommand extends Command { : join(projectPath, '.skillkit', 'plugins'); const pluginDir = join(pluginsDir, this.name); + // Verify pluginDir is within pluginsDir (defense in depth) + const resolvedPluginDir = resolve(pluginDir); + const resolvedPluginsDir = resolve(pluginsDir); + if (!resolvedPluginDir.startsWith(resolvedPluginsDir + sep)) { + this.context.stderr.write(chalk.red('Invalid plugin name\n')); + return 1; + } + if (existsSync(pluginDir)) { rmSync(pluginDir, { recursive: true, force: true }); this.context.stdout.write(chalk.dim(` Removed ${pluginDir}\n`)); diff --git a/packages/cli/src/commands/settings.ts b/packages/cli/src/commands/settings.ts index 967e8869..5b8600f5 100644 --- a/packages/cli/src/commands/settings.ts +++ b/packages/cli/src/commands/settings.ts @@ -86,7 +86,8 @@ export class SettingsCommand extends Command { } const value = this.getConfigValue(config, this.get); if (this.json) { - console.log(JSON.stringify({ [this.get]: value })); + // Use null instead of undefined so the key is preserved in JSON output + console.log(JSON.stringify({ [this.get]: value ?? null })); } else { console.log(value ?? '(not set)'); } diff --git a/packages/core/src/plugins/loader.ts b/packages/core/src/plugins/loader.ts index 7342e630..1098ea9c 100644 --- a/packages/core/src/plugins/loader.ts +++ b/packages/core/src/plugins/loader.ts @@ -296,13 +296,19 @@ export async function loadPluginsFromDirectory(dirPath: string): Promise { const files: Record = {}; - const sections = content.split(/\n--- ([^\n]+) ---\n/); - // First section is empty or content without header + // Match file headers at start of string OR after newline + // This ensures the first file isn't dropped when content starts with "--- file ---" + const sections = content.split(/(?:^|\n)--- ([^\n]+) ---\n/); + + // sections[0] is content before first header (empty if content starts with header) + // Then alternating: filePath, fileContent, filePath, fileContent, ... let i = 1; while (i < sections.length) { const filePath = sections[i];