Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sentry - scrubbing local variables sensitive data

I would like to scrub sensitive data from Python before I send it to Sentry

However, in method before_send and truncate_breadcrumb_message I am not sure where I can get the list of local variables and scrub them.

sentry_sdk.init(
    dsn=settings.get('SENTRY_DSN', ""),
    before_breadcrumb=truncate_breadcrumb_message,
    integrations=[FlaskIntegration()],
    before_send=sanitize_sentry_event,
)

def sanitize_sentry_event(event, hint):
    pass

def truncate_breadcrumb_message(crumb, hint):
    pass

def raise_execption(password):
    auth = 5
    raise Exception()

In the above method, I wouldn't want password and auth to be send to Sentry at all.

How can I do it?

like image 896
Dejell Avatar asked Aug 14 '26 02:08

Dejell


2 Answers

event is a JSON payload that contains the same exact JSON you see in the "JSON" download in Sentry's UI. So you have a event like this:

{
  "exception": {
    "values": [
      {
        "stacktrace": {
          "frames": [
            {"vars": ...}
          ]
        }
      }
    ]
  }
}

And you want to remove vars, you need to do this:

def sanitize_sentry_event(event, hint):
    for exception in event.get("exception", {}).get("values", []):
        for frame in exception.get("stacktrace", {}).get("frames", []):
            frame.pop("vars", None)

    for exception in event.get("threads", {}).get("values", []):
        for frame in exception.get("stacktrace", {}).get("frames", []):
            frame.pop("vars", None)


    return event

You probably want to wrap the entire function body with a try-except. If the function raises an exception the event is dropped. Make sure to test this using init(debug=True) to see all exceptions your before_send hook might throw

like image 127
Markus Unterwaditzer Avatar answered Aug 16 '26 20:08

Markus Unterwaditzer


found it here

Code for anyone who migrated from raven and wants to use raven processors / sanitize_keys

from raven.processors import SanitizeKeysProcessor, SanitizePasswordsProcessor

class FakeRavenClient:
    sanitize_keys = [
        'card_number',
        'card_cvv',
        'card_expiration_date',
    ]

processors = [
    SanitizePasswordsProcessor(FakeRavenClient),
    SanitizeKeysProcessor(FakeRavenClient),
]

def before_send(event, hint):
    for processor in processors:
        event = processor.process(event)
    return event

sentry_sdk.init(
    before_send=before_send,
)
like image 23
pymen Avatar answered Aug 16 '26 21:08

pymen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!