|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { scanArgs, DLP_PATTERNS } from '../dlp.js'; |
| 3 | + |
| 4 | +// NOTE: All fake secret strings are built via concatenation so GitHub's secret |
| 5 | +// scanner doesn't flag this test file. The values are obviously fake (sequential |
| 6 | +// letters/numbers) and are never used outside of these unit tests. |
| 7 | + |
| 8 | +// ── Helpers ─────────────────────────────────────────────────────────────────── |
| 9 | + |
| 10 | +// Fake AWS Access Key ID — split to defeat static secret scanners |
| 11 | +const FAKE_AWS_KEY = 'AKIA' + 'IOSFODNN7' + 'EXAMPLE'; |
| 12 | + |
| 13 | +// Stripe keys: sk_(live|test)_ + exactly 24 alphanumeric chars |
| 14 | +const FAKE_STRIPE_LIVE = 'sk_live_' + 'abcdefghijklmnop' + 'qrstuvwx'; |
| 15 | +const FAKE_STRIPE_TEST = 'sk_test_' + 'abcdefghijklmnop' + 'qrstuvwx'; |
| 16 | + |
| 17 | +// OpenAI key: sk- + 20+ alphanumeric chars |
| 18 | +const FAKE_OPENAI_KEY = 'sk-' + 'abcdefghij' + '1234567890klmn'; |
| 19 | + |
| 20 | +// Slack bot token |
| 21 | +const FAKE_SLACK_TOKEN = 'xoxb-' + '1234-5678-abcdefghij'; |
| 22 | + |
| 23 | +// ── Pattern coverage ────────────────────────────────────────────────────────── |
| 24 | + |
| 25 | +describe('DLP_PATTERNS — built-in patterns', () => { |
| 26 | + it('detects AWS Access Key ID', () => { |
| 27 | + const match = scanArgs({ command: `aws s3 cp --key ${FAKE_AWS_KEY} s3://bucket/` }); |
| 28 | + expect(match).not.toBeNull(); |
| 29 | + expect(match!.patternName).toBe('AWS Access Key ID'); |
| 30 | + expect(match!.severity).toBe('block'); |
| 31 | + expect(match!.redactedSample).not.toContain(FAKE_AWS_KEY); |
| 32 | + expect(match!.redactedSample).toMatch(/AKIA\*+MPLE/); |
| 33 | + }); |
| 34 | + |
| 35 | + it('detects GitHub personal access token (ghp_)', () => { |
| 36 | + const token = 'ghp_' + 'a'.repeat(36); |
| 37 | + const match = scanArgs({ command: `git clone https://${token}@github.com/org/repo` }); |
| 38 | + expect(match).not.toBeNull(); |
| 39 | + expect(match!.patternName).toBe('GitHub Token'); |
| 40 | + expect(match!.severity).toBe('block'); |
| 41 | + expect(match!.redactedSample).not.toContain(token); |
| 42 | + }); |
| 43 | + |
| 44 | + it('detects GitHub OAuth token (gho_)', () => { |
| 45 | + const token = 'gho_' + 'b'.repeat(36); |
| 46 | + const match = scanArgs({ env: { TOKEN: token } }); |
| 47 | + expect(match).not.toBeNull(); |
| 48 | + expect(match!.patternName).toBe('GitHub Token'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('detects Slack bot token', () => { |
| 52 | + const match = scanArgs({ header: `Authorization: ${FAKE_SLACK_TOKEN}` }); |
| 53 | + expect(match).not.toBeNull(); |
| 54 | + expect(match!.patternName).toBe('Slack Bot Token'); |
| 55 | + expect(match!.severity).toBe('block'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('detects OpenAI API key', () => { |
| 59 | + const match = scanArgs({ command: `curl -H "Authorization: ${FAKE_OPENAI_KEY}"` }); |
| 60 | + expect(match).not.toBeNull(); |
| 61 | + expect(match!.patternName).toBe('OpenAI API Key'); |
| 62 | + expect(match!.severity).toBe('block'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('detects Stripe live secret key', () => { |
| 66 | + const match = scanArgs({ env: `STRIPE_KEY=${FAKE_STRIPE_LIVE}` }); |
| 67 | + expect(match).not.toBeNull(); |
| 68 | + expect(match!.patternName).toBe('Stripe Secret Key'); |
| 69 | + expect(match!.severity).toBe('block'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('detects Stripe test secret key', () => { |
| 73 | + const match = scanArgs({ env: `STRIPE_KEY=${FAKE_STRIPE_TEST}` }); |
| 74 | + expect(match).not.toBeNull(); |
| 75 | + expect(match!.patternName).toBe('Stripe Secret Key'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('detects PEM private key header', () => { |
| 79 | + const pemHeader = '-----BEGIN RSA ' + 'PRIVATE KEY-----'; |
| 80 | + const match = scanArgs({ content: `${pemHeader}\nMIIEowIBAAK...` }); |
| 81 | + expect(match).not.toBeNull(); |
| 82 | + expect(match!.patternName).toBe('Private Key (PEM)'); |
| 83 | + expect(match!.severity).toBe('block'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('detects Bearer token with review severity', () => { |
| 87 | + const match = scanArgs({ header: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.payload.sig' }); |
| 88 | + expect(match).not.toBeNull(); |
| 89 | + expect(match!.patternName).toBe('Bearer Token'); |
| 90 | + expect(match!.severity).toBe('review'); // not a hard block |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
| 94 | +// ── Redaction ───────────────────────────────────────────────────────────────── |
| 95 | + |
| 96 | +describe('maskSecret redaction', () => { |
| 97 | + it('shows first 4 + last 4 chars of the matched secret', () => { |
| 98 | + const match = scanArgs({ key: FAKE_AWS_KEY }); |
| 99 | + expect(match).not.toBeNull(); |
| 100 | + // prefix = 'AKIA', suffix = 'MPLE' |
| 101 | + expect(match!.redactedSample).toMatch(/^AKIA/); |
| 102 | + expect(match!.redactedSample).toMatch(/MPLE$/); |
| 103 | + expect(match!.redactedSample).toContain('*'); |
| 104 | + expect(match!.redactedSample).not.toContain('IOSFODNN7EXA'); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +// ── Recursive scanning ──────────────────────────────────────────────────────── |
| 109 | + |
| 110 | +describe('scanArgs — recursive object scanning', () => { |
| 111 | + it('scans nested objects', () => { |
| 112 | + const match = scanArgs({ outer: { inner: { key: FAKE_AWS_KEY } } }); |
| 113 | + expect(match).not.toBeNull(); |
| 114 | + expect(match!.fieldPath).toBe('args.outer.inner.key'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('scans arrays', () => { |
| 118 | + const match = scanArgs({ envVars: ['SAFE=value', `SECRET=${FAKE_AWS_KEY}`] }); |
| 119 | + expect(match).not.toBeNull(); |
| 120 | + expect(match!.fieldPath).toContain('[1]'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('returns null for clean args', () => { |
| 124 | + expect(scanArgs({ command: 'ls -la /tmp', options: { verbose: true } })).toBeNull(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('returns null for non-object primitives', () => { |
| 128 | + expect(scanArgs(42)).toBeNull(); |
| 129 | + expect(scanArgs(null)).toBeNull(); |
| 130 | + expect(scanArgs(undefined)).toBeNull(); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +// ── JSON-in-string ──────────────────────────────────────────────────────────── |
| 135 | + |
| 136 | +describe('scanArgs — JSON-in-string detection', () => { |
| 137 | + it('detects a secret inside a JSON-encoded string field', () => { |
| 138 | + const inner = JSON.stringify({ api_key: FAKE_AWS_KEY, region: 'us-east-1' }); |
| 139 | + const match = scanArgs({ content: inner }); |
| 140 | + expect(match).not.toBeNull(); |
| 141 | + expect(match!.patternName).toBe('AWS Access Key ID'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('does not crash on invalid JSON strings', () => { |
| 145 | + expect(() => scanArgs({ content: '{not valid json' })).not.toThrow(); |
| 146 | + }); |
| 147 | + |
| 148 | + it('skips JSON parse for strings longer than 10 KB', () => { |
| 149 | + const longJson = '{"key": "' + 'x'.repeat(10_001) + '"}'; |
| 150 | + // Should not throw and should not attempt to parse |
| 151 | + expect(() => scanArgs({ content: longJson })).not.toThrow(); |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +// ── Depth & length limits ───────────────────────────────────────────────────── |
| 156 | + |
| 157 | +describe('scanArgs — performance guards', () => { |
| 158 | + it('stops recursion at MAX_DEPTH (5)', () => { |
| 159 | + // 6 levels deep — secret at level 6 should not be found |
| 160 | + const deep = { a: { b: { c: { d: { e: { f: FAKE_AWS_KEY } } } } } }; |
| 161 | + const match = scanArgs(deep); |
| 162 | + // depth=0 is the top-level object, key 'a' is depth 1, ..., key 'f' is depth 6 |
| 163 | + // Our MAX_DEPTH=5 guard returns null at depth > 5, so the string at depth 6 is skipped |
| 164 | + expect(match).toBeNull(); |
| 165 | + }); |
| 166 | + |
| 167 | + it('only scans the first 100 KB of a long string', () => { |
| 168 | + // Secret is beyond the 100 KB limit — should not be found |
| 169 | + const padding = 'x'.repeat(100_001); |
| 170 | + const match = scanArgs({ content: padding + FAKE_AWS_KEY }); |
| 171 | + expect(match).toBeNull(); |
| 172 | + }); |
| 173 | + |
| 174 | + it('finds a secret within the first 100 KB', () => { |
| 175 | + const padding = 'x'.repeat(50_000); |
| 176 | + const match = scanArgs({ content: `${padding} ${FAKE_AWS_KEY} ` }); |
| 177 | + expect(match).not.toBeNull(); |
| 178 | + }); |
| 179 | +}); |
| 180 | + |
| 181 | +// ── All patterns export ─────────────────────────────────────────────────────── |
| 182 | + |
| 183 | +describe('DLP_PATTERNS export', () => { |
| 184 | + it('exports at least 7 built-in patterns', () => { |
| 185 | + expect(DLP_PATTERNS.length).toBeGreaterThanOrEqual(7); |
| 186 | + }); |
| 187 | + |
| 188 | + it('all patterns have name, regex, and severity', () => { |
| 189 | + for (const p of DLP_PATTERNS) { |
| 190 | + expect(p.name).toBeTruthy(); |
| 191 | + expect(p.regex).toBeInstanceOf(RegExp); |
| 192 | + expect(['block', 'review']).toContain(p.severity); |
| 193 | + } |
| 194 | + }); |
| 195 | +}); |
0 commit comments