mirror of
https://github.com/rohitg00/skillkit.git
synced 2026-11-18 18:04:26 +00:00
fix: apply CodeRabbit security and quality improvements
Security Fixes: - Replace custom glob-to-regex with minimatch to prevent ReDoS vulnerabilities - Fix regex pattern generation in globs.ts using battle-tested library - Eliminate unsafe regex patterns in modes.ts and permissions.ts Code Quality Improvements: - Use shared GlobMatcher utility to eliminate code duplication in modes.ts - Preserve command metadata on updates to prevent data loss - Fix single-file vs directory output path handling in command.ts - Pass --strict flag through to plan validator Race Condition & Concurrency Fixes: - Add Promise-based mutex to prevent race condition in audit logger flush() - Handle both sync and async event listeners to prevent unhandled rejections - Maintain synchronous event emission while safely handling async listeners Data Integrity Fixes: - Fix dependency checking to detect missing dependencies - Rehydrate Date fields when importing tasks from JSON - Fix executePlan to properly detect incomplete tasks State Management: - Add validation for missing step executor (fail fast) - Handle cancellation and reset executor state in finally block - Add 'cancelled' status and event to PlanStatus/PlanEvent types Dependencies: - Add minimatch@^10.0.1 to @skillkit/agents package All 639 tests passing.
This commit is contained in:
@@ -21,7 +21,8 @@
|
||||
"test": "vitest run"
|
||||
},
|
||||
"dependencies": {
|
||||
"@skillkit/core": "workspace:*"
|
||||
"@skillkit/core": "workspace:*",
|
||||
"minimatch": "^10.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.10.5",
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Implements file pattern matching for file-scoped skills.
|
||||
*/
|
||||
|
||||
import { minimatch } from 'minimatch';
|
||||
import type { GlobConfig } from './types.js';
|
||||
|
||||
/**
|
||||
@@ -90,95 +91,30 @@ export class GlobMatcher {
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert glob pattern to regex
|
||||
* Convert glob pattern to regex using minimatch
|
||||
*
|
||||
* Uses a tokenization approach to avoid conflicts between glob wildcards
|
||||
* and regex special characters in replacement strings.
|
||||
* Uses the battle-tested minimatch library to avoid ReDoS vulnerabilities
|
||||
* and ensure consistent glob matching behavior.
|
||||
*/
|
||||
private patternToRegex(pattern: string): RegExp {
|
||||
// Handle negation
|
||||
const isNegated = pattern.startsWith('!');
|
||||
const cleanPattern = isNegated ? pattern.slice(1) : pattern;
|
||||
|
||||
// Tokenize the pattern: split into segments handling **, *, ?, and literals
|
||||
const tokens: string[] = [];
|
||||
let i = 0;
|
||||
|
||||
while (i < cleanPattern.length) {
|
||||
if (cleanPattern[i] === '*') {
|
||||
if (cleanPattern[i + 1] === '*') {
|
||||
// Handle **
|
||||
if (cleanPattern[i + 2] === '/') {
|
||||
// **/ pattern
|
||||
tokens.push('**/');
|
||||
i += 3;
|
||||
} else if (i > 0 && cleanPattern[i - 1] === '/') {
|
||||
// /** pattern (already consumed /)
|
||||
tokens.push('**');
|
||||
i += 2;
|
||||
} else {
|
||||
tokens.push('**');
|
||||
i += 2;
|
||||
}
|
||||
} else {
|
||||
tokens.push('*');
|
||||
i += 1;
|
||||
}
|
||||
} else if (cleanPattern[i] === '?') {
|
||||
tokens.push('?');
|
||||
i += 1;
|
||||
} else {
|
||||
// Literal character - collect consecutive literals
|
||||
let literal = '';
|
||||
while (i < cleanPattern.length &&
|
||||
cleanPattern[i] !== '*' &&
|
||||
cleanPattern[i] !== '?') {
|
||||
literal += cleanPattern[i];
|
||||
i += 1;
|
||||
}
|
||||
tokens.push(literal);
|
||||
}
|
||||
// Adjust pattern for directory matching
|
||||
let adjustedPattern = cleanPattern;
|
||||
if (this.config.matchDirectories && !cleanPattern.endsWith('/**')) {
|
||||
// Append /** to match directories and their contents
|
||||
adjustedPattern = cleanPattern.endsWith('/')
|
||||
? `${cleanPattern}**`
|
||||
: `${cleanPattern}/**`;
|
||||
}
|
||||
|
||||
// Convert tokens to regex parts
|
||||
const regexParts: string[] = [];
|
||||
// Use minimatch to create a safe regex
|
||||
// minimatch returns null if pattern is invalid, fallback to match-nothing regex
|
||||
const regex = minimatch.makeRe(adjustedPattern, { dot: this.config.matchHidden });
|
||||
|
||||
for (let t = 0; t < tokens.length; t++) {
|
||||
const token = tokens[t];
|
||||
|
||||
if (token === '**/') {
|
||||
// **/ matches zero or more path segments with trailing /
|
||||
// At start: any prefix or nothing
|
||||
// In middle: any directories
|
||||
if (t === 0) {
|
||||
regexParts.push('(?:[^/]+/)*');
|
||||
} else {
|
||||
regexParts.push('(?:[^/]+/)*');
|
||||
}
|
||||
} else if (token === '**') {
|
||||
// ** matches any path (including /)
|
||||
regexParts.push('.*');
|
||||
} else if (token === '*') {
|
||||
// * matches any character except /
|
||||
regexParts.push('[^/]*');
|
||||
} else if (token === '?') {
|
||||
// ? matches single character except /
|
||||
regexParts.push('[^/]');
|
||||
} else {
|
||||
// Literal - escape regex special characters
|
||||
const escaped = token.replace(/[.+^${}()|[\]\\]/g, '\\$&');
|
||||
regexParts.push(escaped);
|
||||
}
|
||||
}
|
||||
|
||||
let regexPattern = regexParts.join('');
|
||||
|
||||
// Handle directory matching
|
||||
if (this.config.matchDirectories) {
|
||||
regexPattern = `${regexPattern}(?:/.*)?`;
|
||||
}
|
||||
|
||||
return new RegExp(`^${regexPattern}$`);
|
||||
return regex || /(?!)/; // /(?!)/ is a regex that never matches anything
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
import type { AgentMode, ModeConfig } from './types.js';
|
||||
import { GlobMatcher } from './globs.js';
|
||||
|
||||
/**
|
||||
* Default mode configurations
|
||||
@@ -156,28 +157,8 @@ export class ModeManager {
|
||||
return true;
|
||||
}
|
||||
|
||||
return config.allowedFiles.some((pattern) => {
|
||||
// Escape special regex characters except * and ?
|
||||
let regexPattern = pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&');
|
||||
|
||||
// Handle **/ at start (matches any path prefix or nothing)
|
||||
regexPattern = regexPattern.replace(/^\*\*\//, '(?:.*/)?');
|
||||
|
||||
// Handle /**/ in middle (matches any path segment or nothing)
|
||||
regexPattern = regexPattern.replace(/\/\*\*\//g, '(?:/.*)?/');
|
||||
|
||||
// Handle /** at end (matches any path suffix)
|
||||
regexPattern = regexPattern.replace(/\/\*\*$/, '(?:/.*)?');
|
||||
|
||||
// Handle remaining ** (match any path)
|
||||
regexPattern = regexPattern.replace(/\*\*/g, '.*');
|
||||
|
||||
// Handle * (match any except /)
|
||||
regexPattern = regexPattern.replace(/\*/g, '[^/]*');
|
||||
|
||||
const regex = new RegExp(`^${regexPattern}$`);
|
||||
return regex.test(filePath);
|
||||
});
|
||||
const matcher = new GlobMatcher({ include: config.allowedFiles });
|
||||
return matcher.matches(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Implements permission patterns for agent skill access control.
|
||||
*/
|
||||
|
||||
import { minimatch } from 'minimatch';
|
||||
import type {
|
||||
PermissionConfig,
|
||||
PermissionPattern,
|
||||
@@ -105,16 +106,13 @@ export class PermissionManager {
|
||||
|
||||
/**
|
||||
* Match value against pattern (glob-style)
|
||||
*
|
||||
* Uses minimatch for safe glob matching, avoiding ReDoS vulnerabilities.
|
||||
*/
|
||||
private matchPattern(value: string, pattern: string): boolean {
|
||||
// Convert glob pattern to regex
|
||||
const regexPattern = pattern
|
||||
.replace(/[.+^${}()|[\]\\]/g, '\\$&') // Escape special chars
|
||||
.replace(/\*/g, '.*') // * -> .*
|
||||
.replace(/\?/g, '.'); // ? -> .
|
||||
|
||||
const regex = new RegExp(`^${regexPattern}$`, 'i');
|
||||
return regex.test(value);
|
||||
// Use minimatch for safe, battle-tested glob matching
|
||||
// nocase option ensures case-insensitive matching
|
||||
return minimatch(value, pattern, { nocase: true });
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
import { Command, Option } from 'clipanion';
|
||||
import { readFile, writeFile, mkdir } from 'node:fs/promises';
|
||||
import { resolve, join, dirname } from 'node:path';
|
||||
import { resolve, join, dirname, extname } from 'node:path';
|
||||
import { existsSync } from 'node:fs';
|
||||
import {
|
||||
createCommandRegistry,
|
||||
@@ -199,7 +199,22 @@ export class CommandCmd extends Command {
|
||||
// Check if command already exists
|
||||
const existing = commands.findIndex((c) => c.name === commandName);
|
||||
if (existing >= 0) {
|
||||
commands[existing] = command;
|
||||
// Merge new properties with existing command to preserve metadata
|
||||
const previous = commands[existing];
|
||||
commands[existing] = {
|
||||
...previous,
|
||||
name: commandName,
|
||||
skill: this.skill,
|
||||
description: this.description ?? previous.description ?? command.description,
|
||||
category: this.category ?? previous.category,
|
||||
examples: command.examples ?? previous.examples,
|
||||
// Preserve existing metadata if not provided
|
||||
aliases: previous.aliases,
|
||||
args: previous.args,
|
||||
tags: previous.tags,
|
||||
hidden: previous.hidden,
|
||||
metadata: previous.metadata,
|
||||
};
|
||||
this.context.stdout.write(`Updated command: /${commandName}\n`);
|
||||
} else {
|
||||
commands.push(command);
|
||||
@@ -243,23 +258,39 @@ export class CommandCmd extends Command {
|
||||
});
|
||||
|
||||
const format = getAgentFormat(agent);
|
||||
const outputDir = this.output || resolve(process.cwd(), format.directory);
|
||||
const outputTarget = this.output
|
||||
? resolve(this.output)
|
||||
: resolve(process.cwd(), format.directory);
|
||||
const outputDir = extname(outputTarget) ? dirname(outputTarget) : outputTarget;
|
||||
|
||||
const result = generator.generate(commands, agent);
|
||||
|
||||
if (typeof result === 'string') {
|
||||
// Single file output (e.g., Cursor rules)
|
||||
const extension = format.extension
|
||||
? format.extension.startsWith('.') ? format.extension : `.${format.extension}`
|
||||
: '.mdc';
|
||||
const filePath = extname(outputTarget)
|
||||
? outputTarget
|
||||
: join(outputTarget, `commands${extension}`);
|
||||
|
||||
if (this.dryRun) {
|
||||
this.context.stdout.write('Would write to: ' + outputDir + '\n\n');
|
||||
this.context.stdout.write('Would write to: ' + filePath + '\n\n');
|
||||
this.context.stdout.write(result);
|
||||
} else {
|
||||
await mkdir(dirname(outputDir), { recursive: true });
|
||||
const filePath = join(outputDir, 'commands.mdc');
|
||||
await mkdir(dirname(filePath), { recursive: true });
|
||||
await writeFile(filePath, result);
|
||||
this.context.stdout.write(`Generated: ${filePath}\n`);
|
||||
}
|
||||
} else {
|
||||
// Multiple file output
|
||||
if (extname(outputTarget)) {
|
||||
this.context.stderr.write(
|
||||
'Error: --output must be a directory for multi-file formats\n'
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (this.dryRun) {
|
||||
this.context.stdout.write('Would write to: ' + outputDir + '\n\n');
|
||||
for (const [filename, content] of result) {
|
||||
|
||||
@@ -225,7 +225,7 @@ export class PlanCommand extends Command {
|
||||
const plan = parser.parse(content);
|
||||
|
||||
// Validate first
|
||||
const validator = createPlanValidator();
|
||||
const validator = createPlanValidator({ strict: this.strict });
|
||||
const validation = validator.validate(plan);
|
||||
|
||||
if (!validation.valid) {
|
||||
|
||||
@@ -12,6 +12,7 @@ export class AuditLogger {
|
||||
private logFile: string;
|
||||
private buffer: AuditEvent[] = [];
|
||||
private flushInterval: NodeJS.Timeout | null = null;
|
||||
private flushing: Promise<void> | null = null;
|
||||
|
||||
constructor(logDir: string) {
|
||||
this.logFile = path.join(logDir, 'audit.log');
|
||||
@@ -116,16 +117,36 @@ export class AuditLogger {
|
||||
}
|
||||
|
||||
async flush(): Promise<void> {
|
||||
// If already flushing, wait for it to complete
|
||||
if (this.flushing) {
|
||||
return this.flushing;
|
||||
}
|
||||
|
||||
if (this.buffer.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Start flush with mutex
|
||||
this.flushing = this.doFlush();
|
||||
try {
|
||||
await this.flushing;
|
||||
} finally {
|
||||
this.flushing = null;
|
||||
}
|
||||
}
|
||||
|
||||
private async doFlush(): Promise<void> {
|
||||
// Snapshot buffer and clear it before async operations
|
||||
const toFlush = this.buffer;
|
||||
this.buffer = [];
|
||||
|
||||
try {
|
||||
const events = await this.loadEvents();
|
||||
events.push(...this.buffer);
|
||||
events.push(...toFlush);
|
||||
await this.saveEvents(events);
|
||||
this.buffer = [];
|
||||
} catch (error) {
|
||||
// Restore events on failure
|
||||
this.buffer = [...toFlush, ...this.buffer];
|
||||
console.error('Failed to flush audit log:', error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ describe('TaskManager', () => {
|
||||
dependencies: [dep.id],
|
||||
});
|
||||
|
||||
expect(() => manager.assignTask(task.id, 'agent-1')).toThrow('unfinished dependencies');
|
||||
expect(() => manager.assignTask(task.id, 'agent-1')).toThrow('unfinished or missing dependencies');
|
||||
});
|
||||
|
||||
it('should allow assignment when dependencies are complete', () => {
|
||||
|
||||
@@ -115,11 +115,11 @@ export class TaskManager {
|
||||
if (task.dependencies && task.dependencies.length > 0) {
|
||||
const unfinished = task.dependencies.filter((depId) => {
|
||||
const dep = this.tasks.get(depId);
|
||||
return dep && dep.status !== 'completed';
|
||||
return !dep || dep.status !== 'completed';
|
||||
});
|
||||
|
||||
if (unfinished.length > 0) {
|
||||
throw new Error(`Task has unfinished dependencies: ${unfinished.join(', ')}`);
|
||||
throw new Error(`Task has unfinished or missing dependencies: ${unfinished.join(', ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -372,9 +372,16 @@ export class TaskManager {
|
||||
private emit(event: TaskEvent, task: Task): void {
|
||||
for (const listener of this.listeners) {
|
||||
try {
|
||||
listener(event, task);
|
||||
// Call listener (may return Promise or void)
|
||||
const result = listener(event, task);
|
||||
// If it returns a Promise, catch rejections
|
||||
if (result instanceof Promise) {
|
||||
result.catch(() => {
|
||||
// Ignore async listener errors
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
// Ignore listener errors
|
||||
// Ignore sync listener errors
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -399,7 +406,26 @@ export class TaskManager {
|
||||
fromJSON(tasks: Task[]): void {
|
||||
this.clear();
|
||||
for (const task of tasks) {
|
||||
this.tasks.set(task.id, task);
|
||||
// Rehydrate Date fields that were serialized to strings
|
||||
const hydrated: Task = {
|
||||
...task,
|
||||
createdAt: new Date(task.createdAt),
|
||||
updatedAt: new Date(task.updatedAt),
|
||||
plan: task.plan
|
||||
? {
|
||||
...task.plan,
|
||||
submittedAt: new Date(task.plan.submittedAt),
|
||||
approvedAt: task.plan.approvedAt ? new Date(task.plan.approvedAt) : undefined,
|
||||
}
|
||||
: undefined,
|
||||
result: task.result
|
||||
? {
|
||||
...task.result,
|
||||
completedAt: new Date(task.result.completedAt),
|
||||
}
|
||||
: undefined,
|
||||
};
|
||||
this.tasks.set(hydrated.id, hydrated);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -492,13 +492,22 @@ export class TeamOrchestrator {
|
||||
idleAgent.currentTask = undefined;
|
||||
}
|
||||
|
||||
// Check for failed tasks
|
||||
for (const task of taskManager.getAllTasks()) {
|
||||
// Check for failed or incomplete tasks
|
||||
const allTasks = taskManager.getAllTasks();
|
||||
for (const task of allTasks) {
|
||||
if (task.status === 'failed') {
|
||||
failedTasks.push(task.id);
|
||||
}
|
||||
}
|
||||
|
||||
// Also check for incomplete tasks (pending, in_progress, etc.)
|
||||
const incomplete = allTasks.filter(
|
||||
(task) => task.status !== 'completed' && task.status !== 'failed'
|
||||
);
|
||||
if (incomplete.length > 0) {
|
||||
failedTasks.push(...incomplete.map((task) => task.id));
|
||||
}
|
||||
|
||||
return {
|
||||
success: failedTasks.length === 0,
|
||||
taskResults,
|
||||
@@ -588,9 +597,16 @@ export class TeamOrchestrator {
|
||||
private emit(event: TeamEvent, team: Team, agent?: AgentInstance): void {
|
||||
for (const listener of this.listeners) {
|
||||
try {
|
||||
listener(event, team, agent);
|
||||
// Call listener (may return Promise or void)
|
||||
const result = listener(event, team, agent);
|
||||
// If it returns a Promise, catch rejections
|
||||
if (result instanceof Promise) {
|
||||
result.catch(() => {
|
||||
// Ignore async listener errors
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
// Ignore listener errors
|
||||
// Ignore sync listener errors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ export class PlanExecutor {
|
||||
*/
|
||||
async execute(plan: StructuredPlan, options?: PlanExecutionOptions): Promise<PlanExecutionResult> {
|
||||
const startTime = Date.now();
|
||||
let aborted = false;
|
||||
const result: PlanExecutionResult = {
|
||||
success: true,
|
||||
completedTasks: [],
|
||||
@@ -79,6 +80,9 @@ export class PlanExecutor {
|
||||
for (const taskId of executionOrder) {
|
||||
// Check if aborted
|
||||
if (this.abortController.signal.aborted) {
|
||||
aborted = true;
|
||||
result.success = false;
|
||||
result.errors = [...(result.errors ?? []), 'Execution cancelled'];
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -127,11 +131,19 @@ export class PlanExecutor {
|
||||
}
|
||||
|
||||
// Update plan status
|
||||
plan.status = result.success ? 'completed' : 'failed';
|
||||
plan.status = aborted ? 'cancelled' : result.success ? 'completed' : 'failed';
|
||||
plan.updatedAt = new Date();
|
||||
|
||||
result.durationMs = Date.now() - startTime;
|
||||
this.emit(result.success ? 'plan:execution_completed' : 'plan:execution_failed', plan);
|
||||
|
||||
// Emit appropriate event
|
||||
if (aborted) {
|
||||
this.emit('plan:execution_cancelled', plan);
|
||||
} else if (result.success) {
|
||||
this.emit('plan:execution_completed', plan);
|
||||
} else {
|
||||
this.emit('plan:execution_failed', plan);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
@@ -142,6 +154,12 @@ export class PlanExecutor {
|
||||
|
||||
this.emit('plan:execution_failed', plan);
|
||||
return result;
|
||||
} finally {
|
||||
// Reset executor state
|
||||
this.abortController = undefined;
|
||||
this.isPaused = false;
|
||||
this.resumePromise = undefined;
|
||||
this.resumeResolve = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,6 +229,10 @@ export class PlanExecutor {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// No step executor configured - fail fast
|
||||
errors.push('No step executor configured');
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ export type StepType = 'test' | 'verify' | 'implement' | 'commit' | 'review' | '
|
||||
/**
|
||||
* Plan status
|
||||
*/
|
||||
export type PlanStatus = 'draft' | 'ready' | 'executing' | 'paused' | 'completed' | 'failed';
|
||||
export type PlanStatus = 'draft' | 'ready' | 'executing' | 'paused' | 'completed' | 'failed' | 'cancelled';
|
||||
|
||||
/**
|
||||
* Task step within a plan task
|
||||
@@ -257,6 +257,7 @@ export type PlanEvent =
|
||||
| 'plan:task_failed'
|
||||
| 'plan:execution_completed'
|
||||
| 'plan:execution_failed'
|
||||
| 'plan:execution_cancelled'
|
||||
| 'plan:paused'
|
||||
| 'plan:resumed';
|
||||
|
||||
|
||||
Generated
+3
@@ -63,6 +63,9 @@ importers:
|
||||
'@skillkit/core':
|
||||
specifier: workspace:*
|
||||
version: link:../core
|
||||
minimatch:
|
||||
specifier: ^10.0.1
|
||||
version: 10.1.1
|
||||
devDependencies:
|
||||
'@types/node':
|
||||
specifier: ^22.10.5
|
||||
|
||||
Reference in New Issue
Block a user