dotseal 0.1.0

Seal individual dotenv values with scope-bound keys (AES-256-GCM, AAD-bound to (scope, name))
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
//! Dotseal core: per-value AES-256-GCM sealing for dotenv files.
//!
//! See `FORMAT.md` at the repository root for the envelope format, AAD
//! construction, and cross-language compatibility guarantees. See `CLI.md`
//! for the binary's stability contract.
//!
//! The two primary entry points are [`seal_value`] (encrypt) and
//! [`decrypt_value`] (decrypt). [`parse_env`] / [`decrypt_env`] handle
//! whole dotenv files. [`Plaintext`] wraps decrypt outputs in a
//! zeroize-on-drop string.

use aes_gcm::aead::{Aead, KeyInit, Payload};
use aes_gcm::{Aes256Gcm, Key, Nonce};
use base64::engine::general_purpose::{URL_SAFE, URL_SAFE_NO_PAD};
use base64::Engine;
use indexmap::IndexMap;
use rand_core::{OsRng, RngCore};
use std::fmt;
use zeroize::Zeroizing;

/// Plaintext output type. The underlying `String` is zeroed on drop so that
/// the recovered secret does not linger in heap memory after the caller is
/// done with it. Loaders in non-Rust languages cannot offer the same guarantee
/// — see `FORMAT.md` for the documented divergence.
pub type Plaintext = Zeroizing<String>;

/// Envelope version emitted by [`seal_value`] and accepted by
/// [`decrypt_value`]. Bumping this constant is a coordinated breaking
/// change — see the Versioning section in `FORMAT.md`.
pub const VERSION: &str = "v1";

/// Scope name used by the CLI when no `-s/--scope` flag is given.
pub const DEFAULT_SCOPE: &str = "default";

/// Nonce length, in bytes, for AES-256-GCM in the v1 envelope.
pub const NONCE_LEN: usize = 12;

/// Master key length, in bytes.
pub const KEY_LEN: usize = 32;

/// Result alias parameterised over [`Error`].
pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    ParseHexKey {
        source: std::num::ParseIntError,
    },
    ParseBase64Key {
        source: base64::DecodeError,
    },
    DecodedKeyLength {
        expected: usize,
        actual: usize,
    },
    KeyLength {
        expected: usize,
        actual: usize,
    },
    NonceLength {
        expected: usize,
        actual: usize,
    },
    EncryptFailed,
    UnsupportedEncryptedValue {
        name: String,
    },
    DecodeEncryptedValue {
        name: String,
        source: base64::DecodeError,
    },
    EncryptedValueTooShort {
        name: String,
    },
    DecryptFailed {
        name: String,
    },
    PlaintextNotUtf8 {
        name: String,
        source: std::string::FromUtf8Error,
    },
    InvalidEnvName {
        name: String,
    },
    InvalidScope {
        scope: String,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::ParseHexKey { source } => write!(f, "parse hex key: {source}"),
            Error::ParseBase64Key { source } => write!(f, "parse base64url key: {source}"),
            Error::DecodedKeyLength { expected, .. } => {
                write!(f, "master key must decode to {expected} bytes")
            }
            Error::KeyLength { expected, .. } => write!(f, "master key must be {expected} bytes"),
            Error::NonceLength { expected, .. } => write!(f, "nonce must be {expected} bytes"),
            Error::EncryptFailed => f.write_str("encrypt failed"),
            Error::UnsupportedEncryptedValue { name } => {
                write!(f, "unsupported encrypted value for {name:?}")
            }
            Error::DecodeEncryptedValue { name, source } => {
                write!(f, "decode encrypted value for {name:?}: {source}")
            }
            Error::EncryptedValueTooShort { name } => {
                write!(f, "encrypted value for {name:?} is too short")
            }
            Error::DecryptFailed { name } => write!(f, "decrypt failed for {name:?}"),
            Error::PlaintextNotUtf8 { name, source } => {
                write!(f, "plaintext for {name:?} is not utf8: {source}")
            }
            Error::InvalidEnvName { name } => write!(f, "invalid env name: {name:?}"),
            Error::InvalidScope { scope } => write!(f, "invalid scope: {scope:?}"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::ParseHexKey { source } => Some(source),
            Error::ParseBase64Key { source } => Some(source),
            Error::DecodeEncryptedValue { source, .. } => Some(source),
            Error::PlaintextNotUtf8 { source, .. } => Some(source),
            _ => None,
        }
    }
}

