scrt4 0.4.6

Hardware-bound secrets vault for AI coding agents. Secrets are injected into a subprocess and scrubbed from its output, so an agent can use a credential without ever seeing it. The vault key is derived from a FIDO2 authenticator via WebAuthn PRF and is never stored.
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
// scrt4/src/keystore.rs
//! Master key protection and AES-256-CBC vault encryption.
//!
//! In scrt4, the master key is protected by WebAuthn PRF output (wrapping key)
//! via AES-256-GCM. Vault secrets are encrypted with AES-256-CBC (same format as scrt3).
//!
//! The PRF output (32 bytes) from the browser's WebAuthn assertion is used directly
//! as the AES-256-GCM wrapping key. No Argon2id derivation needed.

use std::collections::HashMap;
use std::path::PathBuf;

use aes::Aes256;
use aes_gcm::{Aes256Gcm, KeyInit, Nonce};
use aes_gcm::aead::Aead;
use cbc::{Decryptor, Encryptor, cipher::{BlockDecryptMut, BlockEncryptMut, KeyIvInit}};
use rand::RngCore;

type Aes256CbcDec = Decryptor<Aes256>;
type Aes256CbcEnc = Encryptor<Aes256>;

// ── On-disk file format ────────────────────────────────────────────

/// Authentication method for the master key file
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq)]
pub enum AuthMethod {
    /// scrt4: WebAuthn PRF output as wrapping key
    WebAuthnPrf,
    /// scrt3 compatibility (migration only)
    Argon2id,
}

/// On-disk master key file format (version 2)
#[derive(serde::Serialize, serde::Deserialize)]
pub struct MasterKeyFile {
    pub version: u32,
    pub salt: String,                           // base64 — PRF salt (32 bytes)
    pub nonce: String,                          // 12 bytes, base64 — AES-256-GCM nonce
    pub ciphertext: String,                     // AES-256-GCM encrypted master key, base64
    pub auth_method: AuthMethod,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub webauthn_credential_id: Option<String>, // base64 — which credential was used
}

// ── Dev mode (issue #59) ────────────────────────────────────────────
//
// When SCRT4_DEV_MODE=1 is set in the daemon environment, the daemon:
//   1. Reads + writes the vault under ~/.scrt4-dev/ instead of ~/.scrt4/
//      (different directory = cross-distribution open is impossible by
//      construction — there is no shared state between hardened and dev)
//   2. Uses a fixed master key (DEV_MASTER_KEY_B64 below) so unlocking
//      requires no WebAuthn ceremony
//   3. Auto-bootstraps an active session at daemon startup (see main.rs)
//
// This is documented as "do not store real secrets" and the wrapper
// banner says so loudly. The fixed key is in source so anyone reading
// this file can decrypt a dev vault — that's intentional.

/// True iff SCRT4_DEV_MODE=1 is set in the daemon's environment.
pub fn is_dev_mode() -> bool {
    std::env::var("SCRT4_DEV_MODE")
        .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
        .unwrap_or(false)
}

/// Fixed master key for dev mode. Exactly 32 bytes (the AES-256 key
/// size), base64-encoded. Plaintext: "dev-mode-fixed-key-not-secure-32".
/// Hardcoded in source on purpose — dev mode is a developer convenience,
/// not a security boundary.
pub const DEV_MASTER_KEY_B64: &str = "ZGV2LW1vZGUtZml4ZWQta2V5LW5vdC1zZWN1cmUtMzI=";

/// Get the scrt4 config directory.
///
/// Hardened: ~/.scrt4
/// Dev:      ~/.scrt4-dev   (when SCRT4_DEV_MODE=1)
///
/// SCRT4_CONFIG_DIR overrides the location outright — the whole vault
/// (secrets, master key, share identity) lives under it. It exists so a
/// user can point at an alternate vault, and so multiple independent
/// vaults can run side by side (dirs::home_dir() ignores a USERPROFILE
/// override on Windows, which otherwise makes that impossible). It is not
/// a security boundary: an attacker who sets it just points at a
/// different vault, which still requires that vault's passkey to unlock.
pub fn config_dir() -> PathBuf {
    if let Ok(dir) = std::env::var("SCRT4_CONFIG_DIR") {
        if !dir.is_empty() {
            return PathBuf::from(dir);
        }
    }
    let home = dirs::home_dir().unwrap_or_else(std::env::temp_dir);
    if is_dev_mode() {
        home.join(".scrt4-dev")
    } else {
        home.join(".scrt4")
    }
}

