Skip to content

feat(middleware): Enable viewer context middleware by default#112293

Merged
gricha merged 3 commits intomasterfrom
gricha/feat/enable-viewer-context-middleware
Apr 6, 2026
Merged

feat(middleware): Enable viewer context middleware by default#112293
gricha merged 3 commits intomasterfrom
gricha/feat/enable-viewer-context-middleware

Conversation

@gricha
Copy link
Copy Markdown
Member

@gricha gricha commented Apr 6, 2026

Enable the ViewerContextMiddleware by setting the viewer-context.enabled
option default to True. This causes the middleware to populate a
ViewerContext (unified caller identity) on every authenticated request.

The option uses FLAG_NOSTORE so the value is read at process startup from
deploy config (SENTRY_OPTIONS). Changing the default here means it will be
active everywhere unless explicitly overridden to False in deploy config.

Set the viewer-context.enabled option default to True so the
ViewerContextMiddleware populates ViewerContext on every request.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 6, 2026
@gricha gricha marked this pull request as ready for review April 6, 2026 21:07
@gricha gricha requested a review from a team as a code owner April 6, 2026 21:07
@gricha gricha requested a review from JoshFerge April 6, 2026 21:07
Copy link
Copy Markdown
Contributor

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Default change breaks test relying on old default
    • Added @override_options decorator to explicitly set viewer-context.enabled to False in test_skipped_when_disabled, ensuring the test correctly validates disabled middleware behavior.

Create PR

Or push these changes by commenting:

@cursor push 62bac19e44
Preview (62bac19e44)
diff --git a/tests/sentry/middleware/test_viewer_context.py b/tests/sentry/middleware/test_viewer_context.py
--- a/tests/sentry/middleware/test_viewer_context.py
+++ b/tests/sentry/middleware/test_viewer_context.py
@@ -103,6 +103,7 @@
         super().setUp()
         self.factory = RequestFactory()
 
+    @override_options({"viewer-context.enabled": False})
     def test_skipped_when_disabled(self):
         captured: list = []
 
@@ -110,7 +111,6 @@
             captured.append(get_viewer_context())
             return MagicMock(status_code=200)
 
-        # Default: viewer-context.enabled is False
         middleware = ViewerContextMiddleware(get_response)
 
         request = self.factory.get("/")

This Bugbot Autofix run was free. To enable autofix for future PRs, go to the Cursor dashboard.

Reviewed by Cursor Bugbot for commit fa5498b. Configure here.

Comment thread src/sentry/options/defaults.py
Add explicit @override_options decorator to set viewer-context.enabled
to False, since the default is now True.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gricha gricha requested a review from a team as a code owner April 6, 2026 21:11
@joshuarli joshuarli added the Trigger: Override Selective Testing Run the full test suite; necessary in cases where selected tests are flaky due to reshuffling label Apr 6, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 6, 2026

Backend Test Failures

Failures on c7f2a76 in this run:

tests/sentry/web/frontend/generic/test_static_media.py::StaticMediaTest::test_no_corslog
[gw0] linux -- Python 3.13.1 /home/runner/work/sentry/sentry/.venv/bin/python3
tests/sentry/web/frontend/generic/test_static_media.py:96: in test_no_cors
    assert response["Vary"] == "Accept-Encoding"
E   AssertionError: assert 'Accept-Encoding, Cookie' == 'Accept-Encoding'
E     
E     - Accept-Encoding
E     + Accept-Encoding, Cookie
E     ?                ++++++++
tests/sentry/web/frontend/generic/test_static_media.py::StaticMediaTest::test_versionedlog
[gw0] linux -- Python 3.13.1 /home/runner/work/sentry/sentry/.venv/bin/python3
tests/sentry/web/frontend/generic/test_static_media.py:30: in test_versioned
    assert response["Vary"] == "Accept-Encoding"
E   AssertionError: assert 'Accept-Encoding, Cookie' == 'Accept-Encoding'
E     
E     - Accept-Encoding
E     + Accept-Encoding, Cookie
E     ?                ++++++++
tests/sentry/web/frontend/test_organization_avatar.py::OrganizationAvatarTest::test_headerslog
[gw1] linux -- Python 3.13.1 /home/runner/work/sentry/sentry/.venv/bin/python3
tests/sentry/web/frontend/test_organization_avatar.py:22: in test_headers
    assert response.get("Vary") is None
E   assert 'Cookie' is None
E    +  where 'Cookie' = <bound method HttpResponseBase.get of <HttpResponse status_code=200, "image/png">>('Vary')
E    +    where <bound method HttpResponseBase.get of <HttpResponse status_code=200, "image/png">> = <HttpResponse status_code=200, "image/png">.get
tests/sentry/web/frontend/generic/test_static_media.py::StaticMediaTest::test_basiclog
[gw0] linux -- Python 3.13.1 /home/runner/work/sentry/sentry/.venv/bin/python3
tests/sentry/web/frontend/generic/test_static_media.py:19: in test_basic
    assert response["Vary"] == "Accept-Encoding"
