Skip to content

fix(2fa): rate limit the current-password check of the user update - #211

Open
romanetar wants to merge 1 commit into
feat/mfa-phase1---migrations--and--interfacesfrom
fix/user-update-password-rate-limit
Open

romanetar wants to merge 1 commit into
feat/mfa-phase1---migrations--and--interfacesfrom
fix/user-update-password-rate-limit

Conversation

@romanetar

@romanetar romanetar commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

ref: https://app.clickup.com/t/1k5eh578uwa

Summary

The current-password check of the profile password change had no rate limit. A signed-in session could guess the account password without a bound, and a correct guess both changes the password and gives the secret that 2fa/disable and recovery-codes/regenerate ask for, so the limit added to those two could be sidestepped. The OAuth2 profile update reaches the same check, so a stolen access token could guess the password the same way.

A failed current-password check now counts against the same per-user password_confirm limit as 2fa/disable and recovery-codes/regenerate (5 failures per 15 minutes), on every entry point of that check, and answers 429 with Retry-After once the limit is reached.

The base is the MFA branch (feat/mfa-phase1---migrations--and--interfaces), not main: the password_confirm limiter introduced by #187 only exists there.

Changes

  • UserService::update: a wrong or missing current_password consumes the password_confirm limit of the acting user. The counter is keyed by the (int) user id the service already resolves (resource-server context first, then the session), so it is the same cache key the 2fa.rate:password_confirm middleware uses and it also works on a token request with no session.
  • The attempt is reserved first and refunded when the password is right, the same rule as the middleware: a successful change never counts, and a user over the limit gets the 429 without learning whether the password was right. A save without password is never limited.
  • New TwoFactorRateLimitedException, mapped to 429 (Retry-After, X-RateLimit-*, the mfa_rate_limit body of the middleware) in APICRUDController::_update (PUT admin/api/v1/users/me and PUT admin/api/v1/users/{id}) and in OAuth2UserApiController::_update (PUT api/v1/users/me), which otherwise turned it into a 500. PUT api/v1/users/{id} of the OAuth2 API goes through the same _update.
  • The 2fa.rate:password_confirm middleware is deliberately not put on these routes: it counts every non-2xx response, so the validation errors of an ordinary profile save would use up the limit, and its limiter returns no limit when Auth::id() is null, which is the case with a token.

Tests

  • tests/UserUpdatePasswordRateLimitTest.php (8 tests, the router and the whole middleware stack): sixth wrong guess answers 429 with Retry-After and the password is unchanged; a missing current_password counts; one counter shared with 2fa/disable in both directions; a save without password and validation errors never count; a correct password changes it and does not count; for users/{id} the counter is the acting user's, not the target's.
  • tests/OAuth2UserUpdatePasswordRateLimitTest.php (2 tests): with a valid access token and no session (Auth::id() is null), the sixth wrong guess answers 429 with Retry-After, not 500; a correct password within the limit changes it and does not count.
  • On the previous code 8 of the 10 fail (the other 2 guard that a save without a password is never limited).
  • Related suites still pass: OAuth2UserApiTest, OAuth2UserUpdateApiTest, DisableTwoFactorEndpointTest, RecoveryCodeRegenerationTest, PasswordChangeRevokeTokenTest, AdminUserTwoFactorUpdateTest.
  • Checked in the browser: on the profile page the five wrong guesses show current_password is not correct. (412) and the sixth shows Too many attempts. Please try again later. (429) in the existing error dialog.

Notes

  • Risk: a user who mistypes the current password five times cannot change it for 15 minutes. It is the limit and window already accepted for 2fa/disable, the 429 carries Retry-After, and "forgot password" still works.
  • A token with no acting user (service account) has nobody to key the counter on, so that check stays unlimited, as today.
  • The consume runs inside the update transaction; if DoctrineTransactionService retries it after a lost connection a failure could count twice, which errs on the safe side.
  • Out of scope: CSRF of PUT users/me, rate limiting of the sign-in form, the password policy, how event-site shows the 429.
  • The SDS (idp-mfa.md §4.12) update is in the docs repo and goes separately.

Summary by CodeRabbit

  • New Features
    • Password changes now have protection against repeated incorrect or missing current-password attempts. Once the limit is reached, further attempts are temporarily blocked, including attempts with the correct password.
    • Rate-limit errors include a retry delay so you know when to try again. Failed password confirmations share a limit with two-factor authentication disablement; successful confirmations do not count toward it.

The current-password check of the profile password change had no limit: a
session could guess the password without a bound, and a correct guess both
changes the password and gives the secret that 2fa/disable and
recovery-codes/regenerate ask for, so the limit added to those two could be
sidestepped. The OAuth2 profile update reaches the same check.

