Skip to content

fix(brute-force): record_failure/mark_success work with any StorageBackend - #13

Merged
magi8101 merged 1 commit into
mainfrom
fix/brute-force-async-backend
Sep 6, 2026
Merged

magi8101 merged 1 commit into
mainfrom
fix/brute-force-async-backend

Conversation

@magi8101

@magi8101 magi8101 commented Sep 6, 2026

Copy link
Copy Markdown
Member

Fixes #12. record_failure/mark_success called backend.incr_sync/reset_sync, which _patch_backend() only ever monkey-patched onto MemoryBackend — any other StorageBackend (RedisBackend, or a user's own) would AttributeError on the first failed login.

Made both methods async and call the Protocol's real incr/reset directly, deleted the sync-shim machinery entirely. is_locked/get_retry_after stay sync since they only touch the local _lockouts dict. Added a regression test using a backend with only the async Protocol methods (no _sync anything) to prove it. 249/249 tests green (250 before minus 4 now-pointless sync-shim tests, plus this PR's new one).

Note: this branches off main, not PR #11 — so its own CI run still shows the pre-#11 mypy count; unrelated to this PR's change.

Copilot AI lite review requested due to automatic review settings September 6, 2026 14:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

…geBackend

record_failure/mark_success called backend.incr_sync/reset_sync, which
_patch_backend() only ever monkey-patched onto MemoryBackend (its ops are
plain dict access, so a sync shim was safe there). Any other StorageBackend
Protocol implementer -- RedisBackend, or a user's own -- would AttributeError
on the very first failed login attempt, since it never got the patch.

Made record_failure/mark_success async and call the Protocol's real
incr/reset directly, and deleted the sync-shim/_patch_backend machinery
entirely -- it existed solely to work around not doing this. is_locked/
get_retry_after stay sync (they only ever touched the local _lockouts
dict, never the backend).

velocix's own handlers are async throughout, so this doesn't cost the
"call it from a plain sync handler" use case the module docstring
advertised -- there wasn't really a sync handler to call it from.

Updated the two affected test files for the new async signatures, and
added a regression test using a backend that only implements the async
Protocol (no _sync methods) to prove the actual bug is fixed. Removed
tests/test_security_base.py's incr_sync/get_sync/reset_sync tests since
they tested the now-deleted patch directly -- the async incr/get/reset
they exercised already have full separate coverage in that same file.

Fixes #12.
@magi8101
magi8101 force-pushed the fix/brute-force-async-backend branch from dd79f2e to 4bf4a4c Compare September 6, 2026 14:19
@magi8101
magi8101 merged commit 5e0a070 into main Sep 6, 2026
2 checks passed
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.

BruteForceProtection crashes with a RedisBackend (or any non-MemoryBackend)

2 participants