/// Returns `true` if `value` carries the dotseal `enc:` prefix. This is a
/// cheap-and-cheerful classifier — actual decryption is what authenticates
/// the value.
pub fn is_encrypted_value(value: &str) -> bool {
    value.starts_with("enc:")
}

/// Generate a fresh 32-byte master key from the OS RNG. The returned `Vec<u8>`
/// is unprotected — wrap it in your zeroizing container of choice if it will
/// live longer than a single statement.
pub fn generate_key() -> Vec<u8> {
    let mut key = vec![0u8; KEY_LEN];
    OsRng.fill_bytes(&mut key);
    key
}

/// Encode a 32-byte master key as base64url **without padding**. Pair with
/// [`parse_key`] for round-tripping.
pub fn encode_key(key: &[u8]) -> String {
    URL_SAFE_NO_PAD.encode(key)
}

/// Parse a master key. Accepts either base64url (with or without `=`
/// padding) or 64-character hex. Anything else is rejected.
pub fn parse_key(raw: &str) -> Result<Vec<u8>> {
    let raw = raw.trim();
    if raw.len() == KEY_LEN * 2 && raw.chars().all(|c| c.is_ascii_hexdigit()) {
        let mut out = Vec::with_capacity(KEY_LEN);
        for i in (0..raw.len()).step_by(2) {
            let byte = u8::from_str_radix(&raw[i..i + 2], 16)
                .map_err(|source| Error::ParseHexKey { source })?;
            out.push(byte);
        }
        return Ok(out);
    }

    let key = decode_base64url(raw).map_err(|source| Error::ParseBase64Key { source })?;
    if key.len() != KEY_LEN {
        return Err(Error::DecodedKeyLength {
            expected: KEY_LEN,
            actual: key.len(),
        });
    }
    Ok(key)
}

/// Seal a plaintext into a `enc:v1:<base64url>` envelope using a fresh
/// 96-bit random nonce.
///
/// The ciphertext is bound to the `(scope, name)` tuple via AAD — moving
/// the result to a different `name` or `scope` makes it un-decryptable.
/// `scope` and `name` MUST validate per [`validate_scope`] / [`validate_name`];
/// anything else returns [`Error::InvalidScope`] / [`Error::InvalidEnvName`].
pub fn seal_value(plaintext: &str, key: &[u8], scope: &str, name: &str) -> Result<String> {
    let mut nonce_bytes = [0u8; NONCE_LEN];
    OsRng.fill_bytes(&mut nonce_bytes);
    seal_value_with_nonce(plaintext, key, scope, name, &nonce_bytes)
}

/// As [`seal_value`] but with a caller-supplied nonce. Used for deterministic
/// test vectors. **Production callers MUST use [`seal_value`] instead** —
/// AES-GCM nonce reuse is catastrophic and there is no in-band detection.
pub fn seal_value_with_nonce(
    plaintext: &str,
    key: &[u8],
    scope: &str,
    name: &str,
    nonce_bytes: &[u8],
) -> Result<String> {
    validate_scope(scope)?;
    validate_name(name)?;
    if key.len() != KEY_LEN {
        return Err(Error::KeyLength {
            expected: KEY_LEN,
            actual: key.len(),
        });
    }
    if nonce_bytes.len() != NONCE_LEN {
        return Err(Error::NonceLength {
            expected: NONCE_LEN,
            actual: nonce_bytes.len(),
        });
    }

    let cipher = Aes256Gcm::new(Key::<Aes256Gcm>::from_slice(key));
    let nonce = Nonce::from_slice(nonce_bytes);
    let aad = aad(scope, name);
    let ciphertext = cipher
        .encrypt(nonce, Payload {
            msg: plaintext.as_bytes(),
            aad: aad.as_bytes(),
        })
        .map_err(|_| Error::EncryptFailed)?;

    let mut payload = Vec::with_capacity(NONCE_LEN + ciphertext.len());
    payload.extend_from_slice(nonce_bytes);
    payload.extend_from_slice(&ciphertext);

    Ok(format!("enc:{VERSION}:{}", URL_SAFE_NO_PAD.encode(payload)))
}

