Skip to content

Commit 4d27d1b

Browse files
fix(providers): strip client-supplied hostedKey so it can't skew hosted-key cost/metrics
1 parent c96a2f8 commit 4d27d1b

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

apps/sim/providers/index.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff 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())

apps/sim/providers/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)