diff --git a/sentry_sdk/integrations/starlite.py b/sentry_sdk/integrations/starlite.py index cc5fad4158..18e69d0aaf 100644 --- a/sentry_sdk/integrations/starlite.py +++ b/sentry_sdk/integrations/starlite.py @@ -6,12 +6,10 @@ from sentry_sdk.data_collection import _apply_key_value_collection_filtering from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version from sentry_sdk.integrations.asgi import SentryAsgiMiddleware -from sentry_sdk.scope import should_send_default_pii from sentry_sdk.traces import SOURCE_FOR_STYLE, SegmentNameSource from sentry_sdk.utils import ( ensure_integration_enabled, event_from_exception, - has_data_collection_enabled, package_version, transaction_from_function, ) @@ -255,23 +253,17 @@ async def handle_wrapper( def event_processor(event: "Event", _: "Hint") -> "Event": request_info = event.get("request", {}) request_info["content_length"] = len(scope.get("_body", b"")) - should_attach_request_body = True - - if has_data_collection_enabled(client.options): - cookies = _apply_key_value_collection_filtering( - items=extracted_request_data["cookies"], - behaviour=client.options["data_collection"]["cookies"], - ) - if cookies: - request_info["cookies"] = cookies - - should_attach_request_body = ( - "incoming_request" - in client.options["data_collection"]["http_bodies"] - ) - elif should_send_default_pii(): - request_info["cookies"] = extracted_request_data["cookies"] + cookies = _apply_key_value_collection_filtering( + items=extracted_request_data["cookies"], + behaviour=client.options["data_collection"]["cookies"], + ) + if cookies: + request_info["cookies"] = cookies + + should_attach_request_body = ( + "incoming_request" in client.options["data_collection"]["http_bodies"] + ) if request_data is not None and should_attach_request_body: request_info["data"] = request_data @@ -309,10 +301,7 @@ def exception_handler(exc: Exception, scope: "StarliteScope", _: "State") -> Non user_info: "Optional[dict[str, Any]]" = None client_options = sentry_sdk.get_client().options - if has_data_collection_enabled(client_options): - if client_options["data_collection"]["user_info"]: - user_info = retrieve_user_from_scope(scope) - elif should_send_default_pii(): + if client_options["data_collection"]["user_info"]: user_info = retrieve_user_from_scope(scope) if user_info and isinstance(user_info, dict): diff --git a/tests/integrations/starlite/test_starlite.py b/tests/integrations/starlite/test_starlite.py index a6d5be2018..d24bc465a9 100644 --- a/tests/integrations/starlite/test_starlite.py +++ b/tests/integrations/starlite/test_starlite.py @@ -13,7 +13,7 @@ from sentry_sdk import capture_message from sentry_sdk._types import SENSITIVE_DATA_SUBSTITUTE from sentry_sdk.integrations.starlite import StarliteIntegration -from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES_LEGACY +from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES def starlite_app_factory(middleware=None, debug=True, exception_handlers=None): @@ -100,7 +100,7 @@ def test_catch_exceptions( expected_message, expected_tx_name, ): - sentry_init(integrations=[StarliteIntegration()]) + sentry_init(integrations=[StarliteIntegration()], data_collection={}) starlite_app = starlite_app_factory() exceptions = capture_exceptions() events = capture_events() @@ -146,6 +146,7 @@ def test_transaction_name_and_source( sentry_init( traces_sample_rate=1.0, integrations=[StarliteIntegration()], + data_collection={}, ) starlite_app = starlite_app_factory() client = TestClient(starlite_app) @@ -168,6 +169,7 @@ def test_middleware_spans(sentry_init, capture_items): sentry_init( traces_sample_rate=1.0, integrations=[StarliteIntegration()], + data_collection={}, ) logging_config = LoggingMiddlewareConfig() @@ -222,6 +224,7 @@ async def do_stuff(message): sentry_init( traces_sample_rate=1.0, integrations=[StarliteIntegration()], + data_collection={}, ) starlite_app = starlite_app_factory(middleware=[SampleMiddleware]) @@ -283,6 +286,7 @@ async def __call__(self, scope, receive, send): sentry_init( traces_sample_rate=1.0, integrations=[StarliteIntegration()], + data_collection={}, ) starlite_app = starlite_app_factory(middleware=[SampleReceiveSendMiddleware]) @@ -315,6 +319,7 @@ async def my_send(*args, **kwargs): sentry_init( traces_sample_rate=1.0, integrations=[StarliteIntegration()], + data_collection={}, ) starlite_app = starlite_app_factory(middleware=[SamplePartialReceiveSendMiddleware]) @@ -365,6 +370,7 @@ def test_span_origin(sentry_init, capture_items): sentry_init( integrations=[StarliteIntegration()], traces_sample_rate=1.0, + data_collection={}, ) logging_config = LoggingMiddlewareConfig() @@ -399,10 +405,10 @@ def test_span_origin(sentry_init, capture_items): @pytest.mark.parametrize( - "init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES_LEGACY + "data_collection, expect_user", DATA_COLLECTION_USER_INFO_CASES ) def test_starlite_scope_user_on_exception_event( - sentry_init, capture_exceptions, capture_events, init_kwargs, expect_user + sentry_init, capture_exceptions, capture_events, data_collection, expect_user ): class TestUserMiddleware(AbstractMiddleware): async def __call__(self, scope, receive, send): @@ -413,7 +419,7 @@ async def __call__(self, scope, receive, send): } await self.app(scope, receive, send) - sentry_init(integrations=[StarliteIntegration()], **init_kwargs) + sentry_init(integrations=[StarliteIntegration()], data_collection=data_collection) starlite_app = starlite_app_factory(middleware=[TestUserMiddleware]) exceptions = capture_exceptions() events = capture_events() @@ -446,7 +452,6 @@ async def __call__(self, scope, receive, send): @pytest.mark.parametrize( "data_collection, expect_body", [ - pytest.param(None, True, id="no_data_collection_experiment"), pytest.param({}, True, id="data_collection_http_bodies_default"), pytest.param( {"http_bodies": ["incoming_request"]}, @@ -483,57 +488,26 @@ def test_request_body_data_collection( assert "data" not in event["request"] -def test_request_body_data_collection_wins_over_send_default_pii( - sentry_init, capture_events -): - sentry_init( - traces_sample_rate=1.0, - integrations=[StarliteIntegration()], - send_default_pii=True, - data_collection={"http_bodies": []}, - ) - - starlite_app = starlite_app_factory() - events = capture_events() - - client = TestClient(starlite_app) - client.post("/body/json", json={"foo": {"bar": "baz", "qux": ["1", "2", "3"]}}) - - (event,) = events - - assert "data" not in event["request"] - - @pytest.mark.parametrize( - "init_kwargs, expected_cookies", + "data_collection, expected_cookies", [ pytest.param( - {"send_default_pii": True}, + {}, { - "jwt": "tokenval", + "jwt": SENSITIVE_DATA_SUBSTITUTE, "theme": "dark", "lang": "en", - "identity": "alice", + "identity": SENSITIVE_DATA_SUBSTITUTE, }, - id="send_default_pii_true", - ), - pytest.param( - {"send_default_pii": False}, - None, - id="send_default_pii_false", - ), - pytest.param( - {}, - None, - id="defaults", + id="data_collection_default", ), pytest.param( - {"data_collection": {"cookies": {"mode": "off"}}}, + {"cookies": {"mode": "off"}}, None, id="data_collection_off", ), pytest.param( - {"data_collection": {"cookies": {"mode": "denylist"}}}, + {"cookies": {"mode": "denylist"}}, { "jwt": SENSITIVE_DATA_SUBSTITUTE, "theme": "dark", @@ -543,7 +517,7 @@ def test_request_body_data_collection_wins_over_send_default_pii( id="data_collection_denylist_default", ), pytest.param( - {"data_collection": {"cookies": {"mode": "denylist", "terms": ["theme"]}}}, + {"cookies": {"mode": "denylist", "terms": ["theme"]}}, { "jwt": SENSITIVE_DATA_SUBSTITUTE, "theme": SENSITIVE_DATA_SUBSTITUTE, @@ -553,7 +527,7 @@ def test_request_body_data_collection_wins_over_send_default_pii( id="data_collection_denylist_custom_terms", ), pytest.param( - {"data_collection": {"cookies": {"mode": "allowlist", "terms": ["theme"]}}}, + {"cookies": {"mode": "allowlist", "terms": ["theme"]}}, { "jwt": SENSITIVE_DATA_SUBSTITUTE, "theme": "dark", @@ -563,11 +537,7 @@ def test_request_body_data_collection_wins_over_send_default_pii( id="data_collection_allowlist", ), pytest.param( - { - "data_collection": { - "cookies": {"mode": "allowlist", "terms": ["identity"]} - } - }, + {"cookies": {"mode": "allowlist", "terms": ["identity"]}}, { "jwt": SENSITIVE_DATA_SUBSTITUTE, "theme": SENSITIVE_DATA_SUBSTITUTE, @@ -576,28 +546,15 @@ def test_request_body_data_collection_wins_over_send_default_pii( }, id="data_collection_allowlist_sensitive_term", ), - pytest.param( - { - "send_default_pii": False, - "data_collection": {"cookies": {"mode": "denylist"}}, - }, - { - "jwt": SENSITIVE_DATA_SUBSTITUTE, - "theme": "dark", - "lang": "en", - "identity": SENSITIVE_DATA_SUBSTITUTE, - }, - id="data_collection_wins_over_send_default_pii", - ), ], ) def test_cookie_data_collection( - sentry_init, capture_events, init_kwargs, expected_cookies + sentry_init, capture_events, data_collection, expected_cookies ): sentry_init( traces_sample_rate=1.0, integrations=[StarliteIntegration()], - **init_kwargs, + data_collection=data_collection, ) starlite_app = starlite_app_factory()