devsandbox 0.11.1

Manage devcontainer-style sandboxes on docker, podman, or Apple container — many throwaway instances per repo from one TOML config.
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
//! The member key at rest (docs/architecture/daemon/fleet.md, *Member
//! identity*): no container devsandbox creates or image it builds may reach
//! `member.key` (nor its pending `member.key.next` or its `member.key.lock`),
//! nor the credential broker's files beside it
//! (`daemon_config::credential_files`: `credentials.toml`, the file store,
//! `access.json`, `member-ca.key`, `member-ca.toml`, and their locks).
//! Unlike the worker policy (`worker_policy`), this holds on every host,
//! for every config and trust level, and no allowance lifts it: a bind whose
//! source is one of those files or a dir above them (`$HOME`, the data dir,
//! `/`) is refused, and so is a build whose context, dockerfile,
//! `--build-context`, `--secret` or `--ssh` path covers them (the runtime
//! reads all of it).
//!
//! Checked over the final runtime argv right before it runs ([`check_argv`]),
//! like the worker policy's authority, and over a config's `mounts` before
//! their missing sources are created on the host ([`check_mount`]).
//! Cross-platform: the key's path is `daemon_config::member_key`'s, not
//! `serve`'s.

use std::fmt;
use std::path::{Component, Path, PathBuf};

use anyhow::Result;

use crate::worker_policy::{self, Arg};

/// A host path the runtime would hand a container or a build that covers a
/// member key file.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Exposure {
    /// What the path is (`bind mount of`, `build context`, …).
    pub what: &'static str,
    /// The path as the argv names it.
    pub source: String,
    /// The guarded file it covers.
    pub key: PathBuf,
    /// What that file is, for the message.
    pub guards: &'static str,
}

impl fmt::Display for Exposure {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "refused: {} `{}` covers `{}`; {} must never be reachable from a container (use a narrower \
             path that doesn't contain it)",
            self.what,
            self.source,
            self.key.display(),
            self.guards
        )
    }
}

impl std::error::Error for Exposure {}

const MEMBER_KEY: &str = "this member's private key (fleet identity)";
const CREDENTIALS: &str = "this host's credentials (credential broker)";

/// The files to keep out, each as written and as resolved (a symlinked
/// data dir is caught either way) with what it is, and `$HOME` for a `~`
/// source.
#[derive(Debug, Clone, Default)]
pub struct Guard {
    keys: Vec<(PathBuf, PathBuf, &'static str)>,
    home: Option<PathBuf>,
}

impl Guard {
    /// The guard over the process env (`$XDG_DATA_HOME` / `$HOME`). With
    /// neither set there's no data dir, so no key there to guard.
    pub fn from_env() -> Guard {
        let home = std::env::var_os("HOME").map(PathBuf::from);
        match crate::daemon_config::data_dir_from(std::env::var_os("XDG_DATA_HOME"), home.clone().map(Into::into)) {
            Ok(data) => Guard::for_data(&data, home),
            Err(_) => Guard { keys: Vec::new(), home },
        }
    }

    /// The guard over the data dir `data` (`<data>/devsandbox`).
    pub fn for_data(data: &Path, home: Option<PathBuf>) -> Guard {
        let data = lexical(data);
        let key = crate::daemon_config::member_key(&data);
        let keys = crate::daemon_config::member_key_files(&key)
            .into_iter()
            .map(|k| (k, MEMBER_KEY))
            .chain(crate::daemon_config::credential_files(&data).into_iter().map(|k| (k, CREDENTIALS)))
            .map(|(k, guards)| {
                let resolved = worker_policy::resolve(&k).unwrap_or_else(|| k.clone());
                (k, resolved, guards)
            })
            .collect();
        Guard { keys, home }
    }

    /// A host path `source` the runtime gets as `what`: refused when it is a
    /// key file or a dir above one, as written (made absolute, `..` dropped
    /// lexically, as the runtime cleans it) or resolved (symlinks).
    pub fn check_path(&self, source: &str, what: &'static str) -> Result<(), Exposure> {
        for form in &source_forms(source, self.home.as_deref()) {
            for (key, resolved, guards) in &self.keys {
                if key.starts_with(form) || resolved.starts_with(form) {
                    return Err(Exposure { what, source: source.to_string(), key: key.clone(), guards });
                }
            }
        }
        Ok(())
    }

    /// A `--mount` value: a bind's source passes [`Self::check_path`]
    /// (volumes and tmpfs aren't host paths).
    pub fn check_mount(&self, value: &str) -> Result<(), Exposure> {
        let fields = worker_policy::mount_fields(value);
        match (fields.kind, fields.source) {
            ("bind", Some(source)) => self.check_path(source, "bind mount of"),
            _ => Ok(()),
        }
    }

