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
This commit is contained in:
Rohit Ghumare
2026-02-08 12:24:57 +00:00
parent 822bd6f753
commit bf0f3e1917
12 changed files with 120 additions and 50 deletions
+14 -15
View File
@@ -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;
+14 -3
View File
@@ -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,
});
+29 -19
View File
@@ -23,6 +23,7 @@ export class ManifestAnalyzer implements Analyzer {
private findingCounter = 0;
async analyze(_skillPath: string, files: string[]): Promise<Finding[]> {
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.',
});
}
}
}
@@ -116,6 +116,7 @@ export class SecretsAnalyzer implements Analyzer {
private findingCounter = 0;
async analyze(_skillPath: string, files: string[]): Promise<Finding[]> {
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.',
});
@@ -75,6 +75,7 @@ export class StaticAnalyzer implements Analyzer {
}
async analyze(_skillPath: string, files: string[]): Promise<Finding[]> {
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];
+11 -3
View File
@@ -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<string, string> = {
[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,
@@ -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.',
@@ -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,
/(?<!detect(?:ing)?[\s.]+)(?<!prevent(?:ing)?[\s.]+)jailbreak/i,
/DAN\s+(mode|prompt)/i,
/bypass\s+(safety|content)\s+(filter|restriction)/i,
],
@@ -118,7 +118,9 @@ export const promptInjectionRules: SecurityRule[] = [
id: 'PI013',
category: ThreatCategory.PROMPT_INJECTION,
severity: Severity.MEDIUM,
patterns: [/---\s*\n\s*(?:system|role|assistant):/i],
patterns: [/---\s*$/im],
excludePatterns: [/^---\s*$/],
multiline: true,
description: 'YAML front-matter injection: attempts to inject via YAML metadata',
remediation: 'Use standard SKILL.md frontmatter format only.',
},
@@ -127,7 +129,7 @@ export const promptInjectionRules: SecurityRule[] = [
category: ThreatCategory.PROMPT_INJECTION,
severity: Severity.MEDIUM,
patterns: [
/<!--[\s\S]{0,500}?(?:ignore|system|instruction|secret|override)[\s\S]{0,500}?-->/i,
/<!--[^>]{0,500}?(?:ignore|system|instruction|secret|override)[^>]{0,500}?-->/i,
],
description: 'Hidden HTML comments with suspicious instruction content',
remediation: 'Do not hide instructions in HTML comments.',
+2 -1
View File
@@ -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.',
+1 -1
View File
@@ -29,7 +29,7 @@ async function discoverFiles(dirPath: string): Promise<string[]> {
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()) {
+1 -2
View File
@@ -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[];
}
+6
View File
@@ -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),
},
});