Skip to content

Commit cb7894e

Browse files
cstocktonChris Stockton
and
Chris Stockton
authored
fix: rate limits of 0 take precedence over MAILER_AUTO_CONFIRM (#1837)
This does not fix lower restrictions from being bypassed, but does help in the case the rate limit is explicitly set to 0. Co-authored-by: Chris Stockton <[email protected]>
1 parent 9ce2857 commit cb7894e

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

internal/api/mail.go

+10
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,16 @@ func (a *API) sendEmail(r *http.Request, tx *storage.Connection, u *models.User,
627627
}
628628
}
629629

630+
// if the number of events is set to zero, we immediately apply rate limits.
631+
if config.RateLimitEmailSent.Events == 0 {
632+
emailRateLimitCounter.Add(
633+
ctx,
634+
1,
635+
metric.WithAttributeSet(attribute.NewSet(attribute.String("path", r.URL.Path))),
636+
)
637+
return EmailRateLimitExceeded
638+
}
639+
630640
// TODO(km): Deprecate this behaviour - rate limits should still be applied to autoconfirm
631641
if !config.Mailer.Autoconfirm {
632642
// apply rate limiting before the email is sent out

internal/conf/configuration_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package conf
33
import (
44
"os"
55
"testing"
6+
"time"
67

78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
@@ -32,6 +33,27 @@ func TestGlobal(t *testing.T) {
3233
require.NotNil(t, gc)
3334
assert.Equal(t, "X-Request-ID", gc.API.RequestIDHeader)
3435
assert.Equal(t, "pg-functions://postgres/auth/count_failed_attempts", gc.Hook.MFAVerificationAttempt.URI)
36+
37+
}
38+
39+
func TestRateLimits(t *testing.T) {
40+
{
41+
os.Setenv("GOTRUE_RATE_LIMIT_EMAIL_SENT", "0/1h")
42+
43+
gc, err := LoadGlobal("")
44+
require.NoError(t, err)
45+
assert.Equal(t, float64(0), gc.RateLimitEmailSent.Events)
46+
assert.Equal(t, time.Hour, gc.RateLimitEmailSent.OverTime)
47+
}
48+
49+
{
50+
os.Setenv("GOTRUE_RATE_LIMIT_EMAIL_SENT", "10/1h")
51+
52+
gc, err := LoadGlobal("")
53+
require.NoError(t, err)
54+
assert.Equal(t, float64(10), gc.RateLimitEmailSent.Events)
55+
assert.Equal(t, time.Hour, gc.RateLimitEmailSent.OverTime)
56+
}
3557
}
3658

3759
func TestPasswordRequiredCharactersDecode(t *testing.T) {

0 commit comments

Comments
 (0)