| author | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-10-30 19:42:36 UTC |
| committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-10-30 19:48:09 UTC |
| parent | db7fd60e559212982cc7e152c79884ccf8e63909 |
| server/auth.go | +6 | -10 |
| server/auth_test.go | +65 | -0 |
diff --git a/server/auth.go b/server/auth.go index b08efb4..7e923d3 100644 --- a/server/auth.go +++ b/server/auth.go @@ -3,9 +3,10 @@ package server import ( "context" "crypto/sha256" + "crypto/subtle" "encoding/hex" "fmt" - "math/rand" + "math/rand/v2" "net/http" "os" "strings" @@ -26,13 +27,8 @@ func (a *AuthWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Make sure the call takes authDuration + 0-20% regardless of the // outcome, to prevent basic timing attacks. defer func(start time.Time) { - elapsed := time.Since(start) - delay := authDuration - elapsed - if delay > 0 { - maxDelta := int64(float64(delay) * 0.2) - delay += time.Duration(rand.Int63n(maxDelta)) - time.Sleep(delay) - } + target := authDuration + rand.N(authDuration/5) + time.Sleep(target - time.Since(start)) }(time.Now()) tr, _ := trace.FromContext(r.Context()) @@ -45,7 +41,7 @@ func (a *AuthWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) { } if dbPass, ok := a.users.Plain[user]; ok { - if pass == dbPass { + if subtle.ConstantTimeCompare([]byte(pass), []byte(dbPass)) == 1 { a.success(w, r, user) } else { tr.Printf("incorrect password (plain) for %q", user) @@ -58,7 +54,7 @@ func (a *AuthWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Take the sha256 of the given pass, and compare. buf := sha256.Sum256([]byte(pass)) shaPass := hex.EncodeToString(buf[:]) - if shaPass == dbPass { + if subtle.ConstantTimeCompare([]byte(shaPass), []byte(dbPass)) == 1 { a.success(w, r, user) } else { tr.Printf("incorrect password (sha256) for %q", user) diff --git a/server/auth_test.go b/server/auth_test.go new file mode 100644 index 0000000..6bddf45 --- /dev/null +++ b/server/auth_test.go @@ -0,0 +1,65 @@ +package server + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "blitiri.com.ar/go/gofer/trace" +) + +func TestAuthWrapper(t *testing.T) { + const twoPassSHA = "f1a9cce4acb4460a32413e943554650d0e8916d18f3c41d85eb5bb8e4ee62249" + a := &AuthWrapper{ + handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}), + users: &AuthDB{ + Plain: map[string]string{"oneuser": "onepass"}, + SHA256: map[string]string{"twouser": twoPassSHA}, + }, + } + + cases := []struct { + user, pass string + want int + }{ + {"oneuser", "onepass", http.StatusOK}, + {"oneuser", "onepasx", http.StatusUnauthorized}, + {"oneuser", "onepas", http.StatusUnauthorized}, + {"oneuser", "onepasss", http.StatusUnauthorized}, + {"oneuser", "", http.StatusUnauthorized}, + + {"twouser", "twopass", http.StatusOK}, + {"twouser", "twopasx", http.StatusUnauthorized}, + {"twouser", "", http.StatusUnauthorized}, + // The stored hash is not a valid password. + {"twouser", twoPassSHA, http.StatusUnauthorized}, + + {"unkuser", "onepass", http.StatusUnauthorized}, + } + for _, c := range cases { + req := httptest.NewRequest("GET", "http://unused/", nil) + req = req.WithContext(trace.NewContext(req.Context(), trace.New("test", "/"))) + req.SetBasicAuth(c.user, c.pass) + w := httptest.NewRecorder() + start := time.Now() + a.ServeHTTP(w, req) + if d := time.Since(start); d < authDuration { + t.Errorf("%q:%q: took %v, want at least %v", + c.user, c.pass, d, authDuration) + } + if w.Code != c.want { + t.Errorf("%q:%q: status = %d, want %d", + c.user, c.pass, w.Code, c.want) + } + } + + // No credentials at all. + req := httptest.NewRequest("GET", "http://unused/", nil) + req = req.WithContext(trace.NewContext(req.Context(), trace.New("test", "/"))) + w := httptest.NewRecorder() + a.ServeHTTP(w, req) + if w.Code != http.StatusUnauthorized { + t.Errorf("no credentials: status = %d, want 401", w.Code) + } +}