fix: address remaining CodeRabbit/Devin issues (batch 4)

- ReDoS protection: Add pattern validator to reject dangerous regex patterns
  (nested quantifiers, overlapping alternations, excessive quantifiers)
- Add missing 'cancelled' status message in execution engine
- Make completedAt/durationMs optional in TaskExecutionResult
- Fix tag filter to skip untagged tests when tags are specified
- Improve error-solution matching: require keyword similarity + solution indicator
- Validate --limit and sanitize --tags in memory/marketplace CLI commands
- Guard against missing arrays when importing YAML learnings
- Escape YAML frontmatter values to prevent invalid output
- Preserve original learning source when merging learnings
- Fix keyword double-counting in memory index search
- Normalize keywords by stripping punctuation for better matching
This commit is contained in:
Rohit Ghumare
2026-01-23 18:01:56 +00:00
parent ed90080786
commit b008a46b8d
7 changed files with 171 additions and 39 deletions
+20 -2
View File
@@ -119,13 +119,31 @@ export class MarketplaceCommand extends Command {
private async searchSkills(marketplace: ReturnType<typeof createMarketplaceAggregator>): Promise<number> {
const query = this.query || this.action;
// Validate and parse limit
let limit = 20;
if (this.limit) {
const parsed = parseInt(this.limit, 10);
if (isNaN(parsed) || parsed <= 0) {
console.error(chalk.red('Invalid --limit value. Must be a positive number.'));
return 1;
}
limit = parsed;
}
// Sanitize tags - filter out empty strings
const tags = this.tags
?.split(',')
.map((t) => t.trim())
.filter((t) => t.length > 0);
const spinner = ora(`Searching for "${query}"...`).start();
try {
const options: MarketplaceSearchOptions = {
query: query || undefined,
limit: this.limit ? parseInt(this.limit, 10) : 20,
tags: this.tags?.split(',').map((t) => t.trim()),
limit,
tags: tags && tags.length > 0 ? tags : undefined,
source: this.source,
};
+45 -9
View File
@@ -228,10 +228,27 @@ export class MemoryCommand extends Command {
const projectPath = process.cwd();
const injector = createMemoryInjector(projectPath);
// Validate and parse limit
let maxLearnings = 10;
if (this.limit) {
const parsed = parseInt(this.limit, 10);
if (isNaN(parsed) || parsed <= 0) {
console.log(chalk.red('Invalid --limit value. Must be a positive number.'));
return 1;
}
maxLearnings = parsed;
}
// Sanitize tags - filter out empty strings
const tags = this.tags
?.split(',')
.map((t) => t.trim())
.filter((t) => t.length > 0);
const results = injector.search(query, {
includeGlobal: this.global,
tags: this.tags?.split(',').map((t) => t.trim()),
maxLearnings: this.limit ? parseInt(this.limit, 10) : 10,
tags: tags && tags.length > 0 ? tags : undefined,
maxLearnings,
minRelevance: 0,
});
@@ -570,9 +587,10 @@ export class MemoryCommand extends Command {
source: 'imported',
title: learning.title,
content: learning.content,
tags: learning.tags,
frameworks: learning.frameworks,
patterns: learning.patterns,
// Default to empty arrays if not present in imported YAML
tags: Array.isArray(learning.tags) ? learning.tags : [],
frameworks: Array.isArray(learning.frameworks) ? learning.frameworks : [],
patterns: Array.isArray(learning.patterns) ? learning.patterns : [],
});
imported++;
}
@@ -788,23 +806,41 @@ export class MemoryCommand extends Command {
.slice(0, 50);
}
/**
* Escape a YAML scalar value if it contains special characters
*/
private escapeYamlValue(value: string): string {
// Check if value needs quoting (contains special YAML characters)
if (/[:#\[\]{}|>&*!?,'"\\@`]/.test(value) || value.includes('\n')) {
// Use double quotes and escape internal quotes/backslashes
return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
}
return value;
}
/**
* Generate skill content from learning
*/
private generateSkillContent(learning: Learning, skillName: string): string {
// Escape values that might contain special YAML characters
const escapedName = this.escapeYamlValue(skillName);
const escapedDesc = this.escapeYamlValue(learning.title);
const escapedTags = learning.tags.map((t) => this.escapeYamlValue(t));
const lines: string[] = [
'---',
`name: ${skillName}`,
`description: ${learning.title}`,
`name: ${escapedName}`,
`description: ${escapedDesc}`,
`version: 1.0.0`,
`tags: [${learning.tags.join(', ')}]`,
`tags: [${escapedTags.join(', ')}]`,
`source: skillkit-memory`,
`sourceType: local`,
];
if (learning.frameworks?.length) {
const escapedFrameworks = learning.frameworks.map((f) => this.escapeYamlValue(f));
lines.push(`compatibility:`);
lines.push(` frameworks: [${learning.frameworks.join(', ')}]`);
lines.push(` frameworks: [${escapedFrameworks.join(', ')}]`);
}
lines.push('---', '', `# ${learning.title}`, '');
+50 -12
View File
@@ -195,10 +195,11 @@ export class SkillExecutionEngine {
error: overallError,
};
const statusMessages: Record<typeof overallStatus, string> = {
const statusMessages: Record<'completed' | 'failed' | 'cancelled' | 'paused', string> = {
completed: 'Skill execution completed',
paused: 'Skill execution paused',
failed: overallError || 'Skill execution failed',
cancelled: 'Skill execution cancelled',
};
this.onProgress?.({
@@ -408,23 +409,60 @@ export class SkillExecutionEngine {
}
/**
* Safely test a regex pattern with timeout protection against ReDoS
* Check if a regex pattern is potentially dangerous (ReDoS-vulnerable)
* Rejects patterns with nested quantifiers and other known problematic constructs
*/
private safeRegexTest(pattern: string, input: string, timeoutMs = 1000): boolean {
// Limit pattern and input length to prevent excessive processing
if (pattern.length > 500 || input.length > 100000) {
private isUnsafeRegexPattern(pattern: string): boolean {
// Detect nested quantifiers like (a+)+, (a*)+, (a+)*, (a*)*
// These are the primary cause of catastrophic backtracking
const nestedQuantifierPattern = /\([^)]*[+*][^)]*\)[+*?]/;
if (nestedQuantifierPattern.test(pattern)) {
return true;
}
// Detect overlapping alternations with quantifiers like (a|a)+
const overlappingAlternation = /\([^|)]*\|[^|)]*\)[+*]/;
if (overlappingAlternation.test(pattern)) {
return true;
}
// Detect excessive quantifiers (more than 3 quantifiers in a row-ish)
const quantifierCount = (pattern.match(/[+*?]|\{\d+/g) || []).length;
if (quantifierCount > 10) {
return true;
}
// Detect very long character classes which can be slow
const longCharClass = /\[[^\]]{50,}\]/;
if (longCharClass.test(pattern)) {
return true;
}
return false;
}
/**
* Safely test a regex pattern with ReDoS prevention
*
* This method validates patterns before execution to prevent catastrophic backtracking.
* Note: This cannot guarantee protection against all ReDoS patterns, but catches common ones.
*/
private safeRegexTest(pattern: string, input: string, _timeoutMs = 1000): boolean {
// Strict length limits to prevent excessive processing
if (pattern.length > 200 || input.length > 50000) {
console.warn(`Regex test skipped: pattern or input too long`);
return false;
}
// Reject patterns known to cause ReDoS
if (this.isUnsafeRegexPattern(pattern)) {
console.warn(`Regex pattern rejected as potentially unsafe: ${pattern.slice(0, 50)}...`);
return false;
}
try {
const regex = new RegExp(pattern);
// Simple patterns should complete quickly; complex ones may timeout
const startTime = Date.now();
const result = regex.test(input);
if (Date.now() - startTime > timeoutMs) {
console.warn(`Regex pattern took too long: ${pattern.slice(0, 50)}...`);
}
return result;
return regex.test(input);
} catch {
// Invalid regex pattern
return false;
+4 -4
View File
@@ -95,10 +95,10 @@ export interface TaskExecutionResult {
status: ExecutionTaskStatus;
/** Start time */
startedAt: string;
/** End time */
completedAt: string;
/** Duration in milliseconds */
durationMs: number;
/** End time (only present for completed/failed/skipped tasks) */
completedAt?: string;
/** Duration in milliseconds (only present for completed/failed/skipped tasks) */
durationMs?: number;
/** Output/result */
output?: string;
/** Error message if failed */
+37 -9
View File
@@ -192,13 +192,25 @@ export class RuleBasedCompressor implements CompressionEngine {
const errorText = error.content.error || error.content.action;
const matchingSolution = solutions.find((s) => {
const solutionContext = s.content.context?.toLowerCase() || '';
const solutionAction = s.content.action?.toLowerCase() || '';
const errorLower = errorText.toLowerCase();
// Simple matching: solution mentions similar keywords
return (
solutionContext.includes('fix') ||
solutionContext.includes('resolve') ||
this.hasSimilarKeywords(errorLower, solutionContext)
);
// Require keyword similarity between error and solution
const hasKeywordMatch = this.hasSimilarKeywords(errorLower, solutionContext) ||
this.hasSimilarKeywords(errorLower, solutionAction);
if (!hasKeywordMatch) {
return false;
}
// Solution should indicate it's a fix/resolution, or have strong keyword overlap
const isSolutionIndicator = solutionContext.includes('fix') ||
solutionContext.includes('resolve') ||
solutionContext.includes('solution') ||
solutionAction.includes('fix') ||
solutionAction.includes('resolve');
// Require both keyword match AND solution indicator
return isSolutionIndicator || this.hasStrongKeywordOverlap(errorLower, solutionContext);
});
if (matchingSolution) {
@@ -556,10 +568,25 @@ Consider automating or documenting this workflow for future reference.
}
private hasSimilarKeywords(text1: string, text2: string): boolean {
const words1 = text1.split(/\s+/).filter((w) => w.length > 3);
const words2 = new Set(text2.split(/\s+/).filter((w) => w.length > 3));
// Normalize: lowercase and strip punctuation for better matching
const normalize = (word: string) => word.toLowerCase().replace(/[^a-z0-9]/g, '');
const words1 = text1.split(/\s+/).map(normalize).filter((w) => w.length > 3);
const words2 = new Set(text2.split(/\s+/).map(normalize).filter((w) => w.length > 3));
return words1.some((w) => words2.has(w));
}
/**
* Check for strong keyword overlap (multiple matching words)
* More strict than hasSimilarKeywords - requires at least 2 matching words
*/
private hasStrongKeywordOverlap(text1: string, text2: string): boolean {
// Normalize: lowercase and strip punctuation for better matching
const normalize = (word: string) => word.toLowerCase().replace(/[^a-z0-9]/g, '');
const words1 = text1.split(/\s+/).map(normalize).filter((w) => w.length > 3);
const words2 = new Set(text2.split(/\s+/).map(normalize).filter((w) => w.length > 3));
const matchCount = words1.filter((w) => words2.has(w)).length;
return matchCount >= 2;
}
}
/**
@@ -982,7 +1009,8 @@ export class LearningConsolidator {
const other = base === learning1 ? learning2 : learning1;
return {
source: 'session',
// Preserve the source from the base learning instead of hard-coding 'session'
source: base.source,
sourceObservations: [
...(base.sourceObservations || []),
...(other.sourceObservations || []),
+3 -1
View File
@@ -160,8 +160,10 @@ export class MemoryIndexStore {
}
}
// Partial match (keyword is substring of indexed word)
// Partial match (keyword is substring of indexed word, but not exact match)
for (const [indexed, ids] of Object.entries(data.entries)) {
// Skip exact matches - they're already counted above with higher weight
if (indexed === keyword) continue;
if (indexed.includes(keyword) || keyword.includes(indexed)) {
for (const id of ids) {
matchCounts.set(id, (matchCounts.get(id) || 0) + 1);
+12 -2
View File
@@ -740,8 +740,18 @@ async function runTestCase(
};
}
// Check tag filters
if (options.tags && testCase.tags) {
// Check tag filters - if tags are specified, only run tests with matching tags
if (options.tags && options.tags.length > 0) {
// Tests without tags should be skipped when tag filter is active
if (!testCase.tags || testCase.tags.length === 0) {
return {
testCase,
passed: true,
assertions: [],
duration: 0,
skipped: true,
};
}
const hasRequiredTag = options.tags.some((t) => testCase.tags?.includes(t));
if (!hasRequiredTag) {
return {