Skip to main content

Secret

Struct Secret 

Source
pub struct Secret<T: Zeroize>(/* private fields */);
Expand description

A wrapper for sensitive environment variables that:

  • Redacts the value in Debug and (when the serde feature is enabled) in Serialize output.
  • Zeroes the inner heap memory on Drop via Zeroize.
  • Prevents raw values from leaking into error messages (REDACT_IN_ERRORS = true).

§Limitations

  • std::mem::forget bypasses the Drop impl and will not zeroize the inner value. This is a fundamental limitation of the Zeroize pattern and cannot be solved without a custom allocator.
  • Clone creates an independent copy of the secret on the heap. Both copies are zeroized on drop, but the attack surface is doubled.
  • PartialEq uses the standard short-circuit comparison of T, which is not constant-time. Do not use it in contexts where a timing side-channel could leak information about the secret.
  • Deref exposes &T, which may implement Display. Secret itself intentionally does not implement Display to 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>

Source

pub fn new(val: T) -> Self

Create a new Secret wrapping the given value.

Source

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> AsRef<T> for Secret<T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Zeroize> Borrow<T> for Secret<T>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T: Clone + Zeroize> Clone for Secret<T>

Source§

fn clone(&self) -> Secret<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Zeroize> Debug for Secret<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Zeroize> Deref for Secret<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
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>>).

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Zeroize> From<T> for Secret<T>

Source§

fn from(val: T) -> Self

Converts to this type from the input type.
Source§

impl<T: FromEnvStr + Zeroize> FromEnvStr for Secret<T>

Source§

const REDACT_IN_ERRORS: bool = true

When true, error messages replace the raw value with [REDACTED]. Read more
Source§

type Err = <T as FromEnvStr>::Err

The error type returned when parsing fails.
Source§

fn from_env_str(s: &str) -> Result<Self, Self::Err>

Parse the raw string s into Self.
Source§

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.

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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.

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.
Source§

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.