Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/transform/secrets/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,8 @@ func (s *Secrets) swapBody(req *http.Request, sec *resolvedSecret, realValue str
}

if !bytes.Contains(data, []byte(sec.proxyValue)) {
// Put the body back so the next body-matching secret can read it.
req.Body = transform.NewBufferedBodyFromBytes(data)
return ""
}

Expand Down
25 changes: 25 additions & 0 deletions internal/transform/secrets/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,31 @@ func TestSecrets_BodySwap(t *testing.T) {
require.Contains(t, string(result), `"model": "gpt-4"`)
}

func TestSecrets_BodySwapAfterAbsentBodySecret(t *testing.T) {
// The first body secret's placeholder is not in the request.
s := makeSecrets(t, []secretEntry{
defaultEntry(func(e *secretEntry) {
e.Source = envSource("ANTHROPIC_API_KEY")
e.ProxyValue = "proxy-anthropic-xyz789"
e.MatchHeaders = nil
e.MatchBody = true
}),
defaultEntry(func(e *secretEntry) {
e.MatchHeaders = nil
e.MatchBody = true
}),
})

req := openaiReq("POST", "/v1/chat")
req.Body = transform.NewBufferedBody(io.NopCloser(strings.NewReader(`{"api_key": "proxy-openai-abc123"}`)), 1<<20)

doTransform(t, s, req)

result, err := io.ReadAll(req.Body)
require.NoError(t, err)
require.Equal(t, `{"api_key": "sk-real-openai-key"}`, string(result))
}

func TestSecrets_HostMatch(t *testing.T) {
s := makeSecrets(t, []secretEntry{defaultEntry()})

Expand Down