    /// The runtime argv `argv` (`run …` or `build …`).
    pub fn check_argv(&self, argv: &[String]) -> Result<(), Exposure> {
        match argv.first().map(String::as_str) {
            Some("build") => self.check_build(argv),
            _ => self.check_run(argv),
        }
    }

    /// A `run` argv's binds (`-v`, `--volume`, `--mount`), up to the image.
    /// devsandbox writes every flag of it, so `worker_policy::args` knows
    /// which take a value; a malformed tail is left to the runtime.
    fn check_run(&self, argv: &[String]) -> Result<(), Exposure> {
        for arg in worker_policy::args(argv, worker_policy::RUN_SWITCHES, "runtime") {
            match arg {
                Ok(Arg::Flag("-v" | "--volume", Some(value))) => {
                    if let Some(source) = worker_policy::volume_source(value) {
                        self.check_path(source, "bind mount of")?;
                    }
                }
                Ok(Arg::Flag("--mount", Some(value))) => self.check_mount(value)?,
                Ok(Arg::Flag(..)) => {}
                Ok(Arg::Positional(_)) | Err(_) => break,
            }
        }
        Ok(())
    }

    /// A `build` argv's paths: the context (last: `services::build_args`
    /// puts it there), `-f`, `--build-context`, `--secret`'s `src`, `--ssh`'s
    /// files. `build.options` are free-form, so a flag's arity isn't known:
    /// each path flag is found by itself, not by walking the argv.
    fn check_build(&self, argv: &[String]) -> Result<(), Exposure> {
        let rest = argv.get(1..).unwrap_or_default();
        for (i, arg) in rest.iter().enumerate() {
            let (flag, inline) = worker_policy::split_flag(arg);
            let value = || inline.or_else(|| rest.get(i + 1).map(String::as_str));
            match flag {
                "-f" | "--file" => {
                    if let Some(file) = value().filter(|f| *f != "-") {
                        self.check_path(file, "build file")?;
                    }
                }
                "--build-context" => {
                    let source = value().and_then(|v| v.split_once('=')).map(|(_, s)| s).unwrap_or_default();
                    let source = source.strip_prefix("oci-layout://").unwrap_or(source);
                    if !source.is_empty() && !source.contains("://") {
                        self.check_path(source, "build context")?;
                    }
                }
                "--secret" => {
                    for part in value().unwrap_or_default().split(',') {
                        if let Some(("src" | "source", src)) = part.split_once('=') {
                            self.check_path(src, "build secret")?;
                        }
                    }
                }
                "--ssh" => {
                    if let Some((_, paths)) = value().and_then(|v| v.split_once('=')) {
                        for path in paths.split(',') {
                            self.check_path(path, "build ssh key")?;
                        }
                    }
                }
                _ => {}
            }
        }
        match rest.last().map(String::as_str) {
            Some(context) if context != "-" && !context.starts_with('-') && !context.contains("://") => {
                self.check_path(context, "build context")
            }
            _ => Ok(()),
        }
    }
}

/// [`Guard::check_argv`] over the process env, before `argv` runs.
pub fn check_argv(argv: &[String]) -> Result<()> {
    Guard::from_env().check_argv(argv)?;
    Ok(())
}

/// [`Guard::check_mount`] over the process env.
pub fn check_mount(value: &str) -> Result<()> {
    Guard::from_env().check_mount(value)?;
    Ok(())
}

/// The forms a host path `source` is checked in: as written (a leading `~`
/// expanded with `home`, made absolute, `..` dropped lexically, as the
/// runtime cleans it) and resolved (symlinks). Shared with `run`'s bind
/// checks (`commands::run::binds`).
pub(crate) fn source_forms(source: &str, home: Option<&Path>) -> Vec<PathBuf> {
    let expanded = match (source.strip_prefix('~'), home) {
        (Some(rest), Some(home)) if rest.is_empty() || rest.starts_with('/') => home.join(rest.trim_start_matches('/')),
        _ => PathBuf::from(source),
    };
    let written = lexical(&expanded);
    let resolved = [worker_policy::resolve(&written), worker_policy::resolve(&expanded)];
    std::iter::once(written).chain(resolved.into_iter().flatten()).collect()
}

/// `path` made absolute with `.` and `..` dropped lexically (as a runtime
/// cleans a bind source), no filesystem access.
pub(crate) fn lexical(path: &Path) -> PathBuf {
    let abs = std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf());
    let mut out = PathBuf::new();
    for c in abs.components() {
        match c {
            Component::ParentDir => {
                out.pop();
            }
            Component::CurDir => {}
            c => out.push(c),
        }
    }
    out
}

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

    /// A temp base with `home/.local/share/devsandbox` (the data dir) and a
    /// sibling `home/work`. Returns (base, home, data).
    fn fixture(tag: &str) -> (PathBuf, PathBuf, PathBuf) {
        let base = std::env::temp_dir().join(format!("dsv-keyguard-{tag}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&base);
        let home = base.join("home");
        let data = home.join(".local/share/devsandbox");
        std::fs::create_dir_all(&data).unwrap();
        std::fs::create_dir_all(home.join("work/repo")).unwrap();
        std::fs::write(data.join("member.key"), "x").unwrap();
        let base = base.canonicalize().unwrap();
        (base.clone(), base.join("home"), base.join("home/.local/share/devsandbox"))
    }

    fn argv(args: &[&str]) -> Vec<String> {
        args.iter().map(|s| s.to_string()).collect()
    }

    fn run(extra: &[&str]) -> Vec<String> {
        let mut v = argv(&["run", "-d", "--name", "devsandbox-x", "--init", "-w", "/w"]);
        v.extend(extra.iter().map(|s| s.to_string()));
        v.extend(argv(&["alpine", "sh", "-c", "-v", "/:/r"]));
        v
    }

    #[test]
    fn binds_covering_the_key_are_refused() {
        let (base, home, data) = fixture("binds");
        let g = Guard::for_data(&data, Some(home.clone()));
        let s = |p: &Path| p.display().to_string();
        let key = data.join("member.key");
        let covering = [
            s(&key),
            s(&data.join("member.key.next")),
            s(&data.join("member.key.lock")),
            s(&data),
            s(&home.join(".local/share")),
            s(&home),
            "/".to_string(),
            "~".to_string(),
            "~/.local".to_string(),
            format!("{}/work/../.local", s(&home)),
            format!("{}/gone/..", s(&data)),
        ];
        for source in &covering {
            for extra in [
                vec!["-v".to_string(), format!("{source}:/h")],
                vec!["--volume".to_string(), format!("{source}:/h:ro")],
                vec![format!("--volume={source}:/h")],
                vec!["--mount".to_string(), format!("type=bind,source={source},target=/h,readonly")],
                vec!["--mount".to_string(), format!("src={source},dst=/h")],
            ] {
                let extra: Vec<&str> = extra.iter().map(String::as_str).collect();
                let err = g.check_argv(&run(&extra)).unwrap_err();
                assert_eq!(err.source, *source, "{extra:?}");
                assert!(err.to_string().contains("private key (fleet identity)"), "{err}");
            }
        }
        // A symlink to the data dir (or above it) is caught where it points.
        let link = base.join("link");
        std::os::unix::fs::symlink(&data, &link).unwrap();
        let up = base.join("up");
        std::os::unix::fs::symlink(&home, &up).unwrap();
        for source in [s(&link), s(&up), format!("{}/.local", s(&up))] {
            let err = g.check_argv(&run(&["-v", &format!("{source}:/d")])).unwrap_err();
            assert!(err.key.ends_with("member.key"), "{source}: {err}");
        }
        // The data dir itself a symlink: the bind of its target is caught too.
        let real = base.join("real-data");
        std::fs::create_dir_all(&real).unwrap();
        let linked = base.join("linked/devsandbox");
        std::fs::create_dir_all(linked.parent().unwrap()).unwrap();
        std::os::unix::fs::symlink(&real, &linked).unwrap();
        let g2 = Guard::for_data(&linked, None);
        assert!(g2.check_argv(&run(&["-v", &format!("{}:/d", s(&real))])).is_err());
        assert!(g2.check_argv(&run(&["-v", &format!("{}:/d", s(&base.join("linked")))])).is_err());
        // Narrower or unrelated paths, volumes, tmpfs, the command's args.
        for extra in [
            vec!["-v".to_string(), format!("{}:/w", s(&home.join("work/repo")))],
            vec!["-v".to_string(), format!("{}:/w", s(&home.join("work")))],
            vec!["-v".to_string(), format!("{}:/f", s(&data.join("fleet/peer")))],
            vec!["-v".to_string(), format!("{}:/s", s(&data.join("seccomp-1.json")))],
            vec!["-v".to_string(), format!("{}:/k", s(&data.join("member.key.bak")))],
            vec!["-v".to_string(), "named:/data".to_string()],
            vec!["--mount".to_string(), "type=volume,source=v,target=/v".to_string()],
            vec!["--mount".to_string(), "type=tmpfs,target=/t".to_string()],
            vec!["-e".to_string(), "-v=/:/x".to_string(), "--label".to_string(), "/".to_string()],
        ] {
            let extra: Vec<&str> = extra.iter().map(String::as_str).collect();
            assert_eq!(g.check_argv(&run(&extra)), Ok(()), "{extra:?}");
        }
    }

    #[test]
    fn builds_covering_the_key_are_refused() {
        let (_, home, data) = fixture("build");
        let g = Guard::for_data(&data, Some(home.clone()));
        let h = home.display().to_string();
        let repo = home.join("work/repo").display().to_string();
        let df = format!("{repo}/Dockerfile");
        let ok = argv(&["build", "-t", "t", "-f", &df, "--build-arg", "A=1", "--squash", &repo]);
        assert_eq!(g.check_argv(&ok), Ok(()));
        let ctx_ok = argv(&["build", "-t", "t", "--build-context", "img=docker-image://alpine", "-f", &df, &repo]);
        assert_eq!(g.check_argv(&ctx_ok), Ok(()));
        let key = data.join("member.key").display().to_string();
        for (bad, what) in [
            (argv(&["build", "-t", "t", "-f", &df, &h]), "build context"),
            (argv(&["build", "-t", "t", "-f", &df, "--no-cache", "/"]), "build context"),
            (argv(&["build", "-t", "t", "-f", &key, &repo]), "build file"),
            (argv(&["build", "-t", "t", "--squash", "--build-context", &format!("x={h}"), "-f", &df, &repo]), "build context"),
            (argv(&["build", "-t", "t", &format!("--build-context=x=oci-layout://{h}"), &repo]), "build context"),
            (argv(&["build", "-t", "t", "--secret", &format!("id=k,src={key}"), &repo]), "build secret"),
            (argv(&["build", "-t", "t", "--ssh", &format!("default={key}"), &repo]), "build ssh key"),
        ] {
            let err = g.check_argv(&bad).unwrap_err();
            assert_eq!(err.what, what, "{bad:?}");
        }
    }

    #[test]
    fn credential_files_are_refused_like_the_key() {
        let (_, home, data) = fixture("creds");
        let g = Guard::for_data(&data, Some(home.clone()));
        let names = [
            "credentials.toml",
            "credentials.lock",
            "credentials-store.toml",
            "credentials-store.lock",
            "access.json",
            "member-ca.key",
            "member-ca.toml",
        ];
        for name in names {
            // Missing files too: reserved names are guarded before they exist.
            let path = data.join(name).display().to_string();
            let err = g.check_argv(&run(&["-v", &format!("{path}:/c:ro")])).unwrap_err();
            assert_eq!(err.key, data.join(name));
            assert!(err.to_string().contains("this host's credentials (credential broker)"), "{err}");
            let err = g.check_argv(&argv(&["build", "-t", "t", "--secret", &format!("id=c,src={path}"), "/x"])).unwrap_err();
            assert_eq!(err.what, "build secret");
        }
        // A dir above them names the member key first.
        let err = g.check_argv(&run(&["-v", &format!("{}:/d", data.display())])).unwrap_err();
        assert!(err.key.ends_with("member.key"), "{err}");
        for ok in ["credentials.toml.bak", "access.log", "credentials"] {
            let path = data.join(ok).display().to_string();
            assert_eq!(g.check_argv(&run(&["-v", &format!("{path}:/c")])), Ok(()), "{ok}");
        }
    }

    /// The guard reads no fleet root and no `member.toml`: a plain local
    /// config's dir and `trust = "full"` change nothing.
    #[test]
    fn it_holds_without_a_fleet_root_and_under_full() {
        let (_, home, data) = fixture("full");
        std::fs::write(data.join("member.toml"), "trust = \"full\"\n[allow]\nbinds = [\"/\"]\nruntime-socket = true\n").unwrap();
        let cfg = home.join("work");
        assert!(worker_policy::Policy::detect(&data, &cfg, Vec::new()).unwrap().is_none());
        let g = Guard::for_data(&data, Some(home.clone()));
        assert!(g.check_argv(&run(&["-v", &format!("{}:/h", home.display())])).is_err());
        assert!(g.check_mount(&format!("type=bind,source={},target=/h", data.display())).is_err());
        // No data dir (neither env var): nothing to guard.
        assert_eq!(Guard::default().check_argv(&run(&["-v", "/:/r"])), Ok(()));
    }
}