Skip to main content

RedactionEngine

Struct RedactionEngine 

Source
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

Source

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).

Source

pub fn redact_value(&self, value: &mut Value)

Redact a mutable JSON value in place.

  1. Values at configured JSON pointer paths are replaced first.
  2. 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.
  3. Non-string leaves (numbers, booleans, null) are never modified.
  4. The operation is idempotent: calling it again on already-redacted output produces the same value, provided custom patterns settle within the internal pass limit.
Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for RedactionEngine

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.