Skip to content

Commit c22925c

Browse files
authored
fix: use constant time in user checks (#1004)
1 parent d946926 commit c22925c

6 files changed

Lines changed: 33 additions & 5 deletions

File tree

‎internal/controller/proxy_controller_test.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ func TestProxyController(t *testing.T) {
708708
Log: log,
709709
})
710710

711-
authService := service.NewAuthService(service.AuthServiceInput{
711+
authService, err := service.NewAuthService(service.AuthServiceInput{
712712
Log: log,
713713
Config: &cfg,
714714
Runtime: &runtime,
@@ -721,6 +721,8 @@ func TestProxyController(t *testing.T) {
721721
PolicyEngine: policyEngine,
722722
})
723723

724+
require.NoError(t, err)
725+
724726
for _, test := range tests {
725727
t.Run(test.description, func(t *testing.T) {
726728
router := gin.Default()

‎internal/controller/user_controller.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func (controller *UserController) loginHandler(c *gin.Context) {
9090

9191
if err != nil {
9292
if errors.Is(err, service.ErrUserNotFound) {
93+
controller.auth.DummyPasswordCheck(req.Password)
9394
controller.log.App.Warn().Str("username", req.Username).Msg("User not found during login attempt")
9495
controller.auth.RecordLoginAttempt(req.Username, false)
9596
controller.log.AuditLoginFailure(req.Username, "unknown", c.ClientIP(), "user not found")

‎internal/controller/user_controller_test.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,8 @@ func TestUserController(t *testing.T) {
542542
Runtime: &runtime,
543543
Ctx: ctx,
544544
})
545-
authService := service.NewAuthService(service.AuthServiceInput{
545+
546+
authService, err := service.NewAuthService(service.AuthServiceInput{
546547
Log: log,
547548
Config: &cfg,
548549
Runtime: &runtime,
@@ -555,6 +556,8 @@ func TestUserController(t *testing.T) {
555556
PolicyEngine: policyEngine,
556557
})
557558

559+
require.NoError(t, err)
560+
558561
beforeEach := func() {
559562
// Clear failed login attempts before each test
560563
authService.ClearLoginAttempts()

‎internal/middleware/context_middleware.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package middleware
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
78
"strings"
@@ -244,6 +245,9 @@ func (m *ContextMiddleware) basicAuth(username string, password string) (*model.
244245
search, err := m.auth.SearchUser(username)
245246

246247
if err != nil {
248+
if errors.Is(err, service.ErrUserNotFound) {
249+
m.auth.DummyPasswordCheck(password)
250+
}
247251
return nil, nil, fmt.Errorf("error searching for user: %w", err)
248252
}
249253

‎internal/middleware/context_middleware_test.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ func TestContextMiddleware(t *testing.T) {
264264
Runtime: &runtime,
265265
Ctx: ctx,
266266
})
267-
authService := service.NewAuthService(service.AuthServiceInput{
267+
268+
authService, err := service.NewAuthService(service.AuthServiceInput{
268269
Log: log,
269270
Config: &cfg,
270271
Runtime: &runtime,
@@ -277,6 +278,8 @@ func TestContextMiddleware(t *testing.T) {
277278
PolicyEngine: policyEngine,
278279
})
279280

281+
require.NoError(t, err)
282+
280283
contextMiddleware := NewContextMiddleware(ContextMiddlewareInput{
281284
Log: log,
282285
RuntimeConfig: &runtime,

‎internal/service/auth_service.go‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ type AuthService struct {
6969
tailscale *TailscaleService
7070
policyEngine *PolicyEngine
7171

72+
dummyHash string
73+
7274
lockdown struct {
7375
active bool
7476
until time.Time
@@ -101,7 +103,7 @@ type AuthServiceInput struct {
101103
PolicyEngine *PolicyEngine
102104
}
103105

104-
func NewAuthService(i AuthServiceInput) *AuthService {
106+
func NewAuthService(i AuthServiceInput) (*AuthService, error) {
105107
service := &AuthService{
106108
log: i.Log,
107109
runtime: i.Runtime,
@@ -123,6 +125,15 @@ func NewAuthService(i AuthServiceInput) *AuthService {
123125
loginCacheSize = service.maxLoginLimits
124126
}
125127

128+
// dummy hash
129+
dummyHash, err := bcrypt.GenerateFromPassword([]byte(utils.GenerateString(8)), bcrypt.DefaultCost)
130+
131+
if err != nil {
132+
return nil, fmt.Errorf("failed to generate dummy hash: %w", err)
133+
}
134+
135+
service.dummyHash = string(dummyHash)
136+
126137
// caches setup
127138
oauthCache := NewCacheStore[OAuthPendingSession](256)
128139
loginCache := NewCacheStore[LoginAttempt](loginCacheSize)
@@ -148,7 +159,11 @@ func NewAuthService(i AuthServiceInput) *AuthService {
148159
}
149160
}, ding.RingMinor)
150161

151-
return service
162+
return service, nil
163+
}
164+
165+
func (auth *AuthService) DummyPasswordCheck(password string) {
166+
bcrypt.CompareHashAndPassword([]byte(auth.dummyHash), []byte(password))
152167
}
153168

154169
func (auth *AuthService) SearchUser(username string) (*model.UserSearch, error) {

0 commit comments

Comments
 (0)