Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, it, expect } from 'vitest'; |
| 2 | import { redactSensitiveText } from '../../src/telemetry/redact'; |
| 3 | |
| 4 | describe('redactSensitiveText', () => { |
| 5 | it('returns empty/undefined input unchanged', () => { |
| 6 | expect(redactSensitiveText(undefined)).toBeUndefined(); |
| 7 | expect(redactSensitiveText('')).toBe(''); |
| 8 | }); |
| 9 | |
| 10 | it('redacts URL query strings while keeping the path', () => { |
| 11 | const out = redactSensitiveText( |
| 12 | 'Failed to PUT https://s3.amazonaws.com/bucket/key?X-Amz-Signature=deadbeef&token=abc' |
| 13 | ); |
| 14 | expect(out).toContain('https://s3.amazonaws.com/bucket/key?[REDACTED]'); |
| 15 | expect(out).not.toContain('deadbeef'); |
| 16 | expect(out).not.toContain('token=abc'); |
| 17 | }); |
| 18 | |
| 19 | it('redacts Authorization bearer/basic credentials', () => { |
| 20 | const auth = redactSensitiveText('Authorization: Bearer sk-live-1234567890')!; |
| 21 | expect(auth).toContain('[REDACTED]'); |
| 22 | expect(auth).not.toContain('sk-live-1234567890'); |
| 23 | expect(redactSensitiveText('used Basic dXNlcjpwYXNz here')).toBe('used Basic [REDACTED] here'); |
| 24 | }); |
| 25 | |
| 26 | it('redacts secret-like key/value pairs', () => { |
| 27 | for (const sample of [ |
| 28 | 'api_key=ck_abcdef123456', |
| 29 | 'x-api-key: "ck_secretvalue"', |
| 30 | "client_secret: 'topsecret'", |
| 31 | 'password=hunter2', |
| 32 | 'access_token=ya29.a0Afoobar', |
| 33 | 'auth=app_key:pusher_signature', |
| 34 | ]) { |
| 35 | const out = redactSensitiveText(sample)!; |
| 36 | expect(out, sample).toContain('[REDACTED]'); |
| 37 | expect(out, sample).not.toMatch(/ck_abcdef123456|ck_secretvalue|topsecret|hunter2|ya29/); |
| 38 | } |
| 39 | }); |
| 40 | |
| 41 | it('preserves quotes around redacted values', () => { |
| 42 | expect(redactSensitiveText('x-api-key: "ck_secretvalue"')).toContain('"[REDACTED]"'); |
| 43 | }); |
| 44 | |
| 45 | // Serialized payloads are the common shape in error text: the key's own |
| 46 | // closing quote sits between the name and the colon, so a pattern anchored on |
| 47 | // `name` followed directly by `:` never matches. |
| 48 | it('redacts secrets inside JSON payloads', () => { |
| 49 | for (const [sample, secret] of [ |
| 50 | ['{"api_key": "ck_live_abc123"}', 'ck_live_abc123'], |
| 51 | ['{"api_key":"ck_live_abc123"}', 'ck_live_abc123'], |
| 52 | ['{"api_key" : "ck_live_abc123"}', 'ck_live_abc123'], |
| 53 | ['{"refresh_token":"rt-abc.def-123"}', 'rt-abc.def-123'], |
| 54 | ['{"auth":"app_key:pusher_signature"}', 'app_key:pusher_signature'], |
| 55 | ['{"COMPOSIO_API_KEY":"prefixed-double-secret"}', 'prefixed-double-secret'], |
| 56 | [`{'OPENAI_API_KEY': 'prefixed single secret'}`, 'prefixed single secret'], |
| 57 | ['{"x-api-key":"ck_hdr","user":"bob"}', 'ck_hdr'], |
| 58 | [`{'client_secret': 'cs_live_abc123'}`, 'cs_live_abc123'], |
| 59 | ['{"password": "hunter2"}', 'hunter2'], |
| 60 | ['{"password": "correct horse battery staple"}', 'correct horse battery staple'], |
| 61 | [`{'client_secret': 'multi word secret'}`, 'multi word secret'], |
| 62 | ] as const) { |
| 63 | const out = redactSensitiveText(sample)!; |
| 64 | expect(out, sample).toContain('[REDACTED]'); |
| 65 | expect(out, sample).not.toContain(secret); |
| 66 | } |
| 67 | }); |
| 68 | |
| 69 | it('redacts a secret in a serialized error payload while keeping context', () => { |
| 70 | const out = redactSensitiveText( |
| 71 | 'Error executing tool: request body was rejected: ' + |
| 72 | '{"toolkit": "GMAIL", "arguments": {"api_key": "ck_live_CUSTOMER", "to": "x@y.z"}}' |
| 73 | )!; |
| 74 | expect(out).not.toContain('ck_live_CUSTOMER'); |
| 75 | expect(out).toContain('GMAIL'); |
| 76 | }); |
| 77 | |
| 78 | it('preserves JSON keys and quoting, replacing only the value', () => { |
| 79 | expect(redactSensitiveText('{"api_key": "ck_live_abc123"}')).toBe('{"api_key": "[REDACTED]"}'); |
| 80 | expect(redactSensitiveText('{"api_key":"secret","user":"bob"}')).toBe( |
| 81 | '{"api_key":"[REDACTED]","user":"bob"}' |
| 82 | ); |
| 83 | }); |
| 84 | |
| 85 | it('redacts escaped quoted secrets in serialized messages', () => { |
| 86 | const source = JSON.stringify({ error: 'password: "secret"' }); |
| 87 | const expected = JSON.stringify({ error: 'password: "[REDACTED]"' }); |
| 88 | expect(redactSensitiveText(source)).toBe(expected); |
| 89 | expect(redactSensitiveText("password: \\'secret\\'")).toBe("password: \\'[REDACTED]\\'"); |
| 90 | |
| 91 | const doubleEncoded = JSON.stringify({ body: JSON.stringify({ api_key: 'secret' }) }); |
| 92 | const doubleEncodedExpected = JSON.stringify({ |
| 93 | body: JSON.stringify({ api_key: '[REDACTED]' }), |
| 94 | }); |
| 95 | expect(redactSensitiveText(doubleEncoded)).toBe(doubleEncodedExpected); |
| 96 | }); |
| 97 | |
| 98 | it('preserves serialized adjacent keys with escapes', () => { |
| 99 | const source = JSON.stringify({ |
| 100 | error: 'unknown field auth:', |
| 101 | 'quoted"key': 'safe', |
| 102 | 'path\\key': 'safe', |
| 103 | }); |
| 104 | expect(redactSensitiveText(source)).toBe(source); |
| 105 | }); |
| 106 | |
| 107 | it('redacts unquoted secrets containing backslashes', () => { |
| 108 | expect(redactSensitiveText('password=abc\\def')).toBe('password=[REDACTED]'); |
| 109 | expect(redactSensitiveText('password=\\leading')).toBe('password=[REDACTED]'); |
| 110 | }); |
| 111 | |
| 112 | it('redacts many quoted secrets in one pass', () => { |
| 113 | const source = Array.from({ length: 1_000 }, (_, index) => `password: "secret-${index}"`).join( |
| 114 | ' ' |
| 115 | ); |
| 116 | const expected = Array.from({ length: 1_000 }, () => 'password: "[REDACTED]"').join(' '); |
| 117 | expect(redactSensitiveText(source)).toBe(expected); |
| 118 | }); |
| 119 | |
| 120 | it('leaves a key name with no attached value untouched', () => { |
| 121 | for (const benign of [ |
| 122 | 'the password field is required', |
| 123 | 'no separator here "api_key" and nothing else', |
| 124 | ]) { |
| 125 | expect(redactSensitiveText(benign), benign).toBe(benign); |
| 126 | } |
| 127 | }); |
| 128 | |
| 129 | it('leaves benign error text untouched', () => { |
| 130 | const benign = 'TypeError: cannot read property foo of undefined at Object.<anonymous>'; |
| 131 | expect(redactSensitiveText(benign)).toBe(benign); |
| 132 | }); |
| 133 | |
| 134 | // Underscore before api_key is a word char, so a leading \b would miss these. |
| 135 | it('redacts env-style prefixed API keys', () => { |
| 136 | for (const [sample, secret] of [ |
| 137 | ['COMPOSIO_API_KEY=sk_live_9f3c', 'sk_live_9f3c'], |
| 138 | ['OPENAI_API_KEY=sk_live_9f3c', 'sk_live_9f3c'], |
| 139 | ['export COMPOSIO_API_KEY="sk_live_9f3c"', 'sk_live_9f3c'], |
| 140 | ] as const) { |
| 141 | const out = redactSensitiveText(sample)!; |
| 142 | expect(out, sample).toContain('[REDACTED]'); |
| 143 | expect(out, sample).not.toContain(secret); |
| 144 | } |
| 145 | }); |
| 146 | |
| 147 | it('does not match a secret name embedded in letters', () => { |
| 148 | expect(redactSensitiveText('myapikey=sk_live_9f3c')).toBe('myapikey=sk_live_9f3c'); |
| 149 | }); |
| 150 | |
| 151 | it('does not consume adjacent fields after key-like text', () => { |
| 152 | for (const benign of [ |
| 153 | '{"error": "unknown field auth:", "user": "bob"}', |
| 154 | `{'note': 'unknown field auth:', "user": 'bob'}`, |
| 155 | '{"error": "unknown field auth:", "$schema": "safe"}', |
| 156 | '{"error": "unknown field auth:", "@type": "safe"}', |
| 157 | '{"error": "unknown field auth:", "üser": "safe"}', |
| 158 | '{"error": "unknown field auth:", ".well-known": "safe"}', |
| 159 | '{"error": "unknown field auth:", ":type": "safe"}', |
| 160 | '{"error": "unknown field auth:", "?field": "safe"}', |
| 161 | ]) { |
| 162 | expect(redactSensitiveText(benign), benign).toBe(benign); |
| 163 | } |
| 164 | }); |
| 165 | |
| 166 | it('redacts bare quoted values before punctuation or prose', () => { |
| 167 | for (const [sample, secret] of [ |
| 168 | ['password: "punctuated secret";', 'punctuated secret'], |
| 169 | ["client_secret='period secret'.", 'period secret'], |
| 170 | ['api_key = "prose secret" followed by context', 'prose secret'], |
| 171 | ['password: "escaped \\"secret\\" value"', 'escaped \\"secret\\" value'], |
| 172 | [`can't connect password: "secret"`, 'secret'], |
| 173 | [`users' password: "secret"`, 'secret'], |
| 174 | [`Chris' password: "secret"`, 'secret'], |
| 175 | [`Andrés' password: "secret"`, 'secret'], |
| 176 | [`{"error":"request failed password: 'secret'"}`, 'secret'], |
| 177 | ['5" from target password: "secret"', 'secret'], |
| 178 | ['unmatched " before password: "secret"', 'secret'], |
| 179 | ["unmatched ' before password: 'secret'", 'secret'], |
| 180 | ['password: "}abc"', '}abc'], |
| 181 | ['api_key: ",abc"', ',abc'], |
| 182 | ] as const) { |
| 183 | const out = redactSensitiveText(sample)!; |
| 184 | expect(out, sample).toContain('[REDACTED]'); |
| 185 | expect(out, sample).not.toContain(secret); |
| 186 | } |
| 187 | }); |
| 188 | }); |