Skip to main content

auth_framework/storage/
encryption.rs

1use crate::errors::{AuthError, Result};
2use crate::storage::{AuthStorage, SessionData};
3use crate::tokens::AuthToken;
4use aes_gcm::{
5    Aes256Gcm, Key, KeyInit, Nonce,
6    aead::{Aead, Payload},
7};
8use base64::{Engine, engine::general_purpose::STANDARD as BASE64};
9use serde::{Deserialize, Serialize};
10use std::collections::HashMap;
11use std::env;
12use std::fs;
13use std::time::Duration;
14use zeroize::Zeroize;
15
16/// Serializes the tests in this crate's lib-unit-test binary that
17/// EXPLICITLY read or write `AUTH_STORAGE_ENCRYPTION_KEY`/
18/// `AUTH_STORAGE_ENCRYPTION_KEYS_FILE` via this lock, since those are
19/// process-global state shared across all tests running in parallel
20/// threads within one `cargo test --lib` invocation. Acquire this (and
21/// clean up the env var via [`EncryptionEnvGuard`], not a bare
22/// `set_var`/`remove_var` pair) in any new test that touches those
23/// variables.
24///
25/// **Not a guarantee every such test already does** -- some pre-existing
26/// tests elsewhere in this crate build a persistent storage backend
27/// through the real factory (and therefore read these env vars via
28/// [`StorageEncryption::from_env`]) without taking this lock first; that
29/// gap predates this lock and isn't closed by introducing it.
30/// A `tokio::sync::Mutex`, not `std::sync::Mutex`, because several callers
31/// (in `#[tokio::test]` functions) need to hold this guard across `.await`
32/// points -- use `.lock().await` there, or `.blocking_lock()` in a plain
33/// (non-async) `#[test]`.
34#[cfg(test)]
35pub(crate) static TEST_ENCRYPTION_ENV_LOCK: tokio::sync::Mutex<()> =
36    tokio::sync::Mutex::const_new(());
37
38/// RAII guard that sets `AUTH_STORAGE_ENCRYPTION_KEY` for the lifetime of
39/// the guard and removes it on drop -- including on an early return or a
40/// panic (e.g. from an `assert!`/`.unwrap()` failure), unlike a bare
41/// `set_var` at the top of a test and a `remove_var` at the bottom, which
42/// leaks the variable if the test fails before reaching that last line.
43/// Callers should hold [`TEST_ENCRYPTION_ENV_LOCK`] for the guard's entire
44/// lifetime.
45#[cfg(test)]
46pub(crate) struct EncryptionEnvGuard;
47
48#[cfg(test)]
49impl EncryptionEnvGuard {
50    pub(crate) fn set(key: &str) -> Self {
51        // SAFETY: sound against every caller that ALSO takes
52        // `TEST_ENCRYPTION_ENV_LOCK`; NOT sound otherwise (see the
53        // lock's own doc for which pre-existing tests don't).
54        unsafe {
55            std::env::set_var("AUTH_STORAGE_ENCRYPTION_KEY", key);
56            std::env::remove_var("AUTH_STORAGE_ENCRYPTION_KEYS_FILE");
57        }
58        Self
59    }
60}
61
62#[cfg(test)]
63impl Drop for EncryptionEnvGuard {
64    fn drop(&mut self) {
65        // SAFETY: see `EncryptionEnvGuard::set` -- the same lock is held
66        // for this guard's entire lifetime, including this drop.
67        unsafe {
68            std::env::remove_var("AUTH_STORAGE_ENCRYPTION_KEY");
69        }
70    }
71}
72
73/// Current envelope format version. Bumped whenever the envelope's fields
74/// or decryption rules change in a way that needs disambiguating from
75/// older data still on disk.
76///
77/// * `0` -- the original public `EncryptedStorage`: no `key_id`, no AAD.
78/// * `1` -- versioned `key_id`; AAD is the bare storage key, so the
79///   envelope's own metadata (`v`, `key_id`, `algorithm`) is NOT
80///   authenticated.
81/// * `2` -- AAD binds the metadata too (see [`bound_aad`]).
82///
83/// Versions below the current one are *legacy*: readable only behind the
84/// `allow_legacy_v0` policy (the name is historical; it covers every
85/// pre-current format) and upgraded by [`migrate_kv_to_encrypted`].
86const CURRENT_FORMAT_VERSION: u8 = 2;
87
88/// Format version 1, the first versioned-`key_id` format.
89const FORMAT_V1: u8 = 1;
90
91/// The only algorithm identifier this module reads or writes.
92const ALGORITHM: &str = "AES-256-GCM";
93
94/// The one error text every decryption failure returns. The specific
95/// cause (bad base64, wrong nonce length, unknown key id, failed
96/// authentication, ...) is logged, never returned, so the failure itself
97/// is not an oracle for which check tripped.
98const DECRYPT_FAILED: &str = "Decryption failed";
99
100/// Associated data for a format-v2 envelope: the caller's record AAD
101/// (the storage key) AND the envelope's own `v`, `algorithm` and `key_id`,
102/// each length-prefixed so the concatenation is unambiguous. Binding the
103/// metadata means relabelling an envelope's `key_id`, or rewriting `v` to
104/// downgrade it to a format whose AAD ignores metadata, fails
105/// authentication instead of being trusted.
106fn bound_aad(v: u8, algorithm: &str, key_id: &str, record_aad: &[u8]) -> Vec<u8> {
107    let mut out = Vec::with_capacity(48 + algorithm.len() + key_id.len() + record_aad.len());
108    out.extend_from_slice(b"auth-framework/storage-envelope");
109    out.push(v);
110    for part in [algorithm.as_bytes(), key_id.as_bytes(), record_aad] {
111        out.extend_from_slice(&(part.len() as u32).to_be_bytes());
112        out.extend_from_slice(part);
113    }
114    out
115}
116
117fn default_format_version() -> u8 {
118    // Envelopes serialized before this field existed (format version 0,
119    // the original public `EncryptedStorage`: fields `data`/`nonce`/
120    // `algorithm`/`key_derivation`, no `key_id`, no AAD at encryption
121    // time) deserialize with this field absent -- serde DOES call this
122    // function in that case (per `#[serde(default = "...")]`'s actual
123    // behavior), which happens to return the same `0` the bare type
124    // default would. It's spelled out explicitly, rather than relying on
125    // `#[serde(default)]`'s type-default shorthand, so the "absent field
126    // means format version 0" rule is visible at the field's definition
127    // instead of implicit in `u8::default()`.
128    0
129}
130
131/// Encrypted data container with metadata.
132///
133/// `key_id` makes this a versioned envelope: a value encrypted under one key
134/// can still be decrypted after the *current* key rotates, as long as the
135/// old key is still loadable (see [`KeyProvider`]). Writers always use the
136/// current key; readers look up whichever key the envelope names.
137///
138/// `v` and `key_derivation` exist for backward compatibility with the
139/// original, pre-this-redesign public `EncryptedStorage` (format version
140/// 0): that format had no `key_id` and no AAD binding at encryption time.
141/// An envelope with `v == 0` (including any envelope missing the `v` field
142/// entirely, since `#[serde(default)]` fills it with `0`) is decrypted
143/// against every loaded key with an empty AAD, matching the old behavior;
144/// see [`StorageEncryption::decrypt`].
145#[derive(Debug, Clone, Serialize, Deserialize)]
146pub struct EncryptedData {
147    /// Base64 encoded encrypted data
148    pub data: String,
149    /// Base64 encoded nonce/IV (96 bits, randomly generated per encryption)
150    pub nonce: String,
151    /// Algorithm identifier
152    pub algorithm: String,
153    /// Which key (by id) encrypted this value. Empty for format version 0
154    /// (the original `EncryptedStorage`, which had no key-id concept).
155    #[serde(default)]
156    pub key_id: String,
157    /// Envelope format version. `0` (including an absent field, which
158    /// deserializes to `0`) is the original pre-redesign format; `1` is
159    /// the legacy versioned-`key_id` format; `2` is the current format
160    /// (metadata bound into the AAD).
161    #[serde(default = "default_format_version")]
162    pub v: u8,
163    /// Present only for format-version-0 compatibility (the original
164    /// field name was `key_derivation`); current envelopes don't set it.
165    #[serde(default)]
166    pub key_derivation: String,
167}
168
169/// Supplies the encryption key(s) [`StorageEncryption`] uses.
170///
171/// Exists as a seam so the key *source* (env var today) can be swapped for
172/// a KMS or an `age`/`sops`-style file-based secret without touching
173/// [`StorageEncryption`] or [`EncryptedStorage`] at all. See
174/// [`EnvKeyProvider`]'s own docs for the current, deliberately narrow,
175/// implementation and its known limitation.
176pub trait KeyProvider: Send + Sync {
177    /// Load every key this provider knows about, keyed by key id, plus
178    /// which one is current (new writes use this one; any key in the map
179    /// can still be used to decrypt an old envelope that names it).
180    ///
181    /// Returns an error if no usable key can be loaded at all -- callers
182    /// must fail closed on that, never silently fall back to no
183    /// encryption.
184    fn load_keys(&self) -> Result<LoadedKeys>;
185}
186
187/// The result of [`KeyProvider::load_keys`]: every key this provider could
188/// load, and which one is current.
189pub struct LoadedKeys {
190    pub current_key_id: String,
191    /// All loadable keys, including the current one, by key id. A key id
192    /// must not be empty -- [`StorageEncryption::new`] rejects any that
193    /// are, since an empty id can't be told apart from format-version-0
194    /// envelopes (which have no key id at all).
195    pub keys: HashMap<String, [u8; 32]>,
196}
197
198/// Loads encryption keys from environment variables, or from a file if
199/// `AUTH_STORAGE_ENCRYPTION_KEYS_FILE` is set.
200///
201/// **Known limitation (tracked, per CireSnave's ruling on board item 124):**
202/// env vars and files sit on the same host as the data they protect. An
203/// attacker with read access to the process environment or the key file
204/// can decrypt everything this encrypts. This is accepted as a starting
205/// point, not an endpoint -- [`KeyProvider`] exists specifically so a
206/// KMS-backed or `age`/`sops`-backed provider can replace this one later
207/// without any caller-visible change. See `docs/storage-backends.md` for
208/// the same note aimed at operators.
209///
210/// ## Format
211///
212/// - `AUTH_STORAGE_ENCRYPTION_KEY` (required unless the keys file sets
213///   `current`): the current key, base64-encoded, 32 bytes decoded.
214/// - `AUTH_STORAGE_ENCRYPTION_KEY_ID` (optional, defaults to `"1"`): the id
215///   under which `AUTH_STORAGE_ENCRYPTION_KEY` is recorded in new
216///   envelopes.
217/// - `AUTH_STORAGE_ENCRYPTION_KEYS_FILE` (optional): path to a JSON file
218///   `{"current": "<key_id>", "keys": {"<key_id>": "<base64 key>", ...}}`
219///   for rotation -- old keys stay loadable (decrypt-only in practice,
220///   since `current` picks what new writes use) as long as they're listed
221///   here. If set, this takes priority over the two env vars above. On
222///   Unix, a keys file readable by group or other triggers a `tracing::warn!`;
223///   set `AUTH_STORAGE_ENCRYPTION_STRICT_KEY_FILE_PERMISSIONS=1` to make that
224///   a startup error instead. Not checked at all on Windows.
225/// - **The single env var cannot rotate**: it holds one key, so changing it
226///   makes every stored envelope undecryptable. Use the keys file for
227///   anything that will outlive one key; the storage factory warns when the
228///   env-var key protects data that is already encrypted.
229pub struct EnvKeyProvider;
230
231impl KeyProvider for EnvKeyProvider {
232    fn load_keys(&self) -> Result<LoadedKeys> {
233        if let Ok(path) = env::var("AUTH_STORAGE_ENCRYPTION_KEYS_FILE") {
234            return Self::load_from_file(&path);
235        }
236
237        let mut key_data = env::var("AUTH_STORAGE_ENCRYPTION_KEY").map_err(|_| {
238            AuthError::config(
239                "No storage encryption key configured: set AUTH_STORAGE_ENCRYPTION_KEY \
240                 (or AUTH_STORAGE_ENCRYPTION_KEYS_FILE for multi-key rotation). \
241                 Refusing to start rather than silently storing data unencrypted.",
242            )
243        })?;
244        let key_id = env::var("AUTH_STORAGE_ENCRYPTION_KEY_ID").unwrap_or_else(|_| "1".to_string());
245        let key = decode_key(&key_data);
246        key_data.zeroize();
247        let key = key?;
248
249        let mut keys = HashMap::new();
250        keys.insert(key_id.clone(), key);
251        Ok(LoadedKeys {
252            current_key_id: key_id,
253            keys,
254        })
255    }
256}
257
258impl EnvKeyProvider {
259    fn load_from_file(path: &str) -> Result<LoadedKeys> {
260        #[derive(Deserialize)]
261        struct KeysFile {
262            current: String,
263            keys: HashMap<String, String>,
264        }
265
266        check_keys_file_permissions(path, strict_permissions_requested())?;
267
268        let mut contents = fs::read_to_string(path).map_err(|e| {
269            AuthError::config(format!(
270                "Failed to read AUTH_STORAGE_ENCRYPTION_KEYS_FILE '{path}': {e}"
271            ))
272        })?;
273        let parsed: Result<KeysFile> = serde_json::from_str(&contents).map_err(|e| {
274            AuthError::config(format!(
275                "Failed to parse AUTH_STORAGE_ENCRYPTION_KEYS_FILE '{path}': {e}"
276            ))
277        });
278        contents.zeroize();
279        let mut parsed = parsed?;
280
281        if !parsed.keys.contains_key(&parsed.current) {
282            // Zeroize every base64 key string before returning -- this
283            // early return must not be the one exit path that skips it.
284            for encoded in parsed.keys.values_mut() {
285                encoded.zeroize();
286            }
287            return Err(AuthError::config(format!(
288                "AUTH_STORAGE_ENCRYPTION_KEYS_FILE '{path}': current key id '{}' \
289                 is not present in 'keys'",
290                parsed.current
291            )));
292        }
293
294        // Decode every entry, zeroizing its base64 string regardless of
295        // success or failure, before checking for any error -- so a
296        // failure partway through doesn't leave later entries' strings
297        // (never reached by a `?`-propagating loop) un-zeroized, and
298        // doesn't leave already-decoded key bytes from earlier entries
299        // sitting in `keys` when we return Err below. Pre-sized so
300        // `insert` never triggers a reallocation that could leave a
301        // decoded key copy behind in the old, freed backing array.
302        let mut keys = HashMap::with_capacity(parsed.keys.len());
303        let mut first_error = None;
304        for (id, mut encoded) in parsed.keys {
305            match decode_key(&encoded) {
306                Ok(key) => {
307                    keys.insert(id, key);
308                }
309                Err(e) if first_error.is_none() => {
310                    first_error = Some(e);
311                }
312                Err(_) => {}
313            }
314            encoded.zeroize();
315        }
316
317        if let Some(error) = first_error {
318            for key_bytes in keys.values_mut() {
319                key_bytes.zeroize();
320            }
321            return Err(error);
322        }
323
324        Ok(LoadedKeys {
325            current_key_id: parsed.current,
326            keys,
327        })
328    }
329}
330
331/// Environment variable that turns the keys-file permission warning into a
332/// startup error. Accepts `1`, `true` or `yes` (any case).
333const STRICT_KEY_FILE_PERMISSIONS_ENV: &str = "AUTH_STORAGE_ENCRYPTION_STRICT_KEY_FILE_PERMISSIONS";
334
335/// `true` when a file mode lets group or other read (or write) the file.
336/// (Only the Unix permission check calls it outside tests.)
337#[cfg_attr(not(unix), allow(dead_code))]
338fn keys_file_mode_too_open(mode: u32) -> bool {
339    mode & 0o077 != 0
340}
341
342fn strict_permissions_requested() -> bool {
343    env::var(STRICT_KEY_FILE_PERMISSIONS_ENV)
344        .map(|value| {
345            matches!(
346                value.trim().to_ascii_lowercase().as_str(),
347                "1" | "true" | "yes"
348            )
349        })
350        .unwrap_or(false)
351}
352
353/// Checks the keys file's permissions: always warns when it is readable by
354/// group or other (a file-based key readable by others defeats its point),
355/// and fails startup when `strict` is set
356/// (`AUTH_STORAGE_ENCRYPTION_STRICT_KEY_FILE_PERMISSIONS=1`). The default
357/// stays warn-only because permission semantics vary across containers, CI
358/// and Windows; strict mode is the opt-in for deployments that control them.
359/// Unix only: elsewhere there are no group/other mode bits to inspect, so
360/// strict mode logs that it cannot be enforced.
361fn check_keys_file_permissions(path: &str, strict: bool) -> Result<()> {
362    #[cfg(unix)]
363    {
364        use std::os::unix::fs::PermissionsExt;
365        if let Ok(metadata) = fs::metadata(path) {
366            let mode = metadata.permissions().mode();
367            if keys_file_mode_too_open(mode) {
368                if strict {
369                    return Err(AuthError::config(format!(
370                        "AUTH_STORAGE_ENCRYPTION_KEYS_FILE '{path}' has mode {:o}: it is \
371                         accessible to group or other. Restrict it to the owner (chmod 600) \
372                         or unset {STRICT_KEY_FILE_PERMISSIONS_ENV}.",
373                        mode & 0o777
374                    )));
375                }
376                tracing::warn!(
377                    path = path,
378                    mode = format!("{mode:o}"),
379                    "AUTH_STORAGE_ENCRYPTION_KEYS_FILE is readable by group or other -- \
380                     restrict it to the owner only (chmod 600), or set \
381                     {STRICT_KEY_FILE_PERMISSIONS_ENV}=1 to make this fatal."
382                );
383            }
384        }
385    }
386    #[cfg(not(unix))]
387    {
388        let _ = path;
389        if strict {
390            tracing::warn!(
391                "{STRICT_KEY_FILE_PERMISSIONS_ENV} is set but key-file permissions cannot be \
392                 inspected on this platform; it is not enforced."
393            );
394        }
395    }
396    Ok(())
397}
398
399/// `true` when the key comes from the `AUTH_STORAGE_ENCRYPTION_KEY`
400/// environment variable (no keys file). That source holds exactly one key
401/// and so cannot rotate: replacing it makes every stored envelope
402/// undecryptable. Use a keys file (`AUTH_STORAGE_ENCRYPTION_KEYS_FILE`) for
403/// anything that will outlive one key.
404pub(crate) fn env_key_source_in_use() -> bool {
405    env::var("AUTH_STORAGE_ENCRYPTION_KEYS_FILE").is_err()
406        && env::var("AUTH_STORAGE_ENCRYPTION_KEY").is_ok()
407}
408
409/// Looks at up to `limit` KV entries of `storage` and reports whether any is
410/// already an encryption envelope. Bounded in the number of values read; the
411/// key listing itself is whatever the backend returns for the empty prefix.
412pub(crate) async fn stored_envelopes_exist<S: AuthStorage + ?Sized>(
413    storage: &S,
414    limit: usize,
415) -> Result<bool> {
416    for key in storage.list_kv_keys("").await?.into_iter().take(limit) {
417        if let Some(raw) = storage.get_kv(&key).await?
418            && StorageEncryption::looks_like_envelope(&raw)
419        {
420            return Ok(true);
421        }
422    }
423    Ok(false)
424}
425
426/// Produces a safe-to-log identifier for a storage key, for error
427/// messages, tracing, and migration reports -- **never** the raw key
428/// itself. In this crate, some storage keys literally ARE bearer
429/// secrets (e.g. `api_key:{raw_api_key}` in
430/// `auth_modular::user_manager`, `device:{device_code}` in
431/// `api::oauth_advanced`), so logging or printing a key name verbatim
432/// can leak a working credential into logs or a terminal.
433///
434/// Keeps the portion up to (not including) the first `:` (the
435/// namespace, e.g. `api_key`, assumed not secret since every KV
436/// namespace convention in this crate puts a fixed literal there) plus
437/// a short, one-way hash of the *full* key, so an operator with direct
438/// storage access can still correlate a reported identifier back to
439/// the real record (by recomputing the same hash over a candidate key)
440/// without the identifier itself being usable as a credential. A key
441/// with no `:` at all has no such non-secret prefix to keep -- it is
442/// replaced with the literal `key`, never the raw key itself.
443fn safe_log_id(key: &str) -> String {
444    use sha2::{Digest, Sha256};
445    let hash_hex = hex::encode(&Sha256::digest(key.as_bytes())[..8]);
446    match key.split_once(':') {
447        Some((namespace, _)) => format!("{namespace}:{hash_hex}"),
448        None => format!("key:{hash_hex}"),
449    }
450}
451
452fn decode_key(encoded: &str) -> Result<[u8; 32]> {
453    let mut key_bytes = BASE64
454        .decode(encoded)
455        .map_err(|_| AuthError::config("Invalid base64 in storage encryption key"))?;
456    if key_bytes.len() != 32 {
457        key_bytes.zeroize();
458        return Err(AuthError::config(
459            "Storage encryption key must be 32 bytes (256 bits) once base64-decoded",
460        ));
461    }
462    let mut key = [0u8; 32];
463    key.copy_from_slice(&key_bytes);
464    key_bytes.zeroize();
465    Ok(key)
466}
467
468/// Storage encryption manager using AES-256-GCM, with per-record random
469/// nonces and associated data (AAD) binding each ciphertext to the storage
470/// key it was stored under -- so a ciphertext copied from one record to
471/// another fails to decrypt instead of silently "succeeding" with the
472/// wrong value.
473///
474/// **Known limitation (replay of an older value under the same key):** AAD
475/// binds a ciphertext to *which record* it belongs to, not to *when* it was
476/// written. Someone who can both read an old ciphertext for a given key and
477/// write to that same key can replay the old value -- AES-GCM's
478/// authentication only proves "this is a genuine envelope for this record,"
479/// not "this is the most recent one." Mitigating this needs a monotonic
480/// counter or timestamp bound into the AAD, not done here (most KV-layer
481/// values in this crate are replaced wholesale on every write, which bounds
482/// but does not eliminate the exposure).
483///
484/// **Zeroization scope (accurate, not aspirational):** this module
485/// zeroizes raw key material and intermediate plaintext it owns on every
486/// exit path it controls (success, a validation error, or an encryption
487/// failure partway through), as soon as it's done with it -- the
488/// `[u8; 32]` key bytes in [`LoadedKeys`] (after the AES-GCM ciphers are
489/// built, or before any validation error returns, including the
490/// "current key id not found" check), [`decode_key`]'s intermediate
491/// decoded `Vec<u8>`, every base64-encoded key string
492/// [`EnvKeyProvider`] reads (file contents and every per-key entry, even
493/// ones after a failure elsewhere in the same batch, and even when the
494/// file's `current` id isn't present in `keys`), and
495/// [`migrate_kv_to_encrypted`]'s intermediate plaintext/raw buffers,
496/// including when the subsequent re-encrypt or write-back fails. It does
497/// **not** zeroize: any copy the standard library or `aes-gcm` makes
498/// internally that this code doesn't control (notably `aes` 0.8.4's own
499/// key-schedule storage inside the `Aes256Gcm` cipher object, which has
500/// no zeroize support in the version this crate depends on; and
501/// `HashMap` growth can in principle leave a stale decoded-key copy in
502/// an old, freed backing allocation during a resize -- mitigated, not
503/// eliminated, in `EnvKeyProvider::load_from_file`'s multi-key decoding
504/// loop (the one production path that decodes a variable, potentially
505/// large number of keys into a map) by pre-sizing it with
506/// `with_capacity`; `EnvKeyProvider::load_keys`' single-env-var path
507/// only ever inserts exactly one entry, so it can't resize at all), or
508/// a plaintext value a public method (`decrypt`,
509/// `decrypt_from_storage`) hands back to its caller (that's the actual
510/// return value the caller needs -- it isn't leftover residue this module
511/// can clean up on the caller's behalf).
512pub struct StorageEncryption {
513    current_key_id: String,
514    ciphers: HashMap<String, Aes256Gcm>,
515}
516
517impl StorageEncryption {
518    /// Create a new encryption manager, loading keys from the given
519    /// provider. Fails closed: if the provider can't load a usable key,
520    /// this returns `Err` rather than any fallback.
521    pub fn new(provider: &dyn KeyProvider) -> Result<Self> {
522        let mut loaded = provider.load_keys()?;
523
524        // Validate first, but zeroize the raw key bytes on EVERY exit
525        // path (including these early validation failures) -- not just
526        // the success path -- so a rejected key never lingers in memory
527        // any longer than the other branch does.
528        let validation_error = if loaded.current_key_id.is_empty() {
529            Some(AuthError::config(
530                "Key provider's current_key_id must not be empty",
531            ))
532        } else if loaded.keys.keys().any(|id| id.is_empty()) {
533            Some(AuthError::config(
534                "Key provider returned a key with an empty id -- an empty id is reserved \
535                 to mean \"format-version-0 envelope, no key id at all\"",
536            ))
537        } else if !loaded.keys.contains_key(&loaded.current_key_id) {
538            Some(AuthError::config(format!(
539                "Key provider's current_key_id '{}' is not among the keys it loaded",
540                loaded.current_key_id
541            )))
542        } else {
543            None
544        };
545
546        if let Some(error) = validation_error {
547            for key_bytes in loaded.keys.values_mut() {
548                key_bytes.zeroize();
549            }
550            return Err(error);
551        }
552
553        let ciphers = loaded
554            .keys
555            .iter()
556            .map(|(id, key_bytes)| {
557                let key = Key::<Aes256Gcm>::from_slice(key_bytes);
558                (id.clone(), Aes256Gcm::new(key))
559            })
560            .collect();
561        // The cipher objects above have their own internal copies of the
562        // key material; zeroize the raw bytes we held so they don't sit
563        // around in this struct any longer than necessary.
564        for key_bytes in loaded.keys.values_mut() {
565            key_bytes.zeroize();
566        }
567        Ok(Self {
568            current_key_id: loaded.current_key_id,
569            ciphers,
570        })
571    }
572
573    /// Create a new encryption manager from the environment (see
574    /// [`EnvKeyProvider`]). Convenience wrapper over `new(&EnvKeyProvider)`.
575    pub fn from_env() -> Result<Self> {
576        Self::new(&EnvKeyProvider)
577    }
578
579    /// Create new encryption manager for testing with a single random key.
580    #[cfg(test)]
581    pub fn new_random() -> Self {
582        use rand::Rng;
583        let mut key_bytes = [0u8; 32];
584        rand::rng().fill_bytes(&mut key_bytes);
585        let key = Key::<Aes256Gcm>::from_slice(&key_bytes);
586        let mut ciphers = HashMap::new();
587        ciphers.insert("test".to_string(), Aes256Gcm::new(key));
588        key_bytes.zeroize();
589        Self {
590            current_key_id: "test".to_string(),
591            ciphers,
592        }
593    }
594
595    /// Generate a new 256-bit encryption key (base64 encoded), suitable for
596    /// `AUTH_STORAGE_ENCRYPTION_KEY` or a keys-file entry.
597    pub fn generate_key() -> String {
598        use rand::Rng;
599        let mut key_bytes = [0u8; 32];
600        rand::rng().fill_bytes(&mut key_bytes);
601        let encoded = BASE64.encode(key_bytes);
602        key_bytes.zeroize();
603        encoded
604    }
605
606    /// Encrypt raw bytes. `aad` (associated data) is authenticated but not
607    /// encrypted -- callers should pass the storage key the resulting
608    /// envelope will be stored under, binding the ciphertext to that
609    /// specific record.
610    pub fn encrypt(&self, plaintext: &[u8], aad: &[u8]) -> Result<EncryptedData> {
611        use rand::Rng;
612        let cipher = self.ciphers.get(&self.current_key_id).ok_or_else(|| {
613            AuthError::internal("Current encryption key missing from loaded cipher set")
614        })?;
615
616        // 96-bit nonce, freshly random every call. NIST SP 800-38D caps a
617        // randomly generated 96-bit IV at 2^32 invocations under a single
618        // key before the collision probability becomes unacceptable (the
619        // "32-bit random IV construction" guidance) -- not the ~2^64/2
620        // birthday bound a naive calculation might suggest. A given
621        // deployment's key should be rotated well before approaching that
622        // count; this crate does not currently track or enforce it.
623        let mut nonce_bytes = [0u8; 12];
624        rand::rng().fill_bytes(&mut nonce_bytes);
625        let nonce = Nonce::from_slice(&nonce_bytes);
626
627        let bound = bound_aad(CURRENT_FORMAT_VERSION, ALGORITHM, &self.current_key_id, aad);
628        let ciphertext = cipher
629            .encrypt(
630                nonce,
631                Payload {
632                    msg: plaintext,
633                    aad: &bound,
634                },
635            )
636            .map_err(|e| AuthError::internal(format!("Encryption failed: {}", e)))?;
637
638        Ok(EncryptedData {
639            data: BASE64.encode(&ciphertext),
640            nonce: BASE64.encode(nonce_bytes),
641            algorithm: ALGORITHM.to_string(),
642            key_id: self.current_key_id.clone(),
643            v: CURRENT_FORMAT_VERSION,
644            key_derivation: String::new(),
645        })
646    }
647
648    /// Decrypt an envelope. `aad` must match exactly what was passed to
649    /// [`Self::encrypt`] (the storage key the envelope is stored under) --
650    /// a mismatch (e.g. a ciphertext copied to a different record) fails
651    /// decryption rather than succeeding with the wrong plaintext.
652    ///
653    /// A format-version-0 envelope (see [`EncryptedData`]) is decrypted
654    /// with an empty AAD against every loaded key in turn, matching the
655    /// original `EncryptedStorage`'s behavior (no AAD, no key id) --
656    /// AES-GCM's authentication tag makes trying the wrong key safe (it
657    /// just fails), so this cannot silently produce the wrong plaintext.
658    pub fn decrypt(&self, encrypted: &EncryptedData, aad: &[u8]) -> Result<Vec<u8>> {
659        // Every failure below returns the SAME error text (`DECRYPT_FAILED`);
660        // the cause is logged only.
661        let fail = |why: &str| -> AuthError {
662            // `key_id` comes from the (possibly attacker-writable) store:
663            // log it Debug-escaped and bounded.
664            tracing::warn!(
665                key_id = ?encrypted.key_id.chars().take(64).collect::<String>(),
666                v = encrypted.v,
667                "storage envelope decryption failed: {why}"
668            );
669            AuthError::internal(DECRYPT_FAILED)
670        };
671
672        if encrypted.algorithm != ALGORITHM {
673            return Err(fail("unsupported algorithm"));
674        }
675
676        let ciphertext = BASE64
677            .decode(&encrypted.data)
678            .map_err(|_| fail("invalid base64 in data"))?;
679        let nonce_bytes = BASE64
680            .decode(&encrypted.nonce)
681            .map_err(|_| fail("invalid base64 in nonce"))?;
682        if nonce_bytes.len() != 12 {
683            return Err(fail("invalid nonce length"));
684        }
685        let nonce = Nonce::from_slice(&nonce_bytes);
686
687        if encrypted.v == 0 {
688            // Format version 0 (the original EncryptedStorage): no key id,
689            // no AAD. Try every loaded key, in a stable order; the auth tag
690            // rejects wrong ones.
691            for id in self.sorted_key_ids() {
692                if let Some(cipher) = self.ciphers.get(&id)
693                    && let Ok(plaintext) = cipher.decrypt(
694                        nonce,
695                        Payload {
696                            msg: &ciphertext,
697                            aad: b"",
698                        },
699                    )
700                {
701                    return Ok(plaintext);
702                }
703            }
704            return Err(fail("no loaded key opens this format-version-0 envelope"));
705        }
706
707        let cipher = self
708            .ciphers
709            .get(&encrypted.key_id)
710            .ok_or_else(|| fail("no loaded key for this key_id (rotated out?)"))?;
711
712        // v1 authenticated only the bare record AAD; v2 also binds the
713        // envelope's own metadata. Anything else is unknown to this build.
714        let bound;
715        let effective_aad: &[u8] = match encrypted.v {
716            FORMAT_V1 => aad,
717            CURRENT_FORMAT_VERSION => {
718                bound = bound_aad(encrypted.v, &encrypted.algorithm, &encrypted.key_id, aad);
719                &bound
720            }
721            _ => return Err(fail("unsupported envelope version")),
722        };
723
724        cipher
725            .decrypt(
726                nonce,
727                Payload {
728                    msg: &ciphertext,
729                    aad: effective_aad,
730                },
731            )
732            .map_err(|_| fail("authentication failed (wrong key, wrong record, or tampered)"))
733    }
734
735    /// Encrypt raw bytes for storage, serialized as a self-describing
736    /// envelope. `aad` should be the storage key this will be stored
737    /// under. Accepts arbitrary bytes, not just UTF-8 text.
738    pub fn encrypt_for_storage(&self, data: &[u8], aad: &[u8]) -> Result<Vec<u8>> {
739        let encrypted = self.encrypt(data, aad)?;
740        let serialized = serde_json::to_string(&encrypted).map_err(|e| {
741            AuthError::internal(format!("Failed to serialize encrypted data: {}", e))
742        })?;
743        Ok(serialized.into_bytes())
744    }
745
746    /// Decrypt a storage envelope produced by [`Self::encrypt_for_storage`].
747    /// `aad` must be the same storage key passed to that call.
748    pub fn decrypt_from_storage(&self, data: &[u8], aad: &[u8]) -> Result<Vec<u8>> {
749        // A stored value that is not even an envelope fails with the same
750        // text as every other decryption failure; the cause is logged.
751        let parsed = std::str::from_utf8(data)
752            .map_err(|e| e.to_string())
753            .and_then(|s| serde_json::from_str::<EncryptedData>(s).map_err(|e| e.to_string()));
754        let encrypted = parsed.map_err(|why| {
755            tracing::warn!("stored value is not a readable envelope: {why}");
756            AuthError::internal(DECRYPT_FAILED)
757        })?;
758        self.decrypt(&encrypted, aad)
759    }
760
761    /// Parses `data` as one of this module's own serialized envelopes --
762    /// either the current format or the original format-version-0 shape
763    /// (see [`EncryptedData`]) -- without attempting to decrypt it. `None`
764    /// if `data` isn't valid UTF-8 JSON, doesn't name the right algorithm,
765    /// or claims a post-v0 format without a `key_id`.
766    ///
767    /// This is a **shape** check only. A value that parses here but fails
768    /// to decrypt (wrong key, corrupted, or tampered with) is a hard error
769    /// from [`Self::decrypt_from_storage`], never silently treated as
770    /// plaintext.
771    pub fn parse_envelope(data: &[u8]) -> Option<EncryptedData> {
772        let encrypted = std::str::from_utf8(data)
773            .ok()
774            .and_then(|s| serde_json::from_str::<EncryptedData>(s).ok())?;
775        if encrypted.algorithm != ALGORITHM {
776            return None;
777        }
778        // v0 has neither `v` nor `key_id`. Anything claiming a later
779        // format must carry an explicit non-empty `key_id`; without one it
780        // is just plaintext JSON that happens to resemble an envelope.
781        let well_formed = if encrypted.v == 0 {
782            encrypted.key_id.is_empty()
783        } else {
784            !encrypted.key_id.is_empty()
785        };
786        well_formed.then_some(encrypted)
787    }
788
789    /// Key ids in a stable (ascending) order, for deterministic trial
790    /// decryption.
791    fn sorted_key_ids(&self) -> Vec<String> {
792        let mut ids: Vec<String> = self.ciphers.keys().cloned().collect();
793        ids.sort();
794        ids
795    }
796
797    /// Returns `true` if `data` parses as one of this module's own
798    /// serialized envelopes. Used by [`migrate_kv_to_encrypted`] and
799    /// [`EncryptedStorage::get_kv`] to decide whether a stored value
800    /// should be decrypted at all.
801    pub fn looks_like_envelope(data: &[u8]) -> bool {
802        Self::parse_envelope(data).is_some()
803    }
804}
805
806/// Wrapper for storage backends that adds encryption at rest to the
807/// generic key-value (`store_kv`/`get_kv`) layer.
808///
809/// **Coverage note:** this wraps `store_kv`/`get_kv` only. The dedicated
810/// `store_token`/`store_session` methods (and each backend's own typed
811/// columns for them) are NOT touched by this wrapper -- they delegate to
812/// `inner` unchanged. Encrypting those would need changes to the
813/// `AuthStorage` trait itself (their fields are typed, not opaque bytes),
814/// tracked separately. This wrapper covers everything that already goes
815/// through `store_kv` today: API keys, TOTP secrets, OAuth2 client
816/// registries, MFA codes, and most of the other KV-backed subsystems
817/// found in the storage audit (`docs/STORAGE-AUDIT.md`).
818pub struct EncryptedStorage<T> {
819    inner: T,
820    encryption: StorageEncryption,
821    /// See [`Self::get_kv`]'s doc comment for exactly what this controls.
822    allow_plaintext_reads: bool,
823    /// See [`Self::get_kv`]'s doc comment for exactly what this controls.
824    allow_legacy_v0: bool,
825}
826
827impl<T> EncryptedStorage<T> {
828    /// `allow_plaintext_reads` should be `true` only transiently, while
829    /// migrating an existing deployment's plaintext data (see
830    /// [`migrate_kv_to_encrypted`]) -- it is what makes [`Self::get_kv`]
831    /// tolerate a value that isn't a valid envelope instead of erroring.
832    /// Leaving it `true` permanently means anyone who can write to the
833    /// backing store can overwrite an encrypted value with chosen
834    /// plaintext (or corrupt one) and have it accepted silently, which
835    /// defeats the point of encrypting at rest. Pass `false` for normal
836    /// operation.
837    ///
838    /// `allow_legacy_v0` should likewise be `true` only transiently, while
839    /// migrating pre-existing format-version-0 data: a v0 envelope has no
840    /// AAD binding it to its own storage key, so with this `true`, a
841    /// writer who knows one record's v0 envelope can copy it onto a
842    /// different record's key and it still decrypts -- the exact swap the
843    /// current format's AAD exists to prevent. This flag is independent
844    /// of [`migrate_kv_to_encrypted`]'s own, separate
845    /// `accept_legacy_v0` parameter (that one gates whether migration
846    /// actually *rewrites* a v0 envelope; this one gates whether this
847    /// wrapper's `get_kv` will *read* one at all) -- set this one `true`
848    /// if something needs to read v0 data through this wrapper before,
849    /// or without ever, running migration.
850    pub fn new(
851        storage: T,
852        encryption: StorageEncryption,
853        allow_plaintext_reads: bool,
854        allow_legacy_v0: bool,
855    ) -> Self {
856        Self {
857            inner: storage,
858            encryption,
859            allow_plaintext_reads,
860            allow_legacy_v0,
861        }
862    }
863
864    pub fn into_inner(self) -> T {
865        self.inner
866    }
867}
868
869#[async_trait::async_trait]
870impl<T> AuthStorage for EncryptedStorage<T>
871where
872    T: AuthStorage + Send + Sync,
873{
874    // Token/session methods — NOT encrypted by this wrapper. See the
875    // "Coverage note" on `EncryptedStorage` itself.
876    async fn store_token(&self, token: &AuthToken) -> Result<()> {
877        self.inner.store_token(token).await
878    }
879
880    async fn get_token(&self, token_id: &str) -> Result<Option<AuthToken>> {
881        self.inner.get_token(token_id).await
882    }
883
884    async fn get_token_by_access_token(&self, access_token: &str) -> Result<Option<AuthToken>> {
885        self.inner.get_token_by_access_token(access_token).await
886    }
887
888    async fn update_token(&self, token: &AuthToken) -> Result<()> {
889        self.inner.update_token(token).await
890    }
891
892    async fn delete_token(&self, token_id: &str) -> Result<()> {
893        self.inner.delete_token(token_id).await
894    }
895
896    async fn list_user_tokens(&self, user_id: &str) -> Result<Vec<AuthToken>> {
897        self.inner.list_user_tokens(user_id).await
898    }
899
900    async fn store_session(&self, session_id: &str, data: &SessionData) -> Result<()> {
901        self.inner.store_session(session_id, data).await
902    }
903
904    async fn get_session(&self, session_id: &str) -> Result<Option<SessionData>> {
905        self.inner.get_session(session_id).await
906    }
907
908    async fn delete_session(&self, session_id: &str) -> Result<()> {
909        self.inner.delete_session(session_id).await
910    }
911
912    async fn list_user_sessions(&self, user_id: &str) -> Result<Vec<SessionData>> {
913        self.inner.list_user_sessions(user_id).await
914    }
915
916    async fn count_active_sessions(&self) -> Result<u64> {
917        self.inner.count_active_sessions().await
918    }
919
920    // Bulk methods — delegate directly to `inner`'s own overrides (if any)
921    // instead of inheriting the `AuthStorage` trait's one-at-a-time default
922    // loop, so a backend that implements real batching here keeps that
923    // benefit even when wrapped. Token/session bulk methods are unaffected
924    // by encryption (see the coverage note above) either way.
925    async fn store_tokens_bulk(&self, tokens: &[AuthToken]) -> Result<()> {
926        self.inner.store_tokens_bulk(tokens).await
927    }
928
929    async fn delete_tokens_bulk(&self, token_ids: &[String]) -> Result<()> {
930        self.inner.delete_tokens_bulk(token_ids).await
931    }
932
933    async fn store_sessions_bulk(&self, sessions: &[(String, SessionData)]) -> Result<()> {
934        self.inner.store_sessions_bulk(sessions).await
935    }
936
937    async fn delete_sessions_bulk(&self, session_ids: &[String]) -> Result<()> {
938        self.inner.delete_sessions_bulk(session_ids).await
939    }
940
941    // Key-value methods — encrypted, with the storage key itself as AAD so
942    // a ciphertext can't be swapped between records.
943    async fn get_kv_ttl(&self, key: &str) -> Result<Option<Duration>> {
944        // A TTL is storage metadata, not part of the encrypted value.
945        self.inner.get_kv_ttl(key).await
946    }
947
948    async fn store_kv(&self, key: &str, value: &[u8], ttl: Option<Duration>) -> Result<()> {
949        let encrypted_value = self.encryption.encrypt_for_storage(value, key.as_bytes())?;
950        self.inner.store_kv(key, &encrypted_value, ttl).await
951    }
952
953    async fn store_kv_if_absent(
954        &self,
955        key: &str,
956        value: &[u8],
957        ttl: Option<Duration>,
958    ) -> Result<bool> {
959        let encrypted_value = self.encryption.encrypt_for_storage(value, key.as_bytes())?;
960        self.inner
961            .store_kv_if_absent(key, &encrypted_value, ttl)
962            .await
963    }
964
965    /// Reads back a value stored by [`Self::store_kv`], decrypting it.
966    ///
967    /// A value that **parses as one of this module's envelopes**
968    /// ([`StorageEncryption::looks_like_envelope`]) is always decrypted;
969    /// a decryption failure (wrong key, corrupted, tampered) is a hard
970    /// `Err`, never silently returned as-is.
971    ///
972    /// A value that does **not** parse as an envelope at all is ambiguous:
973    /// it could be genuine pre-encryption plaintext, or it could be a
974    /// plaintext value an attacker (or a bug) overwrote the encrypted one
975    /// with. This method only tolerates that ambiguity -- returning the
976    /// raw bytes as-is -- when `allow_plaintext_reads` is `true`. With it
977    /// `false` (the default via the storage factory once migration is
978    /// done), a non-envelope value is also a hard `Err`, so overwriting an
979    /// encrypted record with plaintext cannot silently succeed.
980    async fn get_kv(&self, key: &str) -> Result<Option<Vec<u8>>> {
981        let Some(raw) = self.inner.get_kv(key).await? else {
982            return Ok(None);
983        };
984
985        if let Some(envelope) = StorageEncryption::parse_envelope(&raw) {
986            if envelope.v < CURRENT_FORMAT_VERSION && !self.allow_legacy_v0 {
987                return Err(AuthError::internal(format!(
988                    "Value for record '{}' uses legacy envelope format version {} (its \
989                     metadata is not bound into the authenticated data), and \
990                     storage_encryption.allow_legacy_v0 is false. Run the migration tool \
991                     (`auth-framework-admin security encrypt-kv --accept-legacy-v0`) to \
992                     upgrade it to the current format, or temporarily set \
993                     storage_encryption.allow_legacy_v0 = true if something else needs to \
994                     read it first.",
995                    safe_log_id(key),
996                    envelope.v
997                )));
998            }
999            let decrypted = self.encryption.decrypt(&envelope, key.as_bytes())?;
1000            return Ok(Some(decrypted));
1001        }
1002
1003        if self.allow_plaintext_reads {
1004            return Ok(Some(raw));
1005        }
1006
1007        Err(AuthError::internal(format!(
1008            "Value for record '{}' is not a valid encrypted envelope, and \
1009             storage_encryption.allow_plaintext_reads is false. If this key genuinely \
1010             predates encryption being enabled, run the migration tool \
1011             (`auth-framework-admin security encrypt-kv`) or temporarily set \
1012             storage_encryption.allow_plaintext_reads = true while migrating.",
1013            safe_log_id(key)
1014        )))
1015    }
1016
1017    fn tracks_kv_ttl(&self) -> bool {
1018        // A TTL is storage metadata, not part of the encrypted value.
1019        self.inner.tracks_kv_ttl()
1020    }
1021
1022    fn encrypts_kv_at_rest(&self) -> bool {
1023        true
1024    }
1025
1026    async fn delete_kv(&self, key: &str) -> Result<()> {
1027        self.inner.delete_kv(key).await
1028    }
1029
1030    // Key NAMES are never encrypted (only values) -- pass through
1031    // unchanged so RBAC role reload, maintenance/backup, and analytics
1032    // (all of which call this) keep working once this wrapper is in the
1033    // default path. Without this override, the AuthStorage trait's
1034    // default implementation would fail (it used to return an empty Vec).
1035    async fn list_kv_keys(&self, prefix: &str) -> Result<Vec<String>> {
1036        self.inner.list_kv_keys(prefix).await
1037    }
1038
1039    async fn cleanup_expired(&self) -> Result<()> {
1040        self.inner.cleanup_expired().await
1041    }
1042}
1043
1044/// Outcome of [`migrate_kv_to_encrypted`].
1045///
1046/// `#[non_exhaustive]`: this is a read-only outcome type, not something
1047/// a caller builds -- only this crate's own tests construct one (with
1048/// `..Default::default()`, which only compiles from *inside* the
1049/// crate), so a later field addition here can never be a breaking
1050/// change for an external caller.
1051#[derive(Debug, Clone, Default, PartialEq, Eq)]
1052#[non_exhaustive]
1053pub struct KvEncryptionMigrationReport {
1054    /// Whether this run was a dry run (no writes performed).
1055    pub dry_run: bool,
1056    /// Total keys examined under the given prefix. Always equal to
1057    /// `already_encrypted + legacy_v0_found + encrypted + vanished +
1058    /// failed.len()`.
1059    pub scanned: u64,
1060    /// Keys that were already a current-format envelope -- left
1061    /// untouched.
1062    pub already_encrypted: u64,
1063    /// Keys that were a LEGACY envelope (format version 0 or 1; the name
1064    /// is historical) that decrypted successfully -- counted here whether
1065    /// or not they were actually rewritten (see `upgraded_from_legacy`).
1066    /// A v0 envelope has no AAD binding its plaintext to this specific
1067    /// record, so this plaintext's association with this key could not
1068    /// be cryptographically verified -- it is exactly as trustworthy as
1069    /// it was before migration, no more, no less. A v1 envelope's AAD
1070    /// does bind the record key (its decryption just verified that), but
1071    /// not its own metadata.
1072    pub legacy_v0_found: u64,
1073    /// Keys whose legacy (v0 or v1) envelope was actually rewritten under
1074    /// the current format. Zero unless this was a real (non-dry) run
1075    /// AND `accept_legacy_v0` was `true` -- see
1076    /// [`migrate_kv_to_encrypted`]'s docs for why that flag exists and
1077    /// what rewriting a v0 envelope does and does not verify.
1078    pub upgraded_from_legacy: u64,
1079    /// Keys that were plaintext (not an envelope at all) and got
1080    /// encrypted (or would have, in a dry run).
1081    pub encrypted: u64,
1082    /// Keys that were read but had vanished by the time of the write-back
1083    /// (concurrent deletion) -- not an error, just skipped.
1084    pub vanished: u64,
1085    /// Format-version-0 records whose envelope could not be decrypted
1086    /// under any loaded key (wrong/missing key, or corrupted). Skipped,
1087    /// not treated as fatal -- the rest of the run still completes.
1088    /// **Only v0 envelopes are decrypt-checked by this function** --
1089    /// a current-format (v1) envelope that can't decrypt (e.g. its key
1090    /// was rotated out) is not migration's concern (it's already in the
1091    /// target format) and is counted in `already_encrypted`, not here.
1092    /// Entries are a **safe identifier** (see [`safe_log_id`]), never the
1093    /// raw storage key -- some keys in this crate are themselves bearer
1094    /// secrets (e.g. `api_key:{raw_key}`).
1095    pub failed: Vec<String>,
1096}
1097
1098/// Options for [`migrate_kv_to_encrypted`]. A named struct rather than
1099/// two adjacent `bool` parameters, specifically so a caller can't
1100/// transpose them -- `(false, true)` instead of `(true, false)` would
1101/// otherwise silently turn an intended dry run into a real upgrade.
1102#[derive(Debug, Clone, Copy, Default)]
1103#[non_exhaustive]
1104pub struct MigrationOptions {
1105    /// Preview only; don't write anything. See
1106    /// [`migrate_kv_to_encrypted`]'s own docs for exactly what a dry run
1107    /// still does (it attempts every decrypt a real run would, for
1108    /// accurate reporting).
1109    pub dry_run: bool,
1110    /// Required, together with `dry_run: false`, to actually rewrite a
1111    /// format-version-0 envelope to the current format. See
1112    /// [`migrate_kv_to_encrypted`]'s own docs for why this is a separate,
1113    /// explicit opt-in.
1114    pub accept_legacy_v0: bool,
1115    /// A backend that cannot report KV TTLs ([`AuthStorage::tracks_kv_ttl`]
1116    /// is `false`) makes a real migration turn every expiring entry into a
1117    /// permanent one. Such a run is refused unless this is `true`.
1118    pub accept_ttl_loss: bool,
1119}
1120
1121impl MigrationOptions {
1122    /// Builder-style setter; see [`Self::with_dry_run`].
1123    pub fn with_accept_ttl_loss(mut self, accept_ttl_loss: bool) -> Self {
1124        self.accept_ttl_loss = accept_ttl_loss;
1125        self
1126    }
1127
1128    /// Builder-style setter. `#[non_exhaustive]` means `MigrationOptions {
1129    /// dry_run: true, ..Default::default() }` only compiles *inside*
1130    /// this crate -- a downstream caller needs `let mut o =
1131    /// MigrationOptions::default(); o.dry_run = true;` or this method
1132    /// chain instead: `MigrationOptions::default().with_dry_run(true)`.
1133    pub fn with_dry_run(mut self, dry_run: bool) -> Self {
1134        self.dry_run = dry_run;
1135        self
1136    }
1137
1138    /// Builder-style setter; see [`Self::with_dry_run`] for why this
1139    /// exists instead of a struct literal.
1140    pub fn with_accept_legacy_v0(mut self, accept_legacy_v0: bool) -> Self {
1141        self.accept_legacy_v0 = accept_legacy_v0;
1142        self
1143    }
1144}
1145
1146/// Re-encrypts every already-plaintext value under `prefix` in the given
1147/// KV storage, in place.
1148///
1149/// - **Idempotent and resumable**: a value already in the *current*
1150///   envelope format is left untouched. Re-running this function (e.g.
1151///   after it was interrupted, or because some keys were skipped as
1152///   `failed`) only ever touches the plaintext and not-yet-upgraded v0
1153///   values still remaining -- there's no separate resume cursor to
1154///   manage.
1155/// - **A single undecryptable v0 envelope never aborts the run**: if one
1156///   key's format-version-0 envelope can't be decrypted under any
1157///   loaded key (wrong/missing key, or corrupted -- only v0 envelopes
1158///   are decrypt-checked this way, see
1159///   [`KvEncryptionMigrationReport::failed`]'s own docs), a safe
1160///   identifier for it (never the raw key) is recorded in `failed` and
1161///   it's skipped -- every other key is still processed. Anyone who can
1162///   write one envelope-shaped value under `prefix` can otherwise make
1163///   an `?` on the first failure block the entire run.
1164/// - **Dry-run is accurate, not optimistic**: `dry_run: true` actually
1165///   attempts every decrypt it would need for a real run (it just skips
1166///   the write-back), so its counts -- including `failed` -- match what
1167///   a real run would do, rather than assuming every v0 envelope found
1168///   would succeed.
1169/// - **Format-version-0 upgrades require `accept_legacy_v0: true` AND a
1170///   real (non-dry) run.** A v0 envelope has no AAD, so decrypting it
1171///   proves nothing about *which record* its plaintext actually belongs
1172///   to -- it decrypts successfully under any loaded key regardless of
1173///   where it's stored. **This function cannot detect a v0 envelope
1174///   that's been copied from one record onto another**; it can only
1175///   decrypt-then-re-encrypt whatever is there, inheriting that
1176///   plaintext's association with this key exactly as unverified as it
1177///   was before. `accept_legacy_v0` exists so that upgrading is never a
1178///   silent, implicit side effect of running this function -- an
1179///   operator must explicitly accept that risk. With it `false` (or in
1180///   a dry run), v0 envelopes are still *decrypted* for reporting
1181///   accuracy (see the bullet above) and counted in
1182///   [`KvEncryptionMigrationReport::legacy_v0_found`], but never
1183///   written back; `upgraded_from_legacy` only increases on an actual
1184///   real-run upgrade. Every real upgrade logs one `tracing::warn!` line
1185///   naming a safe identifier for the record (never its raw key or
1186///   value -- see below).
1187/// - **Never logs or reports the raw storage key, plaintext, or
1188///   ciphertext**: in this crate, some storage keys are themselves
1189///   bearer secrets (e.g. `api_key:{raw_api_key}`), so logging a key
1190///   name verbatim can leak a working credential. Everywhere this
1191///   function logs or reports a record (`tracing::warn!` lines,
1192///   [`KvEncryptionMigrationReport::failed`]), it uses a short,
1193///   non-reversible, safe identifier instead of the raw key; callers
1194///   should preserve that if they add their own logging around this.
1195/// - **Not safe to run concurrently with live writes to the same keys**:
1196///   this does a read, then a write-back, with no compare-and-swap. If
1197///   something else writes a fresh value to a key between this function's
1198///   read and write, the write-back here overwrites it with the stale
1199///   value it read. Run this offline, or at least pause writers to the
1200///   given `prefix`, before running it against a live deployment.
1201///
1202/// `storage` and `encryption` are the same values the caller would pass to
1203/// [`EncryptedStorage::new`] -- this function talks to the *inner*,
1204/// unwrapped storage directly, since [`EncryptedStorage::get_kv`] would
1205/// already transparently decrypt (and thus hide which rows still need
1206/// migrating).
1207///
1208/// **TTLs are preserved** on backends that report them
1209/// ([`AuthStorage::tracks_kv_ttl`]): each rewritten entry is stored with
1210/// the remaining TTL it had (read via [`AuthStorage::get_kv_ttl`] before the
1211/// value, so an entry that expires mid-run is skipped, never made
1212/// permanent). A backend that cannot report TTLs would turn every expiring
1213/// entry (OAuth codes, MFA/SMS codes, WebAuthn challenges, rate-limit
1214/// windows, expiring API keys) permanent, so a real (non-dry) run on one is
1215/// **refused** unless [`MigrationOptions::accept_ttl_loss`] is set; if it
1216/// is, scope `prefix` to durable-secret namespaces only. The CLI
1217/// (`security encrypt-kv`) exposes this as `--accept-ttl-loss`.
1218pub async fn migrate_kv_to_encrypted<S: AuthStorage + ?Sized>(
1219    storage: &S,
1220    encryption: &StorageEncryption,
1221    prefix: &str,
1222    options: MigrationOptions,
1223) -> Result<KvEncryptionMigrationReport> {
1224    let MigrationOptions {
1225        dry_run,
1226        accept_legacy_v0,
1227        accept_ttl_loss,
1228    } = options;
1229
1230    // A backend that cannot report TTLs would have every expiring entry
1231    // re-stored as permanent. Refuse before touching anything unless the
1232    // caller accepts that; a dry run writes nothing, so it is always fine.
1233    if !dry_run && !accept_ttl_loss && !storage.tracks_kv_ttl() {
1234        return Err(AuthError::validation(
1235            "This storage backend does not report KV TTLs, so migrating would make every \
1236             expiring entry (OAuth codes, MFA/SMS codes, rate-limit windows, expiring API \
1237             keys, ...) permanent. Scope the prefix to durable secrets and pass \
1238             accept_ttl_loss (CLI: --accept-ttl-loss) to proceed anyway.",
1239        ));
1240    }
1241    let mut report = KvEncryptionMigrationReport {
1242        dry_run,
1243        ..Default::default()
1244    };
1245
1246    let keys = storage.list_kv_keys(prefix).await?;
1247    report.scanned = keys.len() as u64;
1248
1249    for key in keys {
1250        // The TTL is read BEFORE the value (see the maintenance snapshot
1251        // code): an entry that expires in between yields a TTL and no value
1252        // (counted as vanished), never a value whose expiry was lost.
1253        let ttl = storage.get_kv_ttl(&key).await?;
1254        let Some(mut raw) = storage.get_kv(&key).await? else {
1255            // Deleted concurrently between list_kv_keys and get_kv.
1256            report.vanished += 1;
1257            continue;
1258        };
1259
1260        if let Some(envelope) = StorageEncryption::parse_envelope(&raw) {
1261            if envelope.v >= CURRENT_FORMAT_VERSION {
1262                report.already_encrypted += 1;
1263                continue;
1264            }
1265
1266            // Format-version-0 envelope: always attempt the decrypt (so
1267            // dry-run reporting is accurate), but never let a failure
1268            // here abort the rest of the run.
1269            match encryption.decrypt(&envelope, key.as_bytes()) {
1270                Err(_) => {
1271                    tracing::warn!(
1272                        record = %safe_log_id(&key),
1273                        "migrate_kv_to_encrypted: could not decrypt format-version-0 \
1274                         envelope with any loaded key -- skipping, not aborting the rest \
1275                         of the run"
1276                    );
1277                    report.failed.push(safe_log_id(&key));
1278                }
1279                Ok(mut plaintext) => {
1280                    report.legacy_v0_found += 1;
1281                    if !dry_run && accept_legacy_v0 {
1282                        if envelope.v == 0 {
1283                            tracing::warn!(
1284                                record = %safe_log_id(&key),
1285                                "migrate_kv_to_encrypted: upgrading format-version-0 \
1286                                 envelope to the current format -- v0 has no AAD, so this \
1287                                 plaintext's association with this specific key was not \
1288                                 cryptographically verified by this upgrade"
1289                            );
1290                        } else {
1291                            tracing::info!(
1292                                record = %safe_log_id(&key),
1293                                v = envelope.v,
1294                                "migrate_kv_to_encrypted: upgrading legacy envelope to the \
1295                                 current format (its AAD bound the record key)"
1296                            );
1297                        }
1298                        let encrypt_result =
1299                            encryption.encrypt_for_storage(&plaintext, key.as_bytes());
1300                        plaintext.zeroize();
1301                        let new_envelope = encrypt_result?;
1302                        storage.store_kv(&key, &new_envelope, ttl).await?;
1303                        report.upgraded_from_legacy += 1;
1304                    } else {
1305                        plaintext.zeroize();
1306                    }
1307                }
1308            }
1309            continue;
1310        }
1311
1312        report.encrypted += 1;
1313        if !dry_run {
1314            let encrypt_result = encryption.encrypt_for_storage(&raw, key.as_bytes());
1315            raw.zeroize();
1316            let envelope = encrypt_result?;
1317            storage.store_kv(&key, &envelope, ttl).await?;
1318        } else {
1319            raw.zeroize();
1320        }
1321    }
1322
1323    Ok(report)
1324}
1325
1326#[cfg(test)]
1327mod tests {
1328    use super::*;
1329    use crate::storage::MemoryStorage;
1330
1331    #[test]
1332    fn test_key_generation() {
1333        let key = StorageEncryption::generate_key();
1334        assert!(!key.is_empty());
1335        let decoded = BASE64.decode(&key).unwrap();
1336        assert_eq!(decoded.len(), 32);
1337    }
1338
1339    #[test]
1340    fn test_encrypt_decrypt_roundtrip() {
1341        let enc = StorageEncryption::new_random();
1342        let plaintext = b"super secret totp seed, not utf8 safe either: \xff\xfe";
1343        let encrypted = enc.encrypt(plaintext, b"user:alice:totp_secret").unwrap();
1344        let decrypted = enc.decrypt(&encrypted, b"user:alice:totp_secret").unwrap();
1345        assert_eq!(decrypted, plaintext);
1346    }
1347
1348    /// Proves the AAD actually binds the ciphertext to its storage key:
1349    /// decrypting under a DIFFERENT aad than it was encrypted with must
1350    /// fail, not silently succeed with wrong-but-plausible plaintext. This
1351    /// is the mechanism that stops a ciphertext from being swapped between
1352    /// two records.
1353    #[test]
1354    fn test_decrypt_rejects_wrong_aad() {
1355        let enc = StorageEncryption::new_random();
1356        let encrypted = enc
1357            .encrypt(b"alice's secret", b"user:alice:totp_secret")
1358            .unwrap();
1359        let result = enc.decrypt(&encrypted, b"user:bob:totp_secret");
1360        assert!(
1361            result.is_err(),
1362            "decrypting with the wrong AAD (a different record's key) must fail"
1363        );
1364    }
1365
1366    /// Proves nonces are actually random per call, not reused -- the
1367    /// security of AES-GCM depends on this. Encrypts the same plaintext
1368    /// many times and checks every nonce is unique.
1369    #[test]
1370    fn test_nonces_are_unique_across_many_encryptions() {
1371        let enc = StorageEncryption::new_random();
1372        let mut seen = std::collections::HashSet::new();
1373        for _ in 0..1000 {
1374            let encrypted = enc
1375                .encrypt(b"same plaintext every time", b"same:aad")
1376                .unwrap();
1377            assert_eq!(
1378                BASE64.decode(&encrypted.nonce).unwrap().len(),
1379                12,
1380                "nonce must be 96 bits"
1381            );
1382            assert!(
1383                seen.insert(encrypted.nonce.clone()),
1384                "nonce reused across encryptions -- this breaks AES-GCM's security guarantee"
1385            );
1386        }
1387    }
1388
1389    #[test]
1390    fn test_storage_roundtrip_non_utf8_bytes() {
1391        let enc = StorageEncryption::new_random();
1392        let raw_salt: &[u8] = &[0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0xFF, 0x80, 0x81];
1393        let stored = enc.encrypt_for_storage(raw_salt, b"salt:key").unwrap();
1394        let restored = enc.decrypt_from_storage(&stored, b"salt:key").unwrap();
1395        assert_eq!(restored, raw_salt);
1396    }
1397
1398    /// A key provider that resolves to no usable key at all must make
1399    /// construction fail, not silently produce a `StorageEncryption` that
1400    /// can't actually encrypt anything (e.g. by skipping encryption).
1401    struct EmptyKeyProvider;
1402    impl KeyProvider for EmptyKeyProvider {
1403        fn load_keys(&self) -> Result<LoadedKeys> {
1404            Err(AuthError::config("no key configured (test)"))
1405        }
1406    }
1407
1408    #[test]
1409    fn test_construction_fails_closed_with_no_key() {
1410        let result = StorageEncryption::new(&EmptyKeyProvider);
1411        assert!(
1412            result.is_err(),
1413            "StorageEncryption::new must fail, not silently construct an unusable/no-op encryptor"
1414        );
1415    }
1416
1417    struct FixedKeyProvider(LoadedKeys);
1418    impl KeyProvider for FixedKeyProvider {
1419        fn load_keys(&self) -> Result<LoadedKeys> {
1420            Ok(LoadedKeys {
1421                current_key_id: self.0.current_key_id.clone(),
1422                keys: self.0.keys.clone(),
1423            })
1424        }
1425    }
1426
1427    #[test]
1428    fn test_construction_rejects_empty_key_id() {
1429        // current_key_id is non-empty and present in the map (so the
1430        // separate, earlier checks for "current_key_id is empty" and
1431        // "current_key_id isn't in the map" don't fire first) -- this
1432        // specifically exercises the "no key in the map has an empty id"
1433        // check via a SECOND entry.
1434        let mut keys = HashMap::new();
1435        keys.insert("1".to_string(), [1u8; 32]);
1436        keys.insert(String::new(), [2u8; 32]);
1437        let result = StorageEncryption::new(&FixedKeyProvider(LoadedKeys {
1438            current_key_id: "1".to_string(),
1439            keys,
1440        }));
1441        assert!(
1442            result.is_err(),
1443            "an empty key id anywhere in the loaded key set must be rejected (it's reserved \
1444             for format-version-0 envelopes), even when current_key_id itself is valid"
1445        );
1446    }
1447
1448    /// Key rotation: an envelope encrypted under an OLD key must still
1449    /// decrypt once the current key id has moved on, as long as the old
1450    /// key is still present in the loaded key set.
1451    struct TwoKeyProvider {
1452        current_id: String,
1453        old_key: [u8; 32],
1454        new_key: [u8; 32],
1455        old_id: String,
1456    }
1457    impl KeyProvider for TwoKeyProvider {
1458        fn load_keys(&self) -> Result<LoadedKeys> {
1459            let mut keys = HashMap::new();
1460            keys.insert(self.old_id.clone(), self.old_key);
1461            keys.insert(self.current_id.clone(), self.new_key);
1462            Ok(LoadedKeys {
1463                current_key_id: self.current_id.clone(),
1464                keys,
1465            })
1466        }
1467    }
1468
1469    #[test]
1470    fn test_old_key_still_decrypts_after_rotation() {
1471        let mut keys = HashMap::new();
1472        keys.insert("1".to_string(), [7u8; 32]);
1473        let enc_before_rotation = StorageEncryption::new(&FixedKeyProvider(LoadedKeys {
1474            current_key_id: "1".to_string(),
1475            keys,
1476        }))
1477        .unwrap();
1478        let old_envelope = enc_before_rotation
1479            .encrypt(b"written before rotation", b"some:key")
1480            .unwrap();
1481        assert_eq!(old_envelope.key_id, "1");
1482
1483        // Now rotate: current key id moves to "2", but "1" is kept around.
1484        let provider = TwoKeyProvider {
1485            current_id: "2".to_string(),
1486            old_id: "1".to_string(),
1487            old_key: [7u8; 32],
1488            new_key: [9u8; 32],
1489        };
1490        let enc_after_rotation = StorageEncryption::new(&provider).unwrap();
1491
1492        // New writes use the new key.
1493        let new_envelope = enc_after_rotation
1494            .encrypt(b"written after rotation", b"some:key")
1495            .unwrap();
1496        assert_eq!(new_envelope.key_id, "2");
1497
1498        // The OLD envelope (key_id "1") must still decrypt.
1499        let decrypted_old = enc_after_rotation
1500            .decrypt(&old_envelope, b"some:key")
1501            .unwrap();
1502        assert_eq!(decrypted_old, b"written before rotation");
1503    }
1504
1505    /// Format-version-0 compatibility: an envelope shaped like the
1506    /// original (pre-redesign) `EncryptedStorage` produced -- no `key_id`,
1507    /// no `v`, no AAD at encryption time -- must still decrypt under the
1508    /// new `StorageEncryption`, since real deployments may already hold
1509    /// data in that shape.
1510    #[test]
1511    fn test_decrypts_format_version_0_envelope() {
1512        let mut keys = HashMap::new();
1513        keys.insert("1".to_string(), [3u8; 32]);
1514        let enc = StorageEncryption::new(&FixedKeyProvider(LoadedKeys {
1515            current_key_id: "1".to_string(),
1516            keys,
1517        }))
1518        .unwrap();
1519
1520        // Hand-build a format-version-0 envelope the way the original
1521        // `EncryptedStorage::encrypt` did: no AAD, no key_id, no v.
1522        let cipher = {
1523            let key = Key::<Aes256Gcm>::from_slice(&[3u8; 32]);
1524            Aes256Gcm::new(key)
1525        };
1526        let nonce_bytes = [5u8; 12];
1527        let nonce = Nonce::from_slice(&nonce_bytes);
1528        let ciphertext = cipher
1529            .encrypt(nonce, b"legacy-format plaintext".as_ref())
1530            .unwrap();
1531        let legacy_json = format!(
1532            r#"{{"data":"{}","nonce":"{}","algorithm":"AES-256-GCM","key_derivation":"direct"}}"#,
1533            BASE64.encode(&ciphertext),
1534            BASE64.encode(nonce_bytes)
1535        );
1536
1537        assert!(StorageEncryption::looks_like_envelope(
1538            legacy_json.as_bytes()
1539        ));
1540        let decrypted = enc
1541            .decrypt_from_storage(legacy_json.as_bytes(), b"irrelevant-for-v0")
1542            .unwrap();
1543        assert_eq!(decrypted, b"legacy-format plaintext".to_vec());
1544    }
1545
1546    /// Negative case for the v0 downgrade attack: a GENUINE current-format
1547    /// (v1) envelope, with its `v` and `key_id` fields stripped (so it now
1548    /// parses as a v0-shaped envelope), must not be accepted as that
1549    /// record's value. It fails for two independent reasons here -- the
1550    /// policy check (`allow_legacy_v0: false` by default) rejects it before
1551    /// any decryption is attempted, and even without that check the
1552    /// ciphertext itself was sealed with a real (non-empty) AAD, so the v0
1553    /// path's empty-AAD decrypt attempt would fail the authentication tag
1554    /// anyway.
1555    #[tokio::test]
1556    async fn test_wrapper_rejects_v1_envelope_downgraded_to_look_like_v0() {
1557        let mut keys = HashMap::new();
1558        keys.insert("1".to_string(), [4u8; 32]);
1559        let encryption = StorageEncryption::new(&FixedKeyProvider(LoadedKeys {
1560            current_key_id: "1".to_string(),
1561            keys,
1562        }))
1563        .unwrap();
1564
1565        let inner = MemoryStorage::new();
1566        let wrapped = EncryptedStorage::new(inner, encryption, false, false);
1567
1568        wrapped
1569            .store_kv("user:alice:totp_secret", b"real secret", None)
1570            .await
1571            .unwrap();
1572
1573        let inner = wrapped.into_inner();
1574        let real_envelope: EncryptedData = serde_json::from_slice(
1575            &inner
1576                .get_kv("user:alice:totp_secret")
1577                .await
1578                .unwrap()
1579                .unwrap(),
1580        )
1581        .unwrap();
1582
1583        // Strip the fields a v0 envelope never had, simulating an
1584        // attacker (or a bug) downgrading a real v1 envelope.
1585        let downgraded_json = serde_json::json!({
1586            "data": real_envelope.data,
1587            "nonce": real_envelope.nonce,
1588            "algorithm": real_envelope.algorithm,
1589        })
1590        .to_string();
1591        inner
1592            .store_kv("user:alice:totp_secret", downgraded_json.as_bytes(), None)
1593            .await
1594            .unwrap();
1595
1596        let mut keys2 = HashMap::new();
1597        keys2.insert("1".to_string(), [4u8; 32]);
1598        let encryption2 = StorageEncryption::new(&FixedKeyProvider(LoadedKeys {
1599            current_key_id: "1".to_string(),
1600            keys: keys2,
1601        }))
1602        .unwrap();
1603        let wrapped = EncryptedStorage::new(inner, encryption2, false, false);
1604
1605        let result = wrapped.get_kv("user:alice:totp_secret").await;
1606        assert!(
1607            result.is_err(),
1608            "a v1 envelope downgraded to look like v0 must not be accepted as the record's \
1609             value, with allow_legacy_v0 at its default (false)"
1610        );
1611    }
1612
1613    #[tokio::test]
1614    async fn test_list_kv_keys_passes_through_unencrypted() {
1615        let inner = MemoryStorage::new();
1616        inner
1617            .store_kv("prefix:a", b"ignored by this test", None)
1618            .await
1619            .unwrap();
1620        inner
1621            .store_kv("prefix:b", b"ignored by this test", None)
1622            .await
1623            .unwrap();
1624        let wrapped = EncryptedStorage::new(inner, StorageEncryption::new_random(), false, false);
1625
1626        let mut keys = wrapped.list_kv_keys("prefix:").await.unwrap();
1627        keys.sort();
1628        assert_eq!(
1629            keys,
1630            vec!["prefix:a".to_string(), "prefix:b".to_string()],
1631            "list_kv_keys must pass through to the inner backend, not use the trait's \
1632             empty-Vec default"
1633        );
1634    }
1635
1636    #[tokio::test]
1637    async fn test_encrypted_storage_roundtrip() {
1638        let inner = MemoryStorage::new();
1639        let wrapped = EncryptedStorage::new(inner, StorageEncryption::new_random(), false, false);
1640
1641        wrapped
1642            .store_kv("user:alice:totp_secret", b"JBSWY3DPEHPK3PXP", None)
1643            .await
1644            .unwrap();
1645        let value = wrapped.get_kv("user:alice:totp_secret").await.unwrap();
1646        assert_eq!(value, Some(b"JBSWY3DPEHPK3PXP".to_vec()));
1647    }
1648
1649    /// Proves the wrapper actually changes what's on disk/in the backend
1650    /// -- reading through the INNER storage directly (bypassing
1651    /// `EncryptedStorage`) must NOT show the plaintext.
1652    #[tokio::test]
1653    async fn test_encrypted_storage_actually_encrypts_the_underlying_value() {
1654        let wrapped = EncryptedStorage::new(
1655            MemoryStorage::new(),
1656            StorageEncryption::new_random(),
1657            false,
1658            false,
1659        );
1660
1661        wrapped
1662            .store_kv("user:alice:totp_secret", b"JBSWY3DPEHPK3PXP", None)
1663            .await
1664            .unwrap();
1665
1666        let inner = wrapped.into_inner();
1667        let raw = inner
1668            .get_kv("user:alice:totp_secret")
1669            .await
1670            .unwrap()
1671            .unwrap();
1672        assert_ne!(
1673            raw,
1674            b"JBSWY3DPEHPK3PXP".to_vec(),
1675            "the underlying storage must not hold the plaintext value"
1676        );
1677        assert!(
1678            StorageEncryption::looks_like_envelope(&raw),
1679            "the underlying storage must hold a proper encrypted envelope"
1680        );
1681    }
1682
1683    /// Proves the wrapper's own AAD wiring (not just `StorageEncryption`
1684    /// directly, which `test_decrypt_rejects_wrong_aad` already covers) --
1685    /// swapping two records' raw envelopes in the INNER backend must make
1686    /// both unreadable through the wrapper, because each envelope's AAD no
1687    /// longer matches the key it's stored under. If the wrapper passed a
1688    /// constant (e.g. empty) AAD instead of the real key, this swap would
1689    /// go undetected and both reads would "succeed" with the wrong value.
1690    #[tokio::test]
1691    async fn test_wrapper_rejects_ciphertext_swapped_between_records() {
1692        let mut keys = HashMap::new();
1693        keys.insert("1".to_string(), [11u8; 32]);
1694        let loaded = LoadedKeys {
1695            current_key_id: "1".to_string(),
1696            keys,
1697        };
1698        let encryption_for_wrapper = StorageEncryption::new(&FixedKeyProvider(LoadedKeys {
1699            current_key_id: loaded.current_key_id.clone(),
1700            keys: loaded.keys.clone(),
1701        }))
1702        .unwrap();
1703        // An independent `StorageEncryption` built from the SAME key, used
1704        // only to verify what AAD the wrapper actually used -- never to
1705        // produce the envelopes under test.
1706        let encryption_for_verification =
1707            StorageEncryption::new(&FixedKeyProvider(loaded)).unwrap();
1708
1709        let inner = MemoryStorage::new();
1710        let wrapped = EncryptedStorage::new(inner, encryption_for_wrapper, false, false);
1711
1712        wrapped
1713            .store_kv("user:alice:totp_secret", b"alice-secret", None)
1714            .await
1715            .unwrap();
1716
1717        let inner = wrapped.into_inner();
1718        let raw = inner
1719            .get_kv("user:alice:totp_secret")
1720            .await
1721            .unwrap()
1722            .unwrap();
1723
1724        // The wrapper must have used the storage key itself as AAD: decrypting
1725        // with that exact AAD must succeed...
1726        let decrypted = encryption_for_verification
1727            .decrypt(
1728                &serde_json::from_slice(&raw).unwrap(),
1729                b"user:alice:totp_secret",
1730            )
1731            .unwrap();
1732        assert_eq!(decrypted, b"alice-secret".to_vec());
1733
1734        // ...and decrypting the SAME envelope under a DIFFERENT record's key
1735        // name must fail. If the wrapper instead used some constant AAD (the
1736        // exact mutation this test exists to catch), this swap-style check
1737        // would succeed instead of failing.
1738        let wrong_aad_result = encryption_for_verification.decrypt(
1739            &serde_json::from_slice(&raw).unwrap(),
1740            b"user:bob:totp_secret",
1741        );
1742        assert!(
1743            wrong_aad_result.is_err(),
1744            "the wrapper's envelope must not decrypt under a different record's key name -- \
1745             if it does, the wrapper is using a constant AAD instead of the real storage key"
1746        );
1747    }
1748
1749    /// A value stored by some pre-encryption version of the framework (or
1750    /// while `storage_encryption.enabled = false`) is readable when
1751    /// `allow_plaintext_reads` is explicitly true (the migration-window
1752    /// setting).
1753    #[tokio::test]
1754    async fn test_reads_legacy_plaintext_when_allowed() {
1755        let inner = MemoryStorage::new();
1756        inner
1757            .store_kv("legacy:key", b"pre-existing plaintext value", None)
1758            .await
1759            .unwrap();
1760        let wrapped = EncryptedStorage::new(inner, StorageEncryption::new_random(), true, false);
1761
1762        let value = wrapped.get_kv("legacy:key").await.unwrap();
1763        assert_eq!(value, Some(b"pre-existing plaintext value".to_vec()));
1764    }
1765
1766    /// With `allow_plaintext_reads` false (the normal, post-migration
1767    /// setting), a value that isn't a valid envelope -- whether genuine
1768    /// untouched legacy plaintext, a corrupted envelope, or an attacker's
1769    /// overwrite -- must be a hard error, not silently accepted. This is
1770    /// the fix for the finding that an attacker able to write to the
1771    /// backing store could otherwise overwrite an encrypted secret with
1772    /// chosen plaintext and have it accepted forever.
1773    #[tokio::test]
1774    async fn test_rejects_non_envelope_value_when_plaintext_reads_disallowed() {
1775        let inner = MemoryStorage::new();
1776        inner
1777            .store_kv("user:alice:totp_secret", b"attacker-chosen-plaintext", None)
1778            .await
1779            .unwrap();
1780        let wrapped = EncryptedStorage::new(inner, StorageEncryption::new_random(), false, false);
1781
1782        let result = wrapped.get_kv("user:alice:totp_secret").await;
1783        assert!(
1784            result.is_err(),
1785            "a non-envelope value must be rejected, not silently treated as plaintext, \
1786             when allow_plaintext_reads is false"
1787        );
1788    }
1789
1790    /// The real fix for the v0-downgrade finding: a format-version-0
1791    /// envelope must be actively rewritten to the current format by
1792    /// migration (not just counted as "already encrypted" and left
1793    /// alone), since nothing else in this crate ever upgrades one.
1794    #[tokio::test]
1795    async fn test_migration_upgrades_v0_envelope_to_current_format() {
1796        let mut keys = HashMap::new();
1797        keys.insert("1".to_string(), [6u8; 32]);
1798        let encryption = StorageEncryption::new(&FixedKeyProvider(LoadedKeys {
1799            current_key_id: "1".to_string(),
1800            keys,
1801        }))
1802        .unwrap();
1803
1804        // Hand-build a v0 envelope the way the original pre-redesign
1805        // `EncryptedStorage` did (no AAD, no key_id, no v).
1806        let cipher = {
1807            let key = Key::<Aes256Gcm>::from_slice(&[6u8; 32]);
1808            Aes256Gcm::new(key)
1809        };
1810        let nonce_bytes = [8u8; 12];
1811        let nonce = Nonce::from_slice(&nonce_bytes);
1812        let ciphertext = cipher.encrypt(nonce, b"v0 secret".as_ref()).unwrap();
1813        let v0_json = format!(
1814            r#"{{"data":"{}","nonce":"{}","algorithm":"AES-256-GCM","key_derivation":"direct"}}"#,
1815            BASE64.encode(&ciphertext),
1816            BASE64.encode(nonce_bytes)
1817        );
1818
1819        let storage = MemoryStorage::new();
1820        storage
1821            .store_kv("secret:legacy", v0_json.as_bytes(), None)
1822            .await
1823            .unwrap();
1824
1825        let report = migrate_kv_to_encrypted(
1826            &storage,
1827            &encryption,
1828            "secret:",
1829            MigrationOptions {
1830                dry_run: false,
1831                accept_legacy_v0: true,
1832                ..Default::default()
1833            },
1834        )
1835        .await
1836        .unwrap();
1837        assert_eq!(
1838            report.legacy_v0_found, 1,
1839            "a decryptable v0 envelope must be counted as found"
1840        );
1841        assert_eq!(
1842            report.upgraded_from_legacy, 1,
1843            "with accept_legacy_v0 true on a real run, it must actually be upgraded"
1844        );
1845        assert_eq!(report.already_encrypted, 0);
1846        assert!(report.failed.is_empty());
1847
1848        let raw_after = storage.get_kv("secret:legacy").await.unwrap().unwrap();
1849        let envelope_after: EncryptedData = serde_json::from_slice(&raw_after).unwrap();
1850        assert_eq!(
1851            envelope_after.v, CURRENT_FORMAT_VERSION,
1852            "the stored envelope must now be the current format, not still v0"
1853        );
1854        assert!(
1855            !envelope_after.key_id.is_empty(),
1856            "the upgraded envelope must have a real key_id"
1857        );
1858
1859        // The upgraded envelope must decrypt correctly through the
1860        // normal (AAD-bound) path -- not just via the v0 fallback.
1861        let decrypted = encryption
1862            .decrypt(&envelope_after, b"secret:legacy")
1863            .unwrap();
1864        assert_eq!(decrypted, b"v0 secret".to_vec());
1865
1866        // Idempotent: running it again finds nothing left to upgrade.
1867        let report2 = migrate_kv_to_encrypted(
1868            &storage,
1869            &encryption,
1870            "secret:",
1871            MigrationOptions {
1872                dry_run: false,
1873                accept_legacy_v0: false,
1874                ..Default::default()
1875            },
1876        )
1877        .await
1878        .unwrap();
1879        assert_eq!(report2.upgraded_from_legacy, 0);
1880        assert_eq!(report2.already_encrypted, 1);
1881    }
1882
1883    /// Without `accept_legacy_v0`, a decryptable v0 envelope is reported
1884    /// (`legacy_v0_found`) but never written back as the current format --
1885    /// this is the fix for the laundering finding: upgrading must never be
1886    /// an implicit side effect of just running the migration.
1887    #[tokio::test]
1888    async fn test_migration_does_not_upgrade_legacy_v0_without_accept_flag() {
1889        let mut keys = HashMap::new();
1890        keys.insert("1".to_string(), [7u8; 32]);
1891        let encryption = StorageEncryption::new(&FixedKeyProvider(LoadedKeys {
1892            current_key_id: "1".to_string(),
1893            keys,
1894        }))
1895        .unwrap();
1896
1897        let cipher = {
1898            let key = Key::<Aes256Gcm>::from_slice(&[7u8; 32]);
1899            Aes256Gcm::new(key)
1900        };
1901        let nonce_bytes = [3u8; 12];
1902        let nonce = Nonce::from_slice(&nonce_bytes);
1903        let ciphertext = cipher.encrypt(nonce, b"v0 secret".as_ref()).unwrap();
1904        let v0_json = format!(
1905            r#"{{"data":"{}","nonce":"{}","algorithm":"AES-256-GCM","key_derivation":"direct"}}"#,
1906            BASE64.encode(&ciphertext),
1907            BASE64.encode(nonce_bytes)
1908        );
1909
1910        let storage = MemoryStorage::new();
1911        storage
1912            .store_kv("secret:legacy", v0_json.as_bytes(), None)
1913            .await
1914            .unwrap();
1915
1916        // Real run (not dry), but accept_legacy_v0 is false.
1917        let report = migrate_kv_to_encrypted(
1918            &storage,
1919            &encryption,
1920            "secret:",
1921            MigrationOptions {
1922                dry_run: false,
1923                accept_legacy_v0: false,
1924                ..Default::default()
1925            },
1926        )
1927        .await
1928        .unwrap();
1929        assert_eq!(
1930            report.legacy_v0_found, 1,
1931            "the v0 envelope must still be detected/decrypted"
1932        );
1933        assert_eq!(
1934            report.upgraded_from_legacy, 0,
1935            "without accept_legacy_v0, nothing must actually be rewritten"
1936        );
1937
1938        let raw_after = storage.get_kv("secret:legacy").await.unwrap().unwrap();
1939        assert_eq!(
1940            raw_after,
1941            v0_json.as_bytes(),
1942            "the stored envelope must be byte-for-byte unchanged without accept_legacy_v0"
1943        );
1944    }
1945
1946    /// Fix for the "one bad record aborts the whole run" finding: a v0
1947    /// envelope that fails to decrypt under any loaded key must be
1948    /// skipped and reported by name, not abort processing of the other
1949    /// keys in the same run.
1950    #[tokio::test]
1951    async fn test_migration_skips_undecryptable_v0_envelope_without_aborting() {
1952        let mut keys = HashMap::new();
1953        keys.insert("1".to_string(), [9u8; 32]);
1954        let encryption = StorageEncryption::new(&FixedKeyProvider(LoadedKeys {
1955            current_key_id: "1".to_string(),
1956            keys,
1957        }))
1958        .unwrap();
1959
1960        // A v0-shaped envelope with ciphertext that will NOT decrypt
1961        // under the loaded key (wrong key material entirely).
1962        let bad_cipher = {
1963            let key = Key::<Aes256Gcm>::from_slice(&[0u8; 32]);
1964            Aes256Gcm::new(key)
1965        };
1966        let nonce_bytes = [1u8; 12];
1967        let nonce = Nonce::from_slice(&nonce_bytes);
1968        let bad_ciphertext = bad_cipher
1969            .encrypt(nonce, b"undecryptable".as_ref())
1970            .unwrap();
1971        let bad_v0_json = format!(
1972            r#"{{"data":"{}","nonce":"{}","algorithm":"AES-256-GCM","key_derivation":"direct"}}"#,
1973            BASE64.encode(&bad_ciphertext),
1974            BASE64.encode(nonce_bytes)
1975        );
1976
1977        let storage = MemoryStorage::new();
1978        storage
1979            .store_kv("secret:bad", bad_v0_json.as_bytes(), None)
1980            .await
1981            .unwrap();
1982        storage
1983            .store_kv("secret:good_a", b"plaintext-a", None)
1984            .await
1985            .unwrap();
1986        storage
1987            .store_kv("secret:good_b", b"plaintext-b", None)
1988            .await
1989            .unwrap();
1990
1991        let report = migrate_kv_to_encrypted(
1992            &storage,
1993            &encryption,
1994            "secret:",
1995            MigrationOptions {
1996                dry_run: false,
1997                accept_legacy_v0: true,
1998                ..Default::default()
1999            },
2000        )
2001        .await
2002        .expect("one undecryptable record must not abort the whole migration");
2003
2004        assert_eq!(report.scanned, 3);
2005        assert_eq!(
2006            report.failed,
2007            vec![safe_log_id("secret:bad")],
2008            "the undecryptable record must be named (as a safe identifier, not the raw \
2009             key) in `failed`, not silently dropped"
2010        );
2011        assert_eq!(
2012            report.encrypted, 2,
2013            "the other two plaintext keys must still be processed despite the bad one"
2014        );
2015
2016        let good_a_after = storage.get_kv("secret:good_a").await.unwrap().unwrap();
2017        assert!(StorageEncryption::looks_like_envelope(&good_a_after));
2018        let good_b_after = storage.get_kv("secret:good_b").await.unwrap().unwrap();
2019        assert!(StorageEncryption::looks_like_envelope(&good_b_after));
2020
2021        // The bad record itself is untouched (still its original bytes).
2022        let bad_after = storage.get_kv("secret:bad").await.unwrap().unwrap();
2023        assert_eq!(bad_after, bad_v0_json.as_bytes());
2024    }
2025
2026    /// Fix for "dry run lies": a dry run must actually attempt the v0
2027    /// decrypt (not just assume success), so an undecryptable v0 envelope
2028    /// shows up in `failed` during a dry run too, matching what a real
2029    /// run would report.
2030    #[tokio::test]
2031    async fn test_migration_dry_run_detects_undecryptable_v0_envelope() {
2032        let mut keys = HashMap::new();
2033        keys.insert("1".to_string(), [5u8; 32]);
2034        let encryption = StorageEncryption::new(&FixedKeyProvider(LoadedKeys {
2035            current_key_id: "1".to_string(),
2036            keys,
2037        }))
2038        .unwrap();
2039
2040        let bad_cipher = {
2041            let key = Key::<Aes256Gcm>::from_slice(&[0u8; 32]);
2042            Aes256Gcm::new(key)
2043        };
2044        let nonce_bytes = [2u8; 12];
2045        let nonce = Nonce::from_slice(&nonce_bytes);
2046        let bad_ciphertext = bad_cipher
2047            .encrypt(nonce, b"undecryptable".as_ref())
2048            .unwrap();
2049        let bad_v0_json = format!(
2050            r#"{{"data":"{}","nonce":"{}","algorithm":"AES-256-GCM","key_derivation":"direct"}}"#,
2051            BASE64.encode(&bad_ciphertext),
2052            BASE64.encode(nonce_bytes)
2053        );
2054
2055        let storage = MemoryStorage::new();
2056        storage
2057            .store_kv("secret:bad", bad_v0_json.as_bytes(), None)
2058            .await
2059            .unwrap();
2060
2061        let dry_report = migrate_kv_to_encrypted(
2062            &storage,
2063            &encryption,
2064            "secret:",
2065            MigrationOptions {
2066                dry_run: true,
2067                accept_legacy_v0: true,
2068                ..Default::default()
2069            },
2070        )
2071        .await
2072        .unwrap();
2073        assert_eq!(
2074            dry_report.failed,
2075            vec![safe_log_id("secret:bad")],
2076            "a dry run must actually attempt the decrypt and report the same failure a \
2077             real run would, not optimistically assume every v0 envelope would succeed"
2078        );
2079        assert_eq!(dry_report.upgraded_from_legacy, 0);
2080        assert_eq!(dry_report.legacy_v0_found, 0);
2081    }
2082
2083    /// Fix for the high-severity finding: some storage keys in this
2084    /// crate ARE bearer secrets (`api_key:{raw_key}`), so a safe log
2085    /// identifier must never contain the raw key material -- only the
2086    /// namespace and a hash.
2087    #[test]
2088    fn test_safe_log_id_never_contains_the_raw_secret() {
2089        let raw_secret = "ak_live_super_secret_bearer_token_12345";
2090        let key = format!("api_key:{raw_secret}");
2091        let id = safe_log_id(&key);
2092
2093        assert!(
2094            !id.contains(raw_secret),
2095            "safe_log_id must never contain the raw secret portion of the key, got: {id}"
2096        );
2097        assert!(
2098            id.starts_with("api_key:"),
2099            "safe_log_id should keep the (non-secret) namespace prefix, got: {id}"
2100        );
2101        // Deterministic, so an operator with storage access can
2102        // recompute it to correlate a logged identifier back to a
2103        // specific record.
2104        assert_eq!(id, safe_log_id(&key));
2105    }
2106
2107    /// A key with no `:` at all has no non-secret namespace prefix to
2108    /// keep -- `key.split(':').next()` on a colonless key returns the
2109    /// WHOLE key, which would print the raw secret verbatim. This is
2110    /// the fix for that gap.
2111    #[test]
2112    fn test_safe_log_id_handles_colonless_key_without_leaking_it() {
2113        let raw_secret = "a_bearer_token_with_no_colon_in_it_at_all";
2114        let id = safe_log_id(raw_secret);
2115
2116        assert!(
2117            !id.contains(raw_secret),
2118            "a colonless key must not appear verbatim in its safe_log_id, got: {id}"
2119        );
2120        assert!(
2121            id.starts_with("key:"),
2122            "a colonless key should fall back to the literal 'key:' prefix, got: {id}"
2123        );
2124    }
2125
2126    /// Kills the mutation `if !dry_run && accept_legacy_v0` ->
2127    /// `if accept_legacy_v0` (dropping the dry-run check): with
2128    /// accept_legacy_v0 true but dry_run also true, over a DECRYPTABLE
2129    /// v0 record, nothing must be written -- the only existing
2130    /// dry-run-with-flag test used an undecryptable record, which never
2131    /// reaches this branch at all.
2132    #[tokio::test]
2133    async fn test_dry_run_with_accept_legacy_v0_does_not_write_a_decryptable_v0_record() {
2134        let mut keys = HashMap::new();
2135        keys.insert("1".to_string(), [4u8; 32]);
2136        let encryption = StorageEncryption::new(&FixedKeyProvider(LoadedKeys {
2137            current_key_id: "1".to_string(),
2138            keys,
2139        }))
2140        .unwrap();
2141
2142        let cipher = {
2143            let key = Key::<Aes256Gcm>::from_slice(&[4u8; 32]);
2144            Aes256Gcm::new(key)
2145        };
2146        let nonce_bytes = [6u8; 12];
2147        let nonce = Nonce::from_slice(&nonce_bytes);
2148        let ciphertext = cipher.encrypt(nonce, b"v0 secret".as_ref()).unwrap();
2149        let v0_json = format!(
2150            r#"{{"data":"{}","nonce":"{}","algorithm":"AES-256-GCM","key_derivation":"direct"}}"#,
2151            BASE64.encode(&ciphertext),
2152            BASE64.encode(nonce_bytes)
2153        );
2154
2155        let storage = MemoryStorage::new();
2156        storage
2157            .store_kv("secret:legacy", v0_json.as_bytes(), None)
2158            .await
2159            .unwrap();
2160
2161        // dry_run: true AND accept_legacy_v0: true.
2162        let report = migrate_kv_to_encrypted(
2163            &storage,
2164            &encryption,
2165            "secret:",
2166            MigrationOptions {
2167                dry_run: true,
2168                accept_legacy_v0: true,
2169                ..Default::default()
2170            },
2171        )
2172        .await
2173        .unwrap();
2174
2175        assert_eq!(
2176            report.legacy_v0_found, 1,
2177            "a dry run must still report a decryptable v0 record as found"
2178        );
2179        assert_eq!(
2180            report.upgraded_from_legacy, 0,
2181            "a dry run must never actually upgrade anything, even with accept_legacy_v0 true"
2182        );
2183
2184        let raw_after = storage.get_kv("secret:legacy").await.unwrap().unwrap();
2185        assert_eq!(
2186            raw_after,
2187            v0_json.as_bytes(),
2188            "a dry run with accept_legacy_v0 true must leave the record byte-for-byte \
2189             unchanged"
2190        );
2191    }
2192
2193    #[tokio::test]
2194    async fn test_migration_dry_run_reports_without_writing() {
2195        let storage = MemoryStorage::new();
2196        storage
2197            .store_kv("secret:a", b"plaintext-a", None)
2198            .await
2199            .unwrap();
2200        storage
2201            .store_kv("secret:b", b"plaintext-b", None)
2202            .await
2203            .unwrap();
2204        let encryption = StorageEncryption::new_random();
2205
2206        let report = migrate_kv_to_encrypted(
2207            &storage,
2208            &encryption,
2209            "secret:",
2210            MigrationOptions {
2211                dry_run: true,
2212                accept_legacy_v0: false,
2213                ..Default::default()
2214            },
2215        )
2216        .await
2217        .unwrap();
2218
2219        assert!(report.dry_run);
2220        assert_eq!(report.scanned, 2);
2221        assert_eq!(report.encrypted, 2);
2222        assert_eq!(report.already_encrypted, 0);
2223
2224        // Dry run must not have written anything.
2225        let still_plaintext = storage.get_kv("secret:a").await.unwrap().unwrap();
2226        assert_eq!(still_plaintext, b"plaintext-a".to_vec());
2227    }
2228
2229    #[tokio::test]
2230    async fn test_migration_encrypts_plaintext_values() {
2231        let storage = MemoryStorage::new();
2232        storage
2233            .store_kv("secret:a", b"plaintext-a", None)
2234            .await
2235            .unwrap();
2236        let encryption = StorageEncryption::new_random();
2237
2238        let report = migrate_kv_to_encrypted(
2239            &storage,
2240            &encryption,
2241            "secret:",
2242            MigrationOptions {
2243                dry_run: false,
2244                accept_legacy_v0: false,
2245                ..Default::default()
2246            },
2247        )
2248        .await
2249        .unwrap();
2250        assert_eq!(report.encrypted, 1);
2251
2252        let raw = storage.get_kv("secret:a").await.unwrap().unwrap();
2253        assert!(StorageEncryption::looks_like_envelope(&raw));
2254        let decrypted = encryption.decrypt_from_storage(&raw, b"secret:a").unwrap();
2255        assert_eq!(decrypted, b"plaintext-a".to_vec());
2256    }
2257
2258    /// Proves idempotency and resumability: running the migration a
2259    /// second time over a mix of already-migrated and still-plaintext
2260    /// keys (simulating an interrupted first run) must not re-encrypt
2261    /// (double-encrypt) the already-done ones, and must finish the rest.
2262    #[tokio::test]
2263    async fn test_migration_is_idempotent_and_resumable() {
2264        let storage = MemoryStorage::new();
2265        storage
2266            .store_kv("secret:a", b"plaintext-a", None)
2267            .await
2268            .unwrap();
2269        storage
2270            .store_kv("secret:b", b"plaintext-b", None)
2271            .await
2272            .unwrap();
2273        let encryption = StorageEncryption::new_random();
2274
2275        // Simulate a first run that only got through "secret:a" before
2276        // being interrupted.
2277        let envelope_a = encryption
2278            .encrypt_for_storage(b"plaintext-a", b"secret:a")
2279            .unwrap();
2280        storage
2281            .store_kv("secret:a", &envelope_a, None)
2282            .await
2283            .unwrap();
2284
2285        // Resume: should skip "secret:a" (already encrypted) and finish
2286        // "secret:b".
2287        let report = migrate_kv_to_encrypted(
2288            &storage,
2289            &encryption,
2290            "secret:",
2291            MigrationOptions {
2292                dry_run: false,
2293                accept_legacy_v0: false,
2294                ..Default::default()
2295            },
2296        )
2297        .await
2298        .unwrap();
2299        assert_eq!(report.scanned, 2);
2300        assert_eq!(report.already_encrypted, 1);
2301        assert_eq!(report.encrypted, 1);
2302
2303        let raw_a_after = storage.get_kv("secret:a").await.unwrap().unwrap();
2304        assert_eq!(
2305            raw_a_after, envelope_a,
2306            "an already-encrypted value must be left byte-for-byte unchanged, not double-encrypted"
2307        );
2308        let raw_b_after = storage.get_kv("secret:b").await.unwrap().unwrap();
2309        assert!(StorageEncryption::looks_like_envelope(&raw_b_after));
2310
2311        // Running it again changes nothing further.
2312        let report2 = migrate_kv_to_encrypted(
2313            &storage,
2314            &encryption,
2315            "secret:",
2316            MigrationOptions {
2317                dry_run: false,
2318                accept_legacy_v0: false,
2319                ..Default::default()
2320            },
2321        )
2322        .await
2323        .unwrap();
2324        assert_eq!(report2.already_encrypted, 2);
2325        assert_eq!(report2.encrypted, 0);
2326    }
2327
2328    #[tokio::test]
2329    async fn test_migration_only_touches_matching_prefix() {
2330        let storage = MemoryStorage::new();
2331        storage
2332            .store_kv("secret:a", b"plaintext-a", None)
2333            .await
2334            .unwrap();
2335        storage
2336            .store_kv("other:b", b"plaintext-b", None)
2337            .await
2338            .unwrap();
2339        let encryption = StorageEncryption::new_random();
2340
2341        let report = migrate_kv_to_encrypted(
2342            &storage,
2343            &encryption,
2344            "secret:",
2345            MigrationOptions {
2346                dry_run: false,
2347                accept_legacy_v0: false,
2348                ..Default::default()
2349            },
2350        )
2351        .await
2352        .unwrap();
2353        assert_eq!(report.scanned, 1);
2354
2355        let other_untouched = storage.get_kv("other:b").await.unwrap().unwrap();
2356        assert_eq!(
2357            other_untouched,
2358            b"plaintext-b".to_vec(),
2359            "a key outside the given prefix must not be touched"
2360        );
2361    }
2362
2363    /// The wrapper must report the inner backend's TTL: restore (#121)
2364    /// reads it through the wrapper the framework actually holds.
2365    #[tokio::test]
2366    async fn wrapper_passes_kv_ttl_through() {
2367        let wrapped = EncryptedStorage::new(
2368            MemoryStorage::new(),
2369            StorageEncryption::new_random(),
2370            false,
2371            false,
2372        );
2373        wrapped
2374            .store_kv("k", b"v", Some(Duration::from_secs(600)))
2375            .await
2376            .unwrap();
2377        let ttl = wrapped.get_kv_ttl("k").await.unwrap().unwrap();
2378        assert!(ttl > Duration::from_secs(590) && ttl <= Duration::from_secs(600));
2379    }
2380
2381    // ---- envelope v2 + hardening ------------------------------------------------
2382
2383    struct FixedKeys {
2384        current: &'static str,
2385        keys: Vec<(&'static str, [u8; 32])>,
2386    }
2387
2388    impl KeyProvider for FixedKeys {
2389        fn load_keys(&self) -> Result<LoadedKeys> {
2390            Ok(LoadedKeys {
2391                current_key_id: self.current.to_string(),
2392                keys: self
2393                    .keys
2394                    .iter()
2395                    .map(|(id, key)| (id.to_string(), *key))
2396                    .collect(),
2397            })
2398        }
2399    }
2400
2401    const KEY_A: [u8; 32] = [0x11; 32];
2402    const KEY_B: [u8; 32] = [0x22; 32];
2403
2404    /// Builds a stored envelope by hand, the way an OLDER writer would have:
2405    /// `aad` is used verbatim (v1 used the bare storage key).
2406    fn manual_envelope(
2407        key: &[u8; 32],
2408        key_id: &str,
2409        v: u8,
2410        plaintext: &[u8],
2411        aad: &[u8],
2412    ) -> Vec<u8> {
2413        let cipher = Aes256Gcm::new(Key::<Aes256Gcm>::from_slice(key));
2414        let nonce_bytes = [7u8; 12];
2415        let ciphertext = cipher
2416            .encrypt(
2417                Nonce::from_slice(&nonce_bytes),
2418                Payload {
2419                    msg: plaintext,
2420                    aad,
2421                },
2422            )
2423            .unwrap();
2424        serde_json::to_vec(&EncryptedData {
2425            data: BASE64.encode(ciphertext),
2426            nonce: BASE64.encode(nonce_bytes),
2427            algorithm: "AES-256-GCM".to_string(),
2428            key_id: key_id.to_string(),
2429            v,
2430            key_derivation: String::new(),
2431        })
2432        .unwrap()
2433    }
2434
2435    #[test]
2436    fn new_envelopes_use_format_v2() {
2437        let enc = StorageEncryption::new_random();
2438        assert_eq!(enc.encrypt(b"x", b"k").unwrap().v, 2);
2439    }
2440
2441    /// v2 binds the envelope's own metadata into the AAD: the same key
2442    /// loaded under two ids must NOT let an attacker relabel `key_id`, and
2443    /// rewriting `v` to 1 (a downgrade to the AAD that ignores metadata)
2444    /// must not make a v2 ciphertext verify.
2445    #[test]
2446    fn v2_aad_binds_key_id_and_version() {
2447        let enc = StorageEncryption::new(&FixedKeys {
2448            current: "a",
2449            keys: vec![("a", KEY_A), ("b", KEY_A)],
2450        })
2451        .unwrap();
2452        let envelope = enc.encrypt(b"secret", b"rec:1").unwrap();
2453        assert_eq!(enc.decrypt(&envelope, b"rec:1").unwrap(), b"secret");
2454
2455        let mut relabelled = envelope.clone();
2456        relabelled.key_id = "b".to_string();
2457        assert!(
2458            enc.decrypt(&relabelled, b"rec:1").is_err(),
2459            "relabelling key_id must fail authentication"
2460        );
2461
2462        let mut downgraded = envelope.clone();
2463        downgraded.v = 1;
2464        assert!(
2465            enc.decrypt(&downgraded, b"rec:1").is_err(),
2466            "rewriting v to 1 must not make a v2 ciphertext verify"
2467        );
2468    }
2469
2470    /// Format v1 (AAD = the bare storage key) is a LEGACY format now: only
2471    /// readable behind the allow-legacy policy, and the migration tool
2472    /// upgrades it to v2.
2473    #[tokio::test]
2474    async fn v1_envelopes_are_legacy_and_migrate_to_v2() {
2475        let storage = MemoryStorage::new();
2476        storage
2477            .store_kv(
2478                "rec:1",
2479                &manual_envelope(&KEY_A, "1", 1, b"secret", b"rec:1"),
2480                None,
2481            )
2482            .await
2483            .unwrap();
2484        let provider = || FixedKeys {
2485            current: "1",
2486            keys: vec![("1", KEY_A)],
2487        };
2488
2489        let strict = EncryptedStorage::new(
2490            storage.clone(),
2491            StorageEncryption::new(&provider()).unwrap(),
2492            false,
2493            false,
2494        );
2495        assert!(
2496            strict.get_kv("rec:1").await.is_err(),
2497            "v1 must need the legacy policy"
2498        );
2499
2500        let lenient = EncryptedStorage::new(
2501            storage.clone(),
2502            StorageEncryption::new(&provider()).unwrap(),
2503            false,
2504            true,
2505        );
2506        assert_eq!(lenient.get_kv("rec:1").await.unwrap().unwrap(), b"secret");
2507
2508        let report = migrate_kv_to_encrypted(
2509            &storage,
2510            &StorageEncryption::new(&provider()).unwrap(),
2511            "",
2512            MigrationOptions::default().with_accept_legacy_v0(true),
2513        )
2514        .await
2515        .unwrap();
2516        assert_eq!(report.upgraded_from_legacy, 1, "{report:?}");
2517
2518        let raw = storage.get_kv("rec:1").await.unwrap().unwrap();
2519        assert_eq!(StorageEncryption::parse_envelope(&raw).unwrap().v, 2);
2520        // (`MemoryStorage::clone` copies, so re-wrap the migrated storage.)
2521        let strict_after = EncryptedStorage::new(
2522            storage,
2523            StorageEncryption::new(&provider()).unwrap(),
2524            false,
2525            false,
2526        );
2527        assert_eq!(
2528            strict_after.get_kv("rec:1").await.unwrap().unwrap(),
2529            b"secret"
2530        );
2531    }
2532
2533    /// After the upgrade, a v2 ciphertext copied onto another record must
2534    /// not decrypt (the AAD still binds it to its own key).
2535    #[tokio::test]
2536    async fn upgraded_records_cannot_be_swapped_between_keys() {
2537        let storage = MemoryStorage::new();
2538        for (key, secret) in [("rec:1", &b"one"[..]), ("rec:2", b"two")] {
2539            storage
2540                .store_kv(key, &manual_envelope(&KEY_A, "1", 0, secret, b""), None)
2541                .await
2542                .unwrap();
2543        }
2544        let provider = || FixedKeys {
2545            current: "1",
2546            keys: vec![("1", KEY_A)],
2547        };
2548        migrate_kv_to_encrypted(
2549            &storage,
2550            &StorageEncryption::new(&provider()).unwrap(),
2551            "",
2552            MigrationOptions::default().with_accept_legacy_v0(true),
2553        )
2554        .await
2555        .unwrap();
2556
2557        let swapped = storage.get_kv("rec:1").await.unwrap().unwrap();
2558        storage.store_kv("rec:2", &swapped, None).await.unwrap();
2559        let strict = EncryptedStorage::new(
2560            storage,
2561            StorageEncryption::new(&provider()).unwrap(),
2562            false,
2563            false,
2564        );
2565        assert!(strict.get_kv("rec:2").await.is_err());
2566    }
2567
2568    /// A plaintext JSON value that merely looks like an envelope must not be
2569    /// classified as one: v>=1 needs an explicit non-empty key_id.
2570    #[test]
2571    fn envelope_classification_requires_version_and_key_id() {
2572        let shape = |extra: &str| {
2573            format!(
2574                r#"{{"data":"AAAA","nonce":"AAAAAAAAAAAAAAAA","algorithm":"AES-256-GCM"{extra}}}"#
2575            )
2576        };
2577        // v2 without a key_id: not an envelope.
2578        assert!(StorageEncryption::parse_envelope(shape(r#","v":2"#).as_bytes()).is_none());
2579        // v1 with an empty key_id: not an envelope.
2580        assert!(
2581            StorageEncryption::parse_envelope(shape(r#","v":1,"key_id":"""#).as_bytes()).is_none()
2582        );
2583        // v2 with a key_id: an envelope.
2584        assert!(
2585            StorageEncryption::parse_envelope(shape(r#","v":2,"key_id":"1""#).as_bytes()).is_some()
2586        );
2587        // The original (v0) shape has neither field and is still recognised.
2588        assert!(StorageEncryption::parse_envelope(shape("").as_bytes()).is_some());
2589    }
2590
2591    /// Every way decryption can fail yields the same error text, so the
2592    /// failure itself is not an oracle for which check tripped.
2593    #[test]
2594    fn decrypt_failures_share_one_error_text() {
2595        let enc = StorageEncryption::new(&FixedKeys {
2596            current: "a",
2597            keys: vec![("a", KEY_A), ("b", KEY_B)],
2598        })
2599        .unwrap();
2600        let good = enc.encrypt(b"secret", b"rec:1").unwrap();
2601
2602        let mut bad_base64 = good.clone();
2603        bad_base64.data = "!!not base64!!".to_string();
2604        let mut short_nonce = good.clone();
2605        short_nonce.nonce = BASE64.encode([1u8; 4]);
2606        let mut unknown_key = good.clone();
2607        unknown_key.key_id = "zzz".to_string();
2608        let mut wrong_algorithm = good.clone();
2609        wrong_algorithm.algorithm = "ROT13".to_string();
2610        let mut tampered = good.clone();
2611        let mut raw = BASE64.decode(&tampered.data).unwrap();
2612        raw[0] ^= 0x01;
2613        tampered.data = BASE64.encode(raw);
2614
2615        let messages: Vec<String> = [
2616            (&bad_base64, &b"rec:1"[..]),
2617            (&short_nonce, b"rec:1"),
2618            (&unknown_key, b"rec:1"),
2619            (&wrong_algorithm, b"rec:1"),
2620            (&tampered, b"rec:1"),
2621            (&good, b"rec:OTHER"),
2622        ]
2623        .iter()
2624        .map(|(envelope, aad)| enc.decrypt(envelope, aad).unwrap_err().to_string())
2625        .collect();
2626        assert!(
2627            messages.windows(2).all(|w| w[0] == w[1]),
2628            "decrypt failures must be indistinguishable, got: {messages:#?}"
2629        );
2630    }
2631
2632    #[test]
2633    fn legacy_trial_decryption_order_is_stable() {
2634        let enc = StorageEncryption::new(&FixedKeys {
2635            current: "k0",
2636            keys: (0..8)
2637                .map(|i| {
2638                    let id: &'static str = Box::leak(format!("k{i}").into_boxed_str());
2639                    (id, [i as u8 + 1; 32])
2640                })
2641                .rev()
2642                .collect(),
2643        })
2644        .unwrap();
2645        let ids = enc.sorted_key_ids();
2646        let mut expected = ids.clone();
2647        expected.sort();
2648        assert_eq!(ids, expected);
2649        assert_eq!(ids.len(), 8);
2650    }
2651
2652    #[test]
2653    fn safe_log_id_keeps_eight_hash_bytes() {
2654        let id = safe_log_id("api_key:abc123");
2655        let hash = id.strip_prefix("api_key:").unwrap();
2656        assert_eq!(hash.len(), 16, "8 bytes = 16 hex chars, got {id}");
2657        let colonless = safe_log_id("rawsecret");
2658        assert_eq!(colonless.strip_prefix("key:").unwrap().len(), 16);
2659    }
2660
2661    // ---- key loading fails closed (review gap) ----------------------------------
2662
2663    fn load_with_env(key: Option<&str>) -> Result<LoadedKeys> {
2664        let _lock = TEST_ENCRYPTION_ENV_LOCK.blocking_lock();
2665        // SAFETY: serialized by TEST_ENCRYPTION_ENV_LOCK.
2666        unsafe {
2667            std::env::remove_var("AUTH_STORAGE_ENCRYPTION_KEYS_FILE");
2668            match key {
2669                Some(key) => std::env::set_var("AUTH_STORAGE_ENCRYPTION_KEY", key),
2670                None => std::env::remove_var("AUTH_STORAGE_ENCRYPTION_KEY"),
2671            }
2672        }
2673        let result = EnvKeyProvider.load_keys();
2674        // SAFETY: serialized by TEST_ENCRYPTION_ENV_LOCK (held above).
2675        unsafe { std::env::remove_var("AUTH_STORAGE_ENCRYPTION_KEY") };
2676        result
2677    }
2678
2679    #[test]
2680    fn malformed_env_keys_fail_closed() {
2681        assert!(load_with_env(None).is_err(), "no key at all");
2682        assert!(load_with_env(Some("")).is_err(), "empty key");
2683        assert!(
2684            load_with_env(Some("!!!not base64!!!")).is_err(),
2685            "bad base64"
2686        );
2687        assert!(
2688            load_with_env(Some(&BASE64.encode([1u8; 16]))).is_err(),
2689            "16-byte key"
2690        );
2691        assert!(
2692            load_with_env(Some(&BASE64.encode([1u8; 33]))).is_err(),
2693            "33-byte key"
2694        );
2695        assert!(load_with_env(Some(&BASE64.encode([1u8; 32]))).is_ok());
2696    }
2697
2698    #[test]
2699    fn malformed_keys_file_fails_closed() {
2700        let dir = tempfile::tempdir().unwrap();
2701        let good = BASE64.encode([3u8; 32]);
2702        let cases: [(&str, String); 4] = [
2703            ("not json", "{{{".to_string()),
2704            (
2705                "current missing from keys",
2706                format!(r#"{{"current":"x","keys":{{"1":"{good}"}}}}"#),
2707            ),
2708            (
2709                "one malformed entry",
2710                format!(r#"{{"current":"1","keys":{{"1":"{good}","2":"short"}}}}"#),
2711            ),
2712            ("empty keys", r#"{"current":"1","keys":{}}"#.to_string()),
2713        ];
2714        let _lock = TEST_ENCRYPTION_ENV_LOCK.blocking_lock();
2715        for (name, body) in cases {
2716            let path = dir.path().join("keys.json");
2717            std::fs::write(&path, body).unwrap();
2718            // SAFETY: serialized by TEST_ENCRYPTION_ENV_LOCK.
2719            unsafe {
2720                std::env::set_var("AUTH_STORAGE_ENCRYPTION_KEYS_FILE", &path);
2721            }
2722            let result = EnvKeyProvider.load_keys();
2723            // SAFETY: serialized by TEST_ENCRYPTION_ENV_LOCK (held above).
2724            unsafe { std::env::remove_var("AUTH_STORAGE_ENCRYPTION_KEYS_FILE") };
2725            assert!(result.is_err(), "{name} must fail closed");
2726        }
2727    }
2728
2729    // ---- migration preserves KV TTLs ---------------------------------------------
2730
2731    fn ttl_provider() -> FixedKeys {
2732        FixedKeys {
2733            current: "1",
2734            keys: vec![("1", KEY_A)],
2735        }
2736    }
2737
2738    #[tokio::test]
2739    async fn migration_preserves_ttl_when_encrypting_plaintext() {
2740        let storage = MemoryStorage::new();
2741        storage
2742            .store_kv("otp:1", b"123456", Some(Duration::from_secs(600)))
2743            .await
2744            .unwrap();
2745        storage.store_kv("durable:1", b"keep", None).await.unwrap();
2746
2747        migrate_kv_to_encrypted(
2748            &storage,
2749            &StorageEncryption::new(&ttl_provider()).unwrap(),
2750            "",
2751            MigrationOptions::default(),
2752        )
2753        .await
2754        .unwrap();
2755
2756        let ttl = storage.get_kv_ttl("otp:1").await.unwrap().unwrap();
2757        assert!(
2758            ttl > Duration::from_secs(590) && ttl <= Duration::from_secs(600),
2759            "TTL must survive migration, got {ttl:?}"
2760        );
2761        assert_eq!(storage.get_kv_ttl("durable:1").await.unwrap(), None);
2762        let raw = storage.get_kv("otp:1").await.unwrap().unwrap();
2763        assert!(
2764            StorageEncryption::looks_like_envelope(&raw),
2765            "must be encrypted"
2766        );
2767    }
2768
2769    #[tokio::test]
2770    async fn legacy_upgrade_preserves_ttl() {
2771        let storage = MemoryStorage::new();
2772        storage
2773            .store_kv(
2774                "rec:1",
2775                &manual_envelope(&KEY_A, "", 0, b"secret", b""),
2776                Some(Duration::from_secs(600)),
2777            )
2778            .await
2779            .unwrap();
2780
2781        migrate_kv_to_encrypted(
2782            &storage,
2783            &StorageEncryption::new(&ttl_provider()).unwrap(),
2784            "",
2785            MigrationOptions::default().with_accept_legacy_v0(true),
2786        )
2787        .await
2788        .unwrap();
2789
2790        let ttl = storage.get_kv_ttl("rec:1").await.unwrap().unwrap();
2791        assert!(ttl > Duration::from_secs(590) && ttl <= Duration::from_secs(600));
2792        let raw = storage.get_kv("rec:1").await.unwrap().unwrap();
2793        assert_eq!(
2794            StorageEncryption::parse_envelope(&raw).unwrap().v,
2795            CURRENT_FORMAT_VERSION
2796        );
2797    }
2798
2799    /// A backend that cannot report TTLs must not be migrated silently: a
2800    /// real run would make every expiring entry permanent. It needs an
2801    /// explicit opt-in, and a dry run (which writes nothing) is always fine.
2802    #[tokio::test]
2803    async fn migration_refuses_backend_that_cannot_report_ttl_without_opt_in() {
2804        let storage = crate::testing::utilities::MockStorage::new();
2805        let encryption = StorageEncryption::new(&ttl_provider()).unwrap();
2806
2807        assert!(
2808            migrate_kv_to_encrypted(&storage, &encryption, "", MigrationOptions::default())
2809                .await
2810                .is_err(),
2811            "a real run on a TTL-blind backend must be refused"
2812        );
2813        assert!(
2814            migrate_kv_to_encrypted(
2815                &storage,
2816                &encryption,
2817                "",
2818                MigrationOptions::default().with_dry_run(true)
2819            )
2820            .await
2821            .is_ok(),
2822            "a dry run writes nothing, so it is allowed"
2823        );
2824        assert!(
2825            migrate_kv_to_encrypted(
2826                &storage,
2827                &encryption,
2828                "",
2829                MigrationOptions::default().with_accept_ttl_loss(true)
2830            )
2831            .await
2832            .is_ok(),
2833            "explicit opt-in proceeds"
2834        );
2835    }
2836
2837    #[tokio::test]
2838    async fn backends_that_honour_ttl_say_so() {
2839        let memory = MemoryStorage::new();
2840        assert!(memory.tracks_kv_ttl());
2841        let wrapped = EncryptedStorage::new(
2842            MemoryStorage::new(),
2843            StorageEncryption::new_random(),
2844            false,
2845            false,
2846        );
2847        assert!(
2848            wrapped.tracks_kv_ttl(),
2849            "the wrapper forwards the inner answer"
2850        );
2851        let shared: std::sync::Arc<dyn AuthStorage> = std::sync::Arc::new(MemoryStorage::new());
2852        assert!(shared.tracks_kv_ttl(), "the Arc impl forwards too");
2853        assert!(
2854            !crate::testing::utilities::MockStorage::new().tracks_kv_ttl(),
2855            "a backend that ignores TTLs must say so"
2856        );
2857    }
2858
2859    // ---- keys-file permissions (L4) and env-key rotation warning (L3) ------------
2860
2861    #[test]
2862    fn keys_file_mode_check_flags_group_and_other_access() {
2863        for open in [0o644, 0o640, 0o604, 0o660, 0o666, 0o777, 0o601, 0o610] {
2864            assert!(keys_file_mode_too_open(open), "{open:o} must be flagged");
2865        }
2866        for closed in [0o600, 0o400, 0o700, 0o500, 0o200] {
2867            assert!(!keys_file_mode_too_open(closed), "{closed:o} must pass");
2868        }
2869    }
2870
2871    #[test]
2872    fn strict_permissions_env_parsing() {
2873        let _lock = TEST_ENCRYPTION_ENV_LOCK.blocking_lock();
2874        let read = |value: Option<&str>| {
2875            // SAFETY: serialized by TEST_ENCRYPTION_ENV_LOCK (held above).
2876            unsafe {
2877                match value {
2878                    Some(v) => std::env::set_var(STRICT_KEY_FILE_PERMISSIONS_ENV, v),
2879                    None => std::env::remove_var(STRICT_KEY_FILE_PERMISSIONS_ENV),
2880                }
2881            }
2882            strict_permissions_requested()
2883        };
2884        assert!(!read(None));
2885        assert!(!read(Some("")));
2886        assert!(!read(Some("0")));
2887        assert!(!read(Some("no")));
2888        assert!(read(Some("1")));
2889        assert!(read(Some("true")));
2890        assert!(read(Some("TRUE")));
2891        assert!(read(Some("Yes")));
2892        // SAFETY: serialized by TEST_ENCRYPTION_ENV_LOCK (held above).
2893        unsafe { std::env::remove_var(STRICT_KEY_FILE_PERMISSIONS_ENV) };
2894    }
2895
2896    /// Runs on Unix CI only (Windows has no group/other mode bits).
2897    #[cfg(unix)]
2898    #[test]
2899    fn keys_file_permissions_strict_mode_fails_startup() {
2900        use std::os::unix::fs::PermissionsExt;
2901        let dir = tempfile::tempdir().unwrap();
2902        let path = dir.path().join("keys.json");
2903        std::fs::write(&path, "{}").unwrap();
2904        let path_str = path.to_str().unwrap();
2905
2906        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap();
2907        assert!(
2908            check_keys_file_permissions(path_str, true).is_err(),
2909            "strict + 0644"
2910        );
2911        assert!(
2912            check_keys_file_permissions(path_str, false).is_ok(),
2913            "warn-only + 0644"
2914        );
2915
2916        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
2917        assert!(
2918            check_keys_file_permissions(path_str, true).is_ok(),
2919            "strict + 0600"
2920        );
2921    }
2922
2923    #[test]
2924    fn env_key_source_detection() {
2925        let _lock = TEST_ENCRYPTION_ENV_LOCK.blocking_lock();
2926        // SAFETY: serialized by TEST_ENCRYPTION_ENV_LOCK (held above).
2927        unsafe {
2928            std::env::remove_var("AUTH_STORAGE_ENCRYPTION_KEYS_FILE");
2929            std::env::remove_var("AUTH_STORAGE_ENCRYPTION_KEY");
2930        }
2931        assert!(!env_key_source_in_use(), "no key configured at all");
2932        // SAFETY: as above.
2933        unsafe { std::env::set_var("AUTH_STORAGE_ENCRYPTION_KEY", "irrelevant") };
2934        assert!(
2935            env_key_source_in_use(),
2936            "no keys file => the env var is the source"
2937        );
2938        // SAFETY: as above.
2939        unsafe { std::env::set_var("AUTH_STORAGE_ENCRYPTION_KEYS_FILE", "/some/keys.json") };
2940        assert!(!env_key_source_in_use(), "a keys file supports rotation");
2941        // SAFETY: as above.
2942        unsafe {
2943            std::env::remove_var("AUTH_STORAGE_ENCRYPTION_KEYS_FILE");
2944            std::env::remove_var("AUTH_STORAGE_ENCRYPTION_KEY");
2945        }
2946    }
2947
2948    #[tokio::test]
2949    async fn envelope_probe_finds_stored_envelopes_within_its_limit() {
2950        let enc = StorageEncryption::new_random();
2951        let storage = MemoryStorage::new();
2952        storage
2953            .store_kv("plain:1", b"just text", None)
2954            .await
2955            .unwrap();
2956        assert!(
2957            !stored_envelopes_exist(&storage, 10).await.unwrap(),
2958            "plaintext only"
2959        );
2960
2961        let sealed = enc.encrypt_for_storage(b"secret", b"sealed:1").unwrap();
2962        storage.store_kv("sealed:1", &sealed, None).await.unwrap();
2963        assert!(
2964            stored_envelopes_exist(&storage, 10).await.unwrap(),
2965            "envelope present"
2966        );
2967
2968        // The probe is bounded: with a limit of 0 it looks at nothing.
2969        assert!(!stored_envelopes_exist(&storage, 0).await.unwrap());
2970    }
2971
2972    #[tokio::test]
2973    async fn encrypted_storage_says_it_encrypts() {
2974        let wrapped = EncryptedStorage::new(
2975            MemoryStorage::new(),
2976            StorageEncryption::new_random(),
2977            false,
2978            false,
2979        );
2980        assert!(wrapped.encrypts_kv_at_rest());
2981        assert!(!MemoryStorage::new().encrypts_kv_at_rest());
2982        let shared: std::sync::Arc<dyn AuthStorage> = std::sync::Arc::new(wrapped);
2983        assert!(shared.encrypts_kv_at_rest(), "the Arc impl forwards");
2984    }
2985}