pub struct RedactionEngine { /* private fields */ }Expand description
A thread-safe, reusable sensitive-data redaction engine.
All regex compilation happens at construction time inside
RedactionEngine::new. The resulting engine is Send + Sync and can
be shared across async worker tasks.
§Redaction semantics
For each JSON string value the engine performs span-level replacement:
every non-overlapping match of any rule inside the string is replaced with
the configured replacement text, while surrounding text is preserved. This
keeps audit context intact (e.g. "Connection from 8.8.8.8 accepted"
becomes "Connection from [REDACTED] accepted" rather than losing the
entire message).
Because replacing a span can create new word boundaries that expose additional matches, the engine loops until no new spans are found (up to a bounded cap). The built-in patterns settle within a few passes.
JSON pointer paths are applied first and replace the entire subtree at that path with the replacement string, regardless of its type.
Implementations§
Source§impl RedactionEngine
impl RedactionEngine
Sourcepub fn new(config: RedactionConfig) -> Result<Self, RedactError>
pub fn new(config: RedactionConfig) -> Result<Self, RedactError>
Build a new engine from the given configuration.
Returns an error if any regex pattern fails to compile, any JSON pointer path is malformed, or the replacement string would itself be matched by a configured rule (which would break idempotency).
Sourcepub fn redact_value(&self, value: &mut Value)
pub fn redact_value(&self, value: &mut Value)
Redact a mutable JSON value in place.
- Values at configured JSON pointer paths are replaced first.
- The entire tree is then walked (iteratively, to avoid stack overflow on deeply nested input). Each string value is scanned against all compiled rules: matching spans are replaced with the configured replacement text.
- Non-string leaves (numbers, booleans, null) are never modified.
- The operation is idempotent: calling it again on already-redacted output produces the same value, provided custom patterns settle within the internal pass limit.
Sourcepub fn redact_user_value(&self, value: &mut Value)
pub fn redact_user_value(&self, value: &mut Value)
Redact arbitrary user data, including integer values and object keys.
Unlike Self::redact_value, this can change numeric leaves into
strings. Use it for tool arguments, not typed event accounting fields.
When multiple keys redact to the same name, their values are collected
into an array in input order so no value is silently overwritten.
Floating-point values retain their type because parsing may already
have rounded their original digits.
Sourcepub fn redact_bytes(&self, raw: &[u8]) -> Result<Vec<u8>, RedactError>
pub fn redact_bytes(&self, raw: &[u8]) -> Result<Vec<u8>, RedactError>
Parse raw JSON bytes, redact, and re-serialize.
Returns the redacted JSON as UTF-8 bytes, or an error if the input is not valid JSON.