|
3 | 3 | */ |
4 | 4 | import { describe, expect, it } from 'vitest' |
5 | 5 | import { |
| 6 | + type CreateResolvedSecretMatcherOptions, |
6 | 7 | containsResolvedSecret, |
7 | 8 | createResolvedSecretMatcher, |
8 | 9 | OPAQUE_RESOLVED_SECRET_REPLACEMENT, |
| 10 | + type ResolvedSecretMatch, |
| 11 | + type ResolvedSecretMatcher, |
9 | 12 | sanitizeResolvedSecretPrimitive, |
10 | 13 | sanitizeResolvedSecretString, |
11 | 14 | scanResolvedSecretString, |
@@ -192,3 +195,176 @@ describe('resolved secret matcher', () => { |
192 | 195 | expect(sanitizeResolvedSecretString('Test', matcher)).toBe('') |
193 | 196 | }) |
194 | 197 | }) |
| 198 | + |
| 199 | +describe('resolved secret matcher match policy', () => { |
| 200 | + const SHORT = [{ plaintext: 'test', replacement: '{{TOKEN}}' }] |
| 201 | + const API_KEY = 'sk-proj-Ab3xK9mQ2pLw7nRt5vYc8Zd4' |
| 202 | + const LONG = [{ plaintext: API_KEY, replacement: '{{API_KEY}}' }] |
| 203 | + |
| 204 | + function build( |
| 205 | + matches: ResolvedSecretMatch[], |
| 206 | + options?: CreateResolvedSecretMatcherOptions |
| 207 | + ): ResolvedSecretMatcher { |
| 208 | + const matcher = createResolvedSecretMatcher(matches, options) |
| 209 | + if (!matcher) throw new Error('expected a matcher') |
| 210 | + return matcher |
| 211 | + } |
| 212 | + |
| 213 | + it('matches a short literal anywhere when classifying content', () => { |
| 214 | + const matcher = build(SHORT) |
| 215 | + |
| 216 | + expect(containsResolvedSecret('the latest news', matcher)).toBe(true) |
| 217 | + expect(sanitizeResolvedSecretString('the latest news', matcher)).toBe('the la{{TOKEN}} news') |
| 218 | + }) |
| 219 | + |
| 220 | + it.each([ |
| 221 | + ['test', '{{TOKEN}}'], |
| 222 | + ['key=test', 'key={{TOKEN}}'], |
| 223 | + ['"test"', '"{{TOKEN}}"'], |
| 224 | + ['{"k":"test"}', '{"k":"{{TOKEN}}"}'], |
| 225 | + ['test test', '{{TOKEN}} {{TOKEN}}'], |
| 226 | + ['user_test_id', 'user_{{TOKEN}}_id'], |
| 227 | + ])('still renders a boundary-anchored short literal in %s', (value, expected) => { |
| 228 | + const matcher = build(SHORT, { mode: 'render' }) |
| 229 | + |
| 230 | + expect(sanitizeResolvedSecretString(value, matcher)).toBe(expected) |
| 231 | + expect(containsResolvedSecret(value, matcher)).toBe(true) |
| 232 | + }) |
| 233 | + |
| 234 | + it.each(['the latest news', 'tested', 'prefixtest'])( |
| 235 | + 'leaves an unanchored short literal in %s untouched when rendering', |
| 236 | + (value) => { |
| 237 | + const matcher = build(SHORT, { mode: 'render' }) |
| 238 | + |
| 239 | + expect(sanitizeResolvedSecretString(value, matcher)).toBe(value) |
| 240 | + expect(containsResolvedSecret(value, matcher)).toBe(false) |
| 241 | + } |
| 242 | + ) |
| 243 | + |
| 244 | + it('renders a full-length literal at any offset, including mid-token', () => { |
| 245 | + const matcher = build(LONG, { mode: 'render' }) |
| 246 | + |
| 247 | + expect(sanitizeResolvedSecretString(`prefix${API_KEY}suffix`, matcher)).toBe( |
| 248 | + 'prefix{{API_KEY}}suffix' |
| 249 | + ) |
| 250 | + expect(containsResolvedSecret(`prefix${API_KEY}suffix`, matcher)).toBe(true) |
| 251 | + }) |
| 252 | + |
| 253 | + /** Prefixed shapes are assembled at runtime so no source literal reads as a live credential. */ |
| 254 | + it.each([ |
| 255 | + ['f'.repeat(32), 'all-f HMAC key'], |
| 256 | + ['4111111111111111', 'test PAN'], |
| 257 | + [`AKIA${'0'.repeat(16)}`, 'padded AWS key id'], |
| 258 | + [`sk_live_${'0'.repeat(24)}`, 'padded stripe-style key'], |
| 259 | + ])('renders low-variety full-length credential (%s) mid-token', (secret) => { |
| 260 | + const matcher = build([{ plaintext: secret, replacement: '{{KEY}}' }], { mode: 'render' }) |
| 261 | + |
| 262 | + expect(sanitizeResolvedSecretString(`etag_${secret}x`, matcher)).toBe('etag_{{KEY}}x') |
| 263 | + expect(containsResolvedSecret(`etag_${secret}x`, matcher)).toBe(true) |
| 264 | + }) |
| 265 | + |
| 266 | + it('settles a boundary that an earlier substitution exposed', () => { |
| 267 | + const matcher = build( |
| 268 | + [ |
| 269 | + { plaintext: API_KEY, replacement: '{{API_KEY}}' }, |
| 270 | + { plaintext: 'test', replacement: '{{TOKEN}}' }, |
| 271 | + ], |
| 272 | + { mode: 'render' } |
| 273 | + ) |
| 274 | + |
| 275 | + expect(sanitizeResolvedSecretString(`${API_KEY}test`, matcher)).toBe('{{API_KEY}}{{TOKEN}}') |
| 276 | + }) |
| 277 | + |
| 278 | + it('settles a literal that an empty replacement spliced into existence', () => { |
| 279 | + const matcher = build([ |
| 280 | + { plaintext: API_KEY, replacement: '' }, |
| 281 | + { plaintext: 'password', replacement: '{{PW}}' }, |
| 282 | + ]) |
| 283 | + |
| 284 | + expect(sanitizeResolvedSecretString(`pass${API_KEY}word`, matcher)).toBe('{{PW}}') |
| 285 | + }) |
| 286 | + |
| 287 | + it('keeps the substitution pass and its invariant in agreement', () => { |
| 288 | + const matcher = build(SHORT, { mode: 'render' }) |
| 289 | + |
| 290 | + for (const value of ['the latest news', 'key=test', 'contest testable test']) { |
| 291 | + const sanitized = sanitizeResolvedSecretString(value, matcher) |
| 292 | + expect(containsResolvedSecret(sanitized, matcher)).toBe(false) |
| 293 | + } |
| 294 | + }) |
| 295 | + |
| 296 | + it('reports a suppressed match to provenance callbacks so detection stays conservative', () => { |
| 297 | + const matcher = build(SHORT, { mode: 'render' }) |
| 298 | + const matches: string[] = [] |
| 299 | + |
| 300 | + expect( |
| 301 | + sanitizeResolvedSecretString('the latest news', matcher, undefined, (plaintext) => |
| 302 | + matches.push(plaintext) |
| 303 | + ) |
| 304 | + ).toBe('the latest news') |
| 305 | + expect(matches).toEqual(['test']) |
| 306 | + }) |
| 307 | + |
| 308 | + it.each([ |
| 309 | + ['Test', '{{Test}}'], |
| 310 | + ['{{Test}}', '{{Test}}'], |
| 311 | + ['Test {{Test}} Test', '{{Test}} {{Test}} {{Test}}'], |
| 312 | + ['laTest news', 'laTest news'], |
| 313 | + ])('preserves named provenance labels under the render policy for %s', (value, expected) => { |
| 314 | + const matcher = build([{ plaintext: 'Test', replacement: '{{Test}}' }], { |
| 315 | + ...PRESERVE_NAMED_PROVENANCE, |
| 316 | + mode: 'render', |
| 317 | + }) |
| 318 | + |
| 319 | + expect(sanitizeResolvedSecretString(value, matcher)).toBe(expected) |
| 320 | + }) |
| 321 | + |
| 322 | + it('keeps the protected-placeholder behaviours under the options production uses', () => { |
| 323 | + const composite = build( |
| 324 | + [ |
| 325 | + { plaintext: 'x{{Test}}y', replacement: '{{COMPOSITE}}' }, |
| 326 | + { plaintext: 'Test', replacement: '{{Test}}' }, |
| 327 | + ], |
| 328 | + { ...PRESERVE_NAMED_PROVENANCE, mode: 'render' } |
| 329 | + ) |
| 330 | + expect(sanitizeResolvedSecretString('x{{Test}}y', composite)).toBe('{{COMPOSITE}}') |
| 331 | + |
| 332 | + const malformed = build([{ plaintext: 'Test', replacement: '{{Test{B}}}' }], { |
| 333 | + ...PRESERVE_NAMED_PROVENANCE, |
| 334 | + mode: 'render', |
| 335 | + }) |
| 336 | + expect(sanitizeResolvedSecretString('Test', malformed)).toBe(OPAQUE_RESOLVED_SECRET_REPLACEMENT) |
| 337 | + |
| 338 | + const chained = build( |
| 339 | + [ |
| 340 | + { plaintext: 'Test', replacement: 'visible-Test' }, |
| 341 | + { plaintext: 'REDACTED', replacement: '{{OTHER}}' }, |
| 342 | + ], |
| 343 | + { mode: 'render' } |
| 344 | + ) |
| 345 | + expect(sanitizeResolvedSecretString('Test', chained)).toBe('') |
| 346 | + }) |
| 347 | + |
| 348 | + it('keeps exact replacement available below the length floor', () => { |
| 349 | + const matcher = build([{ plaintext: '23', replacement: '{{TOKEN}}' }], { mode: 'render' }) |
| 350 | + |
| 351 | + expect(sanitizeResolvedSecretPrimitive('23', matcher)).toBe('{{TOKEN}}') |
| 352 | + expect(sanitizeResolvedSecretString('23', matcher)).toBe('{{TOKEN}}') |
| 353 | + expect(sanitizeResolvedSecretString('123', matcher)).toBe('123') |
| 354 | + }) |
| 355 | + |
| 356 | + it('builds a matcher for an astral-plane literal instead of failing construction', () => { |
| 357 | + const secret = 'k\u{1F600}ey12345' |
| 358 | + const matcher = build([{ plaintext: secret, replacement: '{{EMOJI}}' }], { mode: 'render' }) |
| 359 | + |
| 360 | + expect(sanitizeResolvedSecretString(`token ${secret} end`, matcher)).toBe('token {{EMOJI}} end') |
| 361 | + }) |
| 362 | + |
| 363 | + it('does not rewrite a token interior next to an astral-plane letter', () => { |
| 364 | + const matcher = build(SHORT, { mode: 'render' }) |
| 365 | + |
| 366 | + expect(sanitizeResolvedSecretString('\u{1D400}test\u{1D401}', matcher)).toBe( |
| 367 | + '\u{1D400}test\u{1D401}' |
| 368 | + ) |
| 369 | + }) |
| 370 | +}) |
0 commit comments