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
%sargument is not inrecord.msg. The record’sargsare 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.
- 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=(whichJsonFormatteremits); and the rendered traceback, which is formatted here once and stored asexc_textso no handler renders the raw one afterwards.