|
| 1 | +import { strict as assert } from 'assert'; |
| 2 | +import { parsePlanReviewOptions } from '../src/parsePlanReview'; |
| 3 | + |
| 4 | +describe('Parser security & fuzz tests', () => { |
| 5 | + it('handles very large input gracefully (<=50KB) and returns fallback when too large)', () => { |
| 6 | + const hugeInput = 'A'.repeat(60 * 1024); // 60KB |
| 7 | + const out = parsePlanReviewOptions(hugeInput, undefined, { enableFallback: true }); |
| 8 | + // Should not throw and should return minimal fallback (2 items) |
| 9 | + assert.ok(Array.isArray(out)); |
| 10 | + assert.ok(out.length <= 50); |
| 11 | + }); |
| 12 | + |
| 13 | + it('rejects/neutralizes dangerous YAML tags (no functions)', () => { |
| 14 | + const maliciousYaml = "target: !!js/function \"function() { require('child_process').execSync('id'); }\"\n"; |
| 15 | + const out = parsePlanReviewOptions(maliciousYaml, undefined, { enableFallback: true }); |
| 16 | + // The parser should not return executable JS values |
| 17 | + assert.ok(!out.some(item => typeof (item as any).target === 'function')); |
| 18 | + }); |
| 19 | + |
| 20 | + it('handles deeply nested JSON without crashing (returns fallback or sanitizes)', () => { |
| 21 | + let deep: any = {}; |
| 22 | + let cur = deep; |
| 23 | + for (let i = 0; i < 1000; i++) { cur.n = {}; cur = cur.n; } |
| 24 | + const payload = JSON.stringify(deep); |
| 25 | + const out = parsePlanReviewOptions(payload, undefined, { enableFallback: true }); |
| 26 | + assert.ok(Array.isArray(out)); |
| 27 | + }); |
| 28 | + |
| 29 | + it('strips control characters from labels', () => { |
| 30 | + const payload = JSON.stringify({ id: '1', label: "\u001b[31mBad\u001b[0m" }); |
| 31 | + const out = parsePlanReviewOptions(payload, undefined, { enableFallback: true }); |
| 32 | + assert.ok(!out[0].label.includes('\u001b[31m')); |
| 33 | + }); |
| 34 | + |
| 35 | + it('caps menu items at MAX_MENU_ITEMS (50)', () => { |
| 36 | + const items = Array.from({ length: 100 }, (_, i) => ({ id: `i${i}`, label: `Item ${i}` })); |
| 37 | + const payload = JSON.stringify(items); |
| 38 | + const out = parsePlanReviewOptions(payload, undefined, { enableFallback: true }); |
| 39 | + assert.ok(out.length <= 50); |
| 40 | + }); |
| 41 | +}); |
0 commit comments