Skip to content

Commit

Permalink
BED-5264: Defend against time-based enumeration (#1167)
Browse files Browse the repository at this point in the history
* BED-5264: Add arbitrary hash checking to defend against timebased enumeration

* BED-5264: Add arbitrary minimum response time with jitter to Login requests

* BED-5264: Add comment
Remove unnecessary route functionality
  • Loading branch information
wes-mil authored Feb 27, 2025
1 parent c6e2bf8 commit 96db060
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
22 changes: 22 additions & 0 deletions cmd/api/src/api/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package middleware

import (
"fmt"
"math/rand/v2"
"net/http"
"strings"
"time"
Expand Down Expand Up @@ -198,3 +199,24 @@ func AuthorizeAuthManagementAccess(permissions auth.PermissionSet, authorizer au
})
}
}

const loginMinimum = time.Second + 500*time.Millisecond
const loginVariation = 500 * time.Millisecond

// LoginTimer is a middleware to protect against time-based user enumeration on the Login route. It does this by
// starting a timer before the actual login procedure to normalize the duration of this procedure to be within 1.5s and
// 2s.
func LoginTimer() mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
timer := time.NewTimer(loginMinimum + time.Duration(rand.Int64N(loginVariation.Nanoseconds())))

next.ServeHTTP(response, request)

select {
case <-timer.C:
case <-request.Context().Done():
}
})
}
}
3 changes: 2 additions & 1 deletion cmd/api/src/api/registration/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ func registerV2Auth(resources v2.Resources, routerInst *router.Router, permissio
managementResource = authapi.NewManagementResource(resources.Config, resources.DB, resources.Authorizer, resources.Authenticator)
)

routerInst.POST("/api/v2/login", loginResource.Login).Use(middleware.DefaultRateLimitMiddleware(), middleware.LoginTimer())

router.With(middleware.DefaultRateLimitMiddleware,
// Login resources
routerInst.POST("/api/v2/login", loginResource.Login),
routerInst.GET("/api/v2/self", managementResource.GetSelf),
routerInst.POST("/api/v2/logout", loginResource.Logout),

Expand Down

0 comments on commit 96db060

Please sign in to comment.