/// Get the vault directory (~/.scrt4/vault)
pub fn vault_dir() -> PathBuf {
    config_dir().join("vault")
}

/// Get the master key file path (~/.scrt4/master.key)
pub fn master_key_path() -> PathBuf {
    config_dir().join("master.key")
}

/// Get the localhost master key file path (~/.scrt4/master-local.key)
pub fn master_key_local_path() -> PathBuf {
    config_dir().join("master-local.key")
}

/// Save master key wrapped with localhost PRF output
pub fn save_master_key_local(
    master_key_b64: &str,
    prf_output: &[u8; 32],
    prf_salt: &[u8; 32],
    credential_id: Option<&str>,
) -> Result<(), String> {
    save_master_key_webauthn_to(master_key_b64, prf_output, prf_salt, credential_id, &master_key_local_path())
}

/// Load master key using localhost PRF output
pub fn load_master_key_local(prf_output: &[u8; 32]) -> Result<String, String> {
    load_master_key_webauthn_from(prf_output, &master_key_local_path())
}

/// Load PRF salt from the localhost master key file
pub fn load_prf_salt_local() -> Result<[u8; 32], String> {
    load_prf_salt_from(&master_key_local_path())
}

/// Get the secrets file path (~/.scrt4/vault/secrets.enc)
pub fn secrets_path() -> PathBuf {
    vault_dir().join("secrets.enc")
}

// ── Master key management (WebAuthn PRF) ───────────────────────────

/// Generate a new random 32-byte master key, returned as base64
pub fn generate_new_master_key() -> Result<String, String> {
    use base64::Engine;
    let engine = base64::engine::general_purpose::STANDARD;

    let mut key_bytes = [0u8; 32];
    rand::thread_rng().fill_bytes(&mut key_bytes);
    Ok(engine.encode(key_bytes))
}

/// Save a master key to disk, protected by WebAuthn PRF output.
pub fn save_master_key_webauthn(
    master_key_b64: &str,
    prf_output: &[u8; 32],
    prf_salt: &[u8; 32],
    credential_id: Option<&str>,
) -> Result<(), String> {
    save_master_key_webauthn_to(master_key_b64, prf_output, prf_salt, credential_id, &master_key_path())
}

fn save_master_key_webauthn_to(
    master_key_b64: &str,
    prf_output: &[u8; 32],
    prf_salt: &[u8; 32],
    credential_id: Option<&str>,
    path: &std::path::Path,
) -> Result<(), String> {
    use base64::Engine;
    let engine = base64::engine::general_purpose::STANDARD;

    let mut nonce_bytes = [0u8; 12];
    rand::thread_rng().fill_bytes(&mut nonce_bytes);

    let cipher = Aes256Gcm::new_from_slice(prf_output)
        .map_err(|e| format!("AES-GCM init failed: {}", e))?;
    let nonce = Nonce::from_slice(&nonce_bytes);
    let ciphertext = cipher.encrypt(nonce, master_key_b64.as_bytes())
        .map_err(|e| format!("AES-GCM encrypt failed: {}", e))?;

    let file = MasterKeyFile {
        version: 2,
        salt: engine.encode(prf_salt),
        nonce: engine.encode(nonce_bytes),
        ciphertext: engine.encode(ciphertext),
        auth_method: AuthMethod::WebAuthnPrf,
        webauthn_credential_id: credential_id.map(|s| s.to_string()),
    };

    let json = serde_json::to_string_pretty(&file)
        .map_err(|e| format!("JSON serialize failed: {}", e))?;

    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)
            .map_err(|e| format!("Failed to create config directory: {}", e))?;
    }

    std::fs::write(path, &json)
        .map_err(|e| format!("Failed to write master key file: {}", e))?;

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))
            .map_err(|e| format!("Failed to set permissions: {}", e))?;
    }

    tracing::info!("Master key saved (WebAuthn PRF + AES-256-GCM protected)");
    Ok(())
}

/// Load and decrypt the master key using WebAuthn PRF output.
pub fn load_master_key_webauthn(prf_output: &[u8; 32]) -> Result<String, String> {
    load_master_key_webauthn_from(prf_output, &master_key_path())
}

