SecretFilter#

class scikitplot.cleanprompt.SecretFilter(secrets=None)[source]#

Replace known secret surfaces in a record with REDACTION_MARK.

Parameters:
secretsiterable of str, optional

Surfaces to scrub. Short strings are ignored; see the notes.

Parameters:

secrets (Iterable[str] | None)

Notes

Developer notes. Defence in depth, not the primary guarantee. The primary guarantee is that no call site passes a secret; this catches the call site that eventually will.

Two deliberate limits.

Short surfaces are not scrubbed. A two-character secret would match inside ordinary words and turn every message into noise, which trains people to ignore logs. The floor is four characters.

Scrubbing happens on the formatted message, including arguments, because a secret passed as a %s argument is not in record.msg. The record’s args are cleared afterwards so the handler does not re-interpolate them and undo the work.

Examples

>>> import logging
>>> filt = SecretFilter(["topsecret@example.com"])
>>> rec = logging.LogRecord(
...     "x", logging.INFO, "f", 1, "found %s", ("topsecret@example.com",), None
... )
>>> filt.filter(rec) and "topsecret" not in rec.getMessage()
True
MIN_LENGTH = 4#

Surfaces shorter than this are not scrubbed. Scrubbing "Ann" would also rewrite "Annual" and "Planning" in every record, making logs unreadable; this filter is the second line of defence — the submodule never passes a value to a logger in the first place.

add(secrets)[source]#

Add surfaces to scrub, longest first.

Parameters:

secrets (Iterable[str])

Return type:

None

filter(record)[source]#

Scrub the record in place. Always returns True.

Notes

Developer notes. Three places carry text out of a record, and all three are scrubbed: the formatted message; string fields a call site attached with extra= (which JsonFormatter emits); and the rendered traceback, which is formatted here once and stored as exc_text so no handler renders the raw one afterwards.

Parameters:

record (LogRecord)

Return type:

bool

scrub(text)[source]#

Return text with every held surface replaced.

Parameters:
textstr

Any text.

Returns:
str

The scrubbed text.

Parameters:

text (str)

Return type:

str