UNPKG

pury

Version:

🛡️ AI-powered security scanner with advanced threat detection, dual reporting system (detailed & summary), and comprehensive code analysis

104 lines 3.56 kB
import { promises as fs } from 'fs'; export class SarifReporter { async output(report) { const sarif = this.generateSARIF(report); console.log(JSON.stringify(sarif, null, 2)); } async writeToFile(report, filePath) { const sarif = this.generateSARIF(report); await fs.writeFile(filePath, JSON.stringify(sarif, null, 2), 'utf8'); } generateSARIF(report) { return { $schema: 'https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0.json', version: '2.1.0', runs: [ { tool: { driver: { name: 'PuryAI', version: report.metadata.version, informationUri: 'https://github.com/puryai/puryai', rules: this.generateRules(report.findings) } }, artifacts: this.generateArtifacts(report), results: this.generateResults(report.findings) } ] }; } generateRules(findings) { const uniqueRules = new Map(); for (const finding of findings) { if (!uniqueRules.has(finding.type)) { uniqueRules.set(finding.type, { id: finding.type, name: finding.type.replace('_', ' ').replace(/\b\w/g, l => l.toUpperCase()), shortDescription: { text: `${finding.type} detection` }, fullDescription: { text: `Detection of ${finding.type.replace('_', ' ')} issues in source code` }, defaultConfiguration: { level: this.mapSeverityToLevel(finding.severity) } }); } } return Array.from(uniqueRules.values()); } generateArtifacts(report) { const artifacts = new Set(); for (const finding of report.findings) { artifacts.add(finding.file); } return Array.from(artifacts).map(file => ({ location: { uri: file } })); } generateResults(findings) { return findings.map(finding => ({ ruleId: finding.type, message: { text: finding.description }, level: this.mapSeverityToLevel(finding.severity), locations: [ { physicalLocation: { artifactLocation: { uri: finding.file }, region: finding.line ? { startLine: finding.line, startColumn: finding.column || 1 } : undefined } } ], fingerprints: { primaryLocationLineHash: finding.id } })); } mapSeverityToLevel(severity) { switch (severity) { case 'critical': case 'high': return 'error'; case 'medium': return 'warning'; case 'low': return 'note'; default: return 'info'; } } } //# sourceMappingURL=sarif.js.map