fn load_master_key_webauthn_from(prf_output: &[u8; 32], path: &std::path::Path) -> Result<String, String> {
    use base64::Engine;
    let engine = base64::engine::general_purpose::STANDARD;

    let json = std::fs::read_to_string(path)
        .map_err(|e| format!("Failed to read master key file: {}. Run 'scrt4 setup' first.", e))?;

    let file: MasterKeyFile = serde_json::from_str(&json)
        .map_err(|e| format!("Invalid master key file: {}", e))?;

    if file.auth_method != AuthMethod::WebAuthnPrf {
        return Err("Master key file uses a different auth method (expected WebAuthnPrf)".into());
    }

    let nonce_bytes = engine.decode(&file.nonce)
        .map_err(|e| format!("Invalid nonce: {}", e))?;
    let ciphertext = engine.decode(&file.ciphertext)
        .map_err(|e| format!("Invalid ciphertext: {}", e))?;

    let cipher = Aes256Gcm::new_from_slice(prf_output)
        .map_err(|e| format!("AES-GCM init failed: {}", e))?;
    let nonce = Nonce::from_slice(&nonce_bytes);
    let plaintext = cipher.decrypt(nonce, ciphertext.as_ref())
        .map_err(|_| "WebAuthn authentication failed (wrong PRF output)".to_string())?;

    String::from_utf8(plaintext)
        .map_err(|e| format!("Decrypted master key is not valid UTF-8: {}", e))
}

/// Load the PRF salt from the master key file
pub fn load_prf_salt() -> Result<[u8; 32], String> {
    load_prf_salt_from(&master_key_path())
}

fn load_prf_salt_from(path: &std::path::Path) -> Result<[u8; 32], String> {
    use base64::Engine;
    let engine = base64::engine::general_purpose::STANDARD;

    let json = std::fs::read_to_string(path)
        .map_err(|e| format!("Failed to read master key file: {}", e))?;
    let file: MasterKeyFile = serde_json::from_str(&json)
        .map_err(|e| format!("Invalid master key file: {}", e))?;
    let salt_bytes = engine.decode(&file.salt)
        .map_err(|e| format!("Invalid salt: {}", e))?;

    if salt_bytes.len() != 32 {
        return Err(format!("PRF salt must be 32 bytes, got {}", salt_bytes.len()));
    }

    let mut salt = [0u8; 32];
    salt.copy_from_slice(&salt_bytes);
    Ok(salt)
}

// ── Unlock flow ────────────────────────────────────────────────────

// ── Vault (secrets.enc) management ──────────────────────────────────

fn extract_data_from_env_json(json_content: &str) -> Result<String, String> {
    #[derive(serde::Deserialize)]
    struct EncryptedEnv {
        #[serde(rename = "Data")]
        data: String,
    }
    let parsed: EncryptedEnv = serde_json::from_str(json_content)
        .map_err(|e| format!("Invalid env JSON: {}", e))?;
    Ok(parsed.data)
}

fn decrypt_env_content_with_master_key(
    encrypted_base64: &str,
    master_key_base64: &str,
) -> Result<String, String> {
    use base64::Engine;
    let engine = base64::engine::general_purpose::STANDARD;

    let key_bytes = engine.decode(master_key_base64)
        .map_err(|e| format!("Invalid master key base64: {}", e))?;
    if key_bytes.len() != 32 {
        return Err(format!("Master key must be 32 bytes, got {}", key_bytes.len()));
    }

    let encrypted_bytes = engine.decode(encrypted_base64)
        .map_err(|e| format!("Invalid encrypted data base64: {}", e))?;
    if encrypted_bytes.len() < 17 {
        return Err("Encrypted data too short (need at least IV + 1 block)".into());
    }

    let iv = &encrypted_bytes[0..16];
    let ciphertext = &encrypted_bytes[16..];
    let key: [u8; 32] = key_bytes.try_into().map_err(|_| "Key conversion failed")?;
    let iv_arr: [u8; 16] = iv.try_into().map_err(|_| "IV conversion failed")?;

    let decryptor = Aes256CbcDec::new(&key.into(), &iv_arr.into());
    let mut buffer = ciphertext.to_vec();
    let decrypted = decryptor
        .decrypt_padded_mut::<cbc::cipher::block_padding::Pkcs7>(&mut buffer)
        .map_err(|e| format!("Decryption failed: {:?}", e))?;

    String::from_utf8(decrypted.to_vec())
        .map_err(|e| format!("Decrypted data is not valid UTF-8: {}", e))
}

