Expunge
A crate for expunging/redacting and transforming sensitive fields.
Basic usage
use Expunge;
use ;
let user = User;
let expunged_user = user.expunge;
let output = to_string_pretty.expect;
assert_eq!
Attributes
| Attribute | Description | Feature |
|---|---|---|
as |
provide a value that this field should be set to when expunged. e.g. Default::default() or "<expunged>".to_string() |
- |
with |
provide a function that will be called when expunging this value. It must return the same type as it takes. e.g. hash a String with sha256::digest. |
- |
skip |
can be used to skip fields that shouldn't be expunged | - |
zeroize |
zeroize memory for extra security via the secrecy & zeroize crates | zeroize |
slog |
integrates with slog using slog-derive to automatically expunge fields in logs | slog |
Logging with slog
Expunge provides a painless and foolproof way to log structs that may contain sensitive fields.
As long as your type implements serde::Serialize, the slog attribute will derive slog::SerdeValue.
Internally the value will be expunged before logging.
Example
use Expunge;
use ;
use ;
use Mutex;
// must implement Serialize
# let mut buf = vec!;
# let drain = new.fuse;
# let logger = root;
// Just log as is and it will be automatically expunged
let city = City;
info!;
let address = Address;
info!;
// {"msg":"it should log city","location":{"city":"<expunged>"},"level":"INFO","ts":"2024-02-04T12:55:28.627592Z"}
// {"msg":"it should log address","location":{"address":{"line1":"line1","line2":"line2"}},"level":"INFO","ts":"2024-02-04T12:55:28.627627Z"}
About
Other crates offer similar functionality, but either require types to be changed or make it difficult for both the expunged and unexpunged data being used at runtime.
This crate provides a proc_macro that derives the Expunge trait for the given type.
When the Expunge::expunge method is called, sensitive fields are transformed/redacted.
- All fields are transformed unless annotated with
#[expunge(skip)] - The
Expungemacro first looks for transformations declared on field/struct attributes i.e.asorwith. If these aren't set thenExpungemacro will use theExpunge::expungeimplementation for the type. A default implementation for theExpungetrait is provided for primitive types and common container types. These will be expunged as their default values, unless otherwise specified.
Since expunge doesn't require types to be changed, migrating to this crate should be completely frictionless.
This comes with the tradeoff that the user is now responsible for ensuring that Expunge::expunge
has been called as appropriate, this crate includes a type guard Expunged<T>
that can only contain a expunged T. Internally constructing Expunged<T> calls Expunge::expunge,
so it cannot be initialized with unexpunged data.
Similar crates
- secrecy: Prevents secrets being logged/serialized by wrapping them in a
Secret<T>type - veil: A proc_macro similar to this crate to implement expunged
std::fmt::Debugand/orstd::fmt::Display - redact: Similar to secrecy, but without the memory zeroizing
- redacted: Wrappers to control debug formatting of potentially sensitive byte arrays
Comparison
| crate | proc_macro | implements Debug | serde support | toggle on/off at runtime | uses original types | slog support |
|---|---|---|---|---|---|---|
| secrecy | ✘ | ✔ | ✔ | ✘ | ✘ | ✘ |
| redact | ✘ | ✔ | ✔ | ✘ | ✘ | ✘ |
| veil | ✔ | ✔ | ✘ | ✘ | ✘ | ✘ |
| redacted | ✘ | ✔ | ✘ | ✘ | ✘ | ✘ |
| expunge | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
Contributing
- Ensure that all tests are passing
- Open a PR/issue