dodot-lib 4.1.1

Core library for dodot dotfiles manager
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
//! `pass` provider — password-store integration.
//!
//! Reference shape: `pass:path/to/entry` (single-colon, no slashes
//! after the colon for the scheme prefix; provider sees the literal
//! path, e.g. `path/to/entry`).
//!
//! Resolution: shells out to `pass show <path>`. By convention,
//! `pass` entries' first line is the password; subsequent lines are
//! arbitrary metadata. dodot follows this convention — `resolve()`
//! returns the first line, stripped of trailing `\n`.
//!
//! `pass` itself defers crypto to GPG; the gpg-agent dance handles
//! interactive auth (passphrase prompts, smartcard touches) before
//! `pass show` returns. Our `probe()` verifies that the binary is on
//! PATH and that `$PASSWORD_STORE_DIR` (or `~/.password-store`) is
//! initialised; deeper auth-state checks (gpg key access) are
//! deferred to resolve-time because probing them would mean
//! triggering the very prompt we're trying to gate behind probe().
//!
//! See `secrets.lex` §5.2 for the spec table.

use std::path::PathBuf;
use std::sync::Arc;

use crate::datastore::CommandRunner;
use crate::secret::provider::{ProbeResult, SecretProvider};
use crate::secret::secret_string::SecretString;
use crate::{DodotError, Result};

/// `SecretProvider` impl for password-store.
pub struct PassProvider {
    runner: Arc<dyn CommandRunner>,
    /// Directory to check for store initialisation. Defaults to
    /// `$PASSWORD_STORE_DIR` if set, falling back to `~/.password-store`.
    /// Tests inject a hermetic path here.
    store_dir: PathBuf,
}

impl PassProvider {
    /// Construct with a runner and explicit store directory. Tests
    /// use this directly; production code uses [`Self::from_env`].
    pub fn new(runner: Arc<dyn CommandRunner>, store_dir: PathBuf) -> Self {
        Self { runner, store_dir }
    }

    /// Construct from environment: respects `$PASSWORD_STORE_DIR`,
    /// falls back to `$HOME/.password-store`. If `$HOME` is unset
    /// (deeply unusual; suggests a test or a daemon context),
    /// returns a provider rooted at `/.password-store` — `probe()`
    /// will surface `Misconfigured` because that path won't exist.
    pub fn from_env(runner: Arc<dyn CommandRunner>) -> Self {
        let store_dir = std::env::var_os("PASSWORD_STORE_DIR")
            .map(PathBuf::from)
            .unwrap_or_else(|| {
                let mut p = std::env::var_os("HOME")
                    .map(PathBuf::from)
                    .unwrap_or_else(|| PathBuf::from("/"));
                p.push(".password-store");
                p
            });
        Self::new(runner, store_dir)
    }

    /// Validate the reference shape before shelling out. Empty
    /// references and references whose path segments include `..`
    /// (path traversal) are rejected up-front. We don't try to be
    /// clever about shell-quoting because `CommandRunner::run` takes
    /// argv as a slice — there's no shell interpolation in play.
    ///
    /// We check segment-equality instead of a substring match on
    /// `..` so that legitimate entry names containing two
    /// consecutive dots (e.g. `service-foo..staging`) pass through.
    /// Pass entries are stored as files on disk; only `..` as its
    /// own segment escapes the store root.
    fn validate_reference(reference: &str) -> Result<()> {
        if reference.is_empty() {
            return Err(DodotError::Other(
                "pass reference is empty. Expected `pass:path/to/entry`.".into(),
            ));
        }
        if reference.split('/').any(|seg| seg == "..") {
            return Err(DodotError::Other(format!(
                "pass reference `{reference}` contains a `..` path segment — \
                 path-traversal references are refused for safety. \
                 Use the literal entry path under the store root."
            )));
        }
        Ok(())
    }
}

impl SecretProvider for PassProvider {
    fn scheme(&self) -> &str {
        "pass"
    }

