Skip to content

Conversation

dechen-authentik
Copy link
Contributor

@dechen-authentik dechen-authentik commented Jul 12, 2025

Details

This PR implements a very basic version of rate limiting for account verification emails.

Closes #11149

Context

Currently, it is possible to send an infinite number of account recovery emails. To reproduce this:

  • In Flows and Stages, create a new Stage using "Email Stage"
    • Set the Template to "Account Confirmation"
    • Create a new Flow using a Designation of "Recovery"
  • In the new Flow, create Stage Bindings
    • Create an "Identification Stage", then for User fields select "Username" and "Email"
      • Under Recovery flow, set the flow created earlier
      • Set the Order to 0
    • Create an "Email Stage"
      • Set the Order to 10
  • In Directory -> Users, create a user of type "Internal"
  • In the Flow, click the "Normal" button to execute the flow

Once you send the initial email, you will have the option to "Send email again":

image

You can currently click this button an unlimited number of times.

Rate Limiting

To prevent the user from being spammed, this PR introduces a rate limiting feature. To configure it, edit an existing Email Stage, where you'll notice two new fields, "Account Recovery Max Attempts" and "Account Recovery Cache Timeout":

image

Now if you spam the same button, eventually you'll see an error:

image

The error is Too many account verification attempts. Please try again after 10 minutes.. The error keeps track of the actual cache expiry time: if you click the button after a minute, you'll notice the message will ask you to try after 9 minutes.

Checklist

  • Local tests pass (ak test authentik/)
    • I keep getting segfault errors (on unrelated tests) when trying to run the full test suite locally.
  • The code has been formatted (make lint-fix)

If an API change has been made

  • The API schema has been updated (make gen-build)

If changes to the frontend have been made

  • The code has been formatted (make web)

If applicable

  • The documentation has been updated
  • The documentation has been formatted (make website)

@dechen-authentik dechen-authentik requested review from a team as code owners July 12, 2025 17:31
Copy link

netlify bot commented Jul 12, 2025

Deploy Preview for authentik-docs ready!

Name Link
🔨 Latest commit 8ac746a
🔍 Latest deploy log https://app.netlify.com/projects/authentik-docs/deploys/6892554a7fb607000814fa66
😎 Deploy Preview https://deploy-preview-15531--authentik-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link

netlify bot commented Jul 12, 2025

Deploy Preview for authentik-integrations ready!

Name Link
🔨 Latest commit 8ac746a
🔍 Latest deploy log https://app.netlify.com/projects/authentik-integrations/deploys/6892554a4f2a7600085e7516
😎 Deploy Preview https://deploy-preview-15531--authentik-integrations.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link

netlify bot commented Jul 12, 2025

Deploy Preview for authentik-storybook ready!

Name Link
🔨 Latest commit 8ac746a
🔍 Latest deploy log https://app.netlify.com/projects/authentik-storybook/deploys/6892554a586966000861ecf4
😎 Deploy Preview https://deploy-preview-15531--authentik-storybook.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@dechen-authentik dechen-authentik force-pushed the feature/rate-limiting-password-reset branch from c17d035 to 00a7295 Compare July 12, 2025 17:35
@dechen-authentik
Copy link
Contributor Author

cc: @rissson 👋

Copy link

codecov bot commented Jul 12, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.73%. Comparing base (9848e4f) to head (8ac746a).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15531      +/-   ##
==========================================
- Coverage   92.76%   92.73%   -0.03%     
==========================================
  Files         833      833              
  Lines       44731    44868     +137     
==========================================
+ Hits        41494    41610     +116     
- Misses       3237     3258      +21     
Flag Coverage Δ
e2e 46.58% <7.24%> (-0.20%) ⬇️
integration 23.67% <7.24%> (-0.06%) ⬇️
unit 90.88% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dechen-authentik dechen-authentik force-pushed the feature/rate-limiting-password-reset branch from 00a7295 to ba7e7df Compare July 15, 2025 15:57
Copy link
Member

@BeryJu BeryJu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple small things, otherwise lgtm

def challenge_valid(self, response: ChallengeResponse) -> HttpResponse:
return super().challenge_invalid(response)

def _get_cache_key(self) -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit but this might be something to implement as a Throttle in DRF https://www.django-rest-framework.org/api-guide/throttling/#userratethrottle

