envstash 0.1.12

Manage .env files across git branches with versioning, diffing, and optional encryption
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
use std::io::Write;
use std::path::Path;

use colored::Colorize;

use crate::cli;
use crate::error::{Error, Result};
use crate::export;
use crate::export::transport;
use crate::store::queries;

use super::transport as remote;

/// Run the `send` command: serialize a saved version to stdout,
/// optionally encrypting with transport encryption.
#[allow(clippy::too_many_arguments)]
pub fn run(
    file: Option<&str>,
    hash: Option<&str>,
    ignore_checks: bool,
    output_format: &str,
    key_file: Option<&str>,
    encrypt: bool,
    encryption_method: &str,
    recipients: &[String],
    password_file: Option<&Path>,
    force: bool,
    to: Option<&str>,
    public: bool,
) -> Result<()> {
    let cwd = std::env::current_dir()?;
    let conn = cli::require_store()?;
    let aes_key = cli::load_encryption_key(&conn, key_file)?;
    let (project_path, git_ctx) = cli::resolve_project(&cwd)?;
    let current_branch = git_ctx.as_ref().map(|c| c.branch.as_str());

    // Resolve which save to share.
    let save = if let Some(h) = hash {
        // Lookup by content hash.
        queries::get_save_by_hash(&conn, &project_path, h)?
            .ok_or_else(|| Error::SaveNotFound(h.to_string()))?
    } else {
        // Find the latest save, optionally filtered by file path.
        let file_filter = file
            .map(|f| cli::resolve_file_path(f, &cwd, &git_ctx).unwrap_or_else(|_| f.to_string()));

        find_latest_save(&conn, &project_path, current_branch, file_filter.as_deref())?
    };

    // Safety checks (unless --ignore).
    if !ignore_checks {
        run_safety_checks(&save, current_branch, &project_path)?;
    }

    // Load entries (decrypting if needed).
    let entries = cli::load_entries(&conn, &save, aes_key.as_deref())?;

    // Build export envelope and serialize.
    let envelope = export::build_envelope(&save, &entries);

    let serialized = match output_format {
        "json" => export::to_json(&envelope)?,
        _ => export::to_text(&envelope),
    };

    // Prepare the final bytes (optionally encrypted).
    let output_bytes = if encrypt {
        encrypt_export(
            serialized.as_bytes(),
            encryption_method,
            recipients,
            password_file,
        )?
    } else {
        serialized.into_bytes()
    };

    // Resolve `--to`: empty string means bare `--to` flag, fill from config or fallback.
    let resolved_to = to.map(|t| {
        if t.is_empty() {
            let cfg = crate::config::load();
            cfg.send
                .default_to
                .unwrap_or_else(|| "https://0x0.st".to_string())
        } else {
            t.to_string()
        }
    });

    // Route through transport backend or write to stdout.
    if let Some(ref target) = resolved_to {
        match remote::send(target, &output_bytes, public, Some(&save.content_hash))? {
            Some(url) => println!("{} {}", "Shared:".green().bold(), url),
            None => println!("{}", "Shared successfully.".green().bold()),
        }
    } else {
        if encrypt && crate::cli::output::is_stdout_terminal() && !force {
            return Err(Error::Other(
                "Encrypted output is binary data. Redirect to a file or pipe, \
                 or use --force to output to terminal."
                    .to_string(),
            ));
        }
        std::io::stdout()
            .write_all(&output_bytes)
            .map_err(Error::Io)?;
    }

    Ok(())
}

/// Encrypt the export bytes using the specified method.
fn encrypt_export(
    data: &[u8],
    method: &str,
    recipients: &[String],
    password_file: Option<&Path>,
) -> Result<Vec<u8>> {
    match method {
        "password" => {
            let pw = crate::crypto::password::resolve_password(password_file)?;
            transport::encrypt_password(data, &pw)
        }
        _ => {
            if recipients.is_empty() {
                return Err(Error::NoGpgRecipient);
            }
            transport::encrypt_gpg(data, recipients)
        }
    }
}

/// Find the latest save for the project, optionally filtered by file path
/// and current branch.
fn find_latest_save(
    conn: &rusqlite::Connection,
    project_path: &str,
    current_branch: Option<&str>,
    file_filter: Option<&str>,
) -> Result<crate::types::SaveMetadata> {
    // Try current branch first.
    if let Some(branch) = current_branch {
        let saves = queries::list_saves(conn, project_path, Some(branch), None, 1, file_filter)?;
        if let Some(save) = saves.into_iter().next() {
            return Ok(save);
        }
    }

    // Fall back to any branch.
    let saves = queries::list_saves(conn, project_path, None, None, 1, file_filter)?;
    saves
        .into_iter()
        .next()
        .ok_or_else(|| Error::SaveNotFound("no saved versions found".to_string()))
}

