Skip to content

Commit c4e1232

Browse files
always attach cookies
1 parent 32ea46f commit c4e1232

File tree

4 files changed

+154
-34
lines changed

4 files changed

+154
-34
lines changed

sentry_sdk/integrations/fastapi.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,38 +112,44 @@ async def _sentry_app(*args, **kwargs):
112112
sentry_scope = sentry_sdk.get_isolation_scope()
113113
sentry_scope._name = FastApiIntegration.identifier
114114

115-
def _make_request_event_processor(info):
115+
def _make_cookies_event_processor(cookies):
116116
# type: (Optional[Dict[str, Any]]) -> Callable[[Event, Dict[str, Any]], Event]
117117
def event_processor(event, hint):
118118
# type: (Event, Dict[str, Any]) -> Event
119+
if cookies and should_send_default_pii():
120+
event.get("request", {})["cookies"] = deepcopy(cookies)
119121

120-
# Extract information from request
121-
request_info = event.get("request", {})
122-
if info:
123-
if "cookies" in info and should_send_default_pii():
124-
request_info["cookies"] = info["cookies"]
125-
if "data" in info:
126-
request_info["data"] = info["data"]
127-
event["request"] = deepcopy(request_info)
122+
return event
123+
124+
return event_processor
125+
126+
def _make_request_body_event_processor(info):
127+
# type: (Optional[Dict[str, Any]]) -> Callable[[Event, Dict[str, Any]], Event]
128+
def event_processor(event, hint):
129+
# type: (Event, Dict[str, Any]) -> Event
130+
if info and "data" in info:
131+
event.get("request", {})["data"] = deepcopy(info["data"])
128132

129133
return event
130134

131135
return event_processor
132136

137+
extractor = StarletteRequestExtractor(request)
138+
cookies = extractor.extract_cookies_from_request()
139+
sentry_scope.add_event_processor(_make_cookies_event_processor(cookies))
140+
133141
try:
134142
response = await old_app(*args, **kwargs)
135143
except Exception as exception:
136-
extractor = StarletteRequestExtractor(request)
137144
info = await extractor.extract_request_info()
138-
139-
sentry_scope.add_event_processor(_make_request_event_processor(info))
145+
sentry_scope.add_event_processor(
146+
_make_request_body_event_processor(info)
147+
)
140148

141149
raise exception
142150

143-
extractor = StarletteRequestExtractor(request)
144151
info = await extractor.extract_request_info()
145-
146-
sentry_scope.add_event_processor(_make_request_event_processor(info))
152+
sentry_scope.add_event_processor(_make_request_body_event_processor(info))
147153

148154
return response
149155

sentry_sdk/integrations/starlette.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -501,40 +501,46 @@ async def _sentry_async_func(*args, **kwargs):
501501
sentry_scope = sentry_sdk.get_isolation_scope()
502502
sentry_scope._name = StarletteIntegration.identifier
503503

504-
def _make_request_event_processor(info):
504+
def _make_cookies_event_processor(cookies):
505505
# type: (Optional[Dict[str, Any]]) -> Callable[[Event, Dict[str, Any]], Event]
506506
def event_processor(event, hint):
507507
# type: (Event, Dict[str, Any]) -> Event
508+
if cookies and should_send_default_pii():
509+
event.get("request", {})["cookies"] = deepcopy(cookies)
508510

509-
# Add info from request to event
510-
request_info = event.get("request", {})
511-
if info:
512-
if "cookies" in info and should_send_default_pii():
513-
request_info["cookies"] = info["cookies"]
514-
if "data" in info:
515-
request_info["data"] = info["data"]
516-
event["request"] = deepcopy(request_info)
511+
return event
512+
513+
return event_processor
514+
515+
def _make_request_body_event_processor(info):
516+
# type: (Optional[Dict[str, Any]]) -> Callable[[Event, Dict[str, Any]], Event]
517+
def event_processor(event, hint):
518+
# type: (Event, Dict[str, Any]) -> Event
519+
if info and "data" in info:
520+
event.get("request", {})["data"] = deepcopy(info["data"])
517521

