1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
import sys
import logging
from sentry_sdk import capture_exception, capture_event, start_transaction, start_span
from sentry_sdk.utils import event_from_exception
from sentry_sdk.scrubber import EventScrubber
from tests.conftest import ApproxDict
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
def test_request_scrubbing(sentry_init, capture_events):
sentry_init()
events = capture_events()
try:
1 / 0
except ZeroDivisionError:
ev, _hint = event_from_exception(sys.exc_info())
ev["request"] = {
"headers": {
"COOKIE": "secret",
"authorization": "Bearer bla",
"ORIGIN": "google.com",
"ip_address": "127.0.0.1",
},
"cookies": {
"sessionid": "secret",
"foo": "bar",
},
"data": {
"token": "secret",
"foo": "bar",
},
}
capture_event(ev)
(event,) = events
assert event["request"] == {
"headers": {
"COOKIE": "[Filtered]",
"authorization": "[Filtered]",
"ORIGIN": "google.com",
"ip_address": "[Filtered]",
},
"cookies": {"sessionid": "[Filtered]", "foo": "bar"},
"data": {"token": "[Filtered]", "foo": "bar"},
}
assert event["_meta"]["request"] == {
"headers": {
"COOKIE": {"": {"rem": [["!config", "s"]]}},
"authorization": {"": {"rem": [["!config", "s"]]}},
"ip_address": {"": {"rem": [["!config", "s"]]}},
},
"cookies": {"sessionid": {"": {"rem": [["!config", "s"]]}}},
"data": {"token": {"": {"rem": [["!config", "s"]]}}},
}
def test_ip_address_not_scrubbed_when_pii_enabled(sentry_init, capture_events):
sentry_init(send_default_pii=True)
events = capture_events()
try:
1 / 0
except ZeroDivisionError:
ev, _hint = event_from_exception(sys.exc_info())
ev["request"] = {"headers": {"COOKIE": "secret", "ip_address": "127.0.0.1"}}
capture_event(ev)
(event,) = events
assert event["request"] == {
"headers": {"COOKIE": "[Filtered]", "ip_address": "127.0.0.1"}
}
assert event["_meta"]["request"] == {
"headers": {
"COOKIE": {"": {"rem": [["!config", "s"]]}},
}
}
def test_stack_var_scrubbing(sentry_init, capture_events):
sentry_init()
events = capture_events()
try:
password = "supersecret" # noqa
api_key = "1231231231" # noqa
safe = "keepthis" # noqa
1 / 0
except ZeroDivisionError:
capture_exception()
(event,) = events
frames = event["exception"]["values"][0]["stacktrace"]["frames"]
(frame,) = frames
assert frame["vars"]["password"] == "[Filtered]"
assert frame["vars"]["api_key"] == "[Filtered]"
assert frame["vars"]["safe"] == "'keepthis'"
meta = event["_meta"]["exception"]["values"]["0"]["stacktrace"]["frames"]["0"][
"vars"
]
assert meta == {
"password": {"": {"rem": [["!config", "s"]]}},
"api_key": {"": {"rem": [["!config", "s"]]}},
}
def test_breadcrumb_extra_scrubbing(sentry_init, capture_events):
sentry_init()
events = capture_events()
logger.info("bread", extra=dict(foo=42, password="secret"))
logger.critical("whoops", extra=dict(bar=69, auth="secret"))
(event,) = events
assert event["extra"]["bar"] == 69
assert event["extra"]["auth"] == "[Filtered]"
assert event["breadcrumbs"]["values"][0]["data"] == {
"foo": 42,
"password": "[Filtered]",
}
assert event["_meta"]["extra"]["auth"] == {"": {"rem": [["!config", "s"]]}}
assert event["_meta"]["breadcrumbs"] == {
"values": {"0": {"data": {"password": {"": {"rem": [["!config", "s"]]}}}}}
}
def test_span_data_scrubbing(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()
with start_transaction(name="hi"):
with start_span(op="foo", name="bar") as span:
span.set_data("password", "secret")
span.set_data("datafoo", "databar")
(event,) = events
assert event["spans"][0]["data"] == ApproxDict(
{"password": "[Filtered]", "datafoo": "databar"}
)
assert event["_meta"]["spans"] == {
"0": {"data": {"password": {"": {"rem": [["!config", "s"]]}}}}
}
def test_custom_denylist(sentry_init, capture_events):
sentry_init(
event_scrubber=EventScrubber(
denylist=["my_sensitive_var"], pii_denylist=["my_pii_var"]
)
)
events = capture_events()
try:
my_sensitive_var = "secret" # noqa
my_pii_var = "jane.doe" # noqa
safe = "keepthis" # noqa
1 / 0
except ZeroDivisionError:
capture_exception()
(event,) = events
frames = event["exception"]["values"][0]["stacktrace"]["frames"]
(frame,) = frames
assert frame["vars"]["my_sensitive_var"] == "[Filtered]"
assert frame["vars"]["my_pii_var"] == "[Filtered]"
assert frame["vars"]["safe"] == "'keepthis'"
meta = event["_meta"]["exception"]["values"]["0"]["stacktrace"]["frames"]["0"][
"vars"
]
assert meta == {
"my_sensitive_var": {"": {"rem": [["!config", "s"]]}},
"my_pii_var": {"": {"rem": [["!config", "s"]]}},
}
def test_scrubbing_doesnt_affect_local_vars(sentry_init, capture_events):
sentry_init()
events = capture_events()
try:
password = "cat123"
1 / 0
except ZeroDivisionError:
capture_exception()
(event,) = events
frames = event["exception"]["values"][0]["stacktrace"]["frames"]
(frame,) = frames
assert frame["vars"]["password"] == "[Filtered]"
assert password == "cat123"
def test_recursive_event_scrubber(sentry_init, capture_events):
sentry_init(event_scrubber=EventScrubber(recursive=True))
events = capture_events()
complex_structure = {
"deep": {
"deeper": [{"deepest": {"password": "my_darkest_secret"}}],
},
}
capture_event({"extra": complex_structure})
(event,) = events
assert event["extra"]["deep"]["deeper"][0]["deepest"]["password"] == "'[Filtered]'"
def test_recursive_scrubber_does_not_override_original(sentry_init, capture_events):
sentry_init(event_scrubber=EventScrubber(recursive=True))
events = capture_events()
data = {"csrf": "secret"}
try:
raise RuntimeError("An error")
except Exception:
capture_exception()
(event,) = events
frames = event["exception"]["values"][0]["stacktrace"]["frames"]
(frame,) = frames
assert data["csrf"] == "secret"
assert frame["vars"]["data"]["csrf"] == "[Filtered]"
|