Skip to content

Conversation

@xBLACKICEx
Copy link
Contributor

@xBLACKICEx xBLACKICEx commented Nov 22, 2025

This PR includes three focused security improvements based on the latest three commits:

1. Email change request & confirmation

  • Added endpoints to request an email change and confirm it using a verification code.
  • Ensures that a new email address is actually controlled by the user before updating it.
  • Integrates with the existing verification-code system.

2. Password security hardening

  • Replaced all password fields with SecretString to avoid storing raw passwords in memory.
  • Added NormalizedPassword with:
    • NFC Unicode normalization
    • Control/invisible character filtering
    • Unicode-safe grapheme counting
    • Zeroizing storage to reduce exposure
  • Enforced strong password policy:
    • Minimum 10 graphemes
    • zxcvbn strength score ≥ 4
  • Updated hashing & verification to always normalize + validate before Argon2 hashing.
  • Added extended test coverage for normalization, strength rules, and hashing/verification.

3. Sign-in attempt tracking, throttling & account lockout

  • Added locked_until and fail_count_since to the users table.
  • Expanded sign_in_attempts schema with ip, user_agent, target, and optimized indexes.
  • Implemented:
    • Per-IP rate limiting
    • Per-user rate limiting
    • Automatic account lockout after repeated failures
  • Updated the sign_in flow to:
    • Trim & validate email
    • Check locked status
    • Apply throttling rules
    • Record each attempt (success/fail with metadata)
    • Lock the account when thresholds are exceeded

These changes complete the email-change verification feature and significantly strengthen password and login security.


Issue number

#98

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Performance improvement
  • Other (please describe)

Testing

  • Tests pass locally
  • New password-related tests added
  • Manual testing completed:
    • Email-change verification
    • Password policy enforcement
    • Rate limiting & account lockout

Checklist

  • Code follows project style guidelines
  • Self-review completed
  • Code is commented where necessary
  • Documentation updated
  • No merge conflicts

…malization & policy checks

- Switched all password fields to `SecretString` to avoid keeping raw passwords in memory.
- Added `NormalizedPassword` with NFC normalization, invisible-char filtering, Unicode-safe length checks, and zeroizing storage.
- Enforced password policy: ≥10 graphemes and zxcvbn score ≥4.
- Updated hashing and verification to use normalized passwords; removed old byte-based validation logic.
- Expanded test coverage for normalization, strength rules, hashing/verification, and verification codes.
- Added new dependencies (`secrecy`, `zxcvbn`, `unicode-segmentation`, etc.) and updated Cargo.toml/Cargo.lock.
- Add sign-in rate limiting in `AuthService` using per-IP and per-user thresholds and time windows, returning generic credential errors on failure.
- Normalize and reuse trimmed email in sign-up/sign-in flows and improve verification email logging.
- Extend `users` table with `locked_until` and `fail_count_since` fields to support account lockout tracking.
- Enhance `sign_in_attempts` schema with `target`, `INET` IP, `user_agent` and scoped indexes for IP/user-based throttling.
- Update `AuthRepository` trait and `PgAuthRepository` to record sign-in attempts, check per-IP blocking, and decide when to lock accounts.
- Update `UserRepository` trait and `PgUserRepository` to persist lockout state and track when failure counting starts.
@zero-to-mastery-bot
Copy link

Hey @xBLACKICEx 👋

Thank you for your pull request! 🙏 The team will review it shortly. In the meantime, here are a few projects you might find interesting:


PR Statistics

#️⃣ PR Number ➕ Additions 🗑️ Deletions
100 1079 127
📑 Files Changed ⭐ Repo Stars 🔱 Total Forks
15 7 25

Thanks again for contributing — we appreciate it! 🙌

@crustyrustacean
Copy link
Collaborator

Merged!

@crustyrustacean crustyrustacean merged commit d6a3bdc into zero-to-mastery:main Nov 22, 2025
2 checks passed
@xBLACKICEx xBLACKICEx changed the title Feature/auth ✨ feat(auth): Improve email-change verification, password security, signIn failures rate limiting Nov 23, 2025
@xBLACKICEx xBLACKICEx changed the title ✨ feat(auth): Improve email-change verification, password security, signIn failures rate limiting ✨ feat(auth): implement email-change verification, password security, signIn failures rate limiting Nov 23, 2025
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.

2 participants