fix: use getattr for safe access to request.axes_attempt_time#1449
Open
muhamedfazalps wants to merge 1 commit into
Open
fix: use getattr for safe access to request.axes_attempt_time#1449muhamedfazalps wants to merge 1 commit into
muhamedfazalps wants to merge 1 commit into
Conversation
get_cool_off_threshold() declares request as Optional[HttpRequest] with a default of None, but the function body unconditionally dereferences request.axes_attempt_time, causing AttributeError when called without a request or with a request that hasn't gone through the axes failure-tracking path. Use getattr with a default of None to safely handle both cases. Fixes jazzband#1424
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1424
get_cool_off_threshold()declaresrequestasOptional[HttpRequest]with a default ofNone, but the function body unconditionally dereferencesrequest.axes_attempt_time, causing anAttributeErrorwhen called without a request or with a request that hasn't gone through the axes failure-tracking path.Root Cause
Line 23 in
axes/attempts.pydirectly accessesrequest.axes_attempt_timewithout checking ifrequestisNonefirst. The existing# type: ignore[union-attr]comment acknowledges this is unsafe, but the actual guard was missing.Fix
Changed
request.axes_attempt_timetogetattr(request, "axes_attempt_time", None), which safely handles both:request=None(the default case)requestexists but hasn't been through the axes failure handler (noaxes_attempt_timeattribute)In both cases,
attempt_timewill beNone, and the function correctly falls back tonow() - cool_off.Testing