1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
| class SecurityScanner { private snykEnabled: boolean; private virusTotalEnabled: boolean; constructor(config: SecurityScannerConfig) { this.snykEnabled = !!config.snykApiKey; this.virusTotalEnabled = !!config.virusTotalApiKey; } async scan(plugin: Plugin): Promise<SecurityScanResult> { const result: SecurityScanResult = { plugin: plugin.name, version: plugin.version, threats: 0, warnings: 0, passed: true, details: [], }; const staticAnalysis = await this.staticAnalysis(plugin); result.details.push(...staticAnalysis); if (this.snykEnabled) { const snykResult = await this.scanWithSnyk(plugin); result.details.push(...snykResult); result.threats += snykResult.filter(r => r.severity === 'critical').length; } if (this.virusTotalEnabled) { const vtResult = await this.scanWithVirusTotal(plugin); result.details.push(...vtResult); result.threats += vtResult.filter(r => r.positive > 5).length; } const permissionCheck = this.checkPermissions(plugin); result.details.push(...permissionCheck); if (!plugin.signature) { result.details.push({ type: 'warning', severity: 'medium', message: 'Plugin is not signed', }); result.warnings++; } result.passed = result.threats === 0; return result; } private async staticAnalysis(plugin: Plugin): Promise<ScanDetail[]> { const details: ScanDetail[] = []; const dangerousPatterns = [ { pattern: /eval\s*\(/, severity: 'high', message: 'Uses eval()' }, { pattern: /child_process\.exec/, severity: 'medium', message: 'Executes shell commands' }, { pattern: /fs\.readFileSync.*\/etc/, severity: 'critical', message: 'Reads system files' }, { pattern: /process\.env\./, severity: 'low', message: 'Accesses environment variables' }, ]; const sourceFiles = await this.getSourceFiles(plugin); for (const file of sourceFiles) { const content = await fs.promises.readFile(file, 'utf-8'); for (const { pattern, severity, message } of dangerousPatterns) { if (pattern.test(content)) { details.push({ type: 'warning', severity, message: `${message} in ${path.basename(file)}`, file: path.basename(file), }); } } } return details; } private async scanWithSnyk(plugin: Plugin): Promise<ScanDetail[]> { const response = await fetch('https://snyk.io/api/v1/test', { method: 'POST', headers: { 'Authorization': `token ${this.snykApiKey}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ target: { files: [{ path: 'package.json' }], }, }), }); const result = await response.json(); return result.issues?.map((issue: any) => ({ type: 'threat', severity: issue.severity, message: `Vulnerability: ${issue.title}`, cve: issue.identifiers?.CVE?.[0], })) || []; } private async scanWithVirusTotal(plugin: Plugin): Promise<ScanDetail[]> { const hash = await this.calculateHash(plugin); const response = await fetch(`https://www.virustotal.com/api/v3/files/${hash}`, { headers: { 'x-apikey': this.virusTotalApiKey, }, }); const result = await response.json(); if (result.data?.attributes?.last_analysis_stats) { const stats = result.data.attributes.last_analysis_stats; if (stats.malicious > 0) { return [{ type: 'threat', severity: 'critical', message: `Detected by ${stats.malicious} antivirus engines`, positive: stats.malicious, total: stats.malicious + stats.harmless, }]; } } return []; } private checkPermissions(plugin: Plugin): ScanDetail[] { const details: ScanDetail[] = []; if (plugin.permissions?.network && !plugin.homepage) { details.push({ type: 'warning', severity: 'medium', message: 'Plugin requests network access but has no homepage', }); } if (plugin.permissions?.files?.includes('*')) { details.push({ type: 'warning', severity: 'high', message: 'Plugin requests access to all files', }); } return details; } }
interface SecurityScanResult { plugin: string; version: string; threats: number; warnings: number; passed: boolean; details: ScanDetail[]; }
interface ScanDetail { type: 'threat' | 'warning' | 'info'; severity: 'critical' | 'high' | 'medium' | 'low'; message: string; file?: string; cve?: string; positive?: number; total?: number; }
|