E   AssertionError: assert 'Accept-Encoding, Cookie' == 'Accept-Encoding'
E     
E     - Accept-Encoding
E     + Accept-Encoding, Cookie
E     ?                ++++++++
tests/sentry/web/frontend/generic/test_static_media.py::StaticMediaTest::test_frontend_app_assetslog
[gw0] linux -- Python 3.13.1 /home/runner/work/sentry/sentry/.venv/bin/python3
tests/sentry/web/frontend/generic/test_static_media.py:72: in test_frontend_app_assets
    assert response["Vary"] == "Accept-Encoding"
E   AssertionError: assert 'Accept-Encoding, Cookie' == 'Accept-Encoding'
E     
E     - Accept-Encoding
E     + Accept-Encoding, Cookie
E     ?                ++++++++
tests/sentry/web/frontend/generic/test_static_media.py::StaticMediaTest::test_gziplog
[gw1] linux -- Python 3.13.1 /home/runner/work/sentry/sentry/.venv/bin/python3
tests/sentry/web/frontend/generic/test_static_media.py:110: in test_gzip
    assert response["Vary"] == "Accept-Encoding"
E   AssertionError: assert 'Accept-Encoding, Cookie' == 'Accept-Encoding'
E     
E     - Accept-Encoding
E     + Accept-Encoding, Cookie
E     ?                ++++++++
tests/sentry/web/frontend/test_organization_avatar.py::OrganizationAvatarTest::test_origin_headerlog
[gw0] linux -- Python 3.13.1 /home/runner/work/sentry/sentry/.venv/bin/python3
tests/sentry/web/frontend/test_organization_avatar.py:35: in test_origin_header
    assert response.get("Vary") is None
E   assert 'Cookie' is None
E    +  where 'Cookie' = <bound method HttpResponseBase.get of <HttpResponse status_code=200, "image/png">>('Vary')
E    +    where <bound method HttpResponseBase.get of <HttpResponse status_code=200, "image/png">> = <HttpResponse status_code=200, "image/png">.get
tests/sentry/users/web/test_user_avatar.py::UserAvatarTest::test_headers_control_filelog
[gw0] linux -- Python 3.13.1 /home/runner/work/sentry/sentry/.venv/bin/python3
tests/sentry/users/web/test_user_avatar.py:24: in test_headers_control_file
    assert response.get("Vary") is None
E   assert 'Cookie' is None
E    +  where 'Cookie' = <bound method HttpResponseBase.get of <HttpResponse status_code=200, "image/png">>('Vary')
E    +    where <bound method HttpResponseBase.get of <HttpResponse status_code=200, "image/png">> = <HttpResponse status_code=200, "image/png">.get
tests/sentry/web/frontend/test_js_sdk_loader.py::JavaScriptSdkLoaderTest::test_headerslog
[gw0] linux -- Python 3.13.1 /home/runner/work/sentry/sentry/.venv/bin/python3
tests/sentry/web/frontend/test_js_sdk_loader.py:452: in test_headers
    assert "Vary" not in resp, f"Found Vary header: {resp['Vary']}"
E   AssertionError: Found Vary header: Cookie
E   assert 'Vary' not in <HttpResponse status_code=200, "text/javascript">

Avoid accessing request.user for ANONYMOUS_STATIC_PREFIXES paths,
which would add Vary: Cookie and break HTTP caching for static assets.
This matches the pattern used by StaffMiddleware and SuperuserMiddleware.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gricha gricha merged commit a3ce318 into master Apr 6, 2026
80 checks passed
@gricha gricha deleted the gricha/feat/enable-viewer-context-middleware branch April 6, 2026 21:57
joshuarli added a commit that referenced this pull request Apr 6, 2026
#112293 didn't select anything
in sentry or getsentry dispatch for src/sentry/options/defaults.py -
this kind of stuff is evaluated at module load (before any test context
is active) so we don't have coverage data for it

added `src/sentry/options` FULL_SUITE_TRIGGERS and also had claude audit
for more stuff to put in there
george-sentry pushed a commit that referenced this pull request Apr 9, 2026
Enable the `ViewerContextMiddleware` by setting the
`viewer-context.enabled`
option default to `True`. This causes the middleware to populate a
`ViewerContext` (unified caller identity) on every authenticated
request.

The option uses `FLAG_NOSTORE` so the value is read at process startup
from
deploy config (`SENTRY_OPTIONS`). Changing the default here means it
will be
active everywhere unless explicitly overridden to `False` in deploy
config.

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
george-sentry pushed a commit that referenced this pull request Apr 9, 2026
#112293 didn't select anything
in sentry or getsentry dispatch for src/sentry/options/defaults.py -
this kind of stuff is evaluated at module load (before any test context
is active) so we don't have coverage data for it

added `src/sentry/options` FULL_SUITE_TRIGGERS and also had claude audit
for more stuff to put in there
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components Trigger: Override Selective Testing Run the full test suite; necessary in cases where selected tests are flaky due to reshuffling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants