TraceGuard.Redactor (trace_guard v0.1.0)

Copy Markdown View Source

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

normalize_key(key)

@spec normalize_key(atom() | String.t()) :: String.t()

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"

placeholder()

@spec placeholder() :: String.t()

The placeholder that replaces sensitive values.

redact(term, sensitive_keys)

@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}.

scrub_string(string, sensitive_keys)

@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" and key => value, where the key contains a configured sensitive key (_ in a configured key also matches - or nothing, so api_key covers api-key and apikey).
  • Bearer <credential> and Basic <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}.

sensitive?(key, sensitive_keys)

@spec sensitive?(term(), [String.t()]) :: boolean()

Returns true if key matches one of the normalized sensitive_keys.

Keys that are neither atoms nor strings are never sensitive.