|
| 1 | +/** |
| 2 | + * Behavioural tests for SarifReportOutputProvider. |
| 3 | + * |
| 4 | + * Covers the contract that consumers (GitHub Code Scanning, GitLab, |
| 5 | + * Defect Dojo) actually depend on: |
| 6 | + * - SARIF 2.1.0 envelope with the expected top-level fields |
| 7 | + * - Tool driver advertises the five built-in vulnerability rules |
| 8 | + * - One result per non-empty evidence file, ruleId matching the rule |
| 9 | + * - Empty / missing evidence files do not produce results |
| 10 | + * - Result messages are truncated rather than dropping out at limit |
| 11 | + */ |
| 12 | + |
| 13 | +import fs from 'node:fs/promises'; |
| 14 | +import os from 'node:os'; |
| 15 | +import path from 'node:path'; |
| 16 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 17 | +import { SarifReportOutputProvider } from '../services/sarif-output-provider.js'; |
| 18 | +import type { ActivityLogger } from '../types/activity-logger.js'; |
| 19 | + |
| 20 | +const noopLogger: ActivityLogger = { |
| 21 | + info: () => undefined, |
| 22 | + warn: () => undefined, |
| 23 | + error: () => undefined, |
| 24 | +}; |
| 25 | + |
| 26 | +async function setupRepoWithDeliverables( |
| 27 | + evidence: Record<string, string>, |
| 28 | +): Promise<{ repoPath: string; cleanup: () => Promise<void> }> { |
| 29 | + const repoPath = await fs.mkdtemp(path.join(os.tmpdir(), 'vedha-sarif-test-')); |
| 30 | + const deliverablesPath = path.join(repoPath, '.shannon', 'deliverables'); |
| 31 | + await fs.mkdir(deliverablesPath, { recursive: true }); |
| 32 | + for (const [name, body] of Object.entries(evidence)) { |
| 33 | + await fs.writeFile(path.join(deliverablesPath, name), body, 'utf8'); |
| 34 | + } |
| 35 | + return { |
| 36 | + repoPath, |
| 37 | + cleanup: () => fs.rm(repoPath, { recursive: true, force: true }), |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +function makeInput(repoPath: string): { repoPath: string } { |
| 42 | + return { repoPath }; |
| 43 | +} |
| 44 | + |
| 45 | +describe('SarifReportOutputProvider', () => { |
| 46 | + let cleanup: (() => Promise<void>) | null = null; |
| 47 | + |
| 48 | + afterEach(async () => { |
| 49 | + if (cleanup) { |
| 50 | + await cleanup(); |
| 51 | + cleanup = null; |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + it('emits a valid SARIF 2.1.0 envelope when at least one finding exists', async () => { |
| 56 | + const setup = await setupRepoWithDeliverables({ |
| 57 | + 'injection_exploitation_evidence.md': '## SQL injection in /api/users\n\nProof: `' + "1' OR '1'='1" + '`', |
| 58 | + }); |
| 59 | + cleanup = setup.cleanup; |
| 60 | + |
| 61 | + const provider = new SarifReportOutputProvider('1.1.0'); |
| 62 | + const result = await provider.generate(makeInput(setup.repoPath), noopLogger); |
| 63 | + |
| 64 | + expect(result.outputPath).toBeDefined(); |
| 65 | + const sarif = JSON.parse(await fs.readFile(result.outputPath as string, 'utf8')); |
| 66 | + expect(sarif.version).toBe('2.1.0'); |
| 67 | + expect(sarif.$schema).toMatch(/sarif-schema-2\.1\.0/); |
| 68 | + expect(sarif.runs).toHaveLength(1); |
| 69 | + expect(sarif.runs[0].tool.driver.name).toBe('Vedha'); |
| 70 | + expect(sarif.runs[0].tool.driver.version).toBe('1.1.0'); |
| 71 | + expect(sarif.runs[0].tool.driver.rules).toHaveLength(5); |
| 72 | + expect(sarif.runs[0].results).toHaveLength(1); |
| 73 | + expect(sarif.runs[0].results[0].ruleId).toBe('vedha.injection'); |
| 74 | + expect(sarif.runs[0].results[0].message.text).toMatch(/SQL injection/); |
| 75 | + }); |
| 76 | + |
| 77 | + it('emits one result per non-empty evidence file', async () => { |
| 78 | + const setup = await setupRepoWithDeliverables({ |
| 79 | + 'injection_exploitation_evidence.md': 'finding', |
| 80 | + 'xss_exploitation_evidence.md': 'finding', |
| 81 | + 'authz_exploitation_evidence.md': 'finding', |
| 82 | + }); |
| 83 | + cleanup = setup.cleanup; |
| 84 | + |
| 85 | + const provider = new SarifReportOutputProvider(); |
| 86 | + const result = await provider.generate(makeInput(setup.repoPath), noopLogger); |
| 87 | + const sarif = JSON.parse(await fs.readFile(result.outputPath as string, 'utf8')); |
| 88 | + |
| 89 | + expect(sarif.runs[0].results.map((r: { ruleId: string }) => r.ruleId).sort()).toEqual([ |
| 90 | + 'vedha.authz', |
| 91 | + 'vedha.injection', |
| 92 | + 'vedha.xss', |
| 93 | + ]); |
| 94 | + }); |
| 95 | + |
| 96 | + it('skips empty and missing evidence files', async () => { |
| 97 | + const setup = await setupRepoWithDeliverables({ |
| 98 | + 'injection_exploitation_evidence.md': '', |
| 99 | + 'xss_exploitation_evidence.md': ' \n \t\n', |
| 100 | + // auth/ssrf/authz: not written at all |
| 101 | + }); |
| 102 | + cleanup = setup.cleanup; |
| 103 | + |
| 104 | + const provider = new SarifReportOutputProvider(); |
| 105 | + const result = await provider.generate(makeInput(setup.repoPath), noopLogger); |
| 106 | + const sarif = JSON.parse(await fs.readFile(result.outputPath as string, 'utf8')); |
| 107 | + |
| 108 | + expect(sarif.runs[0].results).toHaveLength(0); |
| 109 | + // Even with zero results, the envelope must be valid. |
| 110 | + expect(sarif.version).toBe('2.1.0'); |
| 111 | + expect(sarif.runs[0].tool.driver.rules).toHaveLength(5); |
| 112 | + }); |
| 113 | + |
| 114 | + it('truncates oversized evidence rather than dropping it', async () => { |
| 115 | + const huge = 'A'.repeat(64 * 1024); // 64 KiB, well above the 16 KiB limit |
| 116 | + const setup = await setupRepoWithDeliverables({ |
| 117 | + 'auth_exploitation_evidence.md': huge, |
| 118 | + }); |
| 119 | + cleanup = setup.cleanup; |
| 120 | + |
| 121 | + const provider = new SarifReportOutputProvider(); |
| 122 | + const result = await provider.generate(makeInput(setup.repoPath), noopLogger); |
| 123 | + const sarif = JSON.parse(await fs.readFile(result.outputPath as string, 'utf8')); |
| 124 | + const messageText = sarif.runs[0].results[0].message.text as string; |
| 125 | + |
| 126 | + expect(sarif.runs[0].results).toHaveLength(1); |
| 127 | + expect(messageText.length).toBeLessThan(huge.length); |
| 128 | + expect(messageText).toMatch(/\[truncated\]$/); |
| 129 | + }); |
| 130 | + |
| 131 | + it('writes the SARIF file alongside the markdown report', async () => { |
| 132 | + const setup = await setupRepoWithDeliverables({ |
| 133 | + 'ssrf_exploitation_evidence.md': 'finding', |
| 134 | + }); |
| 135 | + cleanup = setup.cleanup; |
| 136 | + |
| 137 | + const provider = new SarifReportOutputProvider(); |
| 138 | + const result = await provider.generate(makeInput(setup.repoPath), noopLogger); |
| 139 | + |
| 140 | + expect(result.outputPath).toBe( |
| 141 | + path.join(setup.repoPath, '.shannon', 'deliverables', 'comprehensive_security_assessment_report.sarif'), |
| 142 | + ); |
| 143 | + await expect(fs.access(result.outputPath as string)).resolves.toBeUndefined(); |
| 144 | + }); |
| 145 | +}); |
0 commit comments