From bf0f3e19171c40af03ec4feceac876f5cf569c31 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare <48523873+rohitg00@users.noreply.github.com> Date: Sun, 8 Feb 2026 12:24:57 +0000 Subject: [PATCH] fix: address Devin and CodeRabbit review suggestions for security scanner - Fix .env variant files (.env.local, .env.production) being skipped by scanner - Add multiline rule support in StaticAnalyzer for cross-line pattern matching - Fix PI004/PI013 patterns that contained literal \n (never matched per-line) - Fix /g flag on CI006 path traversal regex causing intermittent misses - Fix frontmatter regex to handle Windows CRLF line endings - Fix allowed-tools parsing to detect multi-line YAML arrays - Fix snippet redaction order: redact before truncate to prevent secret leaks - Reset findingCounter per analyze() call for deterministic finding IDs - Make SARIF artifactLocation.uri relative to skillPath for GitHub Code Scanning - Use basename() instead of split('/').pop() for cross-platform paths - Inject package version at build time via tsup define (replaces hardcoded 1.0.0) - Validate --fail-on and --format CLI options with clear error messages - Remove unused format/quiet from ScanOptions interface - Move publish scan after name validation to avoid blocking on invalid skills - Add excludePatterns for jailbreak detection to reduce false positives - Add biome suppression for intentional control character regex in UC005 - Use alternation instead of character class for ZWJ in UC001 --- packages/cli/src/commands/publish.ts | 29 ++++++----- packages/cli/src/commands/scan.ts | 17 +++++-- .../core/src/scanner/analyzers/manifest.ts | 48 +++++++++++-------- .../core/src/scanner/analyzers/secrets.ts | 3 +- packages/core/src/scanner/analyzers/static.ts | 33 +++++++++++++ packages/core/src/scanner/reporter.ts | 14 ++++-- .../src/scanner/rules/command-injection.ts | 2 +- .../src/scanner/rules/prompt-injection.ts | 10 ++-- packages/core/src/scanner/rules/unicode.ts | 3 +- packages/core/src/scanner/scanner.ts | 2 +- packages/core/src/scanner/types.ts | 3 +- packages/core/tsup.config.ts | 6 +++ 12 files changed, 120 insertions(+), 50 deletions(-) diff --git a/packages/cli/src/commands/publish.ts b/packages/cli/src/commands/publish.ts index f97e7122..d1bf3f47 100644 --- a/packages/cli/src/commands/publish.ts +++ b/packages/cli/src/commands/publish.ts @@ -71,22 +71,7 @@ export class PublishCommand extends Command { console.log(chalk.white(`Found ${discoveredSkills.length} skill(s):\n`)); - const scanner = new SkillScanner({ failOnSeverity: Severity.HIGH }); - for (const skill of discoveredSkills) { - const scanResult = await scanner.scan(skill.path); - if (scanResult.verdict === 'fail') { - console.error(chalk.red(`\nSecurity scan FAILED for "${skill.name}"`)); - console.error(formatSummary(scanResult)); - console.error(chalk.dim('Fix security issues before publishing.')); - return 1; - } - if (scanResult.verdict === 'warn') { - console.log(chalk.yellow(` Security warnings for "${skill.name}" (${scanResult.findings.length} findings)`)); - } - } - const wellKnownSkills: WellKnownSkill[] = []; - const validSkills: Array<{ name: string; safeName: string; description?: string; path: string }> = []; for (const skill of discoveredSkills) { @@ -109,6 +94,20 @@ export class PublishCommand extends Command { }); } + const scanner = new SkillScanner({ failOnSeverity: Severity.HIGH }); + for (const skill of validSkills) { + const scanResult = await scanner.scan(skill.path); + if (scanResult.verdict === 'fail') { + console.error(chalk.red(`\nSecurity scan FAILED for "${skill.safeName}"`)); + console.error(formatSummary(scanResult)); + console.error(chalk.dim('Fix security issues before publishing.')); + return 1; + } + if (scanResult.verdict === 'warn') { + console.log(chalk.yellow(` Security warnings for "${skill.safeName}" (${scanResult.findings.length} findings)`)); + } + } + if (validSkills.length === 0) { console.error(chalk.red('\nNo valid skills to publish')); return 1; diff --git a/packages/cli/src/commands/scan.ts b/packages/cli/src/commands/scan.ts index 8209143f..75d07f8f 100644 --- a/packages/cli/src/commands/scan.ts +++ b/packages/cli/src/commands/scan.ts @@ -58,14 +58,25 @@ export class ScanCommand extends Command { return 1; } + const validFormats = ['summary', 'json', 'table', 'sarif']; + if (!validFormats.includes(this.format)) { + this.context.stderr.write(`Invalid format: "${this.format}". Must be one of: ${validFormats.join(', ')}\n`); + return 1; + } + const skipRules = this.skipRules?.split(',').map((s) => s.trim()) ?? []; - const failOnSeverity = this.failOn ? SEVERITY_MAP[this.failOn.toLowerCase()] : undefined; + let failOnSeverity: Severity | undefined; + if (this.failOn) { + failOnSeverity = SEVERITY_MAP[this.failOn.toLowerCase()]; + if (!failOnSeverity) { + this.context.stderr.write(`Invalid --fail-on value: "${this.failOn}". Must be one of: ${Object.keys(SEVERITY_MAP).join(', ')}\n`); + return 1; + } + } const scanner = new SkillScanner({ failOnSeverity, - format: this.format as 'summary' | 'json' | 'table' | 'sarif', - quiet: this.quiet, skipRules, }); diff --git a/packages/core/src/scanner/analyzers/manifest.ts b/packages/core/src/scanner/analyzers/manifest.ts index 80b40689..0aaf23c7 100644 --- a/packages/core/src/scanner/analyzers/manifest.ts +++ b/packages/core/src/scanner/analyzers/manifest.ts @@ -23,6 +23,7 @@ export class ManifestAnalyzer implements Analyzer { private findingCounter = 0; async analyze(_skillPath: string, files: string[]): Promise { + this.findingCounter = 0; const findings: Finding[] = []; const skillMdFiles = files.filter((f) => f.toLowerCase().endsWith('skill.md')); @@ -45,7 +46,7 @@ export class ManifestAnalyzer implements Analyzer { } private validateFrontmatter(content: string, filePath: string, findings: Finding[]): void { - const fmMatch = content.match(/^---\n([\s\S]*?)\n---/); + const fmMatch = content.match(/^---\r?\n([\s\S]*?)\r?\n---/); if (!fmMatch) { findings.push({ id: `MF${++this.findingCounter}`, @@ -110,7 +111,7 @@ export class ManifestAnalyzer implements Analyzer { }); } else { const desc = descMatch[1].trim().replace(/^["']|["']$/g, ''); - if (desc.length < 10) { + if (desc.length < 20) { findings.push({ id: `MF${++this.findingCounter}`, ruleId: 'MF005', @@ -125,23 +126,32 @@ export class ManifestAnalyzer implements Analyzer { } } - const toolsMatch = fm.match(/^allowed-tools:\s*\[([^\]]*)\]/m); - if (toolsMatch) { - const tools = toolsMatch[1].split(',').map((t) => t.trim().replace(/^["']|["']$/g, '')); - for (const tool of tools) { - if (DANGEROUS_TOOLS.has(tool)) { - findings.push({ - id: `MF${++this.findingCounter}`, - ruleId: 'MF006', - category: ThreatCategory.TOOL_ABUSE, - severity: Severity.HIGH, - title: `Dangerous tool in allowed-tools: ${tool}`, - description: `The tool "${tool}" grants shell access. This is a significant security risk.`, - filePath, - analyzer: this.name, - remediation: 'Restrict allowed-tools to the minimum needed. Avoid shell/exec tools.', - }); - } + const inlineToolsMatch = fm.match(/^allowed-tools:\s*\[([^\]]*)\]/m); + const multiLineToolsMatch = fm.match(/^allowed-tools:\s*\n((?:\s+-\s+.+\n?)+)/m); + + let tools: string[] = []; + if (inlineToolsMatch) { + tools = inlineToolsMatch[1].split(',').map((t) => t.trim().replace(/^["']|["']$/g, '')); + } else if (multiLineToolsMatch) { + tools = multiLineToolsMatch[1] + .split('\n') + .map((line) => line.replace(/^\s*-\s*/, '').trim().replace(/^["']|["']$/g, '')) + .filter((t) => t.length > 0); + } + + for (const tool of tools) { + if (DANGEROUS_TOOLS.has(tool)) { + findings.push({ + id: `MF${++this.findingCounter}`, + ruleId: 'MF006', + category: ThreatCategory.TOOL_ABUSE, + severity: Severity.HIGH, + title: `Dangerous tool in allowed-tools: ${tool}`, + description: `The tool "${tool}" grants shell access. This is a significant security risk.`, + filePath, + analyzer: this.name, + remediation: 'Restrict allowed-tools to the minimum needed. Avoid shell/exec tools.', + }); } } } diff --git a/packages/core/src/scanner/analyzers/secrets.ts b/packages/core/src/scanner/analyzers/secrets.ts index 2e3ceb08..f8608f6a 100644 --- a/packages/core/src/scanner/analyzers/secrets.ts +++ b/packages/core/src/scanner/analyzers/secrets.ts @@ -116,6 +116,7 @@ export class SecretsAnalyzer implements Analyzer { private findingCounter = 0; async analyze(_skillPath: string, files: string[]): Promise { + this.findingCounter = 0; const findings: Finding[] = []; for (const file of files) { @@ -166,7 +167,7 @@ export class SecretsAnalyzer implements Analyzer { description: `Potential ${secret.name} found in skill file`, filePath: file, lineNumber: i + 1, - snippet: line.trim().substring(0, 100).replace(secret.pattern, '[REDACTED]'), + snippet: line.trim().replace(secret.pattern, '[REDACTED]').substring(0, 100), analyzer: this.name, remediation: 'Remove hardcoded secrets. Use environment variables or secret managers.', }); diff --git a/packages/core/src/scanner/analyzers/static.ts b/packages/core/src/scanner/analyzers/static.ts index 088ece62..2802910f 100644 --- a/packages/core/src/scanner/analyzers/static.ts +++ b/packages/core/src/scanner/analyzers/static.ts @@ -75,6 +75,7 @@ export class StaticAnalyzer implements Analyzer { } async analyze(_skillPath: string, files: string[]): Promise { + this.findingCounter = 0; const findings: Finding[] = []; for (const file of files) { @@ -97,6 +98,38 @@ export class StaticAnalyzer implements Analyzer { const lines = content.split('\n'); for (const rule of applicableRules) { + if (rule.multiline) { + for (const pattern of rule.patterns) { + pattern.lastIndex = 0; + let match: RegExpExecArray | null; + while ((match = pattern.exec(content)) !== null) { + const lineNumber = content.substring(0, match.index).split('\n').length; + const snippetStart = Math.max(0, match.index - 40); + const snippet = content.substring(snippetStart, match.index + match[0].length + 40).trim().substring(0, 200); + + if (isPlaceholderLine(snippet)) continue; + if (matchesExcludePatterns(snippet, rule)) continue; + + findings.push({ + id: `F${++this.findingCounter}`, + ruleId: rule.id, + category: rule.category, + severity: rule.severity, + title: rule.description, + description: rule.description, + filePath: file, + lineNumber, + snippet, + remediation: rule.remediation, + analyzer: this.name, + }); + + if (!pattern.global) break; + } + } + continue; + } + for (let i = 0; i < lines.length; i++) { const line = lines[i]; diff --git a/packages/core/src/scanner/reporter.ts b/packages/core/src/scanner/reporter.ts index 559d091f..60073bf5 100644 --- a/packages/core/src/scanner/reporter.ts +++ b/packages/core/src/scanner/reporter.ts @@ -1,6 +1,10 @@ +import { basename } from 'node:path'; import type { Finding, ScanResult } from './types.js'; import { Severity } from './types.js'; +declare const __PACKAGE_VERSION__: string; +const SCANNER_VERSION = typeof __PACKAGE_VERSION__ !== 'undefined' ? __PACKAGE_VERSION__ : '0.0.0'; + const SEVERITY_COLORS: Record = { [Severity.CRITICAL]: '\x1b[91m', [Severity.HIGH]: '\x1b[31m', @@ -86,7 +90,7 @@ export function formatTable(result: ScanResult): string { f.severity.toUpperCase().padEnd(widths[0]), f.ruleId.padEnd(widths[1]), f.title.substring(0, widths[2]).padEnd(widths[2]), - (f.filePath?.split('/').pop() ?? '').substring(0, widths[3]).padEnd(widths[3]), + (f.filePath ? basename(f.filePath) : '').substring(0, widths[3]).padEnd(widths[3]), String(f.lineNumber ?? '').padEnd(widths[4]), ]; lines.push(row.join(' | ')); @@ -120,7 +124,7 @@ export function formatSarif(result: ScanResult): string { tool: { driver: { name: 'skillkit-scanner', - version: '1.0.0', + version: SCANNER_VERSION, informationUri: 'https://agenstskills.com', rules: [...rules.values()].map((r) => ({ id: r.ruleId, @@ -145,7 +149,11 @@ export function formatSarif(result: ScanResult): string { ? [ { physicalLocation: { - artifactLocation: { uri: f.filePath }, + artifactLocation: { + uri: f.filePath.startsWith(result.skillPath) + ? f.filePath.slice(result.skillPath.length).replace(/^\//, '') + : f.filePath, + }, region: f.lineNumber ? { startLine: f.lineNumber } : undefined, diff --git a/packages/core/src/scanner/rules/command-injection.ts b/packages/core/src/scanner/rules/command-injection.ts index 0a97ddda..fb0a73bb 100644 --- a/packages/core/src/scanner/rules/command-injection.ts +++ b/packages/core/src/scanner/rules/command-injection.ts @@ -61,7 +61,7 @@ export const commandInjectionRules: SecurityRule[] = [ id: 'CI006', category: ThreatCategory.COMMAND_INJECTION, severity: Severity.HIGH, - patterns: [/\.\.\//g, /\.\.\\/g], + patterns: [/\.\.\//, /\.\.\\/], excludePatterns: [/import\s+.*from\s+['"]\.\.\//, /require\s*\(\s*['"]\.\.\//, /\/\/.*\.\.\//, /#.*\.\.\//], description: 'Path traversal: relative path escaping directory boundaries', remediation: 'Use path.resolve() and validate paths are within expected directories.', diff --git a/packages/core/src/scanner/rules/prompt-injection.ts b/packages/core/src/scanner/rules/prompt-injection.ts index c594089f..5e5f541c 100644 --- a/packages/core/src/scanner/rules/prompt-injection.ts +++ b/packages/core/src/scanner/rules/prompt-injection.ts @@ -29,7 +29,7 @@ export const promptInjectionRules: SecurityRule[] = [ id: 'PI004', category: ThreatCategory.PROMPT_INJECTION, severity: Severity.CRITICAL, - patterns: [/new\s+instructions?:\s*\n/i, /system\s+prompt:\s*\n/i], + patterns: [/new\s+instructions?:\s*$/im, /system\s+prompt:\s*$/im], description: 'System prompt injection: attempts to inject new system-level instructions', remediation: 'Do not include system prompt markers in skill content.', }, @@ -75,7 +75,7 @@ export const promptInjectionRules: SecurityRule[] = [ severity: Severity.CRITICAL, patterns: [ /unrestricted\s+mode/i, - /jailbreak/i, + /(?/i, + //i, ], description: 'Hidden HTML comments with suspicious instruction content', remediation: 'Do not hide instructions in HTML comments.', diff --git a/packages/core/src/scanner/rules/unicode.ts b/packages/core/src/scanner/rules/unicode.ts index 13c06e39..03fdf21d 100644 --- a/packages/core/src/scanner/rules/unicode.ts +++ b/packages/core/src/scanner/rules/unicode.ts @@ -5,7 +5,7 @@ export const unicodeRules: SecurityRule[] = [ id: 'UC001', category: ThreatCategory.UNICODE_STEGANOGRAPHY, severity: Severity.MEDIUM, - patterns: [/[\u200B\u200C\u200D\uFEFF]/], + patterns: [/\u200B|\u200C|\u200D|\uFEFF/], description: 'Zero-width characters detected: may hide invisible instructions', remediation: 'Remove zero-width characters (U+200B, U+200C, U+200D, U+FEFF).', }, @@ -37,6 +37,7 @@ export const unicodeRules: SecurityRule[] = [ id: 'UC005', category: ThreatCategory.UNICODE_STEGANOGRAPHY, severity: Severity.MEDIUM, + // biome-ignore lint/suspicious/noControlCharactersInRegex: intentional detection of control characters patterns: [/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/], description: 'Control characters detected: non-printable characters in content', remediation: 'Remove control characters. Use only printable Unicode content.', diff --git a/packages/core/src/scanner/scanner.ts b/packages/core/src/scanner/scanner.ts index c13e7317..545c6670 100644 --- a/packages/core/src/scanner/scanner.ts +++ b/packages/core/src/scanner/scanner.ts @@ -29,7 +29,7 @@ async function discoverFiles(dirPath: string): Promise { for (const entry of entries) { const fullPath = join(dir, entry.name); - if (entry.name.startsWith('.') && entry.name !== '.env') continue; + if (entry.name.startsWith('.') && entry.name !== '.env' && !entry.name.startsWith('.env.')) continue; if (entry.name === 'node_modules') continue; if (entry.isDirectory()) { diff --git a/packages/core/src/scanner/types.ts b/packages/core/src/scanner/types.ts index ffd6c871..ffb6e199 100644 --- a/packages/core/src/scanner/types.ts +++ b/packages/core/src/scanner/types.ts @@ -27,6 +27,7 @@ export interface SecurityRule { patterns: RegExp[]; excludePatterns?: RegExp[]; fileTypes?: string[]; + multiline?: boolean; description: string; remediation: string; } @@ -64,7 +65,5 @@ export interface ScanResult { export interface ScanOptions { failOnSeverity?: Severity; - format?: 'summary' | 'json' | 'table' | 'sarif'; - quiet?: boolean; skipRules?: string[]; } diff --git a/packages/core/tsup.config.ts b/packages/core/tsup.config.ts index dbcb9a72..b7d78e73 100644 --- a/packages/core/tsup.config.ts +++ b/packages/core/tsup.config.ts @@ -1,5 +1,8 @@ +import { readFileSync } from 'node:fs'; import { defineConfig } from 'tsup'; +const pkg = JSON.parse(readFileSync('./package.json', 'utf-8')); + export default defineConfig({ entry: ['src/index.ts'], format: ['esm'], @@ -11,4 +14,7 @@ export default defineConfig({ splitting: false, skipNodeModulesBundle: true, platform: 'node', + define: { + '__PACKAGE_VERSION__': JSON.stringify(pkg.version), + }, });