/// Run safety checks before sharing.
fn run_safety_checks(
    save: &crate::types::SaveMetadata,
    current_branch: Option<&str>,
    project_path: &str,
) -> Result<()> {
    // Check: saved version is from a different branch.
    if let Some(branch) = current_branch
        && !save.branch.is_empty()
        && save.branch != branch
    {
        return Err(Error::Other(format!(
            "Saved version is from branch '{}', but current branch is '{}'. \
                 Use --ignore to share anyway.",
            save.branch, branch
        )));
    }

    // Check: current .env on disk differs from saved version.
    if let Some(disk_hash) = cli::disk_content_hash(project_path, &save.file_path)?
        && disk_hash != save.content_hash
    {
        return Err(Error::Other(
            "Current .env on disk differs from saved version. \
                 Save first, or use --ignore to share anyway."
                .to_string(),
        ));
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::store::queries;
    use crate::test_helpers::{sample_entries, test_conn};
    use crate::types::SaveMetadata;

    #[test]
    fn find_latest_on_branch() {
        let mut conn = test_conn();
        let entries = sample_entries();
        queries::insert_save(
            &mut conn,
            "/proj",
            ".env",
            "main",
            "a1",
            "2024-01-01T00:00:00Z",
            "h1",
            &entries,
            None,
        )
        .unwrap();
        queries::insert_save(
            &mut conn,
            "/proj",
            ".env",
            "main",
            "a2",
            "2024-01-02T00:00:00Z",
            "h2",
            &entries,
            None,
        )
        .unwrap();

        let save = find_latest_save(&conn, "/proj", Some("main"), None).unwrap();
        assert_eq!(save.content_hash, "h2"); // newest
    }

    #[test]
    fn find_latest_fallback_to_any_branch() {
        let mut conn = test_conn();
        let entries = sample_entries();
        queries::insert_save(
            &mut conn,
            "/proj",
            ".env",
            "dev",
            "a1",
            "2024-01-01T00:00:00Z",
            "h1",
            &entries,
            None,
        )
        .unwrap();

        let save = find_latest_save(&conn, "/proj", Some("main"), None).unwrap();
        assert_eq!(save.branch, "dev");
    }

    #[test]
    fn find_latest_not_found() {
        let conn = test_conn();
        let result = find_latest_save(&conn, "/proj", Some("main"), None);
        assert!(result.is_err());
    }

    #[test]
    fn find_latest_with_file_filter() {
        let mut conn = test_conn();
        let entries = sample_entries();
        queries::insert_save(
            &mut conn,
            "/proj",
            ".env",
            "main",
            "a1",
            "2024-01-01T00:00:00Z",
            "h1",
            &entries,
            None,
        )
        .unwrap();
        queries::insert_save(
            &mut conn,
            "/proj",
            ".db-env",
            "main",
            "a2",
            "2024-01-02T00:00:00Z",
            "h2",
            &entries,
            None,
        )
        .unwrap();

        let save = find_latest_save(&conn, "/proj", Some("main"), Some(".env")).unwrap();
        assert_eq!(save.file_path, ".env");
    }

    #[test]
    fn safety_check_wrong_branch() {
        let save = SaveMetadata {
            id: 1,
            project_path: "/proj".to_string(),
            file_path: ".env".to_string(),
            branch: "dev".to_string(),
            commit_hash: "abc".to_string(),
            timestamp: "2024-01-01T00:00:00Z".to_string(),
            content_hash: "h1".to_string(),
            hmac: String::new(),
            message: None,
        };

        let result = run_safety_checks(&save, Some("main"), "/proj");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("branch"));
    }

    #[test]
    fn safety_check_same_branch_passes() {
        let save = SaveMetadata {
            id: 1,
            project_path: "/proj".to_string(),
            file_path: ".env".to_string(),
            branch: "main".to_string(),
            commit_hash: "abc".to_string(),
            timestamp: "2024-01-01T00:00:00Z".to_string(),
            content_hash: "h1".to_string(),
            hmac: String::new(),
            message: None,
        };

        // This passes because there's no file on disk to compare against
        // (disk_content_hash returns None for non-existent files).
        let result = run_safety_checks(&save, Some("main"), "/proj");
        assert!(result.is_ok());
    }

    #[test]
    fn safety_check_empty_branch_passes() {
        let save = SaveMetadata {
            id: 1,
            project_path: "/proj".to_string(),
            file_path: ".env".to_string(),
            branch: String::new(),
            commit_hash: String::new(),
            timestamp: "2024-01-01T00:00:00Z".to_string(),
            content_hash: "h1".to_string(),
            hmac: String::new(),
            message: None,
        };

        let result = run_safety_checks(&save, Some("main"), "/proj");
        assert!(result.is_ok());
    }

    #[test]
    fn safety_check_disk_differs() {
        // Create a temp dir with a .env file that has a known hash.
        let dir = tempfile::tempdir().unwrap();
        let env_path = dir.path().join(".env");
        std::fs::write(&env_path, "KEY=value\n").unwrap();

        let save = SaveMetadata {
            id: 1,
            project_path: dir.path().to_string_lossy().to_string(),
            file_path: ".env".to_string(),
            branch: "main".to_string(),
            commit_hash: "abc".to_string(),
            timestamp: "2024-01-01T00:00:00Z".to_string(),
            content_hash: "definitely_not_matching_hash".to_string(),
            hmac: String::new(),
            message: None,
        };

        let result = run_safety_checks(&save, Some("main"), &dir.path().to_string_lossy());
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("differs"));
    }

    #[test]
    fn safety_check_disk_matches() {
        let dir = tempfile::tempdir().unwrap();
        let env_path = dir.path().join(".env");
        std::fs::write(&env_path, "KEY=value\n").unwrap();

        // Compute the actual hash.
        let entries = crate::parser::parse("KEY=value\n").unwrap();
        let hash = crate::parser::content_hash(&entries);

        let save = SaveMetadata {
            id: 1,
            project_path: dir.path().to_string_lossy().to_string(),
            file_path: ".env".to_string(),
            branch: "main".to_string(),
            commit_hash: "abc".to_string(),
            timestamp: "2024-01-01T00:00:00Z".to_string(),
            content_hash: hash,
            hmac: String::new(),
            message: None,
        };

        let result = run_safety_checks(&save, Some("main"), &dir.path().to_string_lossy());
        assert!(result.is_ok());
    }

    // ----- Transport encryption integration tests -----

    fn write_pw_file(dir: &tempfile::TempDir, pw: &str) -> std::path::PathBuf {
        let path = dir.path().join("pw");
        std::fs::write(&path, pw).unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
        }
        path
    }

    #[test]
    fn encrypt_export_password() {
        let dir = tempfile::tempdir().unwrap();
        let pw_file = write_pw_file(&dir, "test-pw");
        let data = b"# envstash export\nDB_HOST=localhost\n";
        let encrypted = encrypt_export(data, "password", &[], Some(&pw_file)).unwrap();
        assert!(encrypted.starts_with(b"EVPW"));

        // Decrypt and verify round-trip.
        let decrypted = transport::decrypt_password(&encrypted, "test-pw").unwrap();
        assert_eq!(decrypted, data);
    }

    #[test]
    fn encrypt_export_password_no_password_errors() {
        // Without password and without ENVSTASH_PASSWORD, this should
        // fall through to prompt which will fail in test context.
        // We test by using a password file.
        let dir = tempfile::tempdir().unwrap();
        let pw_file = write_pw_file(&dir, "pw");
        let data = b"test data";

        let result = encrypt_export(data, "password", &[], Some(&pw_file));
        assert!(result.is_ok());
    }

    #[test]
    fn resolve_gpg_recipients_explicit() {
        use crate::crypto::gpg;
        use std::path::Path;
        let result = gpg::resolve_recipients(&["ABCD1234".to_string()], Path::new("/tmp")).unwrap();
        assert_eq!(result, vec!["ABCD1234".to_string()]);
    }

    #[test]
    fn share_password_encrypt_round_trip() {
        let mut conn = test_conn();
        let entries = sample_entries();
        queries::insert_save(
            &mut conn,
            "/proj",
            ".env",
            "main",
            "abc",
            "2024-06-17T12:00:00Z",
            "h1",
            &entries,
            None,
        )
        .unwrap();

        // Simulate share with password encryption.
        let saves = queries::list_saves(&conn, "/proj", Some("main"), None, 1, None).unwrap();
        let loaded = queries::get_save_entries(&conn, saves[0].id, None).unwrap();
        let envelope = export::build_envelope(&saves[0], &loaded);
        let serialized = export::to_json(&envelope).unwrap();

        let encrypted = transport::encrypt_password(serialized.as_bytes(), "share-pw").unwrap();

        // Simulate import: detect, decrypt, parse.
        assert_eq!(
            transport::detect(&encrypted),
            transport::TransportEncryption::Password,
        );
        let decrypted = transport::decrypt_auto(&encrypted, Some("share-pw")).unwrap();
        let text = std::str::from_utf8(&decrypted).unwrap();
        let parsed = export::auto_detect(text).unwrap();
        let imported_entries = export::to_env_entries(&parsed);
        assert_eq!(imported_entries, entries);
    }

    #[test]
    fn share_text_format_password_encrypt_round_trip() {
        let mut conn = test_conn();
        let entries = sample_entries();
        queries::insert_save(
            &mut conn,
            "/proj",
            ".env",
            "dev",
            "def",
            "2024-06-17T12:00:00Z",
            "h2",
            &entries,
            None,
        )
        .unwrap();

        let saves = queries::list_saves(&conn, "/proj", Some("dev"), None, 1, None).unwrap();
        let loaded = queries::get_save_entries(&conn, saves[0].id, None).unwrap();
        let envelope = export::build_envelope(&saves[0], &loaded);
        let serialized = export::to_text(&envelope);

        let encrypted = transport::encrypt_password(serialized.as_bytes(), "text-pw").unwrap();
        let decrypted = transport::decrypt_auto(&encrypted, Some("text-pw")).unwrap();
        let text = std::str::from_utf8(&decrypted).unwrap();
        let parsed = export::auto_detect(text).unwrap();
        let imported_entries = export::to_env_entries(&parsed);
        assert_eq!(imported_entries, entries);
    }
}