    fn probe(&self) -> ProbeResult {
        // Cheap binary-on-PATH check: `pass version` returns 0 with
        // a banner. `pass --help` would also work; `version` is the
        // canonical "I'm here" probe across most tool conventions.
        match self.runner.run("pass", &["version".into()]) {
            Ok(out) if out.exit_code == 0 => {}
            Ok(_) => {
                return ProbeResult::ProbeFailed {
                    details: "`pass version` returned a non-zero exit code; the \
                              binary is on PATH but not behaving as expected"
                        .into(),
                };
            }
            Err(_) => {
                return ProbeResult::NotInstalled {
                    hint: "install pass: https://www.passwordstore.org/ \
                           (e.g. `apt install pass`, `brew install pass`)"
                        .into(),
                };
            }
        }
        // Initialised-store check: a `.gpg-id` file at the store
        // root is `pass init`'s canonical artifact.
        let gpg_id = self.store_dir.join(".gpg-id");
        if !gpg_id.exists() {
            return ProbeResult::Misconfigured {
                hint: format!(
                    "password store not initialised at {} \
                     (no .gpg-id found). \
                     Run `pass init <gpg-key-id>`, or set \
                     $PASSWORD_STORE_DIR to point at an existing store.",
                    self.store_dir.display()
                ),
            };
        }
        ProbeResult::Ok
    }

    fn resolve(&self, reference: &str) -> Result<SecretString> {
        Self::validate_reference(reference)?;
        let out = self
            .runner
            .run("pass", &["show".into(), reference.into()])?;
        if out.exit_code != 0 {
            // Map the most common "entry not found" pattern to a
            // sharper error. `pass show missing/path` exits 1 and
            // prints `Error: missing/path is not in the password
            // store.` to stderr. Detect by exit + a stable phrase.
            let stderr = out.stderr.trim();
            let err_msg = if stderr.contains("not in the password store") {
                format!(
                    "secret `pass:{reference}` not found in the password store. \
                     Verify the entry: `pass ls {}`",
                    parent_path(reference).unwrap_or("/")
                )
            } else if stderr.is_empty() {
                format!("`pass show {reference}` exited with code {}", out.exit_code)
            } else {
                // Provider stderr can carry gpg-agent diagnostics or
                // similar. Surface verbatim — but `pass` doesn't echo
                // the password to stderr, so this is safe to include.
                format!(
                    "`pass show {reference}` failed (exit {}): {stderr}",
                    out.exit_code
                )
            };
            return Err(DodotError::Other(err_msg));
        }
        // pass show emits: <password>\n[optional metadata lines]\n
        // The first line is the password. Strip trailing `\n` only;
        // a multi-line value (subsequent lines) is metadata, not
        // password content.
        let first_line = out.stdout.split('\n').next().unwrap_or("");
        Ok(SecretString::new(first_line.to_string()))
    }
}