/// Decrypt a `enc:v1:<base64url>` envelope. Returns a [`Plaintext`] which
/// zero-fills its allocation on drop.
///
/// Plaintext is required to be valid UTF-8 (see `FORMAT.md`); non-UTF-8
/// authenticated payloads return [`Error::PlaintextNotUtf8`].
///
/// Values that don't carry the `enc:` prefix are returned unchanged (the
/// "passthrough plain values" pattern, used by [`decrypt_env`]).
///
/// `scope` and `name` are loosened relative to the seal-side regex — only
/// `\n` and `\r` are rejected. This is what makes
/// [`decryptTree`-style](https://github.com/clbrge/dotseal#decrypttree) dotted
/// paths work.
pub fn decrypt_value(
    value: &str,
    key: &[u8],
    scope: &str,
    name: &str,
) -> Result<Plaintext> {
    if !is_encrypted_value(value) {
        return Ok(Zeroizing::new(value.to_string()));
    }
    validate_scope(scope)?;
    validate_name(name)?;

    let mut parts = value.splitn(3, ':');
    let marker = parts.next().unwrap_or_default();
    let version = parts.next().unwrap_or_default();
    let payload = parts.next().unwrap_or_default();

    if marker != "enc" || version != VERSION {
        return Err(Error::UnsupportedEncryptedValue {
            name: name.to_string(),
        });
    }

    let bytes = decode_base64url(payload).map_err(|source| Error::DecodeEncryptedValue {
        name: name.to_string(),
        source,
    })?;
    if bytes.len() <= NONCE_LEN {
        return Err(Error::EncryptedValueTooShort {
            name: name.to_string(),
        });
    }

    let (nonce_bytes, ciphertext) = bytes.split_at(NONCE_LEN);
    let cipher = Aes256Gcm::new(Key::<Aes256Gcm>::from_slice(key));
    let aad = aad(scope, name);
    let plaintext = cipher
        .decrypt(Nonce::from_slice(nonce_bytes), Payload {
            msg: ciphertext,
            aad: aad.as_bytes(),
        })
        .map_err(|_| Error::DecryptFailed {
            name: name.to_string(),
        })?;

    String::from_utf8(plaintext)
        .map(Zeroizing::new)
        .map_err(|source| Error::PlaintextNotUtf8 {
            name: name.to_string(),
            source,
        })
}

/// Decrypt every encrypted entry in an env map produced by [`parse_env`].
/// Plaintext values pass through unchanged. Iteration order matches `env`
/// (file order if `env` came from `parse_env`).
pub fn decrypt_env(
    env: &IndexMap<String, String>,
    key: &[u8],
    scope: &str,
) -> Result<IndexMap<String, Plaintext>> {
    let mut out = IndexMap::with_capacity(env.len());
    for (name, value) in env {
        out.insert(name.clone(), decrypt_value(value, key, scope, name)?);
    }
    Ok(out)
}

/// Parse a dotenv-formatted string into `(name, value)` pairs.
///
/// Iteration order matches the order of definitions in `content`. When the
/// same name appears multiple times, the last definition wins and keeps the
/// position of the first occurrence (standard `IndexMap` insert behavior).
pub fn parse_env(content: &str) -> IndexMap<String, String> {
    let content = content.strip_prefix('\u{FEFF}').unwrap_or(content);
    content
        .lines()
        .filter_map(|line| {
            let name = env_line_name(line)?;
            let idx = line.find('=')?;
            Some((name, parse_env_value(&line[idx + 1..])))
        })
        .collect()
}

