Scrubbing Sensitive Data for Python

Learn about filtering or scrubbing sensitive data within the Sentry Python SDK, so that data is not sent with the event. You can also configure server-side scrubbing to ensure the data is not stored.

As with any third-party service it's important to understand what data is being sent to Sentry, and where relevant ensure sensitive data either never reaches the Sentry servers, or at the very least it doesn’t get stored.

These are some great examples for data scrubbing that every company should think about:

  • PII (Personally Identifiable Information) such as a user's name or email address, which post-GDPR should be on every company's mind.
  • Authentication credentials, like your AWS password or key.
  • Confidential IP (Intellectual Property), such as your favorite color, or your upcoming plans for world domination.

We offer the following options depending on your legal and operational needs:

  • filtering or scrubbing sensitive data within the SDK, so that data is not sent to Sentry. Configuration changes require a redeployment of your application.
  • configuring server-side scrubbing to ensure Sentry does not store data. Configuration changes are done in the Sentry UI and apply immediately for new events.
  • running a local Relay on your own server between the SDK and Sentry, so that data is not sent to Sentry while configuration can still be applied without deploying.

Ensure that your team is aware of your company's policy around what can and cannot be sent to Sentry. We recommend determining this policy early in your implementation and communicating it as well as enforcing it via code review.

The SDK can be configured to collect or ignore specific data using the data_collection option.

In SDK versions prior to 3.0, you can alternatively use the simpler send_default_pii option by setting it to True or False.

If you do not wish to use the default PII behavior, you can also choose to identify users in a more controlled manner, using our user identity context.

By default, the SDK sends the values of all local variables in stack frames, without scrubbing them. In SDK versions prior to 3.0, the event scrubber removed values of variables with sensitive names, like password or token.

To filter out variables with sensitive names, set the stack_frame_variables setting of the data_collection option to denylist mode. The SDK then removes the values of variables whose names contain a term from its built-in denylist, such as auth, token, secret, password, key, or session. Matching is case-insensitive, so user_password and API_KEY are both scrubbed.

To scrub additional variables, list them in terms. They're applied on top of the built-in denylist:

import sentry_sdk

sentry_sdk.init(
    dsn="___PUBLIC_DSN___",
    data_collection={
        "stack_frame_variables": {
            "mode": "denylist",
            "terms": ["ssn", "birthday"],
        },
    },
)

To stop sending local variables entirely, set stack_frame_variables to False.

Only available in SDK versions prior to 3.0. In SDK 3.x, use data_collection to control which data is collected, and before_send for custom scrubbing.

Configure the event scrubber (SDK 2.x)

If using send_default_pii, you can use the event_scrubber configuration parameter to simplify removing sensitive data from your event payload. The event scrubber has no effect when using data_collection.

The default scrubber implementation will run automatically and filter anything in the denylist from potentially sensitive interfaces in the event payload. These are typically security values such as passwords, authentication, sessions, cookies, and CSRF tokens.

Additionally, if send_default_pii is set to False, the scrubber will also filter from a separate pii_denylist that typically has PII values such as IP addresses.

import sentry_sdk
from sentry_sdk.scrubber import EventScrubber

sentry_sdk.init(
    # ...
    send_default_pii=False,
    event_scrubber=EventScrubber(),  # this is set by default
)

You can also pass in a custom denylist or pii_denylist to the EventScrubber class and filter additional fields that you want. Make sure you extend the current lists if you want to use the default lists as well.

from sentry_sdk.scrubber import EventScrubber, DEFAULT_DENYLIST, DEFAULT_PII_DENYLIST

# custom denylist
denylist = DEFAULT_DENYLIST + ["my_sensitive_var"]
pii_denylist = DEFAULT_PII_DENYLIST + ["my_private_var"]

sentry_sdk.init(
    # ...
    send_default_pii=False,
    event_scrubber=EventScrubber(denylist=denylist, pii_denylist=pii_denylist),
)

The scrubber will check parts of the event that are most likely to contain data from the denylist, like user and request data. However, by default, it will not search these recursively for performance reasons. If you want the scrubber to be recursive, initialize it with recursive=True.

from sentry_sdk.scrubber import EventScrubber

sentry_sdk.init(
    # ...
    send_default_pii=False,
    event_scrubber=EventScrubber(recursive=True),
)

The SDK provides a before_send hook, which is invoked before an error or message event is sent and can be used to modify event data to remove sensitive information. The SDK also provides a before_send_span hook, that does the same thing for spans. In SDK versions prior to 3.0, before_send_transaction does the same thing for transactions, and before_send_span is only available in stream mode. We recommend using these hooks in the SDK to scrub any data before it is sent, to ensure that sensitive data never leaves the local environment.

Sensitive data may appear in the following areas:

  • Stack-locals → The SDK picks up variable values within the stack trace, and doesn't scrub them by default. These can be scrubbed with the stack_frame_variables setting of data_collection (see Local Variables), or this behavior can be disabled altogether if necessary.
  • Breadcrumbs → The SDK picks up previously executed log statements. Do not log PII if using this feature and including log statements as breadcrumbs in the event. The SDK will also record database queries, which may need to be scrubbed. The SDK will add the HTTP query string and fragment as a data attribute to the breadcrumb, which may need to be scrubbed.
  • User context → Automated behavior is controlled via the user_info setting of data_collection (in SDK versions prior to 3.0, also via send_default_pii).
  • HTTP context → Query strings are picked up in web frameworks as part of the HTTP request context.
  • Transaction Names → In certain situations, transaction names might contain sensitive data. For example, an HTTP request transaction might have a raw URL like /users/1234/details as its name (where 1234 is a user id, which may be considered PII). In most cases, our SDK can parameterize URLs and routes successfully, that is, turn /users/1234/details into /users/:userid/details. However, depending on the web framework, your routing configuration, and a few other factors, the SDKs might not be able to completely parameterize all of your URLs.
  • HTTP Spans → The SDK includes the HTTP query string and fragment as a data attribute, which means the HTTP span may need to be scrubbed.

For more details and data filtering instructions, see Filtering Events.

Contextual information

Instead of sending confidential information in plaintext, consider hashing it:

import sentry_sdk

# Tags are applied to error events
sentry_sdk.set_tag("birthday", checksum_or_hash("08/12/1990"))

# Attributes are applied to spans, logs, and metrics
sentry_sdk.set_attribute("birthday", checksum_or_hash("08/12/1990"))

This will allow you to correlate it within internal systems if needed, but keep it confidential from Sentry.

User details

Your organization may determine that emails are not considered confidential, but if they are, consider instead sending your internal identifier:

import sentry_sdk

sentry_sdk.set_user({"id": user.id})

# or

sentry_sdk.set_user({"username": user.username})

Doing this will ensure you still benefit from user-impact related features.

Logging integrations

As a best practice you should always avoid logging confidential information. If you have legacy systems you need to work around, consider the following:

  • Anonymize the confidential information within the log statements (for example, swap out email addresses -> for internal identifiers)
  • Use before_breadcrumb to filter it out from breadcrumbs before it is attached
  • Set the breadcrumb_level parameter in the logging integrations you use to None (in SDK versions prior to 3.0, the level parameter).
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").