Internal module. Not part of TraceGuard's public API: it may change in
any release. Use the functions in TraceGuard instead.
Recursive, key-based redaction of diagnostic data.
Key matching
Keys are compared in a normalized form so that atom keys, string keys, header names and common casing styles behave the same:
:api_key, "API_KEY", "x-api-key", "apiKey", "X-Api-Key"
#=> "api_key", "api_key", "x_api_key", "api_key", "x_api_key"A key is sensitive when the token sequence of a configured sensitive key
appears contiguously in the token sequence of the normalized key. With
"api_key" configured, "x_api_key" and "api_key_id" match, but
"apikeys" does not. With "token" configured, "csrf_token" matches but
"tokenizer" does not. This deliberately errs towards over-redaction.
Keys are only ever converted from atoms to strings; external strings are never converted to atoms.
Structures
Maps, lists, and two-element {key, value} tuples (keyword lists, Plug
header lists) are traversed. Structs are converted to plain maps before
traversal so that a struct field such as :password cannot slip through.
Summary
Functions
Normalizes a key to lower snake case.
The placeholder that replaces sensitive values.
Recursively replaces the values of sensitive keys with "[REDACTED]".
Scrubs sensitive values embedded in free text, such as exception messages.
Returns true if key matches one of the normalized sensitive_keys.
Functions
Normalizes a key to lower snake case.
iex> TraceGuard.Redactor.normalize_key("X-Api-Key")
"x_api_key"
iex> TraceGuard.Redactor.normalize_key(:refreshToken)
"refresh_token"
iex> TraceGuard.Redactor.normalize_key("AUTHORIZATION")
"authorization"
@spec placeholder() :: String.t()
The placeholder that replaces sensitive values.
@spec redact(term(), [String.t()]) :: {term(), non_neg_integer()}
Recursively replaces the values of sensitive keys with "[REDACTED]".
Returns {redacted_term, number_of_redacted_values}.
@spec scrub_string(String.t(), [String.t()]) :: {String.t(), non_neg_integer()}
Scrubs sensitive values embedded in free text, such as exception messages.
Two patterns are handled:
key=value,key: value,"key": "value"andkey => value, where the key contains a configured sensitive key (_in a configured key also matches-or nothing, soapi_keycoversapi-keyandapikey).Bearer <credential>andBasic <credential>.
This is a best-effort safety net. It cannot recognize secrets that are not labelled, so exception messages remain the riskiest captured field.
Returns {scrubbed_string, number_of_replacements}.
Returns true if key matches one of the normalized sensitive_keys.
Keys that are neither atoms nor strings are never sensitive.