Skip to content

Commit 1ec2d5d

Browse files
authored
fix(starlite): Gate request body collection on data_collection experiment (#7260)
Attach `request.data` only when "incoming_request" is present in `data_collection.http_bodies`. When the experiment is unset, behaviour is unchanged; when it is set, it takes precedence over `send_default_pii`. Refs PY-2419 Refs #6283
1 parent 6875bb6 commit 1ec2d5d

2 files changed

Lines changed: 82 additions & 2 deletions

File tree

sentry_sdk/integrations/starlite.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,24 @@ async def handle_wrapper(
268268
def event_processor(event: "Event", _: "Hint") -> "Event":
269269
request_info = event.get("request", {})
270270
request_info["content_length"] = len(scope.get("_body", b""))
271+
should_attach_request_body = True
272+
271273
if has_data_collection_enabled(client.options):
272274
cookies = _apply_key_value_collection_filtering(
273275
items=extracted_request_data["cookies"],
274276
behaviour=client.options["data_collection"]["cookies"],
275277
)
276278
if cookies:
277279
request_info["cookies"] = cookies
280+
281+
should_attach_request_body = (
282+
"incoming_request"
283+
in client.options["data_collection"]["http_bodies"]
284+
)
278285
elif should_send_default_pii():
279286
request_info["cookies"] = extracted_request_data["cookies"]
280-
if request_data is not None:
287+
288+
if request_data is not None and should_attach_request_body:
281289
request_info["data"] = request_data
282290

283291
event["request"] = deepcopy(request_info)

tests/integrations/starlite/test_starlite.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Any, Dict
55

66
import pytest
7-
from starlite import AbstractMiddleware, Controller, LoggingConfig, Starlite, get
7+
from starlite import AbstractMiddleware, Controller, LoggingConfig, Starlite, get, post
88
from starlite.middleware import LoggingMiddlewareConfig, RateLimitConfig
99
from starlite.middleware.session.memory_backend import MemoryBackendConfig
1010
from starlite.testing import TestClient
@@ -43,6 +43,11 @@ async def message_with_id() -> "Dict[str, Any]":
4343
capture_message("hi")
4444
return {"status": "ok"}
4545

46+
@post("/body/json")
47+
async def body_json(data: Dict[str, Any]) -> Dict[str, Any]:
48+
capture_message("hi")
49+
return {"status": "ok"}
50+
4651
logging_config = LoggingConfig()
4752

4853
app = Starlite(
@@ -51,6 +56,7 @@ async def message_with_id() -> "Dict[str, Any]":
5156
custom_error,
5257
message,
5358
message_with_id,
59+
body_json,
5460
MyController,
5561
],
5662
debug=debug,
@@ -559,6 +565,72 @@ async def __call__(self, scope, receive, send):
559565
COOKIE_HEADER = "jwt=tokenval; theme=dark; lang=en; identity=alice"
560566

561567

568+
@pytest.mark.parametrize(
569+
"data_collection, expect_body",
570+
[
571+
pytest.param(None, True, id="no_data_collection_experiment"),
572+
pytest.param({}, True, id="data_collection_http_bodies_default"),
573+
pytest.param(
574+
{"http_bodies": ["incoming_request"]},
575+
True,
576+
id="data_collection_http_bodies_incoming_request",
577+
),
578+
pytest.param(
579+
{"http_bodies": []}, False, id="data_collection_http_bodies_empty"
580+
),
581+
],
582+
)
583+
def test_request_body_data_collection(
584+
sentry_init, capture_events, data_collection, expect_body
585+
):
586+
sentry_init(
587+
traces_sample_rate=1.0,
588+
integrations=[StarliteIntegration()],
589+
_experiments=(
590+
{} if data_collection is None else {"data_collection": data_collection}
591+
),
592+
)
593+
594+
starlite_app = starlite_app_factory()
595+
events = capture_events()
596+
597+
body = {"foo": {"bar": "baz", "qux": ["1", "2", "3"]}}
598+
599+
client = TestClient(starlite_app)
600+
client.post("/body/json", json=body)
601+
602+
(event, transaction_event) = events
603+
604+
if expect_body:
605+
assert event["request"]["data"] == body
606+
assert transaction_event["request"]["data"] == body
607+
else:
608+
assert "data" not in event["request"]
609+
assert "data" not in transaction_event["request"]
610+
611+
612+
def test_request_body_data_collection_wins_over_send_default_pii(
613+
sentry_init, capture_events
614+
):
615+
sentry_init(
616+
traces_sample_rate=1.0,
617+
integrations=[StarliteIntegration()],
618+
send_default_pii=True,
619+
_experiments={"data_collection": {"http_bodies": []}},
620+
)
621+
622+
starlite_app = starlite_app_factory()
623+
events = capture_events()
624+
625+
client = TestClient(starlite_app)
626+
client.post("/body/json", json={"foo": {"bar": "baz", "qux": ["1", "2", "3"]}})
627+
628+
(event, transaction_event) = events
629+
630+
assert "data" not in event["request"]
631+
assert "data" not in transaction_event["request"]
632+
633+
562634
@pytest.mark.parametrize(
563635
"init_kwargs, expected_cookies",
564636
[

0 commit comments

Comments
 (0)