fn encrypt_env_content_with_master_key(
    plaintext: &str,
    master_key_base64: &str,
) -> Result<String, String> {
    use base64::Engine;
    let engine = base64::engine::general_purpose::STANDARD;

    let key_bytes = engine.decode(master_key_base64)
        .map_err(|e| format!("Invalid master key base64: {}", e))?;
    if key_bytes.len() != 32 {
        return Err(format!("Master key must be 32 bytes, got {}", key_bytes.len()));
    }

    let mut iv = [0u8; 16];
    rand::thread_rng().fill_bytes(&mut iv);

    let key: [u8; 32] = key_bytes.try_into().map_err(|_| "Key conversion failed")?;
    let encryptor = Aes256CbcEnc::new(&key.into(), &iv.into());

    let plaintext_bytes = plaintext.as_bytes();
    let mut buffer = vec![0u8; plaintext_bytes.len() + 16];
    buffer[..plaintext_bytes.len()].copy_from_slice(plaintext_bytes);
    let ciphertext = encryptor
        .encrypt_padded_mut::<cbc::cipher::block_padding::Pkcs7>(&mut buffer, plaintext_bytes.len())
        .map_err(|e| format!("Encryption failed: {:?}", e))?;

    let mut combined = Vec::with_capacity(16 + ciphertext.len());
    combined.extend_from_slice(&iv);
    combined.extend_from_slice(ciphertext);

    Ok(engine.encode(&combined))
}

/// Parse decrypted vault plaintext into a secret map.
/// Tries JSON first (v2 format), falls back to legacy KEY=VALUE\n format.
pub fn parse_env(content: &str) -> HashMap<String, String> {
    if let Ok(secrets) = serde_json::from_str::<HashMap<String, String>>(content) {
        return secrets;
    }

    // Legacy v1 format: newline-separated KEY=VALUE
    let mut vars = HashMap::new();
    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') { continue; }
        if let Some(eq_pos) = trimmed.find('=') {
            let key = trimmed[..eq_pos].trim();
            let value = trimmed[eq_pos + 1..].to_string();
            if !key.is_empty() && key.chars().all(|c| c.is_alphanumeric() || c == '_') {
                vars.insert(key.to_string(), value);
            }
        }
    }
    vars
}

pub fn decrypt_secrets(master_key_b64: &str) -> Result<HashMap<String, String>, String> {
    decrypt_secrets_from(master_key_b64, &secrets_path())
}

fn decrypt_secrets_from(master_key_b64: &str, path: &std::path::Path) -> Result<HashMap<String, String>, String> {
    let json_content = std::fs::read_to_string(path)
        .map_err(|e| format!("Failed to read secrets file: {}", e))?;
    let encrypted_base64 = extract_data_from_env_json(json_content.trim())?;
    let plaintext = decrypt_env_content_with_master_key(&encrypted_base64, master_key_b64)?;
    Ok(parse_env(&plaintext))
}

pub fn save_encrypted_env(
    secrets: &HashMap<String, String>,
    master_key: &str,
) -> Result<(), String> {
    save_encrypted_env_to(secrets, master_key, &secrets_path())
}

fn save_encrypted_env_to(
    secrets: &HashMap<String, String>,
    master_key: &str,
    path: &std::path::Path,
) -> Result<(), String> {
    let plaintext = serde_json::to_string(secrets)
        .map_err(|e| format!("Failed to serialize secrets: {}", e))?;

    let encrypted_base64 = encrypt_env_content_with_master_key(&plaintext, master_key)?;
    let json = format!(r#"{{"Data":"{}"}}"#, encrypted_base64);

    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)
            .map_err(|e| format!("Failed to create vault directory: {}", e))?;
    }

    std::fs::write(path, &json)
        .map_err(|e| format!("Failed to write secrets file: {}", e))?;

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))
            .map_err(|e| format!("Failed to set permissions: {}", e))?;
    }

    tracing::info!("Persisted secrets to disk");
    Ok(())
}

