Skip to content

Commit

Permalink
fix: rate limits of 0 take precedence over MAILER_AUTO_CONFIRM (#1837)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
cstockton and Chris Stockton authored Nov 13, 2024
1 parent 9ce2857 commit cb7894e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/api/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,16 @@ func (a *API) sendEmail(r *http.Request, tx *storage.Connection, u *models.User,
}
}

// if the number of events is set to zero, we immediately apply rate limits.
if config.RateLimitEmailSent.Events == 0 {
emailRateLimitCounter.Add(
ctx,
1,
metric.WithAttributeSet(attribute.NewSet(attribute.String("path", r.URL.Path))),
)
return EmailRateLimitExceeded
}

// TODO(km): Deprecate this behaviour - rate limits should still be applied to autoconfirm
if !config.Mailer.Autoconfirm {
// apply rate limiting before the email is sent out
Expand Down
22 changes: 22 additions & 0 deletions internal/conf/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package conf
import (
"os"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -32,6 +33,27 @@ func TestGlobal(t *testing.T) {
require.NotNil(t, gc)
assert.Equal(t, "X-Request-ID", gc.API.RequestIDHeader)
assert.Equal(t, "pg-functions://postgres/auth/count_failed_attempts", gc.Hook.MFAVerificationAttempt.URI)

}

func TestRateLimits(t *testing.T) {
{
os.Setenv("GOTRUE_RATE_LIMIT_EMAIL_SENT", "0/1h")

gc, err := LoadGlobal("")
require.NoError(t, err)
assert.Equal(t, float64(0), gc.RateLimitEmailSent.Events)
assert.Equal(t, time.Hour, gc.RateLimitEmailSent.OverTime)
}

{
os.Setenv("GOTRUE_RATE_LIMIT_EMAIL_SENT", "10/1h")

gc, err := LoadGlobal("")
require.NoError(t, err)
assert.Equal(t, float64(10), gc.RateLimitEmailSent.Events)
assert.Equal(t, time.Hour, gc.RateLimitEmailSent.OverTime)
}
}

func TestPasswordRequiredCharactersDecode(t *testing.T) {
Expand Down

0 comments on commit cb7894e

Please sign in to comment.