Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions sentry_sdk/integrations/litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
)
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from sentry_sdk.integrations.logging import ignore_logger_for_events
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,
)
Expand Down Expand Up @@ -282,21 +280,16 @@ def event_processor(event: "Event", _: "Hint") -> "Event":
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

Expand Down Expand Up @@ -326,10 +319,7 @@ def exception_handler(exc: Exception, scope: "LitestarScope") -> None:
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):
Expand Down
91 changes: 26 additions & 65 deletions tests/integrations/litestar/test_litestar.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE
from tests.conftest import ApproxDict
from tests.integrations.conftest import parametrize_test_configurable_status_codes
from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES_LEGACY
from tests.integrations.utils import DATA_COLLECTION_USER_INFO_CASES


def litestar_app_factory(middleware=None, debug=True, exception_handlers=None):
Expand Down Expand Up @@ -108,6 +108,7 @@ def test_catch_exceptions(
):
sentry_init(
integrations=[LitestarIntegration()],
data_collection={},
)
litestar_app = litestar_app_factory()
client = TestClient(litestar_app)
Expand Down Expand Up @@ -154,6 +155,7 @@ def test_segment_name_and_source(
sentry_init(
traces_sample_rate=1.0,
integrations=[LitestarIntegration()],
data_collection={},
)
litestar_app = litestar_app_factory()
client = TestClient(litestar_app)
Expand All @@ -179,6 +181,7 @@ def test_middleware_spans(
sentry_init(
traces_sample_rate=1.0,
integrations=[LitestarIntegration()],
data_collection={},
)

logging_config = LoggingMiddlewareConfig()
Expand Down Expand Up @@ -235,6 +238,7 @@ async def do_stuff(message):
sentry_init(
traces_sample_rate=1.0,
integrations=[LitestarIntegration()],
data_collection={},
)

litestar_app = litestar_app_factory(middleware=[SampleMiddleware])
Expand Down Expand Up @@ -315,6 +319,7 @@ async def __call__(self, scope, receive, send):
sentry_init(
traces_sample_rate=1.0,
integrations=[LitestarIntegration()],
data_collection={},
)
litestar_app = litestar_app_factory(middleware=[SampleReceiveSendMiddleware])

Expand Down Expand Up @@ -350,6 +355,7 @@ async def my_send(*args, **kwargs):
sentry_init(
traces_sample_rate=1.0,
integrations=[LitestarIntegration()],
data_collection={},
)

litestar_app = litestar_app_factory(middleware=[SamplePartialReceiveSendMiddleware])
Expand Down Expand Up @@ -419,6 +425,7 @@ def test_span_origin(
sentry_init(
integrations=[LitestarIntegration()],
traces_sample_rate=1.0,
data_collection={},
)

logging_config = LoggingMiddlewareConfig()
Expand Down Expand Up @@ -449,13 +456,13 @@ def test_span_origin(


@pytest.mark.parametrize(
"init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES_LEGACY
"data_collection, expect_user", DATA_COLLECTION_USER_INFO_CASES
)
def test_litestar_scope_user_on_exception_event(
sentry_init,
capture_exceptions,
capture_items,
init_kwargs,
data_collection,
expect_user,
):
class TestUserMiddleware(AbstractMiddleware):
Expand All @@ -469,7 +476,7 @@ async def __call__(self, scope, receive, send):

sentry_init(
integrations=[LitestarIntegration()],
**init_kwargs,
data_collection=data_collection,
)

litestar_app = litestar_app_factory(middleware=[TestUserMiddleware])
Expand Down Expand Up @@ -503,7 +510,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"]},
Expand Down Expand Up @@ -540,57 +546,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=[LitestarIntegration()],
send_default_pii=True,
data_collection={"http_bodies": []},
)

litestar_app = litestar_app_factory()
events = capture_events()

client = TestClient(litestar_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",
Expand All @@ -600,7 +575,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,
Expand All @@ -610,7 +585,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",
Expand All @@ -620,11 +595,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,
Expand All @@ -633,28 +604,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=[LitestarIntegration()],
**init_kwargs,
data_collection=data_collection,
)

litestar_app = litestar_app_factory()
Expand Down Expand Up @@ -686,6 +644,7 @@ def test_configurable_status_codes_handler(
)
sentry_init(
integrations=[LitestarIntegration(**integration_kwargs)],
data_collection={},
)

@get("/error")
Expand Down Expand Up @@ -719,6 +678,7 @@ def test_configurable_status_codes_middleware(

sentry_init(
integrations=[LitestarIntegration(**integration_kwargs)],
data_collection={},
)

def create_raising_middleware(app):
Expand Down Expand Up @@ -747,6 +707,7 @@ def test_catch_non_http_exceptions_in_middleware(
):
sentry_init(
integrations=[LitestarIntegration()],
data_collection={},
)

def create_raising_middleware(app):
Expand Down
Loading