Prefix matching bypasses key validation
Review this Go API-key middleware that already uses a constant-time comparison.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.
0:00 of about 22 min
Mark a line and say what kind of problem it is.0 findings
1// keyStore maps an 8-char key prefix to the full expected key.
2var keyStore map[string]string
3
4func validKey(provided string) bool {
5 expected, ok := keyStore[provided[:8]]
6 if !ok {
7 return false
8 }
9 return subtle.ConstantTimeCompare([]byte(provided), []byte(expected)) == 1
10}
11
12func authMiddleware(next http.Handler) http.Handler {
13 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
14 key := r.Header.Get("X-Api-Key")
15 if !validKey(key) {
16 http.Error(w, "forbidden", 403)
17 return
18 }
19 next.ServeHTTP(w, r)
20 })
21}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.