-
Notifications
You must be signed in to change notification settings - Fork 902
coderd: tighten /login rate limiting #4432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,15 +5,18 @@ import ( | |
"time" | ||
|
||
"github.com/go-chi/httprate" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/coderd/database" | ||
"github.com/coder/coder/coderd/httpapi" | ||
"github.com/coder/coder/coderd/rbac" | ||
"github.com/coder/coder/codersdk" | ||
"github.com/coder/coder/cryptorand" | ||
) | ||
|
||
// RateLimitPerMinute returns a handler that limits requests per-minute based | ||
// RateLimit returns a handler that limits requests per-minute based | ||
// on IP, endpoint, and user ID (if available). | ||
func RateLimitPerMinute(count int) func(http.Handler) http.Handler { | ||
func RateLimit(count int, window time.Duration) func(http.Handler) http.Handler { | ||
// -1 is no rate limit | ||
if count <= 0 { | ||
return func(handler http.Handler) http.Handler { | ||
|
@@ -22,14 +25,37 @@ func RateLimitPerMinute(count int) func(http.Handler) http.Handler { | |
} | ||
return httprate.Limit( | ||
count, | ||
1*time.Minute, | ||
window, | ||
httprate.WithKeyFuncs(func(r *http.Request) (string, error) { | ||
// Prioritize by user, but fallback to IP. | ||
apiKey, ok := r.Context().Value(apiKeyContextKey{}).(database.APIKey) | ||
if ok { | ||
if !ok { | ||
return httprate.KeyByIP(r) | ||
} | ||
|
||
if r.Header.Get(codersdk.BypassRatelimitHeader) == "" { | ||
return apiKey.UserID.String(), nil | ||
} | ||
return httprate.KeyByIP(r) | ||
|
||
// Allow Owner to bypass rate limiting for load tests | ||
// and automation. | ||
auth := UserAuthorization(r) | ||
|
||
for _, role := range auth.Roles { | ||
if role == rbac.RoleOwner() { | ||
// HACK: use a random key each time to | ||
// de facto disable rate limiting. The | ||
// `httprate` package has no | ||
// support for selectively changing the limit | ||
// for particular keys. | ||
return cryptorand.String(16) | ||
} | ||
} | ||
Comment on lines
+50
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another way to do this is add a resource for bypassing the rate limit. Then the FE can actually check if the capability exists, and we can give this to other roles as well. ResourceRateLimitBypass = Object {
Type: "bypass_rate_limit",
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Last I checked Rego was taking 20%+ of test CPU time, so it seems counterproductive the DoS prevention goals of the rate limiter. I added a comment to that effect. |
||
|
||
return apiKey.UserID.String(), xerrors.Errorf( | ||
"%q provided but user is not %v", | ||
codersdk.BypassRatelimitHeader, rbac.RoleOwner(), | ||
) | ||
}, httprate.KeyByEndpoint), | ||
httprate.WithLimitHandler(func(w http.ResponseWriter, r *http.Request) { | ||
httpapi.Write(r.Context(), w, http.StatusTooManyRequests, codersdk.Response{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,9 @@ const ( | |
SessionCustomHeader = "Coder-Session-Token" | ||
OAuth2StateKey = "oauth_state" | ||
OAuth2RedirectKey = "oauth_redirect" | ||
|
||
// nolint: gosec | ||
BypassRatelimitHeader = "X-Coder-Bypass-Ratelimit" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just allow owner to bypass implicitly without needing to set a header? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought it may lead to bugs / misunderstandings with other features that rely on the rate limit. For example, a spinning script. Also, we may not realize if the front end is sending too many requests since we usually test with Owner. |
||
) | ||
|
||
// New creates a Coder client for the provided URL. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you care if the value of the header is
false
? Should youstrconv.ParseBool
?