Spaces:
Paused
Paused
Download auth_fix.patch from sarkyboy/client2api: direct link, hf CLI and curl.
- Browser
- Download file 2.9 kB
-
https://huggingface.co/spaces/sarkyboy/client2api/resolve/main/auth_fix.patch
- Command line
-
hf download hf://spaces/sarkyboy/client2api/auth_fix.patch
-
curl -L -o auth_fix.patch https://huggingface.co/spaces/sarkyboy/client2api/resolve/main/auth_fix.patch
2.9 kB
| --- a/internal/api/handlers/management/handler.go | |
| +++ b/internal/api/handlers/management/handler.go | |
| return | |
| } | |
| - // Accept either Authorization: Bearer <key> or X-Management-Key | |
| - var provided string | |
| + // DEBUG: Log Headers related to auth | |
| + authHeader := c.GetHeader("Authorization") | |
| + xKeyHeader := c.GetHeader("X-Management-Key") | |
| + fmt.Printf("[DEBUG-AUTH] %s: AuthHeaderLen=%d, XKeyHeaderLen=%d\n", clientIP, len(authHeader), len(xKeyHeader)) | |
| + | |
| + // Collect all possible keys from Headers and Query | |
| + var candidates []string | |
| + | |
| + // 1. Authorization: Bearer <key> | |
| if ah := c.GetHeader("Authorization"); ah != "" { | |
| parts := strings.SplitN(ah, " ", 2) | |
| if len(parts) == 2 && strings.ToLower(parts[0]) == "bearer" { | |
| - provided = parts[1] | |
| + candidates = append(candidates, parts[1]) | |
| } else { | |
| - provided = ah | |
| + candidates = append(candidates, ah) | |
| } | |
| } | |
| - if provided == "" { | |
| - provided = c.GetHeader("X-Management-Key") | |
| + | |
| + // 2. X-Management-Key header | |
| + if xk := c.GetHeader("X-Management-Key"); xk != "" { | |
| + candidates = append(candidates, xk) | |
| } | |
| - if provided == "" { | |
| + // 3. Query parameter "key" (fallback for browser/curl usage) | |
| + if qk := c.Query("key"); qk != "" { | |
| + candidates = append(candidates, qk) | |
| + } | |
| + | |
| + if len(candidates) == 0 { | |
| if !localClient { | |
| fail() | |
| } | |
| return | |
| } | |
| - if localClient { | |
| - if lp := h.localPassword; lp != "" { | |
| - if subtle.ConstantTimeCompare([]byte(provided), []byte(lp)) == 1 { | |
| - c.Next() | |
| - return | |
| + // Validate against all candidates | |
| + authenticated := false | |
| + | |
| + // 1. Check Env Secret match | |
| + if envSecret != "" { | |
| + for _, provided := range candidates { | |
| + if subtle.ConstantTimeCompare([]byte(provided), []byte(envSecret)) == 1 { | |
| + authenticated = true | |
| + break | |
| } | |
| } | |
| } | |
| - if envSecret != "" && subtle.ConstantTimeCompare([]byte(provided), []byte(envSecret)) == 1 { | |
| + // 2. Check Hash match (if Env didn't match) | |
| + if !authenticated && secretHash != "" { | |
| + for _, provided := range candidates { | |
| + err := bcrypt.CompareHashAndPassword([]byte(secretHash), []byte(provided)) | |
| + if err == nil { | |
| + authenticated = true | |
| + break | |
| + } | |
| + } | |
| + } | |
| + | |
| + if authenticated { | |
| if !localClient { | |
| h.attemptsMu.Lock() | |
| if ai := h.failedAttempts[clientIP]; ai != nil { | |
| return | |
| } | |
| - if secretHash == "" || bcrypt.CompareHashAndPassword([]byte(secretHash), []byte(provided)) != nil { | |
| - if !localClient { | |
| - fail() | |
| - } | |
| - c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid management key"}) | |
| - return | |
| + // If we get here, all candidates failed | |
| + if !localClient { | |
| + fail() | |
| } | |
| + c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid management key"}) | |
| + return | |
| if !localClient { | |
| h.attemptsMu.Lock() | |