@@ -45,7 +45,14 @@ import { AutomationServicePlugin, type CredentialResolver } from './plugin.js';
4545import type { AutomationEngine } from './engine.js' ;
4646import { SysConnectorCredential } from './sys-connector-credential.object.js' ;
4747import { SysFlowCredential } from './sys-flow-credential.object.js' ;
48- import { CONNECTOR_CREDENTIAL_OBJECT , ConnectorCredentialStore } from './connector-credentials.js' ;
48+ import {
49+ CONNECTOR_CREDENTIAL_OBJECT ,
50+ ConnectorCredentialStore ,
51+ credentialForms ,
52+ upstreamRefusalMessage ,
53+ withoutCredential ,
54+ withoutCredentialThrown ,
55+ } from './connector-credentials.js' ;
4956
5057const REF = 'DUMMY_BILLING_TOKEN_27237' ;
5158const DUMMY_VALUE = 'dummy-value-aaaa-0001' ;
@@ -610,3 +617,69 @@ describe('[#27237] seat review r1: under a walled posture the store is not in th
610617 expect ( ( await engine . listConnectorCredentialRefs ( ) ) [ 0 ] ) . toMatchObject ( { source : 'env' , status : 'set' , writable : false } ) ;
611618 } ) ;
612619} ) ;
620+
621+ describe ( '[#27237] contract review: an encoded spelling of the value is withheld too' , ( ) => {
622+ const USERNAME = 'svc_dummy_27237' ;
623+ // Characters that URL-encode, so the encoded spelling differs from the raw one.
624+ const ENCODABLE = 'dummy/value+eeee 0010' ;
625+ const b64 = ( text : string ) => Buffer . from ( text , 'utf8' ) . toString ( 'base64' ) ;
626+
627+ it ( 'lists the raw, URL-encoded and base64 spellings, and base64(`user:value`) for basic auth' , ( ) => {
628+ const forms = credentialForms ( ENCODABLE , USERNAME ) ;
629+ expect ( forms ) . toContain ( ENCODABLE ) ;
630+ expect ( forms ) . toContain ( encodeURIComponent ( ENCODABLE ) ) ;
631+ expect ( forms ) . toContain ( b64 ( ENCODABLE ) . replace ( / = + $ / , '' ) ) ;
632+ expect ( forms ) . toContain ( b64 ( `${ USERNAME } :${ ENCODABLE } ` ) . replace ( / = + $ / , '' ) ) ;
633+ const echoed = `Authorization: Basic ${ b64 ( `${ USERNAME } :${ ENCODABLE } ` ) } url ?key=${ encodeURIComponent ( ENCODABLE ) } ` ;
634+ const scrubbed = withoutCredential ( echoed , forms ) ;
635+ for ( const form of forms ) expect ( scrubbed . includes ( form ) , form . length . toString ( ) ) . toBe ( false ) ;
636+ expect ( scrubbed ) . toContain ( '[credential withheld]' ) ;
637+ const thrown = withoutCredentialThrown ( Object . assign ( new Error ( 'outer' ) , { cause : new Error ( echoed ) } ) , forms ) as Error ;
638+ expect ( String ( thrown . message ) ) . toBe ( 'outer' ) ;
639+ expect ( ( thrown as { cause ?: unknown } ) . cause ) . toBeUndefined ( ) ;
640+ expect ( upstreamRefusalMessage ( { message : echoed } , forms ) ) . not . toContain ( encodeURIComponent ( ENCODABLE ) ) ;
641+ } ) ;
642+
643+ it ( 'a basic-auth instance whose upstream echoes the encoded header and URL: no spelling reaches the reason, the row or a log' , async ( ) => {
644+ const out = captureOutput ( ) ;
645+ const header = b64 ( `${ USERNAME } :${ ENCODABLE } ` ) ;
646+ const provider = fakeProvider ( ) ;
647+ const factory : ConnectorProviderFactory = ( ctx ) => {
648+ provider . state . materialized . push ( ctx ) ;
649+ const password = ctx . auth ?. type === 'basic' ? ctx . auth . password : '' ;
650+ const echo = `sent Authorization: Basic ${ b64 ( `${ USERNAME } :${ password } ` ) } to https://erp.example/?key=${ encodeURIComponent ( password ) } ` ;
651+ if ( provider . state . rejectAtMaterialization ) throw new ConnectorCredentialRejectedError ( `refused (HTTP 401): ${ echo } ` , 401 ) ;
652+ return {
653+ def : { name : ctx . name , label : ctx . label , type : 'api' , actions : [ { key : 'request' , label : 'Request' } ] } as unknown as Connector ,
654+ handlers : { request : async ( ) => ( { status : 401 , ok : false , body : { message : echo } } ) } ,
655+ } ;
656+ } ;
657+ const erp = { ...billing ( 'erp' ) , auth : { type : 'basic' , username : USERNAME , credentialRef : REF } } ;
658+ const { engine, call } = await boot ( [ erp ] , { provider : { ...provider , factory } } ) ;
659+
660+ // On a call (the rest / openapi shape).
661+ await engine . setConnectorCredential ( { ref : REF , value : ENCODABLE , context : ADMIN } ) ;
662+ expect ( await call ( 'erp' ) ) . toMatchObject ( { status : 401 } ) ;
663+ const onCall = engine . getConnectorDegradedReason ( 'erp' ) ?? '' ;
664+ expect ( onCall ) . toContain ( '[credential withheld]' ) ;
665+ const listedOnCall = JSON . stringify ( await engine . listConnectorCredentialRefs ( ) ) ;
666+
667+ // At materialization (the mcp shape).
668+ provider . state . rejectAtMaterialization = true ;
669+ await engine . setConnectorCredential ( { ref : REF , value : DUMMY_VALUE_2 , context : ADMIN } ) ;
670+ const atMaterialization = engine . getConnectorDegradedReason ( 'erp' ) ?? '' ;
671+ expect ( atMaterialization ) . toContain ( '[credential withheld]' ) ;
672+
673+ for ( const [ where , text ] of [
674+ [ 'reason on a call' , onCall ] ,
675+ [ 'listing after the call' , listedOnCall ] ,
676+ [ 'reason at materialization' , atMaterialization ] ,
677+ [ 'listing after materialization' , JSON . stringify ( await engine . listConnectorCredentialRefs ( ) ) ] ,
678+ [ 'logs' , out . text ( ) ] ,
679+ ] as const ) {
680+ for ( const form of [ header , encodeURIComponent ( ENCODABLE ) , ENCODABLE , b64 ( `${ USERNAME } :${ DUMMY_VALUE_2 } ` ) . replace ( / = + $ / , '' ) ] ) {
681+ expect ( text . includes ( form ) , `${ where } : a spelling of the value` ) . toBe ( false ) ;
682+ }
683+ }
684+ } ) ;
685+ } ) ;
0 commit comments