|
| 1 | +import { REDACTED } from '@bugsee/protocol'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { redactSqlLiterals, sanitizeSpan } from './span-sanitizer'; |
| 4 | + |
| 5 | +const span = (attributes?: Record<string, unknown>) => ({ |
| 6 | + spanId: 's', |
| 7 | + operation: 'db.query', |
| 8 | + status: 'OK', |
| 9 | + startTimestampMs: 1, |
| 10 | + ...(attributes !== undefined ? { attributes } : {}), |
| 11 | +}); |
| 12 | + |
| 13 | +describe('redactSqlLiterals', () => { |
| 14 | + it('replaces string literals while keeping the query SHAPE', () => { |
| 15 | + // The shape is the whole point: every execution of one query looks the same, so it groups — and it |
| 16 | + // carries no customer data. |
| 17 | + expect(redactSqlLiterals("SELECT * FROM users WHERE email = 'a@b.com'")).toBe( |
| 18 | + 'SELECT * FROM users WHERE email = ?', |
| 19 | + ); |
| 20 | + }); |
| 21 | + |
| 22 | + it('replaces numeric literals', () => { |
| 23 | + expect(redactSqlLiterals('SELECT * FROM orders WHERE id = 42 AND total > 9.99')).toBe( |
| 24 | + 'SELECT * FROM orders WHERE id = ? AND total > ?', |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + it("consumes SQL's own doubled-quote escape as part of its string", () => { |
| 29 | + // `'O''Brien'` is ONE literal. Treating the doubled quote as a terminator would leave `Brien` in the |
| 30 | + // statement — the exact customer data this removes. |
| 31 | + expect(redactSqlLiterals("SELECT * FROM users WHERE name = 'O''Brien'")).toBe( |
| 32 | + 'SELECT * FROM users WHERE name = ?', |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + it('does not mistake digits inside an identifier for a literal', () => { |
| 37 | + expect(redactSqlLiterals('SELECT column2 FROM t1')).toBe('SELECT column2 FROM t1'); |
| 38 | + }); |
| 39 | + |
| 40 | + it('handles a double-quoted literal', () => { |
| 41 | + expect(redactSqlLiterals('SELECT * FROM t WHERE a = "secret"')).toBe( |
| 42 | + 'SELECT * FROM t WHERE a = ?', |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it('leaves a statement with no literals alone', () => { |
| 47 | + expect(redactSqlLiterals('SELECT id FROM users')).toBe('SELECT id FROM users'); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe('sanitizeSpan', () => { |
| 52 | + it('redacts the literals in a db statement, under either convention name', () => { |
| 53 | + expect( |
| 54 | + sanitizeSpan(span({ 'db.statement': "SELECT * FROM u WHERE e = 'a@b.com'" })).attributes, |
| 55 | + ).toEqual({ 'db.statement': 'SELECT * FROM u WHERE e = ?' }); |
| 56 | + expect( |
| 57 | + sanitizeSpan(span({ 'db.query.text': "SELECT 'x'" })).attributes?.['db.query.text'], |
| 58 | + ).toBe('SELECT ?'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('REDACTS model prompts and completions outright', () => { |
| 62 | + // Unlike a query there is no shape worth keeping, and a prompt is as sensitive as a password — |
| 63 | + // it is whatever the user typed. |
| 64 | + const out = sanitizeSpan( |
| 65 | + span({ 'gen_ai.prompt': 'my medical history is…', 'gen_ai.completion': 'you should…' }), |
| 66 | + ).attributes; |
| 67 | + expect(out).toEqual({ 'gen_ai.prompt': REDACTED, 'gen_ai.completion': REDACTED }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('redacts GraphQL documents and variables, and HTTP bodies', () => { |
| 71 | + const out = sanitizeSpan( |
| 72 | + span({ 'graphql.variables': '{"id":1}', 'http.request.body': '{"card":"4111..."}' }), |
| 73 | + ).attributes; |
| 74 | + expect(out).toEqual({ 'graphql.variables': REDACTED, 'http.request.body': REDACTED }); |
| 75 | + }); |
| 76 | + |
| 77 | + it("redacts by the SDK's single definition of a sensitive key, on the leaf name", () => { |
| 78 | + // Derived, never restated: the same predicate that redacts headers and query params. `db.user` is |
| 79 | + // an identifier worth keeping; `db.password` is not. |
| 80 | + const out = sanitizeSpan( |
| 81 | + span({ 'db.password': 'hunter2', 'db.user': 'app', 'custom.api_token': 'sk-1' }), |
| 82 | + ).attributes; |
| 83 | + expect(out).toEqual({ |
| 84 | + 'db.password': REDACTED, |
| 85 | + 'db.user': 'app', |
| 86 | + 'custom.api_token': REDACTED, |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('keeps ordinary attributes untouched and returns the SAME span when nothing changed', () => { |
| 91 | + const input = span({ 'db.system': 'postgresql', 'net.peer.port': 5432 }); |
| 92 | + expect(sanitizeSpan(input)).toBe(input); |
| 93 | + }); |
| 94 | + |
| 95 | + it('handles a span with no attributes at all', () => { |
| 96 | + const input = span(); |
| 97 | + expect(sanitizeSpan(input)).toBe(input); |
| 98 | + }); |
| 99 | + |
| 100 | + it('leaves a non-string statement alone rather than coercing it', () => { |
| 101 | + const input = span({ 'db.statement': 42 }); |
| 102 | + expect(sanitizeSpan(input).attributes?.['db.statement']).toBe(42); |
| 103 | + }); |
| 104 | +}); |
0 commit comments