# All files / src/lib/quality-validator/analyzers securityScanner.js

0% Statements 0/297
0% Branches 0/1
0% Functions 0/1
0% Lines 0/297

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298

     /**
     * Security Scanner
     * Scans for vulnerabilities and security anti-patterns
     */
    import { execSync } from 'child_process';
    import { readFile, normalizeFilePath } from '../utils/fileSystem.js';
    import { logger } from '../utils/logger.js';
    /**
     * Security Scanner
     */
    export class SecurityScanner {
        /**
         * Scan for security issues
         */
        async analyze(filePaths) {
            const startTime = performance.now();
            try {
                logger.debug('Starting security analysis...');
                const vulnerabilities = this.scanVulnerabilities();
                const codePatterns = this.detectSecurityPatterns(filePaths);
                const performanceIssues = this.checkPerformanceIssues(filePaths);
                const metrics = {
                    vulnerabilities,
                    codePatterns,
                    performanceIssues,
                };
                const findings = this.generateFindings(metrics);
                const score = this.calculateScore(metrics);
                const executionTime = performance.now() - startTime;
                logger.debug(`Security analysis complete (${executionTime.toFixed(2)}ms)`, {
                    vulnerabilities: vulnerabilities.length,
                    patterns: codePatterns.length,
                });
                return {
                    category: 'security',
                    score,
                    status: (score >= 80 ? 'pass' : score >= 60 ? 'warning' : 'fail'),
                    findings,
                    metrics: metrics,
                    executionTime,
                };
            }
            catch (error) {
                logger.error('Security analysis failed', { error: error.message });
                throw error;
            }
        }
        /**
         * Scan for vulnerabilities using npm audit
         */
        scanVulnerabilities() {
            const vulnerabilities = [];
            try {
                const output = execSync('npm audit --json', {
                    encoding: 'utf-8',
                    timeout: 30000,
                    stdio: ['pipe', 'pipe', 'pipe'],
                });
                const data = JSON.parse(output);
                if (data.vulnerabilities) {
                    for (const [pkgName, vulnData] of Object.entries(data.vulnerabilities)) {
                        const vuln = vulnData;
                        vulnerabilities.push({
                            package: pkgName,
                            currentVersion: vuln.installed || 'unknown',
                            vulnerabilityType: vuln.type || 'unknown',
                            severity: vuln.severity || 'medium',
                            description: vuln.via?.[0]?.title || vuln.description || 'No description',
                            fixedInVersion: vuln.via?.[0]?.fixed || 'No fix available',
                        });
                    }
                }
            }
            catch (error) {
                logger.warn('npm audit scan failed', {
                    error: error.message,
                });
            }
            return vulnerabilities;
        }
        /**
         * Detect security anti-patterns in code
         */
        detectSecurityPatterns(filePaths) {
            const patterns = [];
            for (const filePath of filePaths) {
                if (!filePath.endsWith('.ts') && !filePath.endsWith('.tsx'))
                    continue;
                try {
                    const content = readFile(filePath);
                    const lines = content.split('\n');
                    for (let i = 0; i < lines.length; i++) {
                        const line = lines[i];
                        const lineNum = i + 1;
                        // Check for hard-coded secrets
                        if (this.isHardcodedSecret(line)) {
                            patterns.push({
                                type: 'secret',
                                severity: 'critical',
                                file: normalizeFilePath(filePath),
                                line: lineNum,
                                column: line.indexOf(line.match(/password|secret|token|apiKey|API_KEY/i)[0]),
                                message: 'Possible hard-coded secret detected',
                                remediation: 'Use environment variables or secure configuration management for sensitive data',
                                evidence: line.substring(0, 50) + '...',
                            });
                        }
                        // Check for dangerous patterns
                        if (line.includes('dangerouslySetInnerHTML')) {
                            patterns.push({
                                type: 'unsafeDom',
                                severity: 'high',
                                file: normalizeFilePath(filePath),
                                line: lineNum,
                                message: 'dangerouslySetInnerHTML used',
                                remediation: 'Use safe HTML rendering methods or sanitize HTML content with DOMPurify',
                                evidence: 'dangerouslySetInnerHTML',
                            });
                        }
                        if (line.includes('eval(')) {
                            patterns.push({
                                type: 'unsafeDom',
                                severity: 'critical',
                                file: normalizeFilePath(filePath),
                                line: lineNum,
                                message: 'eval() usage detected',
                                remediation: 'Never use eval(). Use alternative approaches like JSON.parse() or Function constructor with caution',
                                evidence: 'eval(',
                            });
                        }
                        if (line.includes('innerHTML =')) {
                            patterns.push({
                                type: 'unsafeDom',
                                severity: 'high',
                                file: normalizeFilePath(filePath),
                                line: lineNum,
                                message: 'Direct innerHTML assignment',
                                remediation: 'Use textContent for text or createElement/appendChild for safe DOM manipulation',
                                evidence: 'innerHTML =',
                            });
                        }
                        // Check for XSS risks
                        if ((line.includes('innerHTML') || line.includes('dangerouslySetInnerHTML')) &&
                            (line.includes('user') || line.includes('input') || line.includes('data'))) {
                            patterns.push({
                                type: 'xss',
                                severity: 'high',
                                file: normalizeFilePath(filePath),
                                line: lineNum,
                                message: 'Potential XSS vulnerability: unescaped user input in HTML',
                                remediation: 'Escape HTML entities or use a library like DOMPurify',
                                evidence: line.substring(0, 60) + '...',
                            });
                        }
                    }
                }
                catch (error) {
                    logger.debug(`Failed to scan security patterns in ${filePath}`);
                }
            }
            return patterns.slice(0, 20);
        }
        /**
         * Check if a line contains hard-coded secrets
         */
        isHardcodedSecret(line) {
            const secretPatterns = [
                /password\s*[:=]\s*['"]/i,
                /secret\s*[:=]\s*['"]/i,
                /token\s*[:=]\s*['"]/i,
                /apiKey\s*[:=]\s*['"]/i,
                /api_key\s*[:=]\s*['"]/i,
                /authorization\s*[:=]\s*['"]/i,
                /auth\s*[:=]\s*['"]/i,
            ];
            for (const pattern of secretPatterns) {
                if (pattern.test(line)) {
                    return true;
                }
            }
            return false;
        }
        /**
         * Check for performance issues
         */
        checkPerformanceIssues(filePaths) {
            const issues = [];
            for (const filePath of filePaths) {
                if (!filePath.endsWith('.tsx') && !filePath.endsWith('.ts'))
                    continue;
                try {
                    const content = readFile(filePath);
                    const lines = content.split('\n');
                    for (let i = 0; i < lines.length; i++) {
                        const line = lines[i];
                        const lineNum = i + 1;
                        // Check for inline functions in JSX
                        if (line.includes('onClick={') && line.includes('=>')) {
                            issues.push({
                                type: 'inlineFunction',
                                severity: 'medium',
                                file: normalizeFilePath(filePath),
                                line: lineNum,
                                message: 'Inline function definition in JSX',
                                suggestion: 'Define function outside JSX or use useCallback to prevent unnecessary re-renders',
                                estimatedImpact: 'Performance degradation in large lists',
                            });
                        }
                        // Check for missing keys in lists
                        if (line.includes('.map(') &&
                            !line.includes('key=') &&
                            i + 1 < lines.length &&
                            (lines[i + 1].includes('key=') === false)) {
                            issues.push({
                                type: 'missingKey',
                                severity: 'high',
                                file: normalizeFilePath(filePath),
                                line: lineNum,
                                message: 'List items missing key prop',
                                suggestion: 'Add unique key prop to each list item',
                                estimatedImpact: 'Rendering issues and performance problems',
                            });
                        }
                        // Check for inline objects/arrays in props
                        if (line.includes('={{') || line.includes('={[')) {
                            issues.push({
                                type: 'inlineObject',
                                severity: 'medium',
                                file: normalizeFilePath(filePath),
                                line: lineNum,
                                message: 'Inline object/array literal in JSX props',
                                suggestion: 'Move to state or memoize with useMemo',
                                estimatedImpact: 'Unnecessary re-renders of child components',
                            });
                        }
                    }
                }
                catch (error) {
                    logger.debug(`Failed to check performance issues in ${filePath}`);
                }
            }
            return issues.slice(0, 20);
        }
        /**
         * Generate findings from metrics
         */
        generateFindings(metrics) {
            const findings = [];
            // Vulnerability findings
            for (const vuln of metrics.vulnerabilities.slice(0, 5)) {
                findings.push({
                    id: `vuln-${vuln.package}`,
                    severity: vuln.severity === 'critical' ? 'critical' : 'high',
                    category: 'security',
                    title: `Vulnerability in ${vuln.package}`,
                    description: vuln.description,
                    remediation: `Update ${vuln.package} to version ${vuln.fixedInVersion}`,
                    evidence: `${vuln.severity} severity in ${vuln.vulnerabilityType}`,
                });
            }
            // Code pattern findings
            for (const pattern of metrics.codePatterns.slice(0, 5)) {
                findings.push({
                    id: `pattern-${pattern.file}-${pattern.line}`,
                    severity: pattern.severity,
                    category: 'security',
                    title: pattern.message,
                    description: `${pattern.type} vulnerability detected`,
                    location: {
                        file: pattern.file,
                        line: pattern.line,
                    },
                    remediation: pattern.remediation,
                    evidence: pattern.evidence,
                });
            }
            return findings;
        }
        /**
         * Calculate security score
         */
        calculateScore(metrics) {
            let score = 100;
            // Vulnerabilities
            const criticalVulns = metrics.vulnerabilities.filter((v) => v.severity === 'critical').length;
            const highVulns = metrics.vulnerabilities.filter((v) => v.severity === 'high').length;
            score -= criticalVulns * 25 + highVulns * 10;
            // Code patterns
            const criticalPatterns = metrics.codePatterns.filter((p) => p.severity === 'critical').length;
            const highPatterns = metrics.codePatterns.filter((p) => p.severity === 'high').length;
            score -= criticalPatterns * 15 + highPatterns * 5;
            // Performance issues
            score -= Math.min(metrics.performanceIssues.length * 2, 20);
            return Math.max(0, score);
        }
    }
    export const securityScanner = new SecurityScanner();