/// Parse the right-hand side of a single dotenv line (`KEY=...`) into a
/// value string. The behavior is documented in `FORMAT.md` § Dotenv parsing
/// and is part of the public commitment — changing it is a breaking change.
///
/// Highlights:
/// - Leading whitespace is trimmed.
/// - `"..."` is honored with backslash escapes (`\n`, `\r`, `\t`, `\"`, `\\`).
/// - `'...'` is taken literally.
/// - Otherwise unquoted: terminated by the first `#` at value-start or
///   preceded by space/tab; trailing whitespace is stripped.
pub fn parse_env_value(raw: &str) -> String {
    let trimmed_start = raw.trim_start();

    if let Some(rest) = trimmed_start.strip_prefix('"') {
        if let Some(end) = find_double_quote_end(rest) {
            return unescape_double_quoted(&rest[..end]);
        }
    } else if let Some(rest) = trimmed_start.strip_prefix('\'') {
        if let Some(end) = rest.find('\'') {
            return rest[..end].to_string();
        }
    }

    strip_inline_comment(trimmed_start).trim_end().to_string()
}

fn strip_inline_comment(value: &str) -> &str {
    let bytes = value.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'#' && (i == 0 || bytes[i - 1] == b' ' || bytes[i - 1] == b'\t') {
            return &value[..i];
        }
        i += 1;
    }
    value
}

fn find_double_quote_end(rest: &str) -> Option<usize> {
    let bytes = rest.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'\\' && i + 1 < bytes.len() {
            i += 2;
            continue;
        }
        if bytes[i] == b'"' {
            return Some(i);
        }
        i += 1;
    }
    None
}

/// Reject `name`s that don't match the env-name regex
/// (`[A-Za-z_][A-Za-z0-9_]*`). Intended for seal-side input validation.
pub fn validate_name(name: &str) -> Result<()> {
    if is_valid_name(name) {
        Ok(())
    } else {
        Err(Error::InvalidEnvName {
            name: name.to_string(),
        })
    }
}

/// Pure predicate version of [`validate_name`]. Cheap to call from filter
/// chains.
pub fn is_valid_name(name: &str) -> bool {
    let mut chars = name.chars();
    match chars.next() {
        Some(c) if c == '_' || c.is_ascii_alphabetic() => {}
        _ => return false,
    }
    chars.all(|c| c == '_' || c.is_ascii_alphanumeric())
}

/// Reject `scope`s that don't match the seal-side regex
/// (`[A-Za-z0-9_.-]+`). The decrypt side uses a looser
/// no-`\n`/`\r` rule — see [`decrypt_value`].
pub fn validate_scope(scope: &str) -> Result<()> {
    if !scope.is_empty()
        && scope
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-' || c == '.')
    {
        Ok(())
    } else {
        Err(Error::InvalidScope {
            scope: scope.to_string(),
        })
    }
}

/// Extract the variable name from a single dotenv line. Returns `None` for
/// blank, comment, or malformed lines.
///
/// Strips an optional UTF-8 BOM, leading whitespace, and an optional
/// `export` keyword followed by space or tab (matching `parse_env`). The
/// returned name is validated with [`is_valid_name`].
pub fn env_line_name(line: &str) -> Option<String> {
    let trimmed = line.trim_start_matches(|c: char| c == '\u{FEFF}' || c.is_whitespace());
    if trimmed.is_empty() || trimmed.starts_with('#') {
        return None;
    }
    let rest = strip_export_prefix(trimmed).unwrap_or(trimmed);
    let idx = rest.find('=')?;
    let name = rest[..idx].trim();
    if is_valid_name(name) {
        Some(name.to_string())
    } else {
        None
    }
}

fn strip_export_prefix(line: &str) -> Option<&str> {
    let rest = line.strip_prefix("export")?;
    let mut chars = rest.chars();
    let first = chars.next()?;
    if first == ' ' || first == '\t' {
        let trimmed_count = first.len_utf8()
            + rest[first.len_utf8()..]
                .chars()
                .take_while(|c| *c == ' ' || *c == '\t')
                .map(|c| c.len_utf8())
                .sum::<usize>();
        Some(&rest[trimmed_count..])
    } else {
        None
    }
}

