Skip to content

Conversation

@sl0thentr0py
Copy link
Member

Description

For async views in django, we're supposed to use the async version auser to fetch user info.

Issues

@sl0thentr0py sl0thentr0py requested a review from a team as a code owner January 7, 2026 15:54
@linear
Copy link

linear bot commented Jan 7, 2026

Comment on lines 217 to 219
if should_send_default_pii():
with capture_internal_exceptions():
request._sentry_user_info = await _get_user_info(request)

This comment was marked as outdated.

@sl0thentr0py sl0thentr0py force-pushed the neel/fix-django-async-user branch 2 times, most recently from cf2d25c to bd37ba6 Compare January 7, 2026 16:03
@sl0thentr0py sl0thentr0py marked this pull request as draft January 7, 2026 16:13
@pytest_mark_django_db_decorator()
@pytest.mark.skipif(
django.VERSION < (3, 0), reason="Django ASGI support shipped in 3.0"
)
Copy link

Choose a reason for hiding this comment

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

Test skipif condition requires newer Django version

Medium Severity

The test test_user_pii_in_asgi_with_auth has a skipif condition checking for django.VERSION < (3, 0), but the test view async_mylogin uses User.objects.acreate_user() (added in Django 4.1) and alogin (added in Django 5.1). This mismatch means the test will fail with an ImportError when run on Django versions 3.0 through 5.0, since the skipif condition won't exclude those versions but the required APIs don't exist.

🔬 Verification Test

Why verification test was not possible: This test failure would manifest only when running the test suite against specific Django versions (3.x, 4.x, 5.0). The test would fail with an ImportError: cannot import name 'alogin' from 'django.contrib.auth' when Django < 5.1 is used. Verifying this would require setting up multiple Django version environments which is outside the scope of this review.

Additional Locations (1)

Fix in Cursor Fix in Web


def _set_user_info(request: "ASGIRequest", event: "Event") -> None:
user_info = getattr(request, "_sentry_user_info", {})
event.setdefault("user", user_info)
Copy link

Choose a reason for hiding this comment

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

Async user info doesn't merge with existing user data

Low Severity

The new async _set_user_info uses event.setdefault("user", user_info) which only sets the user dict if no "user" key exists. In contrast, the sync version in sentry_sdk/integrations/django/__init__.py uses event.setdefault("user", {}) then user_info.setdefault() on individual fields, which merges request user data into any existing user dict. This means if scope.set_user({"id": "custom"}) is called before the event processor runs, the sync version would still add email and username from the authenticated user, but the async version adds nothing. This behavioral inconsistency could cause missing user fields in async contexts.

🔬 Verification Test

Why verification test was not possible: This requires testing the interaction between scope.set_user() and the event processor in an async Django context, which would need a full integration test environment with Django ASGI running. The behavioral difference is clear from comparing the sync implementation at line 584-605 in __init__.py (which calls setdefault on individual fields) versus the async implementation (which calls setdefault on the entire user dict).

Fix in Cursor Fix in Web

@sl0thentr0py sl0thentr0py force-pushed the neel/fix-django-async-user branch from bd37ba6 to a908d9d Compare January 7, 2026 16:45
@sl0thentr0py sl0thentr0py force-pushed the neel/fix-django-async-user branch from a908d9d to 6c0c2a7 Compare January 7, 2026 16:46
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.

Django ASGI: send_default_pii triggers request.user.is_authenticated sync access

2 participants