pub fn reset_encrypted_env(master_key: &str) -> Result<(), String> {
    let path = secrets_path();
    if path.exists() {
        std::fs::remove_file(&path)
            .map_err(|e| format!("Failed to remove old secrets file: {}", e))?;
    }
    save_encrypted_env(&HashMap::new(), master_key)?;
    tracing::info!("Reset encrypted env to empty store");
    Ok(())
}

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

    #[test]
    fn test_master_key_webauthn_roundtrip() {
        let dir = tempdir().unwrap();
        let key_path = dir.path().join("master.key");

        let master_key = generate_new_master_key().unwrap();
        let mut prf_output = [0u8; 32];
        rand::thread_rng().fill_bytes(&mut prf_output);
        let mut prf_salt = [0u8; 32];
        rand::thread_rng().fill_bytes(&mut prf_salt);

        save_master_key_webauthn_to(&master_key, &prf_output, &prf_salt, Some("test_cred"), &key_path).unwrap();
        let loaded = load_master_key_webauthn_from(&prf_output, &key_path).unwrap();
        assert_eq!(master_key, loaded);
    }

    #[test]
    fn test_master_key_wrong_prf() {
        let dir = tempdir().unwrap();
        let key_path = dir.path().join("master.key");

        let master_key = generate_new_master_key().unwrap();
        let mut prf_output = [0u8; 32];
        rand::thread_rng().fill_bytes(&mut prf_output);
        let mut prf_salt = [0u8; 32];
        rand::thread_rng().fill_bytes(&mut prf_salt);

        save_master_key_webauthn_to(&master_key, &prf_output, &prf_salt, None, &key_path).unwrap();

        let mut wrong_prf = [0u8; 32];
        rand::thread_rng().fill_bytes(&mut wrong_prf);
        let result = load_master_key_webauthn_from(&wrong_prf, &key_path);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("WebAuthn authentication failed"));
    }

    #[test]
    fn test_v2_file_format() {
        let file = MasterKeyFile {
            version: 2,
            salt: "dGVzdF9zYWx0".to_string(),
            nonce: "dGVzdF9ub25jZQ==".to_string(),
            ciphertext: "dGVzdF9jaXBoZXJ0ZXh0".to_string(),
            auth_method: AuthMethod::WebAuthnPrf,
            webauthn_credential_id: Some("Y3JlZF9pZA==".to_string()),
        };

        let json = serde_json::to_string_pretty(&file).unwrap();
        assert!(json.contains("\"version\": 2"));
        assert!(json.contains("\"WebAuthnPrf\""));

        let loaded: MasterKeyFile = serde_json::from_str(&json).unwrap();
        assert_eq!(loaded.version, 2);
        assert_eq!(loaded.auth_method, AuthMethod::WebAuthnPrf);
    }

    #[test]
    fn test_prf_salt_roundtrip() {
        let dir = tempdir().unwrap();
        let key_path = dir.path().join("master.key");

        let master_key = generate_new_master_key().unwrap();
        let mut prf_output = [0u8; 32];
        rand::thread_rng().fill_bytes(&mut prf_output);
        let mut prf_salt = [0u8; 32];
        rand::thread_rng().fill_bytes(&mut prf_salt);

        save_master_key_webauthn_to(&master_key, &prf_output, &prf_salt, None, &key_path).unwrap();
        let loaded_salt = load_prf_salt_from(&key_path).unwrap();
        assert_eq!(prf_salt, loaded_salt);
    }

    #[test]
    fn test_env_encrypt_decrypt() {
        let master_key = generate_new_master_key().unwrap();
        let plaintext = "API_KEY=secret123\nDB_PASSWORD=p@ssw0rd!";
        let encrypted = encrypt_env_content_with_master_key(plaintext, &master_key).unwrap();
        let decrypted = decrypt_env_content_with_master_key(&encrypted, &master_key).unwrap();
        assert_eq!(decrypted, plaintext);
    }

    #[test]
    fn test_parse_env_legacy_format() {
        let content = "# Comment\nAPI_KEY=secret123\nDB_PASSWORD=p@ssw0rd!\nEMPTY=\nINVALID LINE\n";
        let vars = parse_env(content);
        assert_eq!(vars.get("API_KEY"), Some(&"secret123".to_string()));
        assert_eq!(vars.get("DB_PASSWORD"), Some(&"p@ssw0rd!".to_string()));
        assert_eq!(vars.get("EMPTY"), Some(&"".to_string()));
        assert!(!vars.contains_key("INVALID LINE"));
    }

    #[test]
    fn test_parse_env_json_format() {
        let content = r#"{"API_KEY":"secret123","DB_PASSWORD":"p@ssw0rd!","EMPTY":""}"#;
        let vars = parse_env(content);
        assert_eq!(vars.get("API_KEY"), Some(&"secret123".to_string()));
        assert_eq!(vars.get("DB_PASSWORD"), Some(&"p@ssw0rd!".to_string()));
        assert_eq!(vars.get("EMPTY"), Some(&"".to_string()));
    }

    #[test]
    fn test_save_and_load_vault() {
        let dir = tempdir().unwrap();
        let vault_path = dir.path().join("secrets.enc");

        let master_key = generate_new_master_key().unwrap();
        let mut secrets = HashMap::new();
        secrets.insert("API_KEY".to_string(), "sk-123456".to_string());
        secrets.insert("DB_PASS".to_string(), "hunter2".to_string());

        save_encrypted_env_to(&secrets, &master_key, &vault_path).unwrap();
        let loaded = decrypt_secrets_from(&master_key, &vault_path).unwrap();
        assert_eq!(loaded.get("API_KEY"), Some(&"sk-123456".to_string()));
        assert_eq!(loaded.len(), 2);
    }

    #[test]
    fn test_multiline_secret_roundtrip() {
        let dir = tempdir().unwrap();
        let vault_path = dir.path().join("secrets.enc");

        let master_key = generate_new_master_key().unwrap();
        let mut secrets = HashMap::new();
        secrets.insert("SSH_KEY".to_string(),
            "-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEA\nAAAAGnNrLWVkMjU1MTk=\n-----END OPENSSH PRIVATE KEY-----".to_string());
        secrets.insert("SIMPLE".to_string(), "no-newlines".to_string());

        save_encrypted_env_to(&secrets, &master_key, &vault_path).unwrap();
        let loaded = decrypt_secrets_from(&master_key, &vault_path).unwrap();
        assert_eq!(loaded.get("SSH_KEY").unwrap().lines().count(), 4);
        assert!(loaded.get("SSH_KEY").unwrap().starts_with("-----BEGIN"));
        assert!(loaded.get("SSH_KEY").unwrap().ends_with("-----END OPENSSH PRIVATE KEY-----"));
        assert_eq!(loaded.get("SIMPLE"), Some(&"no-newlines".to_string()));
        assert_eq!(loaded.len(), 2);
    }

    #[test]
    fn test_legacy_vault_auto_migrates() {
        let dir = tempdir().unwrap();
        let vault_path = dir.path().join("secrets.enc");

        let master_key = generate_new_master_key().unwrap();

        // Write in legacy KEY=VALUE\n format
        let legacy_plaintext = "API_KEY=sk-legacy\nDB_PASS=old-format";
        let encrypted = encrypt_env_content_with_master_key(legacy_plaintext, &master_key).unwrap();
        let json = format!(r#"{{"Data":"{}"}}"#, encrypted);
        std::fs::write(&vault_path, &json).unwrap();

        // Read should still work via fallback
        let loaded = decrypt_secrets_from(&master_key, &vault_path).unwrap();
        assert_eq!(loaded.get("API_KEY"), Some(&"sk-legacy".to_string()));
        assert_eq!(loaded.get("DB_PASS"), Some(&"old-format".to_string()));

        // Re-save upgrades to JSON format
        save_encrypted_env_to(&loaded, &master_key, &vault_path).unwrap();
        let reloaded = decrypt_secrets_from(&master_key, &vault_path).unwrap();
        assert_eq!(reloaded, loaded);
    }

    #[test]
    fn test_generate_new_master_key() {
        use base64::Engine;
        let engine = base64::engine::general_purpose::STANDARD;
        let key = generate_new_master_key().unwrap();
        assert_eq!(key.len(), 44);
        assert_eq!(engine.decode(&key).unwrap().len(), 32);
    }

    #[test]
    fn test_config_dir_path() {
        let path = config_dir();
        let path_str = path.to_string_lossy();
        assert!(path_str.contains(".scrt4"));
    }
}