-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: configurable email and sms rate limiting (#1800)
Adds two new configuration values for rate limiting the sending of emails and sms messages: - GOTRUE_RATE_LIMIT_EMAIL_SENT - GOTRUE_RATE_LIMIT_SMS_SENT It is implemented with a simple rate limiter that resets a counter at a regular interval. The first intervals start time is set when the counter is initialized. It will be reset when the server is restarted, but preserved when the config is reloaded. Syntax examples: ``` 1.5 # Allow 1.5 events over 1 hour (legacy format) 100 # Allow 100 events over 1 hour (1h is default) 100/1h # Allow 100 events over 1 hour (explicit duration) 100/24h # Allow 100 events over 24 hours 100/72h # Allow 100 events over 72 hours (use hours for days) 10/30m # Allow 10 events over 30 minutes 3/10s # Allow 3 events over 10 seconds ``` Syntax in ABNF to express the format as value: ``` value = count / rate count = 1*DIGIT ["." 1*DIGIT] rate = 1*DIGIT "/" ival ival = ival-sec / ival-min / ival-hr ival-sec = 1*DIGIT "s" ival-min = 1*DIGIT "s" ival-hr = 1*DIGIT "h" ``` This change was a continuation of #1746 adapted to support the recent preservation of rate limiters across server reloads. --------- Co-authored-by: Chris Stockton <[email protected]> Co-authored-by: Stojan Dimitrovski <[email protected]>
- Loading branch information
1 parent
8cc2f0e
commit 5e94047
Showing
12 changed files
with
292 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,52 +185,6 @@ func (ts *MiddlewareTestSuite) TestVerifyCaptchaInvalid() { | |
} | ||
} | ||
|
||
func (ts *MiddlewareTestSuite) TestLimitEmailOrPhoneSentHandler() { | ||
// Set up rate limit config for this test | ||
ts.Config.RateLimitEmailSent = 5 | ||
ts.Config.RateLimitSmsSent = 5 | ||
ts.Config.External.Phone.Enabled = true | ||
|
||
cases := []struct { | ||
desc string | ||
expectedErrorMsg string | ||
requestBody map[string]interface{} | ||
}{ | ||
{ | ||
desc: "Email rate limit exceeded", | ||
expectedErrorMsg: "429: Email rate limit exceeded", | ||
requestBody: map[string]interface{}{ | ||
"email": "[email protected]", | ||
}, | ||
}, | ||
{ | ||
desc: "SMS rate limit exceeded", | ||
expectedErrorMsg: "429: SMS rate limit exceeded", | ||
requestBody: map[string]interface{}{ | ||
"phone": "+1233456789", | ||
}, | ||
}, | ||
} | ||
|
||
limiter := ts.API.limitEmailOrPhoneSentHandler(NewLimiterOptions(ts.Config)) | ||
for _, c := range cases { | ||
ts.Run(c.desc, func() { | ||
var buffer bytes.Buffer | ||
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(c.requestBody)) | ||
req := httptest.NewRequest(http.MethodPost, "http://localhost", &buffer) | ||
req.Header.Set("Content-Type", "application/json") | ||
w := httptest.NewRecorder() | ||
|
||
ctx, err := limiter(w, req) | ||
require.NoError(ts.T(), err) | ||
|
||
// check that shared limiter is set in the request context | ||
sharedLimiter := getLimiter(ctx) | ||
require.NotNil(ts.T(), sharedLimiter) | ||
}) | ||
} | ||
} | ||
|
||
func (ts *MiddlewareTestSuite) TestIsValidExternalHost() { | ||
cases := []struct { | ||
desc string | ||
|
@@ -388,134 +342,6 @@ func (ts *MiddlewareTestSuite) TestLimitHandler() { | |
require.Equal(ts.T(), http.StatusTooManyRequests, w.Code) | ||
} | ||
|
||
func (ts *MiddlewareTestSuite) TestLimitHandlerWithSharedLimiter() { | ||
// setup config for shared limiter and ip-based limiter to work | ||
ts.Config.RateLimitHeader = "X-Rate-Limit" | ||
ts.Config.External.Email.Enabled = true | ||
ts.Config.External.Phone.Enabled = true | ||
ts.Config.Mailer.Autoconfirm = false | ||
ts.Config.Sms.Autoconfirm = false | ||
|
||
ipBasedLimiter := func(max float64) *limiter.Limiter { | ||
return tollbooth.NewLimiter(max, &limiter.ExpirableOptions{ | ||
DefaultExpirationTTL: time.Hour, | ||
}) | ||
} | ||
|
||
okHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
limiter := getLimiter(r.Context()) | ||
if limiter != nil { | ||
var requestBody struct { | ||
Email string `json:"email"` | ||
Phone string `json:"phone"` | ||
} | ||
err := retrieveRequestParams(r, &requestBody) | ||
require.NoError(ts.T(), err) | ||
|
||
if requestBody.Email != "" { | ||
if err := tollbooth.LimitByKeys(limiter.EmailLimiter, []string{"email_functions"}); err != nil { | ||
sendJSON(w, http.StatusTooManyRequests, HTTPError{ | ||
HTTPStatus: http.StatusTooManyRequests, | ||
ErrorCode: ErrorCodeOverEmailSendRateLimit, | ||
Message: "Email rate limit exceeded", | ||
}) | ||
} | ||
} | ||
if requestBody.Phone != "" { | ||
if err := tollbooth.LimitByKeys(limiter.EmailLimiter, []string{"phone_functions"}); err != nil { | ||
sendJSON(w, http.StatusTooManyRequests, HTTPError{ | ||
HTTPStatus: http.StatusTooManyRequests, | ||
ErrorCode: ErrorCodeOverSMSSendRateLimit, | ||
Message: "SMS rate limit exceeded", | ||
}) | ||
} | ||
} | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
}) | ||
|
||
cases := []struct { | ||
desc string | ||
sharedLimiterConfig *conf.GlobalConfiguration | ||
ipBasedLimiterConfig float64 | ||
body map[string]interface{} | ||
expectedErrorCode string | ||
}{ | ||
{ | ||
desc: "Exceed ip-based rate limit before shared limiter", | ||
sharedLimiterConfig: &conf.GlobalConfiguration{ | ||
RateLimitEmailSent: 10, | ||
RateLimitSmsSent: 10, | ||
}, | ||
ipBasedLimiterConfig: 1, | ||
body: map[string]interface{}{ | ||
"email": "[email protected]", | ||
}, | ||
expectedErrorCode: ErrorCodeOverRequestRateLimit, | ||
}, | ||
{ | ||
desc: "Exceed email shared limiter", | ||
sharedLimiterConfig: &conf.GlobalConfiguration{ | ||
RateLimitEmailSent: 1, | ||
RateLimitSmsSent: 1, | ||
}, | ||
ipBasedLimiterConfig: 10, | ||
body: map[string]interface{}{ | ||
"email": "[email protected]", | ||
}, | ||
expectedErrorCode: ErrorCodeOverEmailSendRateLimit, | ||
}, | ||
{ | ||
desc: "Exceed sms shared limiter", | ||
sharedLimiterConfig: &conf.GlobalConfiguration{ | ||
RateLimitEmailSent: 1, | ||
RateLimitSmsSent: 1, | ||
}, | ||
ipBasedLimiterConfig: 10, | ||
body: map[string]interface{}{ | ||
"phone": "123456789", | ||
}, | ||
expectedErrorCode: ErrorCodeOverSMSSendRateLimit, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
ts.Run(c.desc, func() { | ||
ts.Config.RateLimitEmailSent = c.sharedLimiterConfig.RateLimitEmailSent | ||
ts.Config.RateLimitSmsSent = c.sharedLimiterConfig.RateLimitSmsSent | ||
lmt := ts.API.limitHandler(ipBasedLimiter(c.ipBasedLimiterConfig)) | ||
sharedLimiter := ts.API.limitEmailOrPhoneSentHandler(NewLimiterOptions(ts.Config)) | ||
|
||
// get the minimum amount to reach the threshold just before the rate limit is exceeded | ||
threshold := min(c.sharedLimiterConfig.RateLimitEmailSent, c.sharedLimiterConfig.RateLimitSmsSent, c.ipBasedLimiterConfig) | ||
for i := 0; i < int(threshold); i++ { | ||
var buffer bytes.Buffer | ||
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(c.body)) | ||
req := httptest.NewRequest(http.MethodPost, "http://localhost", &buffer) | ||
req.Header.Add(ts.Config.RateLimitHeader, "0.0.0.0") | ||
|
||
w := httptest.NewRecorder() | ||
lmt.handler(sharedLimiter.handler(okHandler)).ServeHTTP(w, req) | ||
require.Equal(ts.T(), http.StatusOK, w.Code) | ||
} | ||
|
||
var buffer bytes.Buffer | ||
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(c.body)) | ||
req := httptest.NewRequest(http.MethodPost, "http://localhost", &buffer) | ||
req.Header.Add(ts.Config.RateLimitHeader, "0.0.0.0") | ||
|
||
// check if the rate limit is exceeded with the expected error code | ||
w := httptest.NewRecorder() | ||
lmt.handler(sharedLimiter.handler(okHandler)).ServeHTTP(w, req) | ||
require.Equal(ts.T(), http.StatusTooManyRequests, w.Code) | ||
|
||
var data map[string]interface{} | ||
require.NoError(ts.T(), json.NewDecoder(w.Body).Decode(&data)) | ||
require.Equal(ts.T(), c.expectedErrorCode, data["error_code"]) | ||
}) | ||
} | ||
} | ||
|
||
func (ts *MiddlewareTestSuite) TestIsValidAuthorizedEmail() { | ||
ts.API.config.External.Email.AuthorizedAddresses = []string{"[email protected]"} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.