Skip to content

API Reference

class Sanitizer(*, keys: Iterable[str] = (), patterns: Iterable[Pattern[AnyStr]] = (), key_patterns: Iterable[Pattern[AnyStr]] = (), replacement: ReplacementType = '**', message: str = '#### WARNING: Message replaced due to sensitive information.', unknown_objects: UnknownObjects = 'vars')

Base class for sensitive data sanitizers.

Parameters

  • keys : Iterable[str] Collection of keys to sanitize, matched by exact name (case-insensitively). Will be normalized to lowercase.

  • patterns : Iterable[Pattern[AnyStr]] Collection of regular expression patterns; will be compiled using re.compile. Matched against string values.

  • key_patterns : Iterable[Pattern[AnyStr]] Collection of regular expression patterns matched against key names (compiled like patterns). A key whose name matches any of them has its value replaced, letting a single rule cover many keys (e.g. secret for secret, aws_secret_access_key, ...). Matched against the key as written, so include (?i) for case-insensitivity.

  • replacement : ReplacementType A string or callable to be used to replace the value. A callable must either accept and return a str value, or accept a bytes object and return an object compatible with the hashlib function.

  • message : str The text to replace the matching string patterns.

  • unknown_objects : UnknownObjects How to handle an object of an unknown type that does not expose a __sanitary_context__ hook. "vars" (the default) walks its attributes via vars(), so every attribute whose name is not in keys passes through. "deny" instead replaces the whole object with replacement, so unrecognised objects are masked by default. Scalars (None, numbers, strings) are not objects and always pass through regardless of this setting. An object exposing __sanitary_context__ is always narrowed to that representation regardless of this setting.

Raises

  • ValueError If unknown_objects is not "vars" or "deny".

Methods

  • sanitize Sanitize data by masking potentially sensitive information.

method Sanitizer.sanitize(data: Any)Any

Sanitize data by masking potentially sensitive information.

If the object exposes a __sanitary_context__ hook (a dict, or a callable/property returning one), that representation is sanitized instead of the object itself. Otherwise the object is handled according to the unknown_objects setting: walked via vars() (default), or replaced wholesale when unknown_objects="deny". An object with neither a __dict__ nor a hook falls back to sanitizing its string representation.

Parameters

  • data : Any The data to sanitize.

Returns

  • Any The sanitized form of data.

class StructlogSanitizer(*, keys: Iterable[str] = (), patterns: Iterable[Pattern[AnyStr]] = (), key_patterns: Iterable[Pattern[AnyStr]] = (), replacement: ReplacementType = '**', message: str = '#### WARNING: Message replaced due to sensitive information.', unknown_objects: UnknownObjects = 'vars')

Bases : Sanitizer

Structlog processor for cleaning up logging context by masking sensitive data.

method StructlogSanitizer.__call__(logger: WrappedLogger, name: str, event_dict: EventDict)EventDict

Makes the sanitizer a callable, compatible with the Structlog processor API.

For details see https://www.structlog.org/en/stable/processors.html

Parameters

  • logger : WrappedLogger The logger instance doing the logging.

  • name : str Name of the logging method, e.g. info or warning.

  • event_dict : EventDict Current context, including modifications by other processors.

Returns

  • EventDict dict