11import { redactSensitiveText } from '../command.js' ;
2- import { describe , expect , it } from 'vitest' ;
2+ import { SignJWT } from 'jose' ;
3+ import { beforeAll , describe , expect , it } from 'vitest' ;
4+
5+ const TEST_SIGNING_SECRET = new TextEncoder ( ) . encode ( 'redaction-unit-test-signing-secret-0123456789' ) ;
6+
7+ async function makeJwt ( claims : Record < string , unknown > = { sub : '1234567890' , aud : 'client-abc' } ) : Promise < string > {
8+ return new SignJWT ( claims )
9+ . setProtectedHeader ( { alg : 'HS256' , typ : 'JWT' } )
10+ . setIssuedAt ( )
11+ . setExpirationTime ( '1h' )
12+ . sign ( TEST_SIGNING_SECRET ) ;
13+ }
314
415describe ( 'redactSensitiveText' , ( ) => {
16+ let jwt : string ;
17+
18+ beforeAll ( async ( ) => {
19+ jwt = await makeJwt ( ) ;
20+ } ) ;
21+
522 it ( 'redacts Bearer tokens' , ( ) => {
6- expect ( redactSensitiveText ( 'Authorization: Bearer eyJhbGciOiJSUzI1NiJ9.payload.sig' ) ) . toBe (
7- 'Authorization: Bearer [REDACTED]'
8- ) ;
23+ expect ( redactSensitiveText ( `Authorization: Bearer ${ jwt } ` ) ) . toBe ( 'Authorization: Bearer [REDACTED]' ) ;
924 } ) ;
1025
1126 it ( 'redacts Bearer tokens in JSON' , ( ) => {
12- expect ( redactSensitiveText ( '{"header":"Bearer eyJhbGciOiJSUzI1NiJ9.payload.sig"}' ) ) . toBe (
13- '{"header":"Bearer [REDACTED]"}'
14- ) ;
27+ expect ( redactSensitiveText ( `{"header":"Bearer ${ jwt } "}` ) ) . toBe ( '{"header":"Bearer [REDACTED]"}' ) ;
28+ } ) ;
29+
30+ it ( 'redacts a JWT by shape even without a "bearer"/key prefix' , ( ) => {
31+ expect ( redactSensitiveText ( `agent response: ${ jwt } ` ) ) . toBe ( 'agent response: [REDACTED]' ) ;
1532 } ) ;
1633
1734 it ( 'redacts client_secret in key=value form' , ( ) => {
@@ -38,13 +55,24 @@ describe('redactSensitiveText', () => {
3855 expect ( redactSensitiveText ( 'client-secret=mysecret' ) ) . toBe ( 'client-secret=[REDACTED]' ) ;
3956 } ) ;
4057
58+ it ( 'handles multiple sensitive values in one string' , ( ) => {
59+ expect ( redactSensitiveText ( `Bearer ${ jwt } and client_secret=xyz789` ) ) . toBe (
60+ 'Bearer [REDACTED] and client_secret=[REDACTED]'
61+ ) ;
62+ } ) ;
63+
4164 it ( 'does not modify text without sensitive content' , ( ) => {
4265 const input = 'Agent responded successfully with 200 OK' ;
4366 expect ( redactSensitiveText ( input ) ) . toBe ( input ) ;
4467 } ) ;
4568
46- it ( 'handles multiple sensitive values in one string' , ( ) => {
47- const input = 'Bearer abc123 and client_secret=xyz789' ;
48- expect ( redactSensitiveText ( input ) ) . toBe ( 'Bearer [REDACTED] and client_secret=[REDACTED]' ) ;
69+ it ( 'does not redact the literal word "token" after "bearer"' , ( ) => {
70+ const input = "Agent 'E2eJwt123' is configured for CUSTOM_JWT but no bearer token is available." ;
71+ expect ( redactSensitiveText ( input ) ) . toBe ( input ) ;
72+ } ) ;
73+
74+ it ( 'does not redact prose like "Invalid Bearer Token"' , ( ) => {
75+ const input = 'Invalid Bearer Token' ;
76+ expect ( redactSensitiveText ( input ) ) . toBe ( input ) ;
4977 } ) ;
5078} ) ;
0 commit comments