Review of 5d5a148ab: it sent every x-bili- header the agent sent to every provider, Claude and Codex accounts and every vendor included. They are the billion-context proxy's own headers, so they now go on only when the request's URL is in bili's prefix form (http://host:8787/bili/<upstream>, the form its README gives for putting it in front of a gateway and the one the report uses), and never for a signed-in account. TestForwardOnceKeepsBiliHeadersFromOthers: a plain URL, a URL with /bili/ deeper in its path, and an account on a /bili/ URL get none. TestForwardOnceCarriesBiliHeaders now posts to a /bili/ URL. Run on mote: go test -run 'TestForwardOnce(Carries|Keeps)BiliHeaders' ./internal/gateway, ok. Broken by hand: the condition set to true, and the new test fails for all three requests. Not tried: a real bili proxy.
110 lines
3.4 KiB
Go
110 lines
3.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// TestForwardOnceCarriesBiliHeaders holds #3052: a billion-context proxy
|
|
// put in front of the provider knows a cooperative plugin's session by the
|
|
// x-bili- headers the agent sent, so forwardOnce copies them on to a URL in
|
|
// bili's /bili/<upstream> form. A header no family allowlists, and the
|
|
// client's own key, still stay off.
|
|
func TestForwardOnceCarriesBiliHeaders(t *testing.T) {
|
|
var mu sync.Mutex
|
|
got := http.Header{}
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
got = r.Header.Clone()
|
|
mu.Unlock()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{}`)
|
|
}))
|
|
defer up.Close()
|
|
|
|
in := http.Header{}
|
|
in.Set("User-Agent", "pi/0.0.56")
|
|
in.Set("x-bili-plugin", "pi")
|
|
in.Set("x-bili-plugin-conversation", "test-123")
|
|
in.Set("x-bili-plugin-context-window", "200000")
|
|
in.Set("X-Custom-Whatever", "never")
|
|
in.Set("Authorization", "Bearer client-key")
|
|
|
|
s := New()
|
|
p := provider.Provider{Responses: up.URL + "/bili/http://127.0.0.1:8317/v1", Key: "provider-key"}
|
|
res, err := s.forwardOnce(context.Background(), p, provider.Responses, "/responses", []byte(`{"model":"gpt-5.5","input":"hi"}`), in)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
res.Body.Close()
|
|
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
for _, h := range []struct{ key, want string }{
|
|
{"X-Bili-Plugin", "pi"},
|
|
{"X-Bili-Plugin-Conversation", "test-123"},
|
|
{"X-Bili-Plugin-Context-Window", "200000"},
|
|
} {
|
|
if v := got.Get(h.key); v != h.want {
|
|
t.Errorf("%s = %q, want %q: the bili proxy reads it to know the cooperative session", h.key, v, h.want)
|
|
}
|
|
}
|
|
if v := got.Get("X-Custom-Whatever"); v != "" {
|
|
t.Errorf("X-Custom-Whatever = %q, want none: only allowlisted families go on", v)
|
|
}
|
|
if v := got.Get("Authorization"); v != "Bearer provider-key" {
|
|
t.Errorf("Authorization = %q, want the provider's key, never the client's", v)
|
|
}
|
|
}
|
|
|
|
// TestForwardOnceKeepsBiliHeadersFromOthers: the same headers never go to
|
|
// a provider that isn't reached through bili's /bili/ prefix, a vendor or a
|
|
// relay, nor to a signed-in account (#3052's review).
|
|
func TestForwardOnceKeepsBiliHeadersFromOthers(t *testing.T) {
|
|
var mu sync.Mutex
|
|
var got []http.Header
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
mu.Lock()
|
|
got = append(got, r.Header.Clone())
|
|
mu.Unlock()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, `{}`)
|
|
}))
|
|
defer up.Close()
|
|
|
|
in := http.Header{}
|
|
in.Set("x-bili-plugin", "pi")
|
|
in.Set("x-bili-plugin-conversation", "test-123")
|
|
|
|
s := New()
|
|
for _, p := range []provider.Provider{
|
|
{Responses: up.URL + "/v1", Key: "provider-key"},
|
|
{Responses: up.URL + "/v1/bili/x", Key: "provider-key"},
|
|
{Responses: up.URL + "/bili/http://127.0.0.1:8317/v1", Account: &provider.Account{}},
|
|
} {
|
|
res, err := s.forwardOnce(context.Background(), p, provider.Responses, "/responses", []byte(`{"model":"gpt-5.5","input":"hi"}`), in)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
res.Body.Close()
|
|
}
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
if len(got) != 3 {
|
|
t.Fatalf("%d requests reached the upstream, want 3", len(got))
|
|
}
|
|
for i, h := range got {
|
|
for k := range h {
|
|
if biliHeader(k) {
|
|
t.Errorf("request %d carried %s; only a /bili/ URL of the user's own provider is sent it", i, k)
|
|
}
|
|
}
|
|
}
|
|
}
|