镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

fix: issue #926 — race conditions in rate limit and failed_login_attempts counters - #1015

Open
Ibochkarev wants to merge 1 commit into
betafrom
fix/926-race-condition-rate-limit
Open

Ibochkarev wants to merge 1 commit into
betafrom
fix/926-race-condition-rate-limit

Conversation

@Ibochkarev

Copy link
Copy Markdown
Member

fix: issue #926 — race conditions in rate limit and failed_login_attempts counters

Problem

Counters for brute-force protection operate via read-modify-write without locking, allowing parallel requests to lose increments.

  • FileRateLimitStore read the counter, incremented by 1, wrote back — without flock on the entire cycle.
  • RateLimiter (cache-based) read from cache manager, incremented in PHP, then wrote back.
  • AuthManager::handleFailedLogin() read $customer->get('failed_login_attempts') + 1 and saved in separate call.

A batch of synchronous login requests for the same email counted as fewer attempts than sent. The password-bruteforce window grew, and the lockout triggered after the threshold was passed.

Classification: CWE-362. Severity: Medium for default file store, Low with Redis/Memcached (atomic INCR available).

Solution

1. src/Services/RateLimit/FileRateLimitStore.php

  • increment() now uses flock(LOCK_EX) on the entire read-modify-write cycle for atomicity.
  • On failure (e.g., open error), falls back to non-atomic increment with a warning.

2. src/Services/Customer/RateLimiter.php

  • check() now uses $modx->cacheManager->add() for the first attempt (atomic if key doesn't exist).
  • Subsequent increments still use read+set (race window remains small for typical single-server deployments).
  • Added documentation noting that high-concurrency deployments should use Redis/Memcached with atomic INCR.

3. src/Services/Customer/AuthManager.php

  • handleFailedLogin() now uses SQL UPDATE ... SET failed_login_attempts = failed_login_attempts + 1, is_blocked = CASE WHEN ... for atomic increment.
  • Lockout and block status are updated in the same SQL statement.
  • Fallback for test environments without exec/getTableName uses the original object-method approach.
  • Expired lockout resets are also done atomically via SQL.

Affected files

  • core/components/minishop3/src/Services/RateLimit/FileRateLimitStore.php — atomic flock in increment()
  • core/components/minishop3/src/Services/Customer/RateLimiter.php — atomic add() for first attempt
  • core/components/minishop3/src/Services/Customer/AuthManager.php — atomic SQL UPDATE for failed_login_attempts

Related issues

Testing

  1. Deploy the build from branch fix/926-race-condition-rate-limit to project.test.
  2. Send N simultaneous login requests with incorrect password for one email.
  3. Verify failed_login_attempts equals N (not less).
  4. Verify lockout triggers after ms3_customer_max_login_attempts attempts exactly.
  5. Verify rate-limit counters increment correctly under concurrent load.
  6. Run existing test suite: composer test — no regressions expected.

Changelog

### Security
- Race conditions in rate limit and failed_login_attempts counters are now mitigated. File-based store uses flock(LOCK_EX), cache-based uses atomic add, and AuthManager uses SQL UPDATE for atomic increments (#926).

Branch: fix/926-race-condition-rate-limit
Based on: beta

@Ibochkarev
Ibochkarev force-pushed the fix/926-race-condition-rate-limit branch from 8b1a646 to ed7dc9c Compare November 15, 2026 13:49
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.

[Bug] Гонки в счётчиках rate limit и failed_login_attempts ослабляют защиту от перебора

1 participant