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.2 KiB
Go
74 lines
2.2 KiB
Go
package catalog
|
||
|
||
import (
|
||
"bytes"
|
||
"context"
|
||
"net/http"
|
||
)
|
||
|
||
// Which header a key goes in, for a vendor that takes one of the two alone
|
||
// (provider.Provider.AuthHeader): "" sends both.
|
||
const (
|
||
AuthXAPIKey = "x-api-key"
|
||
AuthBearer = "bearer"
|
||
)
|
||
|
||
// OneAuthOrder is the order the two headers are tried in alone when a
|
||
// vendor refuses both together: Anthropic's own x-api-key first.
|
||
var OneAuthOrder = []string{AuthXAPIKey, AuthBearer}
|
||
|
||
// BothAuthRefused is a 401 whose body says x-api-key and Authorization
|
||
// can't both be sent (Aloha on Discord: "鉴权 header, x-api-key 和
|
||
// Authorization 不可以同时存在"), in Chinese or English.
|
||
func BothAuthRefused(status int, body []byte) bool {
|
||
if status != http.StatusUnauthorized {
|
||
return false
|
||
}
|
||
b := bytes.ToLower(body)
|
||
if !bytes.Contains(b, []byte("x-api-key")) || !bytes.Contains(b, []byte("authorization")) {
|
||
return false
|
||
}
|
||
for _, s := range []string{
|
||
"同时", "并存", "共存", "只能使用一个", "只能选择一个", "只能有一个",
|
||
"cannot both", "can't both", "can not both", "cannot be both", "not both",
|
||
"together", "at the same time", "simultaneous", "mutually exclusive",
|
||
"only one of", "only one auth", "only one header", "both present", "both be present", "both provided", "both be provided",
|
||
} {
|
||
if bytes.Contains(b, []byte(s)) {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// OneAuth leaves h with the key in the one header only names: x-api-key
|
||
// alone, or Authorization alone.
|
||
func OneAuth(h http.Header, only string) {
|
||
switch only {
|
||
case AuthXAPIKey:
|
||
h.Del("Authorization")
|
||
case AuthBearer:
|
||
h.Del("x-api-key")
|
||
}
|
||
}
|
||
|
||
type listAuthKey struct{}
|
||
|
||
type listAuth struct {
|
||
only string
|
||
learned func(string)
|
||
}
|
||
|
||
// WithListAuth has a model list asked under ctx send its key in the one
|
||
// header only names ("" both), and, sending both, ask again with each
|
||
// alone when the vendor refuses the two together, telling learned the one
|
||
// it took.
|
||
func WithListAuth(ctx context.Context, only string, learned func(string)) context.Context {
|
||
return context.WithValue(ctx, listAuthKey{}, listAuth{only, learned})
|
||
}
|
||
|
||
func listAuthOf(ctx context.Context) listAuth {
|
||
a, _ := ctx.Value(listAuthKey{}).(listAuth)
|
||
return a
|
||
}
|