diff --git a/packages/cli/src/commands/plugin.ts b/packages/cli/src/commands/plugin.ts index 537b91ec..c519ddf9 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, basename, isAbsolute } from 'node:path'; +import { join, isAbsolute } from 'node:path'; import { homedir } from 'node:os'; import { existsSync, mkdirSync, copyFileSync, cpSync, rmSync } from 'node:fs'; import chalk from 'chalk'; @@ -140,7 +140,14 @@ export class PluginCommand extends Command { : join(projectPath, '.skillkit', 'plugins'); // Persist plugin files to disk if source is a local file/directory - const isLocalPath = this.source.startsWith('./') || this.source.startsWith('/') || this.source.includes('\\') || isAbsolute(this.source); + const isLocalPath = + this.source.startsWith('./') || + this.source.startsWith('../') || + this.source.startsWith('/') || + this.source.startsWith('~') || + this.source.includes('\\') || + isAbsolute(this.source); + if (isLocalPath && existsSync(this.source)) { const pluginName = plugin.metadata.name; const targetDir = join(pluginsDir, pluginName); @@ -158,12 +165,13 @@ export class PluginCommand extends Command { // Copy entire directory cpSync(this.source, targetDir, { recursive: true }); } else { - // Copy single file + // Copy single file with a loader-recognized name if (!existsSync(targetDir)) { mkdirSync(targetDir, { recursive: true }); } - const fileName = basename(this.source); - copyFileSync(this.source, join(targetDir, fileName)); + const ext = this.source.endsWith('.json') ? '.json' : '.js'; + const destFileName = ext === '.json' ? 'plugin.json' : 'index.js'; + copyFileSync(this.source, join(targetDir, destFileName)); } this.context.stdout.write(chalk.dim(` Copied to ${targetDir}\n`)); diff --git a/packages/cli/src/commands/settings.ts b/packages/cli/src/commands/settings.ts index b9570a5a..89b7fca3 100644 --- a/packages/cli/src/commands/settings.ts +++ b/packages/cli/src/commands/settings.ts @@ -75,15 +75,20 @@ export class SettingsCommand extends Command { // Get a specific setting if (this.get) { - const value = this.getConfigValue(config, this.get); - if (value === undefined) { + // Check if the key is a known setting (not just if value is undefined) + const knownKeys = [ + 'agent', 'autoSync', 'cacheDir', 'skillsDir', + 'enabledSkills', 'disabledSkills', 'marketplaceSources', 'defaultTimeout' + ]; + if (!knownKeys.includes(this.get)) { console.error(chalk.red(`Unknown setting: ${this.get}`)); return 1; } + const value = this.getConfigValue(config, this.get); if (this.json) { console.log(JSON.stringify({ [this.get]: value })); } else { - console.log(value); + console.log(value ?? '(not set)'); } return 0; } @@ -201,7 +206,7 @@ export class SettingsCommand extends Command { case 'defaultTimeout': { const timeout = parseInt(value, 10); - if (isNaN(timeout) || timeout < 0) { + if (isNaN(timeout) || timeout <= 0) { return { success: false, error: 'defaultTimeout must be a positive number (milliseconds)' }; } config.defaultTimeout = timeout; diff --git a/packages/core/src/plugins/loader.ts b/packages/core/src/plugins/loader.ts index d1e0de98..6de64ef2 100644 --- a/packages/core/src/plugins/loader.ts +++ b/packages/core/src/plugins/loader.ts @@ -118,7 +118,15 @@ export class PluginLoader { for (const entry of entries) { const fullPath = join(dirPath, entry); - const stat = statSync(fullPath); + + // Guard against unreadable entries (permission denied, broken symlinks) + let stat; + try { + stat = statSync(fullPath); + } catch { + // Skip unreadable entries + continue; + } if (stat.isDirectory()) { // Check for package.json or plugin.json @@ -128,7 +136,10 @@ export class PluginLoader { if (existsSync(pkgPath)) { try { const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8')); - if (pkg.skillkitPlugin || pkg.keywords?.includes('skillkit-plugin')) { + // Validate required fields before pushing + if ((pkg.skillkitPlugin || pkg.keywords?.includes('skillkit-plugin')) && + pkg.name && typeof pkg.name === 'string' && + pkg.version && typeof pkg.version === 'string') { plugins.push({ name: pkg.name, version: pkg.version, @@ -143,7 +154,10 @@ export class PluginLoader { } else if (existsSync(pluginPath)) { try { const data = JSON.parse(readFileSync(pluginPath, 'utf-8')); - if (data.metadata) { + // Validate metadata has required fields + if (data.metadata && + data.metadata.name && typeof data.metadata.name === 'string' && + data.metadata.version && typeof data.metadata.version === 'string') { plugins.push(data.metadata); } } catch { @@ -183,10 +197,10 @@ export class PluginLoader { throw new Error('Plugin metadata must include a version'); } - // Validate name format - if (!/^[a-z0-9-]+$/.test(metadata.name)) { + // Validate name format (allow scoped npm names like @scope/name) + if (!/^(?:@[a-z0-9-]+\/)?[a-z0-9-]+$/.test(metadata.name)) { throw new Error( - 'Plugin name must be lowercase alphanumeric with hyphens only' + 'Plugin name must be lowercase alphanumeric with hyphens only (scoped names like @scope/name are allowed)' ); } diff --git a/packages/core/src/team/bundle.ts b/packages/core/src/team/bundle.ts index 8e23a7a3..fee85016 100644 --- a/packages/core/src/team/bundle.ts +++ b/packages/core/src/team/bundle.ts @@ -211,6 +211,9 @@ export function importBundle( skills: Record; }; + // Resolve targetDir to absolute path for consistent path containment checks + const absoluteTargetDir = resolve(targetDir); + for (const skill of data.manifest.skills) { const skillContent = data.skills[skill.name]; if (!skillContent) { @@ -218,7 +221,7 @@ export function importBundle( continue; } - const skillDir = join(targetDir, skill.name); + const skillDir = join(absoluteTargetDir, skill.name); // Check if exists if (existsSync(skillDir) && !options.overwrite) { diff --git a/packages/tui/src/screens/Plugins.tsx b/packages/tui/src/screens/Plugins.tsx index dd33ad9f..fac80adf 100644 --- a/packages/tui/src/screens/Plugins.tsx +++ b/packages/tui/src/screens/Plugins.tsx @@ -51,8 +51,11 @@ export function Plugins({ rows = 24 }: Props) { await manager.register(plugin); } } - } catch { - // Plugins directory may not exist + } catch (err: unknown) { + // Only ignore ENOENT (directory doesn't exist), rethrow other errors + if (err && typeof err === 'object' && 'code' in err && err.code !== 'ENOENT') { + throw err; + } } const allPlugins = manager.listPlugins(); @@ -84,9 +87,26 @@ export function Plugins({ rows = 24 }: Props) { setMessage(null); try { - const { createPluginManager } = await import('@skillkit/core'); + const { createPluginManager, loadPluginsFromDirectory } = await import('@skillkit/core'); + const { join } = await import('node:path'); const manager = createPluginManager(process.cwd()); + // Load plugins from directory first so enable/disable can find them + const pluginsDir = join(process.cwd(), '.skillkit', 'plugins'); + try { + const loadedPlugins = await loadPluginsFromDirectory(pluginsDir); + for (const plugin of loadedPlugins) { + if (!manager.getPlugin(plugin.metadata.name)) { + await manager.register(plugin); + } + } + } catch (err: unknown) { + // Only ignore ENOENT (directory doesn't exist), rethrow other errors + if (err && typeof err === 'object' && 'code' in err && err.code !== 'ENOENT') { + throw err; + } + } + if (currentEnabled) { manager.disablePlugin(pluginName); setMessage(`Disabled: ${pluginName}`);