/// Return everything before the last `/` in `reference`, or `None`
/// if the reference is at the store root.
fn parent_path(reference: &str) -> Option<&str> {
    let idx = reference.rfind('/')?;
    Some(&reference[..idx])
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::datastore::CommandOutput;
    use std::sync::Mutex;

    /// `(executable, args, response)` tuple used by `ScriptedRunner`.
    type ScriptedResponse = (
        String,
        Vec<String>,
        std::result::Result<CommandOutput, String>,
    );

    /// Test runner: maps `(executable, args)` to a canned outcome.
    /// Records every call so probe-then-resolve flows can be
    /// asserted against.
    struct ScriptedRunner {
        responses: Mutex<Vec<ScriptedResponse>>,
        calls: Mutex<Vec<(String, Vec<String>)>>,
    }

    impl ScriptedRunner {
        fn new() -> Self {
            Self {
                responses: Mutex::new(Vec::new()),
                calls: Mutex::new(Vec::new()),
            }
        }
        fn expect(
            self,
            exe: impl Into<String>,
            args: Vec<String>,
            response: std::result::Result<CommandOutput, String>,
        ) -> Self {
            self.responses
                .lock()
                .unwrap()
                .push((exe.into(), args, response));
            self
        }
        fn calls(&self) -> Vec<(String, Vec<String>)> {
            self.calls.lock().unwrap().clone()
        }
    }

    impl CommandRunner for ScriptedRunner {
        fn run(&self, exe: &str, args: &[String]) -> Result<CommandOutput> {
            self.calls
                .lock()
                .unwrap()
                .push((exe.to_string(), args.to_vec()));
            let mut responses = self.responses.lock().unwrap();
            if responses.is_empty() {
                return Err(DodotError::Other(format!(
                    "ScriptedRunner: unexpected call to `{exe} {args:?}` — no responses queued"
                )));
            }
            let (expected_exe, expected_args, response) = responses.remove(0);
            assert_eq!(exe, expected_exe, "executable mismatch");
            assert_eq!(args, expected_args.as_slice(), "args mismatch");
            response.map_err(DodotError::Other)
        }
    }

    fn ok(stdout: &str) -> std::result::Result<CommandOutput, String> {
        Ok(CommandOutput {
            exit_code: 0,
            stdout: stdout.into(),
            stderr: String::new(),
        })
    }

    fn err(exit: i32, stderr: &str) -> std::result::Result<CommandOutput, String> {
        Ok(CommandOutput {
            exit_code: exit,
            stdout: String::new(),
            stderr: stderr.into(),
        })
    }

    fn make_store_dir(initialised: bool) -> tempfile::TempDir {
        let dir = tempfile::tempdir().unwrap();
        if initialised {
            std::fs::write(dir.path().join(".gpg-id"), "test@example.invalid\n").unwrap();
        }
        dir
    }

    #[test]
    fn scheme_is_pass() {
        let dir = make_store_dir(true);
        let p = PassProvider::new(Arc::new(ScriptedRunner::new()), dir.path().into());
        assert_eq!(p.scheme(), "pass");
    }

    #[test]
    fn resolve_returns_first_line_of_pass_show_output() {
        let dir = make_store_dir(true);
        let runner = Arc::new(ScriptedRunner::new().expect(
            "pass",
            vec!["show".into(), "personal/db".into()],
            ok("hunter2\nuser: alice\nurl: https://db.example\n"),
        ));
        let p = PassProvider::new(runner, dir.path().into());
        let s = p.resolve("personal/db").unwrap();
        assert_eq!(s.expose().unwrap(), "hunter2");
    }

    #[test]
    fn resolve_handles_value_without_trailing_newline() {
        let dir = make_store_dir(true);
        let runner = Arc::new(ScriptedRunner::new().expect(
            "pass",
            vec!["show".into(), "k".into()],
            ok("no-newline-at-end"),
        ));
        let p = PassProvider::new(runner, dir.path().into());
        assert_eq!(
            p.resolve("k").unwrap().expose().unwrap(),
            "no-newline-at-end"
        );
    }

    #[test]
    fn resolve_maps_not_in_store_to_actionable_error() {
        let dir = make_store_dir(true);
        let runner = Arc::new(ScriptedRunner::new().expect(
            "pass",
            vec!["show".into(), "missing/k".into()],
            err(1, "Error: missing/k is not in the password store."),
        ));
        let p = PassProvider::new(runner, dir.path().into());
        let e = p.resolve("missing/k").unwrap_err().to_string();
        assert!(e.contains("`pass:missing/k` not found"));
        // Lists the parent path so the user can run `pass ls <parent>`
        // to spot a typo.
        assert!(e.contains("`pass ls missing`"));
    }

    #[test]
    fn resolve_other_failures_include_stderr_verbatim() {
        let dir = make_store_dir(true);
        let runner = Arc::new(ScriptedRunner::new().expect(
            "pass",
            vec!["show".into(), "k".into()],
            err(2, "gpg: decryption failed: No secret key"),
        ));
        let p = PassProvider::new(runner, dir.path().into());
        let e = p.resolve("k").unwrap_err().to_string();
        assert!(e.contains("decryption failed"));
        assert!(e.contains("(exit 2)"));
    }

    #[test]
    fn resolve_rejects_empty_reference() {
        let dir = make_store_dir(true);
        let p = PassProvider::new(Arc::new(ScriptedRunner::new()), dir.path().into());
        let e = p.resolve("").unwrap_err().to_string();
        assert!(e.contains("empty"));
    }

    #[test]
    fn resolve_rejects_dotdot_reference() {
        let dir = make_store_dir(true);
        let p = PassProvider::new(Arc::new(ScriptedRunner::new()), dir.path().into());
        let e = p.resolve("../escape").unwrap_err().to_string();
        assert!(e.contains("path-traversal"));
    }

    #[test]
    fn resolve_rejects_dotdot_in_middle_segment() {
        let dir = make_store_dir(true);
        let p = PassProvider::new(Arc::new(ScriptedRunner::new()), dir.path().into());
        let e = p.resolve("foo/../escape").unwrap_err().to_string();
        assert!(e.contains("path-traversal"));
    }

    #[test]
    fn resolve_accepts_double_dot_inside_a_segment() {
        // `foo..bar` is a legitimate pass entry name (two consecutive
        // dots within one segment, not a `..` path segment). The
        // validator must let it through; the runner answers with the
        // entry's value.
        let dir = make_store_dir(true);
        let runner = Arc::new(ScriptedRunner::new().expect(
            "pass",
            vec!["show".into(), "service-foo..staging".into()],
            ok("hunter2\n"),
        ));
        let p = PassProvider::new(runner, dir.path().into());
        let v = p.resolve("service-foo..staging").unwrap();
        assert_eq!(v.expose().unwrap(), "hunter2");
    }

    #[test]
    fn probe_ok_when_binary_present_and_store_initialised() {
        let dir = make_store_dir(true);
        let runner = Arc::new(ScriptedRunner::new().expect(
            "pass",
            vec!["version".into()],
            ok("=============================================\n= pass: the standard unix password manager =\n"),
        ));
        let p = PassProvider::new(runner.clone(), dir.path().into());
        assert!(matches!(p.probe(), ProbeResult::Ok));
        assert_eq!(runner.calls().len(), 1);
    }

    #[test]
    fn probe_not_installed_when_runner_errors() {
        let dir = make_store_dir(true);
        let runner = Arc::new(ScriptedRunner::new().expect(
            "pass",
            vec!["version".into()],
            Err("command not found: pass".into()),
        ));
        let p = PassProvider::new(runner, dir.path().into());
        match p.probe() {
            ProbeResult::NotInstalled { hint } => {
                assert!(hint.contains("install pass"));
                assert!(hint.contains("apt install"));
                assert!(hint.contains("brew install"));
            }
            other => panic!("expected NotInstalled, got {other:?}"),
        }
    }

    #[test]
    fn probe_misconfigured_when_store_uninitialised() {
        let dir = make_store_dir(false); // no .gpg-id
        let runner = Arc::new(ScriptedRunner::new().expect(
            "pass",
            vec!["version".into()],
            ok("pass v1.7\n"),
        ));
        let p = PassProvider::new(runner, dir.path().into());
        match p.probe() {
            ProbeResult::Misconfigured { hint } => {
                assert!(hint.contains("not initialised"));
                assert!(hint.contains("pass init"));
                assert!(hint.contains("PASSWORD_STORE_DIR"));
            }
            other => panic!("expected Misconfigured, got {other:?}"),
        }
    }

    #[test]
    fn probe_failed_on_nonzero_version_exit() {
        let dir = make_store_dir(true);
        let runner =
            Arc::new(ScriptedRunner::new().expect("pass", vec!["version".into()], err(127, "")));
        let p = PassProvider::new(runner, dir.path().into());
        match p.probe() {
            ProbeResult::ProbeFailed { details } => {
                assert!(details.contains("non-zero exit"));
            }
            other => panic!("expected ProbeFailed, got {other:?}"),
        }
    }

    #[test]
    fn parent_path_strips_last_segment() {
        assert_eq!(parent_path("a/b/c"), Some("a/b"));
        assert_eq!(parent_path("a/b"), Some("a"));
        assert_eq!(parent_path("a"), None);
        assert_eq!(parent_path(""), None);
    }
}