|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { Finding } from '@engine/core'; |
| 4 | +import { stellarKB } from '../../../knowledge-base/stellar/kb'; |
| 5 | +import { Evidence, EvidenceGeneratorOptions, CodeSnippet } from './types'; |
| 6 | + |
| 7 | +export class SorobanEvidenceGenerator { |
| 8 | + private fileCache: Map<string, string[]> = new Map(); |
| 9 | + private options: Required<EvidenceGeneratorOptions>; |
| 10 | + |
| 11 | + constructor(options?: EvidenceGeneratorOptions) { |
| 12 | + this.options = { |
| 13 | + contextLines: options?.contextLines ?? 2, |
| 14 | + }; |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Extracts a code snippet from a file, adding context lines. |
| 19 | + * Uses an internal cache to prevent redundant disk reads for the same file. |
| 20 | + */ |
| 21 | + public extractCodeSnippet(filePath: string, startLine: number, endLine: number): CodeSnippet | null { |
| 22 | + if (!fs.existsSync(filePath)) { |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + let lines = this.fileCache.get(filePath); |
| 27 | + if (!lines) { |
| 28 | + try { |
| 29 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 30 | + lines = content.split('\n'); |
| 31 | + this.fileCache.set(filePath, lines); |
| 32 | + } catch (error) { |
| 33 | + // Fallback for permissions or unreadable files |
| 34 | + return null; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Convert 1-based line numbers to 0-based index |
| 39 | + const contextLines = this.options.contextLines; |
| 40 | + const extractStart = Math.max(0, startLine - 1 - contextLines); |
| 41 | + const extractEnd = Math.min(lines.length, endLine + contextLines); |
| 42 | + |
| 43 | + const extractedLines = lines.slice(extractStart, extractEnd); |
| 44 | + |
| 45 | + return { |
| 46 | + filePath, |
| 47 | + startLine: extractStart + 1, // Convert back to 1-based |
| 48 | + endLine: extractEnd, |
| 49 | + content: extractedLines.join('\n'), |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Generates supporting evidence for a single finding. |
| 55 | + */ |
| 56 | + public generateForFinding(finding: Finding): Evidence { |
| 57 | + const rule = stellarKB.getRule(finding.ruleId); |
| 58 | + |
| 59 | + let codeSnippet: CodeSnippet | null = null; |
| 60 | + if (finding.location && finding.location.file) { |
| 61 | + codeSnippet = this.extractCodeSnippet( |
| 62 | + finding.location.file, |
| 63 | + finding.location.startLine, |
| 64 | + finding.location.endLine |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + return { |
| 69 | + ruleId: finding.ruleId, |
| 70 | + severity: finding.severity, |
| 71 | + description: rule ? rule.description : finding.message, |
| 72 | + explanation: rule ? rule.explanation : 'No detailed explanation available.', |
| 73 | + codeSnippet, |
| 74 | + documentationUrl: rule?.documentationUrl, |
| 75 | + generatedAt: new Date().toISOString(), |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Generates evidence for an array of findings. |
| 81 | + */ |
| 82 | + public generateEvidence(findings: Finding[]): Evidence[] { |
| 83 | + return findings.map((finding) => this.generateForFinding(finding)); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Generates evidence and exports it as a JSON artifact. |
| 88 | + */ |
| 89 | + public generateAndExportEvidence(findings: Finding[], outputPath: string): Evidence[] { |
| 90 | + const evidence = this.generateEvidence(findings); |
| 91 | + |
| 92 | + // Ensure the output directory exists |
| 93 | + const dir = path.dirname(outputPath); |
| 94 | + if (!fs.existsSync(dir)) { |
| 95 | + fs.mkdirSync(dir, { recursive: true }); |
| 96 | + } |
| 97 | + |
| 98 | + fs.writeFileSync(outputPath, JSON.stringify(evidence, null, 2), 'utf8'); |
| 99 | + return evidence; |
| 100 | + } |
| 101 | +} |
0 commit comments