sec/
lib.rs

1//! sec
2//! ===
3//!
4//! The `sec` crate prevent secrets from accidentally leaking through `Debug`
5//! or `Display` implementations. It does so by wrapping any kind of
6//! confidential information in a zero-overhead type:
7//!
8//! ```rust
9//! use sec::Secret;
10//!
11//! #[derive(Debug)]
12//! struct User {
13//!     id: usize,
14//!     username: String,
15//!     session_token: Secret<String>,
16//! }
17//!
18//! let alice = User{
19//!     id: 1,
20//!     username: "alice".to_owned(),
21//!     session_token: Secret::new("no one should see this".to_owned()),
22//! };
23//!
24//! println!("Now talking to: {:?}", alice);
25//! ```
26//!
27//! This will yield the following output:
28//!
29//! ```raw
30//! Now talking to: User{ id = 1, username: String("alice"), session_token: "..." }
31//! ```
32//!
33//! This functionality is very useful when dealing with data that should always
34//! be prevented from accidentally leaking through panics, log files.
35//!
36//! The contained data can be accessed by any of the `reveal` methods:
37//!
38//! ```rust
39//! #  use sec::Secret;
40//! #
41//! #  #[derive(Debug)]
42//! #  struct User {
43//! #      id: usize,
44//! #      username: String,
45//! #      session_token: Secret<String>,
46//! #  }
47//! #
48//! #  let alice = User{
49//! #      id: 1,
50//! #      username: "alice".to_owned(),
51//! #      session_token: Secret::new("no one should see this".to_owned()),
52//! #  };
53//! #
54//! println!("Don't tell anyone, but Alice's token is: {}",
55//!          alice.session_token.reveal());
56//! ```
57//!
58//! Only methods that contain `reveal` in their name actually allow accessing
59//! the secret value.
60//!
61//!
62//! ## Serde support (`deserialize`/`serialize` features)
63//!
64//! If the `deserialize` feature is enabled, any `Secret<T>` will automatically
65//! implement `Deserialize` from [Serde](https://crates.io/crates/serde):
66//!
67//! ```ignore
68//! #[derive(Deserialize)]
69//! struct AuthRequest{
70//!     username: String,
71//!     password: Secret<String>,
72//! }
73//! ```
74//!
75//! `AuthRequest` will be deserialized as if `password` was a regular `String`,
76//! the result will be stored as a `Secret<String>`. Additionally, if any
77//! deserialization errors occur, the resulting serde error will be replaced
78//! to avoid leaking the unparsed value.
79//!
80//! Serialization can be enabled through the `serialize` feature.
81//!
82//! **IMPORTANT**: Serializing data to a readable format is still a way to leak
83//! secrets. Only enable this feature if you need it.
84//!
85//!
86//! ## Diesel support (`diesel_sql` feature)
87//!
88//! Limited support for inserting and loading `Secret<T>` values through
89//! [Diesel](https://crates.io/crates/diesel) can be enabled by the `diesel_sql`
90//! feature.
91//!
92//! **IMPORTANT**: The database may log and echo back (on error) any query that
93//! fails, takes to long or is otherwise deemed interesting. Using `Secret`
94//! values in expressions should be avoided.
95//!
96//!
97//! ## `no_std` support
98//!
99//! By disabling the default features, `no_std` is supported. It can be
100//! re-enabled through the `std` feature.
101//!
102//!
103//! ## Additional traits
104//!
105//! The traits `PartialEq`, `Eq` and `Hash` are implemented for `Secret`, by
106//! simply passing through the operation to the underlying type. These traits
107//! should be safe in a way that they will not accidentally leak the enclosed
108//! secret.
109//!
110//! Additional, by enabling the `ord` feature, the `PartialOrd` and `Ord`
111//! traits will be implemented. Since ordering could potentially leak
112//! information when a collection order by a Secret is printed in-order, these
113//! are opt-in by default.
114//!
115//!
116//! ## Security
117//!
118//! While `sec` usually does a good job from preventing accidentally leaks
119//! through logging mistakes, it currently does not protect the actual memory
120//! (while not impossible, this requires a lot of extra effort due to heap
121//! allocations). The data protected by sec is usually sent across the network
122//! and passed around among different applications (e.g. a token authorizing a
123//! client) or could reasonably be used as a key for a HashMap.
124//!
125//! To prevent copies inside an application, data is usually allocated on the
126//! heap only and scrubbed afer deallocation. `sec` makes a trade-off in favor
127//! of performance and generality here by not supporting this pattern. It is
128//! not written to protect your GPG private key from core dumps, but rather
129//! login tokens from accidental disclosure.
130//!
131//! If protecting cryptographic secrets in-memory from stackdumps and similar
132//! is a concern, have a look at the [secrets]
133//! (https://crates.io/crates/secrets), [secstr]
134//! (https://crates.io/crates/secstr) or similar crates.
135
136#![no_std]
137
138#[cfg(feature = "diesel_sql")]
139extern crate diesel;
140
141#[macro_use]
142#[cfg(feature = "std")]
143extern crate std;
144
145#[cfg(any(feature = "serialize", feature = "deserialize"))]
146extern crate serde;
147
148#[cfg(test)]
149mod tests;
150
151use core::fmt;
152use core::hash::{Hash, Hasher};
153
154#[cfg(feature = "ord")]
155use core::cmp::Ordering;
156
157#[cfg(feature = "diesel_sql")]
158use std::io::Write;
159
160#[cfg(feature = "std")]
161use std::string::String;
162
163#[cfg(feature = "serialize")]
164use serde::Serializer;
165
166#[cfg(feature = "deserialize")]
167use serde::Deserializer;
168
169/// Wraps a type `T`, preventing it from being accidentally revealed.
170pub struct Secret<T>(T);
171
172#[cfg(feature = "std")]
173impl Secret<String> {
174    /// Returns a `str` reference, wrapped in a secret
175    #[inline]
176    pub fn as_str(&self) -> Secret<&str> {
177        Secret(self.0.as_str())
178    }
179
180    /// Return and **reveal** a `str` reference.
181    #[inline]
182    pub fn reveal_str(&self) -> &str {
183        self.0.as_str()
184    }
185}
186
187impl<T> Secret<T> {
188    /// Creates a new secret
189    #[inline]
190    pub fn new(val: T) -> Secret<T> {
191        Secret(val)
192    }
193
194    /// Create a secret immutable reference
195    #[inline]
196    pub fn as_ref(&self) -> Secret<&T> {
197        Secret(&self.0)
198    }
199
200    /// Create a secret mutable reference
201    #[inline]
202    pub fn as_mut(&mut self) -> Secret<&mut T> {
203        Secret(&mut self.0)
204    }
205
206    /// **Reveal** the held value by returning a reference
207    #[inline]
208    pub fn reveal(&self) -> &T {
209        &self.0
210    }
211
212    /// **Reveal** the held value by unwrapping
213    #[inline]
214    pub fn reveal_into(self) -> T {
215        self.0
216    }
217
218    /// **Reveals** the held value by applying a function to it
219    #[inline]
220    pub fn map_revealed<V, F: FnOnce(T) -> V>(self, f: F) -> Secret<V> {
221        Secret(f(self.0))
222    }
223}
224
225impl<T> fmt::Debug for Secret<T> {
226    #[inline]
227    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
228        write!(f, "...")
229    }
230}
231
232impl<T: fmt::Display> fmt::Display for Secret<T> {
233    #[inline]
234    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
235        write!(f, "...")
236    }
237}
238
239impl<T: Clone> Clone for Secret<T> {
240    #[inline]
241    fn clone(&self) -> Self {
242        Secret(self.0.clone())
243    }
244}
245
246impl<T: PartialEq> PartialEq for Secret<T> {
247    #[inline]
248    fn eq(&self, other: &Secret<T>) -> bool {
249        self.0.eq(&other.0)
250    }
251}
252
253#[cfg(feature = "ord")]
254impl<T: PartialOrd> PartialOrd for Secret<T> {
255    #[inline]
256    fn partial_cmp(&self, other: &Secret<T>) -> Option<Ordering> {
257        self.0.partial_cmp(&other.0)
258    }
259}
260
261#[cfg(feature = "ord")]
262impl<T: Ord> Ord for Secret<T> {
263    #[inline]
264    fn cmp(&self, other: &Secret<T>) -> Ordering {
265        self.0.cmp(&other.0)
266    }
267}
268
269impl<T: Hash> Hash for Secret<T> {
270    #[inline]
271    fn hash<H: Hasher>(&self, state: &mut H) {
272        self.0.hash(state);
273    }
274}
275
276impl<T: Default> Default for Secret<T> {
277    #[inline]
278    fn default() -> Secret<T> {
279        Secret(T::default())
280    }
281}
282
283impl<T: Copy> Copy for Secret<T> {}
284impl<T: Eq> Eq for Secret<T> {}
285unsafe impl<T: Sync> Sync for Secret<T> {}
286unsafe impl<T: Send> Send for Secret<T> {}
287
288impl<T> From<T> for Secret<T> {
289    #[inline]
290    fn from(v: T) -> Secret<T> {
291        Secret(v)
292    }
293}
294
295#[cfg(feature = "serialize")]
296impl<T: serde::Serialize> serde::Serialize for Secret<T> {
297    #[inline]
298    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
299    where
300        S: Serializer,
301    {
302        self.0.serialize(serializer)
303    }
304}
305
306#[cfg(feature = "deserialize")]
307use serde::de::Error;
308
309#[cfg(feature = "deserialize")]
310impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for Secret<T> {
311    #[inline]
312    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
313    where
314        D: Deserializer<'de>,
315    {
316        // we need to intercept the exception, as it might contain the actual
317        // raw value being deserialized
318        match T::deserialize(deserializer).map(Secret) {
319            Err(_) => Err(D::Error::custom(
320                "a confidential value could not be deserialized",
321            )),
322            Ok(v) => Ok(v),
323        }
324    }
325}
326
327#[cfg(all(feature = "diesel_sql", feature = "std"))]
328impl<A, DB, T> diesel::types::ToSql<A, DB> for Secret<T>
329where
330    T: diesel::types::ToSql<A, DB> + fmt::Debug,
331    DB: diesel::backend::Backend + diesel::types::HasSqlType<A>,
332{
333    #[inline]
334    fn to_sql<W: Write>(
335        &self,
336        out: &mut diesel::serialize::Output<W, DB>,
337    ) -> Result<diesel::types::IsNull, std::boxed::Box<dyn std::error::Error + Send + Sync>> {
338        self.0.to_sql(out)
339    }
340}
341
342#[cfg(all(feature = "diesel_sql", feature = "std"))]
343impl<'a, E, T> diesel::expression::AsExpression<E> for &'a Secret<T>
344where
345    T: diesel::expression::AsExpression<E>,
346    &'a T: diesel::expression::AsExpression<E>,
347{
348    type Expression = <&'a T as diesel::expression::AsExpression<E>>::Expression;
349
350    #[inline]
351    fn as_expression(self) -> Self::Expression {
352        (&self.0).as_expression()
353    }
354}
355
356#[cfg(all(feature = "diesel_sql", feature = "std"))]
357impl<T, ST, DB> diesel::query_source::Queryable<ST, DB> for Secret<T>
358where
359    DB: diesel::backend::Backend + diesel::types::HasSqlType<ST>,
360    T: diesel::query_source::Queryable<ST, DB>,
361{
362    type Row = T::Row;
363
364    #[inline]
365    fn build(row: Self::Row) -> Self {
366        Secret(T::build(row))
367    }
368}