pub struct Secret<T: Zeroize>(/* private fields */);Expand description
A wrapper for sensitive environment variables that:
- Redacts the value in
Debugand (when theserdefeature is enabled) inSerializeoutput. - Zeroes the inner heap memory on
DropviaZeroize. - Prevents raw values from leaking into error messages
(
REDACT_IN_ERRORS=true).
§Limitations
std::mem::forgetbypasses theDropimpl and will not zeroize the inner value. This is a fundamental limitation of theZeroizepattern and cannot be solved without a custom allocator.Clonecreates an independent copy of the secret on the heap. Both copies are zeroized on drop, but the attack surface is doubled.PartialEquses the standard short-circuit comparison ofT, which is not constant-time. Do not use it in contexts where a timing side-channel could leak information about the secret.Derefexposes&T, which may implementDisplay.Secretitself intentionally does not implementDisplayto prevent accidental logging.
§Example
let m: std::collections::HashMap<String, String> =
[("TOKEN".into(), "secret".into())].into_iter().collect();
let cfg = lockedenv::from_map! { map: m, TOKEN: lockedenv::Secret<String> };
assert_eq!(cfg.TOKEN.as_ref(), "secret");
// Debug never leaks the value:
assert!(format!("{:?}", cfg).contains("[REDACTED]"));Implementations§
Source§impl<T: Zeroize> Secret<T>
impl<T: Zeroize> Secret<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consume the wrapper and return the inner value.
Uses std::mem::ManuallyDrop to bypass the Drop impl (which
zeroizes T). The caller takes full responsibility for the
returned value and its eventual cleanup.
Trait Implementations§
Source§impl<T: Zeroize> Drop for Secret<T>
Automatically zero the heap when the Secret goes out of scope.
Applies whenever T: Zeroize (e.g. Secret<String>, Secret<Vec<u8>>).
impl<T: Zeroize> Drop for Secret<T>
Automatically zero the heap when the Secret goes out of scope.
Applies whenever T: Zeroize (e.g. Secret<String>, Secret<Vec<u8>>).
Source§impl<T: FromEnvStr + Zeroize> FromEnvStr for Secret<T>
impl<T: FromEnvStr + Zeroize> FromEnvStr for Secret<T>
Source§const REDACT_IN_ERRORS: bool = true
const REDACT_IN_ERRORS: bool = true
Source§type Err = <T as FromEnvStr>::Err
type Err = <T as FromEnvStr>::Err
The error type returned when parsing fails.
Source§fn missing_value(key: &str) -> Result<Self, EnvLockError>
fn missing_value(key: &str) -> Result<Self, EnvLockError>
Called when the corresponding key is absent.
Defaults to an error;
Option<T> returns Ok(None).Source§impl<T: Zeroize + PartialEq> PartialEq for Secret<T>
Warning: uses the standard short-circuit comparison of T — not
constant-time. Avoid in timing-sensitive contexts.
impl<T: Zeroize + PartialEq> PartialEq for Secret<T>
Warning: uses the standard short-circuit comparison of T — not
constant-time. Avoid in timing-sensitive contexts.
Source§impl<T: Zeroize> Zeroize for Secret<T>
Zero the inner value on drop when T supports it.
This prevents the secret from lingering in heap memory after the struct is dropped.
impl<T: Zeroize> Zeroize for Secret<T>
Zero the inner value on drop when T supports it.
This prevents the secret from lingering in heap memory after the struct is dropped.
impl<T: Zeroize + Eq> Eq for Secret<T>
Auto Trait Implementations§
impl<T> Freeze for Secret<T>where
T: Freeze,
impl<T> RefUnwindSafe for Secret<T>where
T: RefUnwindSafe,
impl<T> Send for Secret<T>where
T: Send,
impl<T> Sync for Secret<T>where
T: Sync,
impl<T> Unpin for Secret<T>where
T: Unpin,
impl<T> UnsafeUnpin for Secret<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Secret<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more