Skip to content

Commit 8807035

Browse files
asottile-sentryandrewshie-sentry
authored andcommitted
ref: fix AttributeError on request.auth when request is rejected by a middleware (#84386)
resolves SENTRY-3N0J <!-- Describe your PR here. -->
1 parent 271e729 commit 8807035

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/sentry/middleware/access_log.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ def get_request_duration(self) -> float:
3232
def _get_request_auth(request: Request) -> AuthenticatedToken | None:
3333
if request.path_info.startswith(settings.ANONYMOUS_STATIC_PREFIXES):
3434
return None
35-
return request.auth
35+
# may not be present if request was rejected by a middleware between this
36+
# and the auth middleware
37+
return getattr(request, "auth", None)
3638

3739

3840
def _get_token_name(auth: AuthenticatedToken | None) -> str | None:

tests/sentry/middleware/test_access_log_middleware.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ class TestAccessLogSuccess(LogCaptureAPITestCase):
200200

201201
def test_access_log_success(self):
202202
self._caplog.set_level(logging.INFO, logger="sentry")
203-
token = None
204203
with assume_test_silo_mode(SiloMode.CONTROL):
205204
token = ApiToken.objects.create(user=self.user, scope_list=["event:read", "org:read"])
206205
self.login_as(user=self.create_user())
@@ -210,6 +209,16 @@ def test_access_log_success(self):
210209
assert tested_log.token_type == "api_token"
211210
assert tested_log.token_last_characters == token.token_last_characters
212211

212+
def test_with_subdomain_redirect(self):
213+
# the subdomain middleware is in between this and the access log middelware
214+
# meaning if a request is rejected between those then it will not have `auth`
215+
# set up properly
216+
# this previously logged an error to sentry
217+
resp = self.get_response(extra_headers={"HTTP_HOST": "invalid_domain.testserver"})
218+
assert resp.status_code == 302
219+
records = [record for record in self._caplog.records if record.levelno == logging.ERROR]
220+
assert not records # no errors should occur
221+
213222

214223
@all_silo_test
215224
@override_settings(LOG_API_ACCESS=False)

0 commit comments

Comments
 (0)