/// Construct the AAD payload that binds a sealed value to a `(scope, name)`
/// tuple. The byte sequence is documented in `FORMAT.md` § Algorithm. The
/// trailing `\n` after `name=<NAME>` is part of the contract — every loader
/// reproduces it byte-for-byte.
///
/// Callers MUST validate `scope` and `name` against the AAD-injection
/// charset (no `\n`, no `\r`) before calling this; `seal_value_with_nonce`
/// and `decrypt_value` already do.
fn aad(scope: &str, name: &str) -> String {
    format!("dotseal:{VERSION}\nscope={scope}\nname={name}\n")
}

fn decode_base64url(value: &str) -> std::result::Result<Vec<u8>, base64::DecodeError> {
    if value.contains('=') {
        URL_SAFE.decode(value)
    } else {
        URL_SAFE_NO_PAD.decode(value)
    }
}

fn unescape_double_quoted(value: &str) -> String {
    let mut out = String::with_capacity(value.len());
    let mut chars = value.chars();
    while let Some(ch) = chars.next() {
        if ch != '\\' {
            out.push(ch);
            continue;
        }
        match chars.next() {
            Some('n') => out.push('\n'),
            Some('r') => out.push('\r'),
            Some('t') => out.push('\t'),
            Some('"') => out.push('"'),
            Some('\\') => out.push('\\'),
            Some(other) => {
                out.push('\\');
                out.push(other);
            }
            None => out.push('\\'),
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn roundtrip_binds_name_and_scope() {
        let key = [7u8; KEY_LEN];
        let sealed = seal_value("secret", &key, "production", "API_KEY").unwrap();
        assert_eq!(
            &**decrypt_value(&sealed, &key, "production", "API_KEY").unwrap(),
            "secret"
        );
        assert!(decrypt_value(&sealed, &key, "development", "API_KEY").is_err());
        assert!(decrypt_value(&sealed, &key, "production", "OTHER_KEY").is_err());
    }

    #[test]
    fn public_errors_are_typed() {
        match validate_scope("prod\nname=ADMIN").unwrap_err() {
            Error::InvalidScope { scope } => {
                assert_eq!(scope, "prod\nname=ADMIN");
            }
            other => panic!("unexpected error: {other}"),
        }
    }

    #[test]
    fn seal_rejects_invalid_scope_and_name() {
        let key = [7u8; KEY_LEN];
        let nonce = [1u8; NONCE_LEN];

        assert!(seal_value_with_nonce(
            "secret",
            &key,
            "prod\nname=ADMIN",
            "API_KEY",
            &nonce,
        )
        .is_err());
        assert!(seal_value_with_nonce(
            "secret",
            &key,
            "production",
            "ADMIN\nname=API_KEY",
            &nonce,
        )
        .is_err());
    }

    #[test]
    fn decrypt_rejects_invalid_expected_scope_and_name() {
        let key = [7u8; KEY_LEN];
        let sealed = seal_value("secret", &key, "production", "API_KEY").unwrap();

        assert!(decrypt_value(&sealed, &key, "prod\nname=ADMIN", "API_KEY").is_err());
        assert!(decrypt_value(&sealed, &key, "production", "ADMIN\nname=API_KEY").is_err());
    }

    #[test]
    fn decrypt_rejects_authenticated_invalid_utf8_plaintext() {
        let key: Vec<u8> = (0..32).collect();
        let sealed = "enc:v1:aGlqa2xtbm9wcXJz0R30E2YLXTiKpdMVm3jkrejLYw";

        let err = decrypt_value(sealed, &key, "production", "BINARY_VALUE").unwrap_err();
        assert!(err.to_string().contains("not utf8"));
    }

    #[test]
    fn key_parser_accepts_base64url_and_hex() {
        let key = [3u8; KEY_LEN];
        let encoded = encode_key(&key);
        assert_eq!(parse_key(&encoded).unwrap(), key);
        assert_eq!(parse_key(&(encoded + "=")).unwrap(), key);
        assert_eq!(parse_key(&"03".repeat(KEY_LEN)).unwrap(), key);
    }

    #[test]
    fn decrypt_accepts_padded_base64url_payload() {
        let key: Vec<u8> = (0..32).collect();
        let sealed = "enc:v1:ICEiIyQlJicoKSoroV_FAgnsN3h7EDerj53e0Qpsr2lTDYsfbYmoIQ==";

        assert_eq!(
            &**decrypt_value(sealed, &key, "production", "API_SUPER_KEY").unwrap(),
            "secret-value"
        );
    }

    #[test]
    fn decrypt_rejects_mutations_and_downgrades() {
        let key: Vec<u8> = (0..32).collect();
        let nonce: Vec<u8> = (32..44).collect();
        let sealed = seal_value_with_nonce(
            "secret-value",
            &key,
            "production",
            "API_SUPER_KEY",
            &nonce,
        )
        .unwrap();

        assert!(decrypt_value("enc:v1:ICEi", &key, "production", "API_SUPER_KEY").is_err());
        assert!(decrypt_value(
            &sealed.replacen("enc:v1:", "enc:v0:", 1),
            &key,
            "production",
            "API_SUPER_KEY",
        )
        .is_err());
        assert!(decrypt_value(&sealed, &key, "development", "API_SUPER_KEY").is_err());
        assert!(decrypt_value(&sealed, &key, "production", "OTHER_KEY").is_err());

        let payload = payload_bytes(&sealed);
        for len in 0..payload.len() {
            let truncated = replace_payload(&sealed, &payload[..len]);
            assert!(
                decrypt_value(&truncated, &key, "production", "API_SUPER_KEY").is_err(),
                "accepted truncated payload length {len}",
            );
        }

        for index in 0..payload.len() {
            let mut mutated = payload.clone();
            mutated[index] ^= 0x01;
            let mutated = replace_payload(&sealed, &mutated);
            assert!(
                decrypt_value(&mutated, &key, "production", "API_SUPER_KEY").is_err(),
                "accepted bit flip at payload byte {index}",
            );
        }
    }

    #[test]
    fn parses_and_decrypts_env() {
        let key = [4u8; KEY_LEN];
        let sealed = seal_value("secret", &key, "production", "API_KEY").unwrap();
        let env = parse_env(&format!("API_KEY={sealed}\nPLAIN=value\n"));
        let decrypted = decrypt_env(&env, &key, "production").unwrap();
        assert_eq!(&***decrypted.get("API_KEY").unwrap(), "secret");
        assert_eq!(&***decrypted.get("PLAIN").unwrap(), "value");
    }

    #[test]
    fn roundtrip_edge_plaintexts() {
        let key: Vec<u8> = (0..32).collect();
        let long = "0123456789abcdef".repeat(64);
        let cases = [
            ("production", "EMPTY_VALUE", ""),
            ("production", "UNICODE_VALUE", "héllo 🌍 Привет こんにちは"),
            ("default", "DEFAULT_SECRET", "default-secret"),
            ("production", "LONG_VALUE", long.as_str()),
            ("production", "MULTILINE_VALUE", "line one\nline two\nline three"),
        ];

        for (index, (scope, name, plaintext)) in cases.into_iter().enumerate() {
            let nonce: Vec<u8> = ((index * NONCE_LEN)..((index + 1) * NONCE_LEN))
                .map(|value| value as u8)
                .collect();
            let sealed = seal_value_with_nonce(plaintext, &key, scope, name, &nonce).unwrap();
            assert_eq!(
                &**decrypt_value(&sealed, &key, scope, name).unwrap(),
                plaintext
            );
        }
    }

    #[test]
    fn parses_dotenv_quoted_values() {
        let env = parse_env(
            "PLAIN= value \nDOUBLE=\" hello world \"\nSINGLE=' keep spaces '\nESCAPED=\"line\\nnext\\t\\\"q\\\"\"\n",
        );
        assert_eq!(env.get("PLAIN").unwrap(), "value");
        assert_eq!(env.get("DOUBLE").unwrap(), " hello world ");
        assert_eq!(env.get("SINGLE").unwrap(), " keep spaces ");
        assert_eq!(env.get("ESCAPED").unwrap(), "line\nnext\t\"q\"");
    }

    #[test]
    fn parse_env_preserves_file_order() {
        let env = parse_env("ZEBRA=z\nALPHA=a\nMIDDLE=m\n");
        let names: Vec<&str> = env.keys().map(String::as_str).collect();
        assert_eq!(names, ["ZEBRA", "ALPHA", "MIDDLE"]);
    }

    #[test]
    fn parse_env_dedup_keeps_last_value_at_first_position() {
        let env = parse_env("FIRST=1\nDUP=a\nSECOND=2\nDUP=b\n");
        let names: Vec<&str> = env.keys().map(String::as_str).collect();
        assert_eq!(names, ["FIRST", "DUP", "SECOND"]);
        assert_eq!(env.get("DUP").unwrap(), "b");
    }

    #[test]
    fn parse_env_strips_utf8_bom() {
        let env = parse_env("\u{FEFF}FIRST=lost\nSECOND=kept\n");
        assert_eq!(env.get("FIRST").unwrap(), "lost");
        assert_eq!(env.get("SECOND").unwrap(), "kept");
    }

    #[test]
    fn parse_env_accepts_tab_after_export() {
        let env = parse_env("export\tTABBED=val\nNORMAL=ok\nexport   PADDED=padded\n");
        assert_eq!(env.get("TABBED").unwrap(), "val");
        assert_eq!(env.get("NORMAL").unwrap(), "ok");
        assert_eq!(env.get("PADDED").unwrap(), "padded");
    }

    #[test]
    fn parse_env_value_strips_inline_comments() {
        let env = parse_env(
            "PLAIN=value # trailing\nNOSPACE=foo#bar\nLEADING=  spaced   # tail\nEMPTYISH=#all comment\n",
        );
        assert_eq!(env.get("PLAIN").unwrap(), "value");
        assert_eq!(env.get("NOSPACE").unwrap(), "foo#bar");
        assert_eq!(env.get("LEADING").unwrap(), "spaced");
        assert_eq!(env.get("EMPTYISH").unwrap(), "");
    }

    #[test]
    fn parse_env_value_keeps_hash_inside_quotes() {
        let env = parse_env(
            "DOUBLE=\"with # inside\" # tail\nSINGLE='also # literal' # tail\nESCAPED=\"line\\nx#y\"\n",
        );
        assert_eq!(env.get("DOUBLE").unwrap(), "with # inside");
        assert_eq!(env.get("SINGLE").unwrap(), "also # literal");
        assert_eq!(env.get("ESCAPED").unwrap(), "line\nx#y");
    }

    #[test]
    fn parses_dotenv_edge_cases() {
        let env = parse_env(
            "\r\n# comment\r\nA=1\r\nexport B=\"two words\"\r\nC=left=right\r\nSPACED = value \r\nSINGLE=' a=b '\r\n",
        );

        assert_eq!(env.get("A").unwrap(), "1");
        assert_eq!(env.get("B").unwrap(), "two words");
        assert_eq!(env.get("C").unwrap(), "left=right");
        assert_eq!(env.get("SPACED").unwrap(), "value");
        assert_eq!(env.get("SINGLE").unwrap(), " a=b ");
        assert!(!env.contains_key("# comment"));
    }

    #[test]
    fn deterministic_vector_matches_shared_vector() {
        let key: Vec<u8> = (0..32).collect();
        let nonce: Vec<u8> = (32..44).collect();
        let sealed = seal_value_with_nonce(
            "secret-value",
            &key,
            "production",
            "API_SUPER_KEY",
            &nonce,
        )
        .unwrap();
        assert_eq!(
            sealed,
            "enc:v1:ICEiIyQlJicoKSoroV_FAgnsN3h7EDerj53e0Qpsr2lTDYsfbYmoIQ"
        );
    }

    fn payload_bytes(sealed: &str) -> Vec<u8> {
        let payload = sealed.rsplit_once(':').unwrap().1;
        decode_base64url(payload).unwrap()
    }

    fn replace_payload(sealed: &str, payload: &[u8]) -> String {
        let prefix = sealed.rsplit_once(':').unwrap().0;
        format!("{prefix}:{}", URL_SAFE_NO_PAD.encode(payload))
    }
}