Skip to content

Commit c59f312

Browse files
authored
chore(staff): Cleanup u2f state in finally again (#67246)
1 parent 982d436 commit c59f312

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

src/sentry/auth/authenticators/u2f.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -327,22 +327,8 @@ def validate_response(self, request: HttpRequest, challenge, response):
327327
sentry_sdk.capture_exception(err)
328328
return False
329329
finally:
330-
staff_state = request.session.get("staff_webauthn_authentication_state")
331-
default_state = request.session.get("webauthn_authentication_state")
332-
logger.info(
333-
"State in finally",
334-
extra={
335-
"user": request.user.id,
336-
"state_timestamp": (
337-
datetime.fromtimestamp(default_state[1]) if default_state else 0
338-
),
339-
"staff_timestamp": (
340-
datetime.fromtimestamp(staff_state[1]) if staff_state else 0
341-
),
342-
},
343-
)
344330
# Cleanup the U2F state from the session
345-
# request.session.pop("webauthn_authentication_state", None)
346-
# request.session.pop("staff_webauthn_authentication_state", None)
347-
# request.session.pop("staff_auth_flow", None)
331+
request.session.pop("webauthn_authentication_state", None)
332+
request.session.pop("staff_webauthn_authentication_state", None)
333+
request.session.pop("staff_auth_flow", None)
348334
return True

tests/sentry/auth/authenticators/test_u2f.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def test_validate_response_normal_state(self):
126126
assert self.u2f.validate_response(self.request, None, self.response)
127127
_, kwargs = mock_state.call_args
128128
assert kwargs.get("state") == "normal state"
129+
assert "webauthn_authentication_state" not in self.request.session
129130

130131
@freeze_time(CURRENT_TIME)
131132
def test_validate_response_staff_state_valid_timestamp(self):
@@ -139,6 +140,7 @@ def test_validate_response_staff_state_valid_timestamp(self):
139140
assert self.u2f.validate_response(self.request, None, self.response)
140141
_, kwargs = mock_state.call_args
141142
assert kwargs.get("state") == "staff state"
143+
assert "staff_webauthn_authentication_state" not in self.request.session
142144

143145
@freeze_time(CURRENT_TIME)
144146
def test_validate_response_staff_state_invalid_timestamp(self):
@@ -153,13 +155,15 @@ def test_validate_response_staff_state_invalid_timestamp(self):
153155
assert self.u2f.validate_response(self.request, None, self.response)
154156
_, kwargs = mock_state.call_args
155157
assert kwargs.get("state") == "non-staff state"
158+
assert "webauthn_authentication_state" not in self.request.session
156159

157160
# Test timestamp too far in the future
158161
self.request.session["webauthn_authentication_state"] = ("non-staff state", 5)
159162
self.request.session["staff_auth_flow"] = self.INVALID_FUTURE_TIMESTAMP
160163
assert self.u2f.validate_response(self.request, None, self.response)
161164
_, kwargs = mock_state.call_args
162165
assert kwargs.get("state") == "non-staff state"
166+
assert "webauthn_authentication_state" not in self.request.session
163167

164168
@freeze_time(CURRENT_TIME)
165169
def test_validate_response_failing_still_clears_all_states(self):
@@ -175,3 +179,6 @@ def test_validate_response_failing_still_clears_all_states(self):
175179
self.u2f.validate_response(self.request, None, self.response)
176180
_, kwargs = mock_state.call_args
177181
assert kwargs.get("state") == "staff state"
182+
assert "webauthn_authentication_state" not in self.request.session
183+
assert "staff_webauthn_authentication_state" not in self.request.session
184+
assert "staff_auth_flow" not in self.request.session

0 commit comments

Comments
 (0)