(this code is fine, thats just a design idea)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 That's an interesting idea. We could create a custom throttling class that uses DRF's throttling mechanism (we don't want to return a 429 status code, need to check stage specific settings, etc). This sounds like a viable option➕

@dechen-authentik dechen-authentik force-pushed the feature/rate-limiting-password-reset branch from ba7e7df to 525e61c Compare July 18, 2025 06:48
@dechen-authentik
Copy link
Contributor Author

Couple small things, otherwise lgtm

Thanks for your feedback @BeryJu 🙏 I've implemented your suggestions and it's ready for another review.

@dechen-authentik dechen-authentik requested a review from BeryJu July 18, 2025 07:04
@dechen-authentik dechen-authentik changed the title stages/email: implement rate limiting for account recovery stages/email: implement rate limiting for account verification Jul 18, 2025
@dechen-authentik dechen-authentik force-pushed the feature/rate-limiting-password-reset branch from 525e61c to fbd027e Compare July 23, 2025 11:52
@dechen-authentik
Copy link
Contributor Author

Thanks for your review @melizeche 🙏

I've addressed your feedback and it's ready for another review when you get a chance.

@dechen-authentik dechen-authentik force-pushed the feature/rate-limiting-password-reset branch from fbd027e to 92f94c2 Compare July 26, 2025 08:16
@rissson rissson enabled auto-merge (squash) August 6, 2025 14:43
@rissson rissson merged commit cfe113b into goauthentik:main Aug 6, 2025
106 checks passed
kensternberg-authentik added a commit that referenced this pull request Aug 7, 2025
* main: (32 commits)
  core: bump goauthentik.io/api/v3 from 3.2025064.6 to 3.2025064.7 (#16024)
  core, web: update translations (#16021)
  ci: move images from beryju/* to authentik/* (#15321)
  core, web: update translations (#15985)
  core: bump cattrs from 24.1.3 to v25.1.1 (#15981)
  web: bump API Client version (#16002)
  ci: bump actions/download-artifact from 4 to 5 (#15995)
  core: bump certifi from 2025.7.14 to v2025.8.3 (#15982)
  core: bump anyio from 4.9.0 to v4.10.0 (#15979)
  core: bump boto3 from 1.40.1 to v1.40.2 (#15980)
  core: bump astral-sh/uv from 0.8.4 to 0.8.5 (#15998)
  core: bump goauthentik.io/api/v3 from 3.2025064.5 to 3.2025064.6 (#15997)
  stages/email: implement rate limiting for account verification (#15531)
  web: Fix stale application slug, missing error state. (#15941)
  website/docs: change azure ad to entra id (#15691)
  website/docs: add tips for image optimization (#15978)
  web: bump API Client version (#15976)
  providers/oauth2: backchannel logout (#15401)
  web: bump API Client version (#15953)
  translate: Updates for file web/xliff/en.xlf in fr (#15974)
  ...
kensternberg-authentik added a commit that referenced this pull request Aug 26, 2025
* main: (32 commits)
  core: bump goauthentik.io/api/v3 from 3.2025064.6 to 3.2025064.7 (#16024)
  core, web: update translations (#16021)
  ci: move images from beryju/* to authentik/* (#15321)
  core, web: update translations (#15985)
  core: bump cattrs from 24.1.3 to v25.1.1 (#15981)
  web: bump API Client version (#16002)
  ci: bump actions/download-artifact from 4 to 5 (#15995)
  core: bump certifi from 2025.7.14 to v2025.8.3 (#15982)
  core: bump anyio from 4.9.0 to v4.10.0 (#15979)
  core: bump boto3 from 1.40.1 to v1.40.2 (#15980)
  core: bump astral-sh/uv from 0.8.4 to 0.8.5 (#15998)
  core: bump goauthentik.io/api/v3 from 3.2025064.5 to 3.2025064.6 (#15997)
  stages/email: implement rate limiting for account verification (#15531)
  web: Fix stale application slug, missing error state. (#15941)
  website/docs: change azure ad to entra id (#15691)
  website/docs: add tips for image optimization (#15978)
  web: bump API Client version (#15976)
  providers/oauth2: backchannel logout (#15401)
  web: bump API Client version (#15953)
  translate: Updates for file web/xliff/en.xlf in fr (#15974)
  ...
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.

Rate limiting password reset mails
5 participants