Skip to content

fix(api): check rate limit before updating pending email - #68836

Merged
Sembauke merged 2 commits into
freeCodeCamp:mainfrom
hexonal:fix/api-update-email-rate-limit-order
Jul 23, 2026
Merged

Sembauke merged 2 commits into
freeCodeCamp:mainfrom
hexonal:fix/api-update-email-rate-limit-order

Conversation

@hexonal

@hexonal hexonal commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Checklist:

Closes #68738

What this changes

As confirmed by @Sembauke in the issue, the PUT /update-my-email handler updated newEmail, emailVerified and emailVerifyTTL before checking the general emailAuthLinkTTL resend rate limit. A second, rate-limited request to a different address therefore returned 429 but still overwrote user.newEmail, which invalidated the confirmation link sent for the first address.

This PR moves the emailAuthLinkTTL rate-limit check ahead of the user update, so a rate-limited request now exits without mutating the pending email state. No other behaviour changes — responses for all paths stay the same.

Regression test

The existing test PUT rejects the second request if is immediately after the first covers exactly the "request A, then immediately request B" scenario, so it now additionally asserts that after B receives 429, user.newEmail still equals address A.

Verified locally (Node 26, MongoDB 8.3 replica set):

  • with the fix: all 10 /update-my-email tests pass;
  • with the fix reverted (test kept): the new assertion fails with expected '[email protected]' to deeply equal '[email protected]' — i.e. the test reliably catches the original bug.

eslint --max-warnings 0 and tsc --noEmit both pass on the changed files.


Disclosure: this change was drafted with AI assistance; I have personally reviewed, understood and tested it locally.

@hexonal
hexonal requested review from a team as code owners July 15, 2026 04:51
@github-actions github-actions Bot added the platform: api Server application that needs familiarity with Express, Fastify, MongoDB etc. label Jul 15, 2026
@Sembauke
Sembauke enabled auto-merge (squash) July 23, 2026 11:27
@Sembauke
Sembauke merged commit 38a4b27 into freeCodeCamp:main Jul 23, 2026
11 checks passed
zairahira pushed a commit to zairahira/freeCodeCamp that referenced this pull request Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform: api Server application that needs familiarity with Express, Fastify, MongoDB etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rate-limited email changes overwrite user's pending email address (invalidating existing confirmation links)

2 participants