mirror of
https://github.com/rohitg00/skillkit.git
synced 2026-11-18 18:04:26 +00:00
fix: scanner false positives on markdown code blocks and frontmatter
Strip fenced code blocks and standard YAML frontmatter before scanning markdown files. This eliminates three categories of false positives: - PI013: no longer flags standard --- frontmatter delimiters - CI007: no longer flags curl|sh in code examples - DE003: no longer flags process.env in code examples Real threats in prose (outside code blocks) are still caught.
This commit is contained in:
@@ -58,6 +58,38 @@ function ruleMatchesFileType(rule: SecurityRule, fileType: string | undefined):
|
||||
return rule.fileTypes.includes(fileType);
|
||||
}
|
||||
|
||||
function stripMarkdownCodeAndFrontmatter(content: string): string {
|
||||
const lines = content.split('\n');
|
||||
let inCodeBlock = false;
|
||||
let inFrontmatter = false;
|
||||
let frontmatterDone = false;
|
||||
|
||||
return lines.map((line, i) => {
|
||||
const trimmed = line.trim();
|
||||
|
||||
if (!frontmatterDone && /^---\s*$/.test(trimmed)) {
|
||||
if (!inFrontmatter && i < 2) {
|
||||
inFrontmatter = true;
|
||||
return '';
|
||||
}
|
||||
if (inFrontmatter) {
|
||||
inFrontmatter = false;
|
||||
frontmatterDone = true;
|
||||
return '';
|
||||
}
|
||||
}
|
||||
if (inFrontmatter) return '';
|
||||
|
||||
if (/^(`{3,}|~{3,})/.test(trimmed)) {
|
||||
inCodeBlock = !inCodeBlock;
|
||||
return '';
|
||||
}
|
||||
if (inCodeBlock) return '';
|
||||
|
||||
return line;
|
||||
}).join('\n');
|
||||
}
|
||||
|
||||
function matchesExcludePatterns(line: string, rule: SecurityRule): boolean {
|
||||
if (!rule.excludePatterns) return false;
|
||||
return rule.excludePatterns.some((p) => p.test(line));
|
||||
@@ -95,6 +127,10 @@ export class StaticAnalyzer implements Analyzer {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fileType === 'markdown') {
|
||||
content = stripMarkdownCodeAndFrontmatter(content);
|
||||
}
|
||||
|
||||
const lines = content.split('\n');
|
||||
|
||||
for (const rule of applicableRules) {
|
||||
|
||||
Reference in New Issue
Block a user