magpie sends a user's own Anthropic-compatible provider its key in both
x-api-key and Authorization: Bearer. Some relays answer 401 "鉴权 header,
x-api-key 和 Authorization 不可以同时存在", so every request failed.
- Provider.AuthHeader ("" both, "x-api-key", "bearer") picks which
AuthHeaders sends on the Anthropic protocol. Anthropic's own API,
Bedrock, Azure, Vertex and subscription accounts are untouched: they
send x-api-key alone (or their own) whatever it says, and BothAuth()
is false for them.
- Left at both, a 401 whose body catalog.BothAuthRefused tells as that
refusal (names both headers and says they can't be together, Chinese or
English) is asked once more with x-api-key alone, then Bearer alone
(RetryOneAuth, in the gateway's forwardOnce and the provider test's
probe; the model-list fetch through catalog.fetchOne too). The first not
answered 401/403 is saved (LearnAuthHeader), never over a pick the user
made. A request carrying the user's own Authorization/x-api-key header
is never retried; any other 401 is passed on as it came, asked once.
- The editor has a "Key header" row (Both / x-api-key /
Authorization: Bearer, the app's segmented control) while the provider
has a third-party Anthropic URL; strings in zh, zh-TW, ja, de.
- docs/subsystems/providers-accounts.md: an "Anthropic key header" row.
Run (sandbox HOME, GOCACHE/GOMODCACHE pinned, set -o pipefail):
- gofmt -l; go vet ./...; GOOS=linux go vet -tags nogui ./...;
GOOS=windows go vet ./...; GOOS=linux/windows go build.
- go test -tags nogui ./...: 47 packages ok; agent and gateway timed out
at 10m under load average ~50-98 (other sessions' test runs), so rerun
with -p 1 -timeout 60m: agent ok; gateway ok but for
TestOTelWaterfallIncludesUnbilledFallback, a load flake unrelated to
this (it reads only the first 200ms OTel export; passes 5/5 alone),
filed as #1794.
- New tests: internal/provider/authheader_test.go (official Anthropic and
Bedrock unchanged for every pick; Aloha's 401 body retried with
x-api-key and kept; Bearer-only relay learns bearer; the model list
learns; another 401 and the user's own header aren't retried; a user's
pick isn't overwritten), internal/gateway/authheader_test.go (a
/v1/messages turn through the gateway to such a relay succeeds and the
next Chat Completions turn goes with one header first).
- Broken by hand: RetryOneAuth returning at once and AuthHeaders ignoring
the pick -> TestAuthHeaderChoice, TestRetryOneAuthLearns,
TestRetryOneAuthBearer, TestOneAuthHeaderRelay FAIL; the list retry off
in live.go -> TestListLearnsOneAuth FAIL.
- GUI: provider-auth-header.test.cjs 20/20 in chromium and webkit, en and
zh, 390px wide; the editor's other tests (provider-*, nokey-editor,
editor-short-window, segs-*, vertex-editor) 439/440, the one
(provider-discovery, chromium zh) passing 26/26 alone.
Not tried: a real relay of Aloha's; the retry is built from the body the
user quoted.
74 lines
2.4 KiB
Go
74 lines
2.4 KiB
Go
package gateway
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
"io"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"sync"
|
||
"testing"
|
||
|
||
"github.com/yetone/magpie/internal/provider"
|
||
)
|
||
|
||
// Aloha on Discord: a relay answering 401 "鉴权 header, x-api-key 和
|
||
// Authorization 不可以同时存在" to the two key headers magpie sends a
|
||
// third-party Anthropic API. The agent's turn goes through: asked again with
|
||
// x-api-key alone, the same body, and the provider keeps x-api-key, so the
|
||
// next turn is sent one header from the first (Anthropic's own API is
|
||
// TestAuthHeaderChoice's, in internal/provider).
|
||
func TestOneAuthHeaderRelay(t *testing.T) {
|
||
fresh(t)
|
||
var mu sync.Mutex
|
||
type ask struct{ x, a, model string }
|
||
var asked []ask
|
||
up := &adaptiveVendor{}
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
var b struct {
|
||
Model string `json:"model"`
|
||
}
|
||
raw, _ := io.ReadAll(r.Body)
|
||
json.Unmarshal(raw, &b)
|
||
x, a := r.Header.Get("x-api-key"), r.Header.Get("Authorization")
|
||
mu.Lock()
|
||
asked = append(asked, ask{x, a, b.Model})
|
||
mu.Unlock()
|
||
if x != "" && a != "" {
|
||
w.WriteHeader(http.StatusUnauthorized)
|
||
io.WriteString(w, `{"error":{"type":"authentication_error","message":"鉴权 header, x-api-key 和 Authorization 不可以同时存在"}}`)
|
||
return
|
||
}
|
||
r.Body = io.NopCloser(bytes.NewReader(raw))
|
||
up.ServeHTTP(w, r)
|
||
}))
|
||
t.Cleanup(srv.Close)
|
||
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Anthropic: srv.URL, Models: []string{"claude-sonnet-4-5"}}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
code, body := post(t, "/v1/messages", `{"model":"relay/claude-sonnet-4-5","max_tokens":100,"messages":[{"role":"user","content":"hi"}]}`)
|
||
if code != 200 {
|
||
t.Fatalf("status %d: %s", code, body)
|
||
}
|
||
mu.Lock()
|
||
got := append([]ask(nil), asked...)
|
||
mu.Unlock()
|
||
if len(got) != 2 || got[0].a == "" || got[1] != (ask{"k", "", "claude-sonnet-4-5"}) {
|
||
t.Fatalf("asked %+v", got)
|
||
}
|
||
if p, _ := provider.Find("relay"); p.AuthHeader != provider.AuthXAPIKey {
|
||
t.Fatalf("kept %q", p.AuthHeader)
|
||
}
|
||
// the next turn, from Chat Completions too, goes with one header first
|
||
code, body = post(t, "/v1/chat/completions", `{"model":"relay/claude-sonnet-4-5","messages":[{"role":"user","content":"hi"}]}`)
|
||
if code != 200 {
|
||
t.Fatalf("status %d: %s", code, body)
|
||
}
|
||
mu.Lock()
|
||
got = append([]ask(nil), asked[2:]...)
|
||
mu.Unlock()
|
||
if len(got) != 1 || got[0].x != "k" || got[0].a != "" {
|
||
t.Errorf("next turn asked %+v", got)
|
||
}
|
||
}
|