- UserService::update: a wrong or missing current_password consumes the
  password_confirm limit of the acting user (the counter 2fa/disable and
  recovery-codes/regenerate share), keyed by the (int) user id the service
  already resolves, so it also works on a token request with no session
- the attempt is reserved first and refunded when the password is right: a
  successful change never counts, and a user over the limit gets the 429
  without learning if the password was right; a save without password is
  never limited
- new TwoFactorRateLimitedException, mapped to 429 with Retry-After in
  APICRUDController::_update (PUT admin/api/v1/users/me and users/{id}) and
  OAuth2UserApiController::_update (PUT api/v1/users/me), which turned it
  into a 500
- tests with the whole middleware stack for the admin routes, the shared
  counter and the OAuth2 route with a token and no session

ClickUp 1k5eh578uwa
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Password updates that require current-password verification now use the shared password-confirm rate limit. When the limit is reached, API update handlers return HTTP 429 with retry information. New tests cover standard and OAuth2 update flows.

Changes: Password update rate limiting

Layer / File(s) Summary
Enforce the password-confirm limit
app/libs/Auth/Exceptions/TwoFactorRateLimitedException.php, app/Services/OpenId/UserService.php
UserService consumes an attempt for the acting user when current-password verification is required. Failed or missing passwords count toward the limit; a correct password refunds the attempt.
Return rate-limit responses
app/Http/Controllers/Traits/JsonResponses.php, app/Http/Controllers/Api/APICRUDController.php, app/Http/Controllers/Api/OAuth2/OAuth2UserApiController.php
Both API update handlers catch the rate-limit exception and return HTTP 429. The response includes Retry-After and, when provided, the limit and remaining-attempt headers.
Test user and OAuth2 update paths
tests/UserUpdatePasswordRateLimitTest.php, tests/OAuth2UserUpdatePasswordRateLimitTest.php
Tests cover failed and successful password checks, shared counters with two-factor disablement, attribution to the acting user, and HTTP 429 responses.

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

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant OAuth2UserApiController
  participant UserService
  participant ITwoFactorRateLimitService
  participant JsonResponses
  OAuth2UserApiController->>UserService: update user
  UserService->>ITwoFactorRateLimitService: consume password_confirm attempt
  ITwoFactorRateLimitService-->>UserService: consumed count
  UserService-->>OAuth2UserApiController: throw TwoFactorRateLimitedException
  OAuth2UserApiController->>JsonResponses: error429 with retry delay and limit
  JsonResponses-->>OAuth2UserApiController: HTTP 429 response and rate-limit headers
Loading

Possibly related PRs

  • OpenStackweb/openstackid#187: Adds the two-factor disable endpoint, which shares the password-confirm rate-limit counter used by this change.

Suggested labels: security

Suggested reviewers: smarcet

Merge Risk: Minimal · up to 614bf

Password updates that require the current password now share the five-failures-per-15-minutes limit and return HTTP 429 with Retry-After once it is reached. No concrete merge-blocking defect was found. A narrow race at the window boundary could allow one extra guess.

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 "fix(2fa): rate limit the current-password check of the user update" matches the main change in the changeset. The raw summary and PR objectives show that the current-password check in the u…
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 "fix(2fa): rate limit the current-password check of the user update" matches the main change in the changeset. The raw summary and PR objectives show that the current-password check in the user update now reserves a shared password_confirm attempt, refunds it on success, and returns HTTP 429 once the limit is reached. The title is concise, specific, and free of file lists or vague terms. It omits the 429 mapping and the exception handling in the controllers, but those are supporting details.

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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8c34d7a2-5485-4657-9392-6c8188673c7a
Commits

Reviewing files that changed from the base of the PR and between 58f9a34 and 614bf98.

Files selected for processing: 7

  • app/Http/Controllers/Api/APICRUDController.php
  • app/Http/Controllers/Api/OAuth2/OAuth2UserApiController.php
  • app/Http/Controllers/Traits/JsonResponses.php
  • app/Services/OpenId/UserService.php
  • app/libs/Auth/Exceptions/TwoFactorRateLimitedException.php
  • tests/OAuth2UserUpdatePasswordRateLimitTest.php
  • tests/UserUpdatePasswordRateLimitTest.php

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

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

📘 OpenAPI / Swagger preview

➡️ https://OpenStackweb.github.io/openstackid/openapi/pr-211/

This page is automatically updated on each push to this PR.

@romanetar romanetar self-assigned this Dec 1, 2026
@romanetar
romanetar requested a review from smarcet December 2, 2026 13:16

This branch has not been deployed

No deployments
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