akashyadav758 Claude Opus 4.7 (1M context) commited on
Commit
23bf5e5
·
1 Parent(s): 5c7f4b5

Fix gateway auth on private Space: check ?key=/cookie before Authorization

Browse files

A private HF Space reserves the Authorization: Bearer header for its own HF
token, which collided with the gateway reading the API_KEY from the same header
(deadlock: send HF token -> "unauthorized"; send API_KEY -> HF 404). Reorder
authOK() to read ?key= and the apikey cookie first, falling back to
Authorization only when both are absent. Backward-compatible for public Spaces.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

Files changed (1) hide show
  1. monitor/main.go +8 -4
monitor/main.go CHANGED
@@ -542,15 +542,19 @@ func authOK(w http.ResponseWriter, r *http.Request) bool {
542
  http.Error(w, "gateway disabled: API_KEY not set on the server", http.StatusServiceUnavailable)
543
  return false
544
  }
545
- got := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
546
- if got == "" {
547
- got = r.URL.Query().Get("key")
548
- }
 
549
  if got == "" {
550
  if c, err := r.Cookie("apikey"); err == nil {
551
  got = c.Value
552
  }
553
  }
 
 
 
554
  if got != apiKey {
555
  http.Error(w, "unauthorized", http.StatusUnauthorized)
556
  return false
 
542
  http.Error(w, "gateway disabled: API_KEY not set on the server", http.StatusServiceUnavailable)
543
  return false
544
  }
545
+ // Check ?key= and the apikey cookie BEFORE the Authorization header. On a
546
+ // private HF Space the platform claims Authorization: Bearer <HF_token>, so the
547
+ // API key must travel via ?key= or the cookie; only fall back to Authorization
548
+ // (public Space / OpenAI-compatible clients) when those are absent.
549
+ got := r.URL.Query().Get("key")
550
  if got == "" {
551
  if c, err := r.Cookie("apikey"); err == nil {
552
  got = c.Value
553
  }
554
  }
555
+ if got == "" {
556
+ got = strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
557
+ }
558
  if got != apiKey {
559
  http.Error(w, "unauthorized", http.StatusUnauthorized)
560
  return false