redactkit
Keep secrets out of your logs.
redactkit helps prevent accidental leakage of passwords, tokens, API keys,
and other sensitive data into debug output, logs, and serialized data.
Warning
This crate focuses on redaction of output. It does not provide secure memory erasure.
For secure secret handling, consider using:
Examples
Derive
use RedactDebug;
let config = Config ;
let debug = format!;
assert!;
assert!;
assert!;
Builder
use Redactor;
let redactor = builder
.field
.field
.build;
assert!;
assert!;
assert!;
assert_eq!;
assert_eq!;
Custom mask
use Redactor;
let redactor = builder
.field
.mask
.build;
assert_eq!;
Default redactor
use default_redactor;
let redactor = default_redactor;
assert!;
assert!;
assert!;
assert!;
Features
redactkit uses optional feature flags.
| Feature | Default | Description |
|---|---|---|
std |
yes | Standard library support. |
derive |
yes | Enables the RedactDebug derive macro. |
regex |
no | Enables regex-based field and value rules. |
tracing |
no | Enables tracing-subscriber field redaction. |
serde |
no | Enables the RedactSerialize derive macro for redacting fields during serialization. |
Regex rules
Enable the regex feature:
[]
= { = "0.2", = ["regex"] }
Then use regex-based rules:
use Redactor;
let redactor = builder
.field
.field_pattern
.unwrap
.value_pattern
.unwrap
.build;
assert!;
assert!;
assert!;
assert!;
assert_eq!;
field_pattern matches against field names.
value_pattern replaces matched parts of the value.
Both methods return Result, because an invalid regex pattern can fail to compile:
use Redactor;
let result = builder
.field_pattern;
assert!;
Tracing integration
Enable the tracing feature:
[]
= { = "0.2", = ["tracing"] }
Then configure tracing-subscriber to use the redacting field formatter:
use fmt;
fmt
.fmt_fields
.init;
info!;
The log output will contain:
user="anna" password="******"
You can add custom sensitive fields:
use fmt;
fmt
.fmt_fields
.init;
With the regex feature enabled, you can also use patterns:
use fmt;
fmt
.fmt_fields
.init;
The tracing integration redacts formatted output only. It does not modify or erase original field values in memory.
Serde integration
redactkit can also redact sensitive fields during serialization.
Enable the serde feature:
[]
= { = "0.2", = ["serde"] }
Then derive RedactSerialize:
use RedactSerialize;
When serialized, redacted fields are replaced with a mask:
The original values in memory are not modified.
Serde limitations
Currently RedactSerialize:
- supports only structs with named fields;
- does not support generics yet;
- serializes redacted fields as the mask string
"******"; - may change the serialized type of a redacted field.
For example, if a redacted field was originally an integer, it will be serialized as a string mask.
Motivation
Accidental secret leakage in logs is a common security incident.
A single tracing::debug! or println!("{:?}", user) can expose
passwords, tokens, API keys, and session identifiers.
redactkit provides a small, explicit toolkit for redacting such
values at formatting time, so that sensitive fields are replaced with
a mask before they reach logs, debug output, or error messages.
When to use redactkit
Use redactkit when you need to:
- redact sensitive fields in
Debugoutput of structs; - redact fields in
tracinglog output; - redact fields during serialization;
- build a custom redaction policy with exact field names or regex rules;
- keep the original values intact in memory and only redact formatted output.
When NOT to use redactkit
redactkit is not a memory-protection crate.
It does not:
- erase secrets from memory;
- prevent access to original field values;
- encrypt data at rest or in transit;
- replace proper secret management.
If you need to wrap secrets and control their lifetime, consider
combining redactkit with a dedicated crate such as secrecy.
Comparison with secrecy
secrecy and redactkit solve related but different problems.
| Crate | Primary focus | How it works |
|---|---|---|
secrecy |
Secret storage and lifetime | Wraps a secret value in Secret<T> and prevents accidental Debug / Display leakage. |
redactkit |
Output redaction | Redacts selected fields when formatting output, while original values remain accessible in memory. |
A typical split:
- use
secrecywhen you want to wrap a secret value itself; - use
redactkitwhen you want to redact selected fields in logs, debug output, tracing output, or serialized output.
They can also be used together: secrecy protects the value,
while redactkit helps enforce redaction policies for formatted output.
Status
Current feature set:
- core
Redactor - builder API
- exact field-name redaction
- custom mask support
- default redactor
RedactDebugderive macroRedactSerializederive macro- regex rules
tracingintegration- examples
- CI quality gates