Skip to content

fix: use getattr for safe access to request.axes_attempt_time#1449

Open
muhamedfazalps wants to merge 1 commit into
jazzband:masterfrom
muhamedfazalps:fix-get-cool-off-threshold-attribute-error
Open

fix: use getattr for safe access to request.axes_attempt_time#1449
muhamedfazalps wants to merge 1 commit into
jazzband:masterfrom
muhamedfazalps:fix-get-cool-off-threshold-attribute-error

Conversation

@muhamedfazalps

Copy link
Copy Markdown

Summary

Fixes #1424

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 an AttributeError when 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.py directly accesses request.axes_attempt_time without checking if request is None first. The existing # type: ignore[union-attr] comment acknowledges this is unsafe, but the actual guard was missing.

Fix

Changed request.axes_attempt_time to getattr(request, "axes_attempt_time", None), which safely handles both:

  1. request=None (the default case)
  2. request exists but hasn't been through the axes failure handler (no axes_attempt_time attribute)

In both cases, attempt_time will be None, and the function correctly falls back to now() - cool_off.

Testing

from axes.attempts import get_cool_off_threshold

# Before fix: AttributeError
# After fix: returns now() - cool_off
datetime = get_cool_off_threshold()

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
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: get_cool_off_threshold() breaks in some cases after upgrading from 5.x to 8.x

1 participant