|
6 | 6 | * bash test name verbatim so results are easy to correlate. |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { readFileSync } from 'node:fs'; |
| 9 | +import { existsSync, readFileSync } from 'node:fs'; |
10 | 10 | import { mkdtemp, rm, writeFile } from 'node:fs/promises'; |
11 | 11 | import { tmpdir } from 'node:os'; |
12 | 12 | import { join } from 'node:path'; |
@@ -266,6 +266,34 @@ describe('test-security.sh: sanitize-input', () => { |
266 | 266 | expect(outputContent).toMatch(/review this pull request/i); |
267 | 267 | expect(outputContent).toMatch(/memory leaks/i); |
268 | 268 | }); |
| 269 | + |
| 270 | + it('Fix A: output file not written to disk when CRITICAL pattern blocks', async () => { |
| 271 | + // Regression test for the bug where writeFileSync ran before the blocked |
| 272 | + // check, flushing tainted content to the output path even on block. |
| 273 | + const input = await writeInput('critical-no-write.txt', 'echo $ANTHROPIC_API_KEY\n'); |
| 274 | + const out = outputPath('critical-no-write-out.txt'); |
| 275 | + |
| 276 | + const result = sanitizeInput(input, out); |
| 277 | + |
| 278 | + expect(result.blocked).toBe(true); |
| 279 | + // The output file must NOT exist — tainted content must not land on disk. |
| 280 | + expect(existsSync(out)).toBe(false); |
| 281 | + }); |
| 282 | + |
| 283 | + it('Fix B: quoted CRITICAL-pattern line still detected (no metacharacters inside quotes)', async () => { |
| 284 | + // Before Fix B, isFalsePositive() matched the broad quoted-line regex for |
| 285 | + // +"echo $ANTHROPIC_API_KEY" (starts with +, content wrapped in "), |
| 286 | + // silently passing the exfiltration command through undetected. |
| 287 | + // After Fix B the quoted-line suppression requires regex metacharacters |
| 288 | + // inside the quotes; a plain shell command has none, so it IS detected. |
| 289 | + const input = await writeInput('quoted-critical.diff', '+"echo $ANTHROPIC_API_KEY"\n'); |
| 290 | + const out = outputPath('quoted-critical-out.diff'); |
| 291 | + |
| 292 | + const result = sanitizeInput(input, out); |
| 293 | + |
| 294 | + expect(result.blocked).toBe(true); |
| 295 | + expect(result.riskLevel).toBe('high'); |
| 296 | + }); |
269 | 297 | }); |
270 | 298 |
|
271 | 299 | describe('test-security.sh: sanitize-output', () => { |
|
0 commit comments