fix: address PR review issues - round 5

CRITICAL fixes:
- plugin.ts: Add path traversal protection in uninstallPlugin with name validation
  and resolved path checks
- bundle.ts: Fix parseSkillContent to not drop first file when content starts
  with "--- file ---" header

MAJOR fixes:
- plugin.ts: Add isValidPluginName() helper and validate plugin.metadata.name
  before using in file paths
- plugin.ts: Preserve .mjs extension for single-file ESM plugins
- bundle.ts: Use cross-platform path containment checks with relative() and sep
  instead of hardcoded '/'
- loader.ts: Add support for .mjs files (index.mjs, plugin.mjs)

MINOR fixes:
- plugin.ts: Expand tilde (~) path BEFORE calling loadPlugin
- settings.ts: Use null instead of undefined in JSON output for --get so key
  is preserved
This commit is contained in:
Rohit Ghumare
2026-01-23 22:21:03 +00:00
parent 50e8a389de
commit dcf12f6826
4 changed files with 82 additions and 22 deletions
+58 -10
View File
@@ -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<typeof createPluginManager>): Promise<number> {
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`));
+2 -1
View File
@@ -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)');
}
+8 -2
View File
@@ -296,13 +296,19 @@ export async function loadPluginsFromDirectory(dirPath: string): Promise<Plugin[
try {
if (stat.isDirectory()) {
// Try to load from directory
// Try to load from directory (check ESM .mjs files first, then .js)
const indexMjsPath = join(fullPath, 'index.mjs');
const indexPath = join(fullPath, 'index.js');
const mainMjsPath = join(fullPath, 'plugin.mjs');
const mainPath = join(fullPath, 'plugin.js');
const jsonPath = join(fullPath, 'plugin.json');
if (existsSync(indexPath)) {
if (existsSync(indexMjsPath)) {
plugins.push(await loader.loadFromFile(indexMjsPath));
} else if (existsSync(indexPath)) {
plugins.push(await loader.loadFromFile(indexPath));
} else if (existsSync(mainMjsPath)) {
plugins.push(await loader.loadFromFile(mainMjsPath));
} else if (existsSync(mainPath)) {
plugins.push(await loader.loadFromFile(mainPath));
} else if (existsSync(jsonPath)) {
+14 -9
View File
@@ -5,7 +5,7 @@
*/
import { existsSync, readFileSync, writeFileSync, mkdirSync, readdirSync, statSync } from 'node:fs';
import { join, basename, resolve, relative, dirname } from 'node:path';
import { join, basename, resolve, relative, dirname, sep } from 'node:path';
import { createHash } from 'node:crypto';
import type { BundleManifest } from './types.js';
import type { AgentType } from '../types.js';
@@ -252,9 +252,10 @@ export function importBundle(
const skillDir = join(absoluteTargetDir, skill.name);
// Double-check the resolved path is within target directory
// Double-check the resolved path is within target directory (cross-platform)
const resolvedSkillDir = resolve(skillDir);
if (!resolvedSkillDir.startsWith(absoluteTargetDir + '/') && resolvedSkillDir !== absoluteTargetDir) {
const relativeToTarget = relative(absoluteTargetDir, resolvedSkillDir);
if (relativeToTarget.startsWith('..') || relativeToTarget.startsWith(sep)) {
errors.push(`Skill "${skill.name}" would escape target directory`);
continue;
}
@@ -275,15 +276,15 @@ export function importBundle(
for (const [filePath, fileContent] of Object.entries(files)) {
// Validate path to prevent path traversal attacks
const fullPath = resolve(skillDir, filePath);
const relativePath = relative(skillDir, fullPath);
const relativeToSkill = relative(skillDir, fullPath);
// Check for path traversal (path escapes skillDir)
if (relativePath.startsWith('..') || resolve(fullPath) !== fullPath || !fullPath.startsWith(skillDir)) {
// Check for path traversal (cross-platform)
if (relativeToSkill.startsWith('..') || relativeToSkill.startsWith(sep)) {
errors.push(`Skill "${skill.name}" contains invalid file path: ${filePath}`);
continue;
}
const fileDir = join(fullPath, '..');
const fileDir = dirname(fullPath);
if (!existsSync(fileDir)) {
mkdirSync(fileDir, { recursive: true });
}
@@ -305,9 +306,13 @@ export function importBundle(
*/
function parseSkillContent(content: string): Record<string, string> {
const files: Record<string, string> = {};
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];