@@ -99,3 +99,118 @@ def test_scrub_django_custom_session_cookies_filtered(
9999 "csrf_secret" : "[Filtered]" ,
100100 "foo" : "bar" ,
101101 }
102+
103+
104+ @pytest .mark .forked
105+ @pytest_mark_django_db_decorator ()
106+ @pytest .mark .parametrize (
107+ "cookies_to_set, data_collection, expected_cookies" ,
108+ [
109+ pytest .param (
110+ {"sessionid" : "123" , "csrftoken" : "456" , "foo" : "bar" },
111+ {"cookies" : {"mode" : "off" }},
112+ None ,
113+ id = "off" ,
114+ ),
115+ pytest .param (
116+ {"sessionid" : "123" , "csrftoken" : "456" , "foo" : "bar" },
117+ {"cookies" : {"mode" : "denylist" }},
118+ {
119+ "sessionid" : "[Filtered]" ,
120+ "csrftoken" : "[Filtered]" ,
121+ "foo" : "bar" ,
122+ },
123+ id = "denylist-default" ,
124+ ),
125+ pytest .param (
126+ {"sessionid" : "123" , "csrftoken" : "456" , "foo" : "bar" },
127+ {"cookies" : {"mode" : "denylist" , "terms" : ["foo" ]}},
128+ {
129+ "sessionid" : "[Filtered]" ,
130+ "csrftoken" : "[Filtered]" ,
131+ "foo" : "[Filtered]" ,
132+ },
133+ id = "denylist-extra-terms" ,
134+ ),
135+ pytest .param (
136+ {"sessionid" : "123" , "csrftoken" : "456" , "foo" : "bar" , "bar" : "baz" },
137+ {"cookies" : {"mode" : "allowlist" , "terms" : ["foo" ]}},
138+ {
139+ "sessionid" : "[Filtered]" ,
140+ "csrftoken" : "[Filtered]" ,
141+ "foo" : "bar" ,
142+ "bar" : "[Filtered]" ,
143+ },
144+ id = "allowlist" ,
145+ ),
146+ pytest .param (
147+ {"sessionid" : "123" , "csrftoken" : "456" , "foo" : "bar" , "bar" : "baz" },
148+ {"cookies" : {"mode" : "allowlist" , "terms" : ["sessionid" , "foo" ]}},
149+ {
150+ "sessionid" : "[Filtered]" ,
151+ "csrftoken" : "[Filtered]" ,
152+ "foo" : "bar" ,
153+ "bar" : "[Filtered]" ,
154+ },
155+ id = "allowlist-cannot-override-sensitive" ,
156+ ),
157+ pytest .param (
158+ {"sessionid" : "123" , "csrftoken" : "456" , "foo" : "bar" },
159+ {},
160+ {
161+ "sessionid" : "[Filtered]" ,
162+ "csrftoken" : "[Filtered]" ,
163+ "foo" : "bar" ,
164+ },
165+ id = "cookies-omitted-defaults-to-denylist" ,
166+ ),
167+ ],
168+ )
169+ def test_data_collection_cookies (
170+ sentry_init ,
171+ client ,
172+ capture_items ,
173+ cookies_to_set ,
174+ data_collection ,
175+ expected_cookies ,
176+ ):
177+ sentry_init (
178+ integrations = [DjangoIntegration ()],
179+ _experiments = {"data_collection" : data_collection },
180+ )
181+ items = capture_items ("event" )
182+ for name , value in cookies_to_set .items ():
183+ werkzeug_set_cookie (client , "localhost" , name , value )
184+ client .get (reverse ("view_exc" ))
185+
186+ (event ,) = (item .payload for item in items if item .type == "event" )
187+ if expected_cookies is None :
188+ assert "cookies" not in event ["request" ]
189+ else :
190+ assert event ["request" ]["cookies" ] == expected_cookies
191+
192+
193+ @pytest .mark .forked
194+ @pytest_mark_django_db_decorator ()
195+ def test_data_collection_cookies_precedence_over_send_default_pii (
196+ sentry_init , client , capture_items
197+ ):
198+ # ``data_collection`` is the single source of truth: even with
199+ # ``send_default_pii=False``, the configured cookie behaviour still applies.
200+ sentry_init (
201+ integrations = [DjangoIntegration ()],
202+ send_default_pii = False ,
203+ _experiments = {"data_collection" : {"cookies" : {"mode" : "denylist" }}},
204+ )
205+ items = capture_items ("event" )
206+ werkzeug_set_cookie (client , "localhost" , "sessionid" , "123" )
207+ werkzeug_set_cookie (client , "localhost" , "csrftoken" , "456" )
208+ werkzeug_set_cookie (client , "localhost" , "foo" , "bar" )
209+ client .get (reverse ("view_exc" ))
210+
211+ (event ,) = (item .payload for item in items if item .type == "event" )
212+ assert event ["request" ]["cookies" ] == {
213+ "sessionid" : "[Filtered]" ,
214+ "csrftoken" : "[Filtered]" ,
215+ "foo" : "bar" ,
216+ }
0 commit comments