File size: 2,903 Bytes
fde13cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
--- a/internal/api/handlers/management/handler.go
+++ b/internal/api/handlers/management/handler.go
@@ -201,21 +201,35 @@
 			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()
 			}
@@ -223,16 +237,31 @@
 			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 {
@@ -245,13 +274,12 @@
 			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()