|
6 | 6 | from unittest import mock |
7 | 7 |
|
8 | 8 | import fastapi |
| 9 | +import starlette |
9 | 10 | from fastapi import FastAPI, HTTPException, Request |
10 | 11 | from fastapi.testclient import TestClient |
11 | 12 | from fastapi.middleware.trustedhost import TrustedHostMiddleware |
|
20 | 21 |
|
21 | 22 |
|
22 | 23 | FASTAPI_VERSION = parse_version(fastapi.__version__) |
| 24 | +STARLETTE_VERSION = parse_version(starlette.__version__) |
23 | 25 |
|
24 | 26 | from tests.integrations.conftest import parametrize_test_configurable_status_codes |
25 | 27 | from tests.integrations.starlette import test_starlette |
@@ -245,6 +247,142 @@ def test_active_thread_id_span_streaming(sentry_init, capture_items, endpoint): |
245 | 247 | assert str(data["active"]) == segments[0]["attributes"]["thread.id"] |
246 | 248 |
|
247 | 249 |
|
| 250 | +def _post_body_fastapi_app(handler_awaitable): |
| 251 | + app = FastAPI() |
| 252 | + |
| 253 | + @app.post("/body") |
| 254 | + async def _route(request: Request): |
| 255 | + await handler_awaitable(request) |
| 256 | + return {"ok": True} |
| 257 | + |
| 258 | + return app |
| 259 | + |
| 260 | + |
| 261 | +@pytest.mark.parametrize("middleware_spans", [False, True]) |
| 262 | +def test_request_body_data_does_not_scrub_pii_span_streaming( |
| 263 | + sentry_init, capture_items, middleware_spans |
| 264 | +): |
| 265 | + sentry_init( |
| 266 | + auto_enabling_integrations=False, |
| 267 | + integrations=[ |
| 268 | + StarletteIntegration(middleware_spans=middleware_spans), |
| 269 | + FastApiIntegration(middleware_spans=middleware_spans), |
| 270 | + ], |
| 271 | + traces_sample_rate=1.0, |
| 272 | + _experiments={"trace_lifecycle": "stream"}, |
| 273 | + ) |
| 274 | + |
| 275 | + async def _read_json(request): |
| 276 | + await request.json() |
| 277 | + |
| 278 | + items = capture_items("span") |
| 279 | + |
| 280 | + client = TestClient(_post_body_fastapi_app(_read_json)) |
| 281 | + response = client.post( |
| 282 | + "/body", |
| 283 | + json={ |
| 284 | + "password": "ohno", |
| 285 | + "authorization": "Bearer token", |
| 286 | + "message": "hello", |
| 287 | + }, |
| 288 | + ) |
| 289 | + assert response.status_code == 200 |
| 290 | + |
| 291 | + sentry_sdk.flush() |
| 292 | + |
| 293 | + segments = [item.payload for item in items if item.payload.get("is_segment")] |
| 294 | + assert len(segments) == 1 |
| 295 | + attr = segments[0]["attributes"]["http.request.body.data"] |
| 296 | + |
| 297 | + # Going forward, the sanitization of data will need to happen within the `before_send_span` hooks |
| 298 | + # See https://sentry.slack.com/archives/C09RR0KD2N7/p1776951331206129?thread_ts=1776951227.440659&cid=C09RR0KD2N7 |
| 299 | + assert "ohno" in attr |
| 300 | + assert "Bearer token" in attr |
| 301 | + assert "hello" in attr |
| 302 | + |
| 303 | + |
| 304 | +@pytest.mark.skipif( |
| 305 | + STARLETTE_VERSION < (0, 21), |
| 306 | + reason="Requires Starlette >= 0.21, because earlier versions use a requests-based TestClient which does not support the 'content' kwarg", |
| 307 | +) |
| 308 | +@pytest.mark.parametrize("middleware_spans", [False, True]) |
| 309 | +def test_request_body_data_annotated_value_top_level_span_streaming( |
| 310 | + sentry_init, capture_items, middleware_spans |
| 311 | +): |
| 312 | + sentry_init( |
| 313 | + auto_enabling_integrations=False, |
| 314 | + integrations=[ |
| 315 | + StarletteIntegration(middleware_spans=middleware_spans), |
| 316 | + FastApiIntegration(middleware_spans=middleware_spans), |
| 317 | + ], |
| 318 | + traces_sample_rate=1.0, |
| 319 | + _experiments={"trace_lifecycle": "stream"}, |
| 320 | + ) |
| 321 | + |
| 322 | + async def _read_body(request): |
| 323 | + await request.body() |
| 324 | + |
| 325 | + items = capture_items("span") |
| 326 | + |
| 327 | + client = TestClient(_post_body_fastapi_app(_read_body)) |
| 328 | + response = client.post( |
| 329 | + "/body", |
| 330 | + content=b"not json and not form", |
| 331 | + headers={"content-type": "application/octet-stream"}, |
| 332 | + ) |
| 333 | + assert response.status_code == 200 |
| 334 | + |
| 335 | + sentry_sdk.flush() |
| 336 | + |
| 337 | + segments = [item.payload for item in items if item.payload.get("is_segment")] |
| 338 | + assert len(segments) == 1 |
| 339 | + attr = segments[0]["attributes"]["http.request.body.data"] |
| 340 | + |
| 341 | + assert isinstance(attr, str) |
| 342 | + assert attr == '""' |
| 343 | + |
| 344 | + |
| 345 | +@pytest.mark.parametrize("middleware_spans", [False, True]) |
| 346 | +def test_request_body_data_annotated_value_nested_span_streaming( |
| 347 | + sentry_init, capture_items, middleware_spans |
| 348 | +): |
| 349 | + pytest.importorskip("multipart") |
| 350 | + |
| 351 | + sentry_init( |
| 352 | + auto_enabling_integrations=False, |
| 353 | + integrations=[ |
| 354 | + StarletteIntegration(middleware_spans=middleware_spans), |
| 355 | + FastApiIntegration(middleware_spans=middleware_spans), |
| 356 | + ], |
| 357 | + traces_sample_rate=1.0, |
| 358 | + _experiments={"trace_lifecycle": "stream"}, |
| 359 | + ) |
| 360 | + |
| 361 | + async def _read_form(request): |
| 362 | + await request.form() |
| 363 | + |
| 364 | + items = capture_items("span") |
| 365 | + |
| 366 | + client = TestClient(_post_body_fastapi_app(_read_form)) |
| 367 | + response = client.post( |
| 368 | + "/body", |
| 369 | + data={"name": "erica"}, |
| 370 | + files={"avatar": ("photo.jpg", b"fake-bytes", "image/jpeg")}, |
| 371 | + ) |
| 372 | + assert response.status_code == 200 |
| 373 | + |
| 374 | + sentry_sdk.flush() |
| 375 | + |
| 376 | + segments = [item.payload for item in items if item.payload.get("is_segment")] |
| 377 | + assert len(segments) == 1 |
| 378 | + attr = segments[0]["attributes"]["http.request.body.data"] |
| 379 | + |
| 380 | + assert isinstance(attr, str) |
| 381 | + parsed = json.loads(attr) |
| 382 | + assert parsed["name"] == "erica" |
| 383 | + assert "fake-bytes" not in attr |
| 384 | + |
| 385 | + |
248 | 386 | @pytest.mark.parametrize("span_streaming", [True, False]) |
249 | 387 | @pytest.mark.asyncio |
250 | 388 | async def test_original_request_not_scrubbed( |
|
0 commit comments