Просмотр исходного кода

fix(googlereader): harden ClientLogin against username enumeration

Run a dummy bcrypt comparison when no integration matches, as
CheckPassword does, so response time doesn't hint at which usernames
exist. Add tests for the dummy hash cost and ClientLogin timing.
Fred 9 часов назад
Родитель
Сommit
b310b7ddac

+ 48 - 0
internal/googlereader/auth_integration_test.go

@@ -10,6 +10,7 @@ import (
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
+	"time"
 )
 )
 
 
 func TestClientLogin(t *testing.T) {
 func TestClientLogin(t *testing.T) {
@@ -78,6 +79,53 @@ func TestClientLoginRejectsInvalidCredentials(t *testing.T) {
 	}
 	}
 }
 }
 
 
+// ClientLogin must take as long for usernames that cannot log in as for a wrong password, or the response time reveals which usernames exist.
+func TestClientLoginTakesAsLongForUnknownUsernames(t *testing.T) {
+	// Not parallel: Go runs sequential tests while the parallel ones are paused, so no other ClientLogin skews the measurements.
+	user := newTestUser(t)
+	disabledUser := newTestUser(t)
+	setGoogleReaderIntegration(t, disabledUser.id, false, testGoogleReaderPassword)
+
+	anonymous := &googleReaderClient{baseURL: requireTestServer(t).URL}
+
+	// The first scenario is the baseline: the username exists, so the password is compared with its bcrypt hash.
+	scenarios := []struct {
+		name     string
+		username string
+	}{
+		{"wrong password", user.username},
+		{"unknown username", "unknown_googlereader_user"},
+		{"Google Reader disabled", disabledUser.username},
+	}
+
+	// Interleave the scenarios so that load on the machine affects them all alike, and keep the fastest attempt because noise only adds time.
+	fastest := make([]time.Duration, len(scenarios))
+	for attempt := range 5 {
+		for i, scenario := range scenarios {
+			start := time.Now()
+			status, body := anonymous.post(t, "/accounts/ClientLogin", url.Values{
+				"Email":  {scenario.username},
+				"Passwd": {"wrong password"},
+				"output": {"json"},
+			})
+			elapsed := time.Since(start)
+			requireStatus(t, status, body, http.StatusUnauthorized)
+
+			if attempt == 0 || elapsed < fastest[i] {
+				fastest[i] = elapsed
+			}
+		}
+	}
+
+	baseline := fastest[0]
+	for i, scenario := range scenarios[1:] {
+		if elapsed := fastest[i+1]; elapsed < baseline/2 {
+			t.Errorf(`ClientLogin with %s took %v, much less than the %v it took with a wrong password: the response time reveals whether the username exists`,
+				scenario.name, elapsed, baseline)
+		}
+	}
+}
+
 func TestAuthenticationRejectsInvalidTokens(t *testing.T) {
 func TestAuthenticationRejectsInvalidTokens(t *testing.T) {
 	t.Parallel()
 	t.Parallel()
 
 

+ 2 - 0
internal/storage/integration.go

@@ -69,6 +69,8 @@ func (s *Storage) GoogleReaderUserCheckPassword(username, password string) error
 
 
 	err := s.db.QueryRow(query, username).Scan(&hash)
 	err := s.db.QueryRow(query, username).Scan(&hash)
 	if errors.Is(err, sql.ErrNoRows) {
 	if errors.Is(err, sql.ErrNoRows) {
+		// Perform a dummy bcrypt comparison to avoid leaking whether the Google Reader username exists via response timing.
+		_ = bcrypt.CompareHashAndPassword(dummyBcryptHash, []byte(password))
 		return fmt.Errorf(`store: unable to find this user: %s`, username)
 		return fmt.Errorf(`store: unable to find this user: %s`, username)
 	} else if err != nil {
 	} else if err != nil {
 		return fmt.Errorf(`store: unable to fetch user: %v`, err)
 		return fmt.Errorf(`store: unable to fetch user: %v`, err)

+ 33 - 0
internal/storage/user_test.go

@@ -0,0 +1,33 @@
+// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+package storage
+
+import (
+	"testing"
+
+	"golang.org/x/crypto/bcrypt"
+	"miniflux.app/v2/internal/crypto"
+)
+
+// The dummy comparison made for unknown users must cost as much as a real one, or the response time reveals which users exist.
+func TestDummyBcryptHashHasSameCostAsPasswordHashes(t *testing.T) {
+	passwordHash, err := crypto.HashPassword("password")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	expectedCost, err := bcrypt.Cost([]byte(passwordHash))
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	cost, err := bcrypt.Cost(dummyBcryptHash)
+	if err != nil {
+		t.Fatalf(`The dummy hash is not a valid bcrypt hash, so comparing with it returns immediately: %v`, err)
+	}
+
+	if cost != expectedCost {
+		t.Errorf(`The dummy hash has a cost of %d instead of %d, the cost of password hashes`, cost, expectedCost)
+	}
+}