518522
return event
519523

520524
return event_processor
521525

526+
extractor = StarletteRequestExtractor(request)
527+
cookies = extractor.extract_cookies_from_request()
528+
sentry_scope.add_event_processor(_make_cookies_event_processor(cookies))
529+
522530
try:
523531
response = await old_func(*args, **kwargs)
524532
except Exception as exception:
525-
extractor = StarletteRequestExtractor(request)
526533
info = await extractor.extract_request_info()
527-
528534
sentry_scope.add_event_processor(
529-
_make_request_event_processor(info)
535+
_make_request_body_event_processor(info)
530536
)
531537

532538
raise exception
533539

534-
extractor = StarletteRequestExtractor(request)
535540
info = await extractor.extract_request_info()
536-
537-
sentry_scope.add_event_processor(_make_request_event_processor(info))
541+
sentry_scope.add_event_processor(
542+
_make_request_body_event_processor(info)
543+
)
538544

539545
return response
540546

tests/integrations/fastapi/test_fastapi.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,46 @@ def test_active_thread_id(sentry_init, capture_envelopes, teardown_profiling, en
223223
assert str(data["active"]) == trace_context["data"]["thread.id"]
224224

225225

226+
def test_cookies_available(sentry_init, capture_events):
227+
sentry_init(
228+
integrations=[StarletteIntegration(), FastApiIntegration()],
229+
send_default_pii=True,
230+
)
231+
232+
app = FastAPI()
233+
234+
@app.post("/exception")
235+
async def _exception(request: Request):
236+
logging.critical("Oh no!")
237+
return {"error": "Oh no!"}
238+
239+
events = capture_events()
240+
241+
client = TestClient(app)
242+
243+
try:
244+
client.post(
245+
"/exception",
246+
json=BODY_JSON,
247+
cookies={
248+
"tasty_cookie": "strawberry",
249+
"yummy_cookie": "choco",
250+
},
251+
)
252+
except ZeroDivisionError:
253+
capture_exception()
254+
255+
event = events[0]
256+
assert event["request"]["cookies"] == {
257+
"tasty_cookie": "strawberry",
258+
"yummy_cookie": "choco",
259+
}
260+
261+
226262
def test_request_body_not_cached_with_exception(sentry_init, capture_events):
227263
sentry_init(
228264
integrations=[StarletteIntegration(), FastApiIntegration()],
265+
send_default_pii=True,
229266
)
230267

231268
app = FastAPI()
@@ -243,17 +280,26 @@ async def _exception(request: Request):
243280
client.post(
244281
"/exception",
245282
json=BODY_JSON,
283+
cookies={
284+
"tasty_cookie": "strawberry",
285+
"yummy_cookie": "choco",
286+
},
246287
)
247288
except ZeroDivisionError:
248289
capture_exception()
249290

250291
event = events[0]
292+
assert event["request"]["cookies"] == {
293+
"tasty_cookie": "strawberry",
294+
"yummy_cookie": "choco",
295+
}
251296
assert event["request"]["data"] == ""
252297

253298

254299
def test_request_body_cached_with_exception(sentry_init, capture_events):
255300
sentry_init(
256301
integrations=[StarletteIntegration(), FastApiIntegration()],
302+
send_default_pii=True,
257303
)
258304

259305
app = FastAPI()
@@ -272,11 +318,19 @@ async def _exception(request: Request):
272318
client.post(
273319
"/exception",
274320
json=BODY_JSON,
321+
cookies={
322+
"tasty_cookie": "strawberry",
323+
"yummy_cookie": "choco",
324+
},
275325
)
276326
except ZeroDivisionError:
277327
capture_exception()
278328

279329
event = events[0]
330+
assert event["request"]["cookies"] == {
331+
"tasty_cookie": "strawberry",
332+
"yummy_cookie": "choco",
333+
}
280334
assert event["request"]["data"] == BODY_JSON
281335

282336

tests/integrations/starlette/test_starlette.py

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -975,16 +975,53 @@ def test_active_thread_id(sentry_init, capture_envelopes, teardown_profiling, en
975975
assert str(data["active"]) == trace_context["data"]["thread.id"]
976976

977977

978-
def test_request_body_not_cached_with_exception(sentry_init, capture_events):
978+
def test_cookies_available(sentry_init, capture_events):
979979
sentry_init(
980980
integrations=[StarletteIntegration()],
981+
send_default_pii=True,
982+
)
983+
984+
events = capture_events()
985+
986+
async def _exception(request: Request):
987+
logging.critical("Oh no!")
988+
return starlette.responses.JSONResponse({"status": "Oh no!"})
989+
990+
app = starlette.applications.Starlette(
991+
routes=[
992+
starlette.routing.Route("/exception", _exception, methods=["POST"]),
993+
],
994+
)
995+
996+
client = TestClient(app)
997+
998+
client.post(
999+
"/exception",
1000+
json=BODY_JSON,
1001+
cookies={
1002+
"tasty_cookie": "strawberry",
1003+
"yummy_cookie": "choco",
1004+
},
1005+
)
1006+
1007+
event = events[0]
1008+
assert event["request"]["cookies"] == {
1009+
"tasty_cookie": "strawberry",
1010+
"yummy_cookie": "choco",
1011+
}
1012+
1013+
1014+
def test_request_body_not_cached_exception(sentry_init, capture_events):
1015+
sentry_init(
1016+
integrations=[StarletteIntegration()],
1017+
send_default_pii=True,
9811018
)
9821019

9831020
events = capture_events()
9841021

9851022
async def _exception(request):
9861023
1 / 0
987-
return {"error": "Oh no!"}
1024+
return starlette.responses.JSONResponse({"status": "Oh no!"})
9881025

9891026
app = starlette.applications.Starlette(
9901027
routes=[
@@ -998,25 +1035,34 @@ async def _exception(request):
9981035
client.post(
9991036
"/exception",
10001037
json=BODY_JSON,
1038+
cookies={
1039+
"tasty_cookie": "strawberry",
1040+
"yummy_cookie": "choco",
1041+
},
10011042
)
10021043
except ZeroDivisionError:
10031044
capture_exception()
10041045

10051046
event = events[0]
1047+
assert event["request"]["cookies"] == {
1048+
"tasty_cookie": "strawberry",
1049+
"yummy_cookie": "choco",
1050+
}
10061051
assert event["request"]["data"] == ""
10071052

10081053

1009-
def test_request_body_cached_with_exception(sentry_init, capture_events):
1054+
def test_request_body_cached_exception(sentry_init, capture_events):
10101055
sentry_init(
10111056
integrations=[StarletteIntegration()],
1057+
send_default_pii=True,
10121058
)
10131059

10141060
events = capture_events()
10151061

10161062
async def _exception(request):
10171063
request.json()
10181064
1 / 0
1019-
return {"error": "Oh no!"}
1065+
return starlette.responses.JSONResponse({"status": "Oh no!"})
10201066

10211067
app = starlette.applications.Starlette(
10221068
routes=[
@@ -1030,11 +1076,19 @@ async def _exception(request):
10301076
client.post(
10311077
"/exception",
10321078
json=BODY_JSON,
1079+
cookies={
1080+
"tasty_cookie": "strawberry",
1081+
"yummy_cookie": "choco",
1082+
},
10331083
)
10341084
except ZeroDivisionError:
10351085
capture_exception()
10361086

10371087
event = events[0]
1088+
assert event["request"]["cookies"] == {
1089+
"tasty_cookie": "strawberry",
1090+
"yummy_cookie": "choco",
1091+
}
10381092
assert event["request"]["data"] == BODY_JSON
10391093

10401094

0 commit comments

Comments
 (0)