|
| 1 | +// buildChanges → constants pull Prisma enums at module load; stub @db so the |
| 2 | +// pure redaction logic can be tested without a real client. |
| 3 | +jest.mock('@db', () => ({ |
| 4 | + db: {}, |
| 5 | + AuditLogEntityType: new Proxy({}, { get: (_t, p) => p }), |
| 6 | + CommentEntityType: new Proxy({}, { get: (_t, p) => p }), |
| 7 | +})); |
| 8 | + |
| 9 | +import { buildChanges } from './audit-log.utils'; |
| 10 | + |
| 11 | +describe('buildChanges — redaction', () => { |
| 12 | + it('redacts the existing exact-match sensitive keys', () => { |
| 13 | + const changes = buildChanges( |
| 14 | + { password: 'p', apiKey: 'k', name: 'ok' }, |
| 15 | + null, |
| 16 | + {}, |
| 17 | + ); |
| 18 | + expect(changes?.password.current).toBe('[REDACTED]'); |
| 19 | + expect(changes?.apiKey.current).toBe('[REDACTED]'); |
| 20 | + expect(changes?.name.current).toBe('ok'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('redacts credential-named keys the exact list misses (clientSecret, secretAccessKey)', () => { |
| 24 | + const changes = buildChanges( |
| 25 | + { |
| 26 | + clientSecret: 'abc', |
| 27 | + secretAccessKey: 'xyz', |
| 28 | + aws_secret_access_key: 'q', |
| 29 | + name: 'ok', |
| 30 | + }, |
| 31 | + null, |
| 32 | + {}, |
| 33 | + ); |
| 34 | + expect(changes?.clientSecret.current).toBe('[REDACTED]'); |
| 35 | + expect(changes?.secretAccessKey.current).toBe('[REDACTED]'); |
| 36 | + expect(changes?.aws_secret_access_key.current).toBe('[REDACTED]'); |
| 37 | + expect(changes?.name.current).toBe('ok'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('summarizes object elements inside arrays so secrets in generic fields cannot leak', () => { |
| 41 | + // e.g. browserbase `extraFields: [{ label, value }]` — `value` is a secret |
| 42 | + // under a non-credential key; the whole element is hidden as [Object]. |
| 43 | + const changes = buildChanges( |
| 44 | + { extraFields: [{ label: 'workspace', value: 'sekret' }] }, |
| 45 | + null, |
| 46 | + {}, |
| 47 | + ); |
| 48 | + expect(changes?.extraFields.current).toEqual(['[Object]']); |
| 49 | + }); |
| 50 | + |
| 51 | + it('keeps primitive array elements (ids, scopes, tags) visible', () => { |
| 52 | + const changes = buildChanges({ scopes: ['read', 'write'] }, null, {}); |
| 53 | + expect(changes?.scopes.current).toEqual(['read', 'write']); |
| 54 | + }); |
| 55 | + |
| 56 | + it('keeps summarizing nested plain objects as [Object]', () => { |
| 57 | + const changes = buildChanges({ config: { a: 1, token: 't' } }, null, {}); |
| 58 | + expect(changes?.config.current).toBe('[Object]'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('leaves ordinary values untouched', () => { |
| 62 | + const changes = buildChanges({ status: 'active', count: 3 }, null, {}); |
| 63 | + expect(changes?.status.current).toBe('active'); |
| 64 | + expect(changes?.count.current).toBe(3); |
| 65 | + }); |
| 66 | +}); |
0 commit comments