fix: address PR review feedback (round 3)

- plugin.ts: Add '../' and '~' to isLocalPath detection, use
  loader-recognized names (index.js, plugin.json) for single-file copy
- plugin.ts: Remove unused 'basename' import
- settings.ts: Change defaultTimeout validation from < 0 to <= 0
- settings.ts: Use knownKeys whitelist for unknown key detection
- loader.ts: Wrap statSync in try/catch for scanDirectory
- loader.ts: Validate required fields (name, version) before pushing
- loader.ts: Update regex to allow scoped npm names (@scope/name)
- bundle.ts: Resolve targetDir to absolute path for path containment
- Plugins.tsx: Only ignore ENOENT errors, rethrow others
- Plugins.tsx: Load plugins in togglePlugin before enable/disable
This commit is contained in:
Rohit Ghumare
2026-01-23 21:48:24 +00:00
parent 402d179c06
commit be06956fd4
5 changed files with 69 additions and 19 deletions
+13 -5
View File
@@ -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`));
+9 -4
View File
@@ -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;
+20 -6
View File
@@ -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)'
);
}
+4 -1
View File
@@ -211,6 +211,9 @@ export function importBundle(
skills: Record<string, string>;
};
// 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) {
+23 -3
View File
@@ -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}`);