Skip to content

fix(user): store the hash of the email codes, reset link valid 1 day - #2830

Merged
lbesson merged 1 commit into
masterfrom
fix/hashed-nonces
Dec 3, 2026
Merged

lbesson merged 1 commit into
masterfrom
fix/hashed-nonces

Conversation

@lbesson

@lbesson lbesson commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

What

  • The codes in the links of the registration, password reset and email change emails are no longer stored in clear: only their SHA-256 is kept (<purpose>_sha256_<digest>). The purpose stays readable, for the purpose checks and the purge of accounts never validated.
  • A code received must have the exact format of a sent code, so a stored value is never accepted as a code.
  • The password reset link is valid 1 day instead of 3.

Deploy

No migration. Codes stored in clear before the deploy keep working until they expire (3 days at most); the fallback for them is marked in validate_user_from_nonce and can be removed after 2026-12-08.

Tests

  • The stored value is the hash, the hash itself is refused, the reset expires within a day, the real code works.
  • A code stored in clear before the change still works.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Security Improvements
    • Verification tokens are now stored in a hashed form rather than as readable values.
    • Password-reset tokens expire after one day, instead of three.
  • Bug Fixes
    • Registration, password-reset, and email-change confirmation links continue to receive the correct verification token.
    • Existing, unexpired password-reset links issued before this change remain valid during the transition. Raw tokens must match the expected format and be unexpired to validate.

The codes of the registration, password reset and email change links
were stored in clear: whoever read the users table could use them.
Only their SHA-256 is stored now, with the purpose kept readable for
the purpose checks and the purge of accounts never validated. Incoming
codes must have the exact format of a sent code, so a stored hash is
never accepted as a code.

Codes stored in clear before this change are still accepted until they
expire (3 days at most); that fallback can go after 2026-12-08.

The password reset link is now valid 1 day instead of 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@coderabbitai

coderabbitai Bot commented Dec 3, 2026

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Validation nonces are now stored as SHA-256 hashes while raw values remain available for email links. User views accept hashed and legacy cleartext nonces before expiry. Password-reset nonces expire after one day.

Changes

Nonce handling

Layer / File(s) Summary
Nonce format and storage
c2corg_api/models/user.py, c2corg_api/tests/models/test_user.py
The model defines a nonce format and hash function. It stores the hash and returns the raw nonce. Model tests check the stored hash and nonce validation.
Nonce validation and password reset
c2corg_api/views/user.py, c2corg_api/tests/views/test_user.py
Views validate hashed and legacy cleartext nonces before expiry. Password-reset nonces now expire after one day. Tests cover validation, legacy compatibility, and expiry.
Email-link nonce handling
c2corg_api/views/user.py, c2corg_api/views/user_account.py
Registration and email-change flows use the returned nonce to construct confirmation links instead of reading the stored value.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant UserViews
  participant UserModel
  participant UserStorage
  participant EmailLink
  UserViews->>UserModel: Create password-reset nonce
  UserModel->>UserStorage: Store hashed nonce
  UserModel-->>UserViews: Return raw nonce
  UserViews->>EmailLink: Include raw nonce in reset link
  UserViews->>UserStorage: Look up hashed or legacy nonce before expiry
Loading

Suggested reviewers: gberaudo, nayor

Merge Risk: Minimal · up to 77f19

Validation codes are now stored as hashes, so a leaked database value cannot be used as a code. Password-reset links expire after one day. Older codes keep working until they expire, so no concrete merge risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: registration, password-reset and email-change codes are now stored as SHA-256 digests instead of in clear text, and the password-reset link validity drops from three d…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: registration, password-reset and email-change codes are now stored as SHA-256 digests instead of in clear text, and the password-reset link validity drops from three days to one day. The title covers the core security change and the expiry change, and the summary and objectives match it. It is a short, single-sentence title with the conventional fix(user): prefix.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit keeps the nonce close,
A hashed trail is what gets stored.
The raw one hops into the mail,
To reach the reset link without fail.
Old cleartext trails still pass in time,
One-day reset tokens mark the line.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3db1a79e-5c06-4d7e-a5d8-f173b99d2e05
Commits

Reviewing files that changed from the base of the PR and between 440845d and 77f19ff.

Files selected for processing: 5

  • c2corg_api/models/user.py
  • c2corg_api/tests/models/test_user.py
  • c2corg_api/tests/views/test_user.py
  • c2corg_api/views/user.py
  • c2corg_api/views/user_account.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@lbesson
lbesson merged commit 802fbb2 into master Dec 3, 2026
7 checks passed
@lbesson
lbesson deleted the fix/hashed-nonces branch December 3, 2026 10:18
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.

1 participant