|
1 | | -import { ReportSummary, renderReportSummary } from '../src/report' |
| 1 | +import { MAX_TITLE_LENGTH } from '../src/formatting' |
| 2 | +import { MAX_TESTS_PER_SECTION, ReportSummary, renderReportSummary } from '../src/report' |
2 | 3 |
|
3 | 4 | describe('renderReportSummary security', () => { |
4 | 5 | it('sanitizes test titles containing HTML and markdown injection', () => { |
@@ -43,4 +44,176 @@ describe('renderReportSummary security', () => { |
43 | 44 | expect(output).toContain('harmless test') |
44 | 45 | expect(output).toContain('<') |
45 | 46 | }) |
| 47 | + |
| 48 | + function reportWithFailedTitle(title: string, file = 'test.spec.ts'): ReportSummary { |
| 49 | + const failedTest = { |
| 50 | + passed: false, |
| 51 | + failed: true, |
| 52 | + flaky: false, |
| 53 | + skipped: false, |
| 54 | + file, |
| 55 | + line: 1, |
| 56 | + column: 1, |
| 57 | + path: [file, title], |
| 58 | + title, |
| 59 | + results: [{ duration: 100, started: new Date() }] |
| 60 | + } |
| 61 | + return { |
| 62 | + version: '1.0.0', |
| 63 | + started: new Date(), |
| 64 | + duration: 1000, |
| 65 | + workers: 1, |
| 66 | + shards: 0, |
| 67 | + projects: ['chromium'], |
| 68 | + files: ['test.spec.ts'], |
| 69 | + suites: [ |
| 70 | + { |
| 71 | + file: 'test.spec.ts', |
| 72 | + line: 0, |
| 73 | + column: 0, |
| 74 | + path: [], |
| 75 | + title: 'test.spec.ts', |
| 76 | + level: 0, |
| 77 | + root: true, |
| 78 | + specs: [] |
| 79 | + } |
| 80 | + ], |
| 81 | + specs: [], |
| 82 | + tests: [failedTest], |
| 83 | + failed: [failedTest], |
| 84 | + passed: [], |
| 85 | + flaky: [], |
| 86 | + skipped: [], |
| 87 | + results: [] |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + it('neutralizes markdown link injection in test titles', () => { |
| 92 | + const report = reportWithFailedTitle('[Click here for free pizza](https://attacker.example)') |
| 93 | + const output = renderReportSummary(report, { title: 'Test Report' }) |
| 94 | + expect(output).not.toMatch(/\[Click here[^\]]*\]\(https:\/\/attacker\.example\)/) |
| 95 | + expect(output).toContain('attacker.example') |
| 96 | + }) |
| 97 | + |
| 98 | + it('neutralizes markdown image injection in test titles', () => { |
| 99 | + const report = reportWithFailedTitle('') |
| 100 | + const output = renderReportSummary(report, { title: 'Test Report' }) |
| 101 | + expect(output).not.toMatch(/(^|[^\\])!\[pixel\]\(https:\/\/attacker\.example/) |
| 102 | + }) |
| 103 | + |
| 104 | + it('neutralizes inline code-span injection in test titles', () => { |
| 105 | + const report = reportWithFailedTitle('uses `rm -rf /` in setup') |
| 106 | + const output = renderReportSummary(report, { title: 'Test Report' }) |
| 107 | + const titleLine = output.split('\n').find((l) => l.includes('rm -rf')) ?? '' |
| 108 | + expect(titleLine).not.toMatch(/(^|[^\\])`/) |
| 109 | + }) |
| 110 | + |
| 111 | + it('neutralizes emphasis and heading metacharacters in test titles', () => { |
| 112 | + const report = reportWithFailedTitle('*bold* _under_ #heading') |
| 113 | + const output = renderReportSummary(report, { title: 'Test Report' }) |
| 114 | + const titleLine = output.split('\n').find((l) => l.includes('bold')) ?? '' |
| 115 | + expect(titleLine).toContain('\\*bold\\*') |
| 116 | + expect(titleLine).toContain('\\_under\\_') |
| 117 | + expect(titleLine).toContain('\\#heading') |
| 118 | + }) |
| 119 | + |
| 120 | + it('prevents code-fence breakout via attacker-controlled test.file', () => { |
| 121 | + const maliciousFile = 'tests/a.spec.ts\n```\n## Pwned\n[click](https://attacker.example)\n```\nb.spec.ts' |
| 122 | + const report = reportWithFailedTitle('a test', maliciousFile) |
| 123 | + const output = renderReportSummary(report, { |
| 124 | + title: 'Test Report', |
| 125 | + testCommand: 'npx playwright test' |
| 126 | + }) |
| 127 | + |
| 128 | + // Fences must pair up, and the payload must not escape any code block. |
| 129 | + const fenceMatches = output.match(/^`{3,}/gm) ?? [] |
| 130 | + expect(fenceMatches.length % 2).toBe(0) |
| 131 | + const outsideCode = output.replace(/^(`{3,})[^\n]*\n[\s\S]*?\n\1$/gm, '') |
| 132 | + expect(outsideCode).not.toMatch(/^## Pwned/m) |
| 133 | + expect(outsideCode).not.toMatch(/\[click\]\(https:\/\/attacker\.example\)/) |
| 134 | + }) |
| 135 | + |
| 136 | + it('truncates pathologically long test titles', () => { |
| 137 | + const longTitle = 'A'.repeat(MAX_TITLE_LENGTH + 500) |
| 138 | + const report = reportWithFailedTitle(longTitle) |
| 139 | + const output = renderReportSummary(report, { title: 'Test Report' }) |
| 140 | + |
| 141 | + expect(output).not.toContain(longTitle) |
| 142 | + expect(output).toContain(`${'A'.repeat(MAX_TITLE_LENGTH)}\u2026`) |
| 143 | + }) |
| 144 | + |
| 145 | + it('caps the number of tests rendered per section', () => { |
| 146 | + const overflow = 5 |
| 147 | + const total = MAX_TESTS_PER_SECTION + overflow |
| 148 | + const tests = Array.from({ length: total }, (_, i) => ({ |
| 149 | + passed: false, |
| 150 | + failed: true, |
| 151 | + flaky: false, |
| 152 | + skipped: false, |
| 153 | + file: `test-${i}.spec.ts`, |
| 154 | + line: i + 1, |
| 155 | + column: 1, |
| 156 | + path: [`test-${i}.spec.ts`, `failing test ${i}`], |
| 157 | + title: `failing test ${i}`, |
| 158 | + results: [{ duration: 1, started: new Date() }] |
| 159 | + })) |
| 160 | + const report: ReportSummary = { |
| 161 | + version: '1.0.0', |
| 162 | + started: new Date(), |
| 163 | + duration: 1000, |
| 164 | + workers: 1, |
| 165 | + shards: 0, |
| 166 | + projects: ['chromium'], |
| 167 | + files: [], |
| 168 | + suites: [], |
| 169 | + specs: [], |
| 170 | + tests, |
| 171 | + failed: tests, |
| 172 | + passed: [], |
| 173 | + flaky: [], |
| 174 | + skipped: [], |
| 175 | + results: [] |
| 176 | + } |
| 177 | + |
| 178 | + const output = renderReportSummary(report, { title: 'Test Report' }) |
| 179 | + |
| 180 | + expect(output).toContain(`failing test ${MAX_TESTS_PER_SECTION - 1}`) |
| 181 | + expect(output).not.toContain(`failing test ${MAX_TESTS_PER_SECTION}`) |
| 182 | + expect(output).toContain(`\u2026 and ${overflow} more`) |
| 183 | + }) |
| 184 | + |
| 185 | + it('strips ANSI escape sequences from test titles', () => { |
| 186 | + const report = reportWithFailedTitle('\x1b[31mFAILED\x1b[0m: login broken') |
| 187 | + const output = renderReportSummary(report, { title: 'Test Report' }) |
| 188 | + |
| 189 | + expect(output).not.toContain('\x1b') |
| 190 | + expect(output).toContain('FAILED') |
| 191 | + expect(output).toContain('login broken') |
| 192 | + }) |
| 193 | + |
| 194 | + it('strips ANSI escape sequences from test file paths', () => { |
| 195 | + const report = reportWithFailedTitle('a test', 'tests/\x1b[31mevil\x1b[0m.spec.ts') |
| 196 | + const output = renderReportSummary(report, { |
| 197 | + title: 'Test Report', |
| 198 | + testCommand: 'npx playwright test' |
| 199 | + }) |
| 200 | + |
| 201 | + expect(output).not.toContain('\x1b') |
| 202 | + expect(output).toContain('tests/evil.spec.ts:1') |
| 203 | + }) |
| 204 | + |
| 205 | + it('preserves backticks in legitimate file paths via adaptive fencing', () => { |
| 206 | + const report = reportWithFailedTitle('a test', 'tests/weird```name.spec.ts') |
| 207 | + const output = renderReportSummary(report, { |
| 208 | + title: 'Test Report', |
| 209 | + testCommand: 'npx playwright test' |
| 210 | + }) |
| 211 | + |
| 212 | + expect(output).toContain('tests/weird```name.spec.ts:1') |
| 213 | + const fenceMatches = output.match(/^`{3,}/gm) ?? [] |
| 214 | + expect(fenceMatches.length % 2).toBe(0) |
| 215 | + for (const f of fenceMatches) { |
| 216 | + expect(f.length).toBeGreaterThanOrEqual(4) |
| 217 | + } |
| 218 | + }) |
46 | 219 | }) |
0 commit comments