File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -85,6 +85,22 @@ describe('executeProviderRequest — BYOK regression', () => {
8585 expect ( result . cost ?. output ) . toBe ( 0 )
8686 } )
8787
88+ it ( 'strips a client-supplied hostedKey so it cannot skew streaming cost/metrics' , async ( ) => {
89+ // User-provided key path: no platform pool key acquired (no hostedKeyEnvVar).
90+ mockGetApiKeyWithBYOK . mockResolvedValue ( { apiKey : 'sk-user' , isBYOK : false } )
91+ mockExecuteRequest . mockResolvedValue ( makeAnthropicResponse ( ) )
92+
93+ await executeProviderRequest ( 'anthropic' , {
94+ model : 'claude-opus-4-6' ,
95+ workspaceId : 'ws-1' ,
96+ // Untrusted, client-supplied — must not reach the provider/streaming settlement.
97+ hostedKey : { provider : 'anthropic' , envVar : 'ANTHROPIC_API_KEY_1' } ,
98+ } )
99+
100+ const passedRequest = mockExecuteRequest . mock . calls [ 0 ] [ 0 ] as { hostedKey ?: unknown }
101+ expect ( passedRequest . hostedKey ) . toBeUndefined ( )
102+ } )
103+
88104 it ( 'zeroes per-segment model cost for BYOK callers so trace aggregation does not re-charge' , async ( ) => {
89105 mockGetApiKeyWithBYOK . mockResolvedValue ( { apiKey : 'sk-byok' , isBYOK : true } )
90106 mockExecuteRequest . mockResolvedValue ( makeAnthropicResponse ( ) )
Original file line number Diff line number Diff line change @@ -36,6 +36,11 @@ function sanitizeRequest(request: ProviderRequest): ProviderRequest {
3636 const sanitizedRequest = { ...request }
3737 const model = sanitizedRequest . model
3838
39+ // `hostedKey` is server-only: strip any client-supplied value so it can never
40+ // be trusted. executeProviderRequest sets it solely when it acquires a platform
41+ // pool key, which gates streaming cost settlement / hosted-key metrics.
42+ sanitizedRequest . hostedKey = undefined
43+
3944 if ( model && ! supportsTemperature ( model ) ) {
4045 sanitizedRequest . temperature = undefined
4146 }
You can’t perform that action at this time.
0 commit comments