Skip to content

chore(otp): clamp OtpExp to the max safe duration - #2781

Open
annabkr wants to merge 1 commit into
masterfrom
annabaker/auth-1572-clamp-otp-exp
Open

chore(otp): clamp OtpExp to the max safe duration#2781
annabkr wants to merge 1 commit into
masterfrom
annabaker/auth-1572-clamp-otp-exp

Conversation

@annabkr

@annabkr annabkr commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

The platform caps *_OTP_EXP values between (0, POSTGRES_MAX_INT) which is below the max threshold.

However, this isn't enforced on our end. @fadymak raised a question about it on another PR, so it felt like adding clamping logic would help make the answer to the question more direct next time.

What kind of change does this PR introduce?

Feat ?

What is the current behavior?

No maximum values are enforced. ApplyDefaults only converts 0 to the defaults, otherwise the values get passed through as configured.

If a value too large is configured by a self-hosted user, it will overflow and mark tokens as expired upon creation.

What is the new behavior?

There is an enforced max value. ApplyDefaults clamps both SMS and email OTP expiration config values to:

const maxOtpExp uint = math.MaxInt64 / uint(time.Second)

This means that values above the ceiling would reduce to it instead of becoming negative.

Additional context

Add any other context or screenshots.

`time.Duration` counts nanoseconds in an int64, so converting an OtpExp
above math.MaxInt64 / time.Second wraps to a negative duration. That would
make `isOtpExpired` treat every token as already expired.

This is defensive, not a fix for an observed bug. Hosted projects are
capped at 2147483647 by the platform's int4 config column, roughly 4x
below the overflow threshold, so the clamp cannot trigger for them. It
guards self-hosted deployments, where GOTRUE_MAILER_OTP_EXP and
GOTRUE_SMS_OTP_EXP are unbounded, and it lets the #nosec G115 annotation
at verify.go:792 be justified from within this repo.
@annabkr
annabkr force-pushed the annabaker/auth-1572-clamp-otp-exp branch from 7c351a2 to 4d8405b Compare September 2, 2026 20:46
@annabkr
annabkr marked this pull request as ready for review September 2, 2026 20:51
@annabkr
annabkr requested a review from a team as a code owner September 2, 2026 20:51
Comment thread internal/conf/configuration_test.go
Comment thread internal/conf/configuration.go
Comment thread internal/conf/configuration.go
@hf

hf commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Yea title should be feat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants