devsandbox 0.10.0

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
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
//! The worker policy (docs/architecture/daemon/fleet.md, *Worker policy*):
//! what a config under a fleet root (`<data>/devsandbox/fleet/<peer>/`, the
//! root a link peer reaches on this host) may ask of this host, beyond
//! containers that see only what was placed there. Denied by default; each item has
//! an allowance in `member.toml`'s `[allow]` table ([`Allow`]); none of it
//! applies under `trust = "full"` ([`Trust`]).
//!
//! A config is under the policy by its path, whatever process runs it (a
//! daemon's verb subprocess for a peer, a local CLI pointed at the dir): every
//! entry point calls [`Policy::for_dir`] itself, so there is no flag to
//! forget. The authority is the check of the final runtime argv
//! ([`Policy::check_run`], [`Policy::check_build`]) right before it runs, so
//! nothing the pipeline adds after the config (features, identity, caches)
//! escapes it: a flag that isn't on its allow list is refused. The config
//! checks ([`Policy::check_config`], [`Policy::check_path`]) refuse earlier,
//! before anything is done on the host, and cover what never reaches a
//! container's argv (`initializeCommand`, `dispatcher`, `inbox`,
//! `${localEnv:…}`, which `config::resolve_var` also never expands for a
//! name [`local_env_allowed`] refuses).
//!
//! Cross-platform (no `serve`): `member.toml` is read here for its `trust`
//! and `[allow]` only (the rest is `serve::fleet::member`'s).

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

use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};

use crate::config::{Config, GpuNeed, ResolvedSandbox, ResolvedService, ServiceScope};
use crate::runtime::NAME_PREFIX;

/// `member.toml`'s `trust`: what a remote caller of this member's daemon
/// reaches (docs/architecture/daemon/fleet.md, *Trust*). Here, not in
/// `serve::fleet::member`, because the policy reads it on every platform.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Trust {
    /// The member's own roots and the fleet root, placed work under the
    /// worker policy.
    #[default]
    Worker,
    /// The whole API, as a local client has it: no worker policy.
    Full,
}

/// `member.toml`'s `[allow]`: what the worker policy permits beyond its
/// defaults. Every allowance is off (empty) unless set. Unknown keys are
/// ignored: an allowance a newer binary knows is one an older one doesn't
/// grant.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct Allow {
    /// `--privileged`: the sandbox's `privileged` or a feature's.
    #[serde(skip_serializing_if = "is_false")]
    pub privileged: bool,
    /// A bind of a container runtime's socket (or of a dir holding it).
    #[serde(skip_serializing_if = "is_false")]
    pub runtime_socket: bool,
    /// `--network host` (`run` or `build`).
    #[serde(skip_serializing_if = "is_false")]
    pub host_network: bool,
    /// `scope = "global"` services.
    #[serde(skip_serializing_if = "is_false")]
    pub global_services: bool,
    /// `dispatcher`: control ops that create children on this host.
    #[serde(skip_serializing_if = "is_false")]
    pub dispatcher: bool,
    /// `inbox = true`: Inbox threads on this host.
    #[serde(skip_serializing_if = "is_false")]
    pub inbox: bool,
    /// `initializeCommand`, which runs on this host, outside any container.
    #[serde(skip_serializing_if = "is_false")]
    pub initialize_command: bool,
    /// A published port (`-p`) on more than loopback.
    #[serde(skip_serializing_if = "is_false")]
    pub publish: bool,
    /// `hostRequirements.gpu`: every NVIDIA GPU (`--gpus all`,
    /// `--device nvidia.com/gpu=all`).
    #[serde(skip_serializing_if = "is_false")]
    pub gpus: bool,
    /// Names of this host's env vars a config may read with
    /// `${localEnv:NAME}` (exact names; the control plane writes the config, so any
    /// other would hand it this host's env).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub local_env: Vec<String>,
    /// Host paths outside the fleet root that binds, build contexts,
    /// `folder`s and local features may use (absolute; a path covers what's
    /// under it).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub binds: Vec<PathBuf>,
}

fn is_false(b: &bool) -> bool {
    !*b
}

impl Allow {
    /// `binds` entries must be absolute.
    pub fn check(&self) -> Result<()> {
        if let Some(bad) = self.binds.iter().find(|b| !b.is_absolute()) {
            bail!("`allow.binds`: `{}` is not an absolute path", bad.display());
        }
        Ok(())
    }
}

/// Why the policy refused: what was asked and the allowance that permits it
/// (`None`: none does).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Refusal {
    pub what: String,
    pub allow: Option<String>,
}

impl Refusal {
    pub fn new(what: impl Into<String>, allow: impl Into<String>) -> Self {
        Refusal { what: what.into(), allow: Some(allow.into()) }
    }

    fn always(what: impl Into<String>) -> Self {
        Refusal { what: what.into(), allow: None }
    }
}

impl fmt::Display for Refusal {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "refused by this machine's worker policy: {}", self.what)?;
        match &self.allow {
            Some(allow) => write!(f, " (allow with {allow} under [allow] in member.toml)"),
            None => write!(f, " (no member.toml allowance permits it)"),
        }
    }
}

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

/// The policy over one fleet root.
#[derive(Debug, Clone)]
pub struct Policy {
    /// The canonical fleet root (`<data>/devsandbox/fleet/<peer>`), or the
    /// fleet dir itself for a config placed directly in it.
    pub root: PathBuf,
    /// The canonical `<data>/devsandbox`: where devsandbox's own seccomp
    /// profiles live.
    pub data: PathBuf,
    pub allow: Allow,
    /// The runtimes' socket paths a bind must not reach ([`runtime_sockets`]).
    pub sockets: Vec<PathBuf>,
}

/// Socket file names refused wherever they are.
const SOCKET_NAMES: [&str; 3] = ["docker.sock", "podman.sock", "containerd.sock"];

/// The runtime socket paths of this host: the usual ones, `DOCKER_HOST`'s
/// and `CONTAINER_HOST`'s unix paths, and the per-user ones under
/// `$XDG_RUNTIME_DIR` and `$HOME`.
pub fn runtime_sockets(
    docker_host: Option<&str>,
    container_host: Option<&str>,
    xdg_runtime_dir: Option<&Path>,
    home: Option<&Path>,
) -> Vec<PathBuf> {
    let mut out: Vec<PathBuf> =
        ["/var/run/docker.sock", "/run/docker.sock", "/run/podman/podman.sock", "/var/run/podman/podman.sock"]
            .into_iter()
            .map(PathBuf::from)
            .collect();
    for host in [docker_host, container_host].into_iter().flatten() {
        if let Some(path) = host.strip_prefix("unix://") {
            out.push(PathBuf::from(path));
        }
    }
    if let Some(run) = xdg_runtime_dir {
        out.extend([run.join("docker.sock"), run.join("podman/podman.sock")]);
    }
    if let Some(home) = home {
        out.extend([home.join(".docker/run/docker.sock"), home.join(".colima/default/docker.sock")]);
    }
    out
}

/// [`runtime_sockets`] over the process env.
pub fn host_sockets() -> Vec<PathBuf> {
    let var = |k: &str| std::env::var(k).ok();
    let os = |k: &str| std::env::var_os(k).map(PathBuf::from);
    runtime_sockets(
        var("DOCKER_HOST").as_deref(),
        var("CONTAINER_HOST").as_deref(),
        os("XDG_RUNTIME_DIR").as_deref(),
        os("HOME").as_deref(),
    )
}

/// The fleet root `dir` is under, from the canonical `<data>/devsandbox`
/// (`data`): `<data>/fleet/<first component of dir's path below it>`, or
/// the fleet dir itself for `dir` = it. `None` when `dir` (canonical) isn't
/// under it; symlinks count where they point.
/// A `dir` that doesn't exist (nothing runs from it) is placed by its path
/// as written, made absolute; one with a `..` there is an error.
pub fn fleet_root_of(data: &Path, dir: &Path) -> Result<Option<PathBuf>> {
    let fleet = data.join("fleet");
    let (dir, fleet) = match dir.canonicalize() {
        Ok(dir) => match fleet.canonicalize() {
            Ok(fleet) => (dir, fleet),
            Err(_) => return Ok(None), // no fleet dir: nothing can be in it.
        },
        Err(_) => {
            let abs = std::path::absolute(dir).with_context(|| format!("cannot resolve {}", dir.display()))?;
            if abs.components().any(|c| c == Component::ParentDir) {
                bail!("cannot resolve {}", dir.display());
            }
            (abs, fleet.canonicalize().unwrap_or(fleet))
        }
    };
    let Ok(rel) = dir.strip_prefix(&fleet) else {
        return Ok(None);
    };
    Ok(Some(match rel.components().next() {
        Some(first) => fleet.join(first),
        None => fleet,
    }))
}

/// `trust` and `[allow]` of the `member.toml` at `path`; missing is the
/// defaults (`worker`, nothing allowed). Only those are read (the rest is
/// `serve::fleet::member`'s), so a file that's otherwise invalid still
/// yields them; one whose `trust` or `[allow]` doesn't parse is an error,
/// never the defaults' silent stand-in.
pub fn load_member(path: &Path) -> Result<(Trust, Allow)> {
    #[derive(Deserialize)]
    struct File {
        #[serde(default)]
        trust: Trust,
        #[serde(default)]
        allow: Allow,
    }
    match std::fs::read_to_string(path) {
        Ok(text) => {
            let file: File = toml::from_str(&text).with_context(|| format!("invalid {}", path.display()))?;
            file.allow.check().with_context(|| format!("invalid {}", path.display()))?;
            Ok((file.trust, file.allow))
        }
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok((Trust::default(), Allow::default())),
        Err(e) => Err(e).with_context(|| format!("cannot read {}", path.display())),
    }
}

impl Policy {
    /// The policy for config dir `dir` over the process env (`$XDG_DATA_HOME`
    /// / `$HOME`, the runtime env vars); `None` when `dir` is under no fleet
    /// root.
    pub fn for_dir(dir: &Path) -> Result<Option<Policy>> {
        let data = crate::daemon_config::data_dir_from(std::env::var_os("XDG_DATA_HOME"), std::env::var_os("HOME"))?;
        Self::detect(&data, dir, host_sockets())
    }

    /// [`Self::for_dir`] over a given `<data>/devsandbox` and socket list.
    /// `None` too under `trust = "full"`: no worker policy then (fleet
    /// plan decision 9).
    pub fn detect(data: &Path, dir: &Path, sockets: Vec<PathBuf>) -> Result<Option<Policy>> {
        let data = data.canonicalize().unwrap_or_else(|_| data.to_path_buf());
        let Some(root) = fleet_root_of(&data, dir)? else {
            return Ok(None);
        };
        let (trust, allow) = load_member(&data.join("member.toml"))?;
        if trust == Trust::Full {
            return Ok(None);
        }
        Ok(Some(Policy { root, data, allow, sockets }))
    }

    /// What the resolved sandbox `sandbox` of `config` (at `dir`) declares
    /// that never shows in a container's argv or is done on the host before
    /// one: `initializeCommand`, `dispatcher`, `inbox`, `privileged`, a
    /// required GPU, the sandbox's or a service's (an optional one runs
    /// without), `global` services, local features outside the fleet root (or with a file
    /// resolving outside it), and `${localEnv:NAME}` in the merged sandbox
    /// or its services for a name `local-env` doesn't list.
    pub fn check_config(&self, dir: &Path, config: &Config, sandbox: &ResolvedSandbox) -> Result<(), Refusal> {
        match self.config_refusals(dir, config, sandbox).into_iter().next() {
            Some(r) => Err(r),
            None => Ok(()),
        }
    }

    /// Every refusal of [`Self::check_config`], in its order (one per
    /// `${localEnv:…}` name, the first per local feature): what ties a
    /// sandbox to its host (`fleet::code::ties`) lists them all.
    pub fn config_refusals(&self, dir: &Path, config: &Config, sandbox: &ResolvedSandbox) -> Vec<Refusal> {
        let mut out = Vec::new();
        let props = &sandbox.properties;
        let a = &self.allow;
        let flags = [
            (props.initialize_command.is_some(), a.initialize_command, "`initializeCommand`", "initialize-command"),
            (props.dispatcher.is_some(), a.dispatcher, "`dispatcher`", "dispatcher"),
            (props.inbox == Some(true), a.inbox, "`inbox`", "inbox"),
            (props.privileged == Some(true), a.privileged, "privileged", "privileged"),
            (
                props.host_requirements.as_ref().and_then(|h| h.gpu_need()) == Some(GpuNeed::Required),
                a.gpus,
                "a required GPU (`hostRequirements.gpu`)",
                "gpus",
            ),
        ];
        for (asked, allowed, what, key) in flags {
            if asked && !allowed {
                out.push(Refusal::new(what, format!("{key} = true")));
            }
        }
        // `${localEnv:…}` anywhere in the merged sandbox or its services,
        // before anything is substituted.
        let mut texts = Vec::new();
        let merged = config.resolved_table(&sandbox.name).map(|(t, _)| t).unwrap_or_default();
        table_strings(&merged, &mut texts);
        for name in props.services.iter().flatten() {
            if let Some(table) = config.services.get(name) {
                table_strings(table, &mut texts);
            }
        }
        let mut names: Vec<&str> = Vec::new();
        for name in texts.iter().flat_map(|t| local_env_names(t)) {
            if !names.contains(&name) {
                names.push(name);
            }
        }
        for name in names {
            if !self.allows_local_env(name) {
                out.push(Refusal::new(
                    format!("`${{localEnv:{name}}}` (this host's env)"),
                    format!("local-env = [\"{name}\"]"),
                ));
            }
        }
        for name in props.services.iter().flatten() {
            // An unknown service fails later with its own error.
            if let Ok(service) = config.resolve_service(name) {
                out.extend(self.check_service(&service).err());
                let gpu = service.spec.host_requirements.as_ref().and_then(|h| h.gpu_need());
                if gpu == Some(GpuNeed::Required) && !a.gpus {
                    out.push(Refusal::new(
                        format!("service `{name}`'s required GPU (`hostRequirements.gpu`)"),
                        "gpus = true",
                    ));
                }
            }
        }
        for reference in props.features.iter().flat_map(|f| f.keys()) {
            let Ok(source) = crate::features::FeatureSource::parse(reference) else { continue };
            if let Some(local) = source.local_dir(dir) {
                out.extend(self.check_tree(&local, &format!("local feature `{reference}`")).err());
            }
        }
        out
    }

    /// Whether `allow.local-env` lists `var`'s name (up to a `:`).
    pub fn allows_local_env(&self, var: &str) -> bool {
        let name = var.split(':').next().unwrap_or_default();
        self.allow.local_env.iter().any(|n| n == name)
    }

    /// A service's `scope = "global"`.
    pub fn check_service(&self, service: &ResolvedService) -> Result<(), Refusal> {
        if service.spec.scope == ServiceScope::Global && !self.allow.global_services {
            return Err(Refusal::new(format!("global service `{}`", service.name), "global-services = true"));
        }
        Ok(())
    }

    /// A host path the host reads or a container sees (`what` names it):
    /// resolved (symlinks, `..`) under the fleet root or an `allow.binds`
    /// entry.
    pub fn check_path(&self, path: &Path, what: &str) -> Result<(), Refusal> {
        let resolved = resolve(path).ok_or_else(|| Refusal::always(format!("{what} `{}` (cannot resolve)", path.display())))?;
        if resolved.starts_with(&self.root) || self.in_binds(&resolved) {
            return Ok(());
        }
        Err(Refusal::new(
            format!("{what} `{}` (outside the fleet root)", path.display()),
            format!("binds = [\"{}\"]", resolved.display()),
        ))
    }

    /// [`Self::check_path`] over `dir` and everything under it, each entry
    /// resolved where it points (a symlink out of the tree is refused). Real
    /// subdirs are walked; a missing `dir` is left to its user to report.
    fn check_tree(&self, dir: &Path, what: &str) -> Result<(), Refusal> {
        self.check_path(dir, what)?;
        let Ok(entries) = std::fs::read_dir(dir) else { return Ok(()) };
        for entry in entries.flatten() {
            let path = entry.path();
            self.check_path(&path, what)?;
            if entry.file_type().is_ok_and(|t| t.is_dir()) {
                self.check_tree(&path, what)?;
            }
        }
        Ok(())
    }

    fn in_binds(&self, resolved: &Path) -> bool {
        self.allow.binds.iter().filter_map(|b| resolve(b)).any(|b| resolved.starts_with(b))
    }

    /// A bind's host source: a runtime socket (by name, as one of
    /// [`Self::sockets`], or a dir holding one) needs `runtime-socket`;
    /// anything else must pass [`Self::check_path`]. A source through a
    /// config root's `shared-volumes/` or `shared-files/` needs no allowance:
    /// what it resolves to must only be under the fleet root, like any
    /// other. What a container can plant in those dirs is guarded host-side
    /// (`commands::run::mounts::guard_create`, `worktree-link` skipped under
    /// a policy).
    pub fn check_bind(&self, source: &str) -> Result<(), Refusal> {
        let path = Path::new(source);
        let what = "bind mount of";
        let resolved =
            resolve(path).ok_or_else(|| Refusal::always(format!("{what} `{source}` (cannot resolve)")))?;
        let named = |p: &Path| p.file_name().and_then(|n| n.to_str()).is_some_and(|n| SOCKET_NAMES.contains(&n));
        let socket = named(path)
            || named(&resolved)
            || self.sockets.iter().any(|s| {
                let s = resolve(s).unwrap_or_else(|| s.clone());
                s.starts_with(&resolved)
            });
        if socket && !self.allow.runtime_socket {
            return Err(Refusal::new(format!("runtime socket mount `{source}`"), "runtime-socket = true"));
        }
        self.check_path(path, what)
    }

    /// A `--mount` value: `type` `bind` (the default here, stricter than
    /// docker's), `volume` (named or anonymous) or `tmpfs`, with only the
    /// keys devsandbox writes; a bind's source passes [`Self::check_bind`].
    /// Driver options (`volume-opt`, which can bind host paths) are refused.
    pub fn check_mount(&self, value: &str) -> Result<(), Refusal> {
        let MountFields { kind, source, unknown } = mount_fields(value);
        if let Some(part) = unknown {
            return Err(Refusal::always(format!("mount option `{part}` in `{value}`")));
        }
        match kind {
            "bind" => match source {
                Some(source) => self.check_bind(source),
                None => Err(Refusal::always(format!("bind mount without a source `{value}`"))),
            },
            "volume" | "tmpfs" => Ok(()),
            other => Err(Refusal::always(format!("mount type `{other}` in `{value}`"))),
        }
    }

    /// A `-v` value, `src:dst[:opts]`: an absolute (or `.`-relative) `src`
    /// is a bind ([`Self::check_bind`]), any other a named volume; options
    /// only `ro`, `rw`, `z`, `Z`.
    pub fn check_volume(&self, value: &str) -> Result<(), Refusal> {
        if let Some(opts) = value.splitn(3, ':').nth(2) {
            if let Some(bad) = opts.split(',').find(|o| !matches!(*o, "ro" | "rw" | "z" | "Z")) {
                return Err(Refusal::always(format!("volume option `{bad}` in `{value}`")));
            }
        }
        match volume_source(value) {
            Some(src) => self.check_bind(src),
            None => Ok(()),
        }
    }

    /// The final `run` argv of a container (sandbox or service), before it
    /// reaches the runtime. Flags devsandbox writes, and only those, pass,
    /// with their values checked: `--privileged`, binds (`-v`, `--mount`),
    /// `--network` (devsandbox's networks; `host` needs `host-network`),
    /// `--userns` (`keep-id…`), `--security-opt` (devsandbox's own seccomp
    /// profile), `--gpus all` / `--device nvidia.com/gpu=all` (`gpus`).
    /// Every other flag (`--cap-add`, any other `--device`, `--pid`,
    /// `--ipc`, …) is refused. The first positional is the image; what
    /// follows is its command.
    pub fn check_run(&self, argv: &[String]) -> Result<(), Refusal> {
        match argv.first().map(String::as_str) {
            Some("run") => {}
            other => return Err(Refusal::always(format!("runtime command `{}`", other.unwrap_or_default()))),
        }
        for arg in args(argv, RUN_SWITCHES, "runtime") {
            let (flag, inline) = match arg? {
                Arg::Positional(_) => return Ok(()), // the image
                Arg::Flag(flag, value) => (flag, value),
            };
            // A flag outside `RUN_SWITCHES` always has its value here.
            let value = || Ok::<_, Refusal>(inline.unwrap_or_default().to_string());
            match flag {
                "-d" | "--detach" | "--init" if inline.is_none() => {}
                "--privileged" if inline.is_none() => {
                    if !self.allow.privileged {
                        return Err(Refusal::new("privileged", "privileged = true"));
                    }
                }
                "--name" | "--label" | "-w" | "--workdir" | "-e" | "--env" | "--user" | "-u" | "--restart"
                | "--network-alias" => {
                    value()?;
                }
                "-p" | "--publish" => {
                    let v = value()?;
                    if !loopback_bound(&v) && !self.allow.publish {
                        return Err(Refusal::new(format!("published port `{v}` (beyond loopback)"), "publish = true"));
                    }
                }
                "--userns" => {
                    let v = value()?;
                    if !v.starts_with("keep-id") {
                        return Err(Refusal::always(format!("`--userns {v}`")));
                    }
                }
                "--security-opt" => {
                    let v = value()?;
                    if !self.own_seccomp(&v) {
                        return Err(Refusal::always(format!("`--security-opt {v}`")));
                    }
                }
                "--network" | "--net" => {
                    let v = value()?;
                    self.check_network(&v)?;
                }
                "--gpus" | "--device" => {
                    let v = value()?;
                    let gpus = matches!((flag, v.as_str()), ("--gpus", "all") | ("--device", "nvidia.com/gpu=all"));
                    if !gpus {
                        return Err(Refusal::always(format!("`{flag} {v}`")));
                    }
                    if !self.allow.gpus {
                        return Err(Refusal::new("GPUs", "gpus = true"));
                    }
                }
                "-v" | "--volume" => self.check_volume(&value()?)?,
                "--mount" => self.check_mount(&value()?)?,
                _ => return Err(Refusal::always(format!("runtime flag `{flag}`"))),
            }
        }
        Err(Refusal::always("a `run` without an image"))
    }

    /// The `build` argv of a sandbox's or service's `build` (the feature
    /// image's own builds use a context devsandbox generates): the
    /// dockerfile and the context pass [`Self::check_path`]; `--network host`
    /// needs `host-network`; flags beyond `-t`, `-f`, `--build-arg`,
    /// `--target`, `--cache-from`, `--platform`, `--progress`, `--pull`,
    /// `--no-cache`, `-q` (what `build.options` might add: `--secret`,
    /// `--ssh`, `--build-context`, `--allow`, …) are refused.
    pub fn check_build(&self, argv: &[String]) -> Result<(), Refusal> {
        match argv.first().map(String::as_str) {
            Some("build") => {}
            other => return Err(Refusal::always(format!("runtime command `{}`", other.unwrap_or_default()))),
        }
        let mut context = None;
        for arg in args(argv, BUILD_SWITCHES, "build") {
            let (flag, inline) = match arg? {
                Arg::Positional(arg) => {
                    if context.replace(arg).is_some() {
                        return Err(Refusal::always(format!("a second build context `{arg}`")));
                    }
                    continue;
                }
                Arg::Flag(flag, value) => (flag, value),
            };
            // A flag outside `BUILD_SWITCHES` always has its value here.
            let value = || Ok::<_, Refusal>(inline.unwrap_or_default().to_string());
            match flag {
                "--pull" | "--no-cache" | "-q" | "--quiet" if inline.is_none() => {}
                "-t" | "--tag" | "--build-arg" | "--target" | "--cache-from" | "--platform" | "--progress" => {
                    value()?;
                }
                "-f" | "--file" => self.check_path(Path::new(&value()?), "build file")?,
                "--network" => match value()?.as_str() {
                    "default" | "none" | "bridge" => {}
                    "host" if self.allow.host_network => {}
                    "host" => return Err(Refusal::new("`--network host` (build)", "host-network = true")),
                    other => return Err(Refusal::always(format!("build `--network {other}`"))),
                },
                _ => return Err(Refusal::always(format!("build flag `{flag}`"))),
            }
        }
        match context {
            Some("-") => Err(Refusal::always("a build context from stdin")),
            Some(context) => self.check_path(Path::new(context), "build context"),
            None => Err(Refusal::always("a build without a context")),
        }
    }

    fn check_network(&self, network: &str) -> Result<(), Refusal> {
        if network.starts_with(&format!("{NAME_PREFIX}net-")) {
            return Ok(());
        }
        if network == "host" {
            if self.allow.host_network {
                return Ok(());
            }
            return Err(Refusal::new("`--network host`", "host-network = true"));
        }
        Err(Refusal::always(format!("`--network {network}`")))
    }

    /// `seccomp=<path>` of a profile devsandbox writes: `seccomp-*.json`
    /// directly in the data dir (`runtime::seccomp`).
    fn own_seccomp(&self, opt: &str) -> bool {
        let Some(path) = opt.strip_prefix("seccomp=").map(Path::new) else { return false };
        let named = path
            .file_name()
            .and_then(|n| n.to_str())
            .is_some_and(|n| n.starts_with("seccomp-") && n.ends_with(".json"));
        named && resolve(path).and_then(|p| p.parent().map(|d| d == self.data)).unwrap_or(false)
    }
}

/// `--flag=value` as `("--flag", Some("value"))`; anything else as is.
pub(crate) fn split_flag(arg: &str) -> (&str, Option<&str>) {
    match arg.split_once('=') {
        Some((flag, value)) if arg.starts_with("--") => (flag, Some(value)),
        _ => (arg, None),
    }
}

/// The `run` flags devsandbox writes that take no value.
pub(crate) const RUN_SWITCHES: &[&str] = &["-d", "--detach", "--init", "--privileged"];

/// The `build` flags [`Policy::check_build`] allows that take no value.
pub(crate) const BUILD_SWITCHES: &[&str] = &["--pull", "--no-cache", "-q", "--quiet"];

/// One item of a runtime argv: a flag with its value, or a positional (`-`
/// included: stdin).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Arg<'a> {
    Flag(&'a str, Option<&'a str>),
    Positional(&'a str),
}

/// The items of `argv` after its command (`argv[0]`): `--flag=value` is a
/// flag with its value; any other flag takes the next arg as its value unless
/// it's one of `switches` (which keep only an inline one). A flag missing its
/// value is an error (`what` names the argv: `runtime`, `build`), after which
/// nothing more is yielded. The one walk both [`Policy::check_run`] /
/// [`Policy::check_build`] and `key_guard` read an argv with.
pub(crate) fn args<'a>(
    argv: &'a [String],
    switches: &'a [&'a str],
    what: &'a str,
) -> impl Iterator<Item = Result<Arg<'a>, Refusal>> + 'a {
    let mut it = argv.iter().skip(1);
    let mut failed = false;
    std::iter::from_fn(move || {
        if failed {
            return None;
        }
        let arg = it.next()?;
        if !arg.starts_with('-') || arg == "-" {
            return Some(Ok(Arg::Positional(arg)));
        }
        let (flag, inline) = split_flag(arg);
        if inline.is_some() || switches.contains(&flag) {
            return Some(Ok(Arg::Flag(flag, inline)));
        }
        Some(match it.next() {
            Some(value) => Ok(Arg::Flag(flag, Some(value))),
            None => {
                failed = true;
                Err(Refusal::always(format!("{what} flag `{flag}` without a value")))
            }
        })
    })
}

/// A `--mount` value's fields: its `type` (`bind` when absent, stricter than
/// docker's default), its `source`, and its first part outside the keys
/// devsandbox writes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct MountFields<'a> {
    pub kind: &'a str,
    pub source: Option<&'a str>,
    pub unknown: Option<&'a str>,
}

pub(crate) fn mount_fields(value: &str) -> MountFields<'_> {
    let (mut kind, mut source, mut unknown) = (None, None, None);
    for part in value.split(',').map(str::trim).filter(|p| !p.is_empty()) {
        let (key, v) = part.split_once('=').map_or((part, None), |(k, v)| (k.trim(), Some(v.trim())));
        match key {
            "type" => kind = v,
            "source" | "src" => source = v,
            "target" | "destination" | "dst" | "readonly" | "ro" | "consistency" => {}
            "relabel" if v == Some("shared") => {}
            _ => {
                unknown.get_or_insert(part);
            }
        }
    }
    MountFields { kind: kind.unwrap_or("bind"), source, unknown }
}

/// A `-v` value's (`src:dst[:opts]`) host source: an absolute (or `.`/`~`-
/// relative) `src`, a bind; `None` for any other, a named volume.
pub(crate) fn volume_source(value: &str) -> Option<&str> {
    let src = value.split(':').next().unwrap_or_default();
    (src.starts_with('/') || src.starts_with('.') || src.starts_with('~')).then_some(src)
}

/// `path` as the host resolves it: made absolute, its deepest existing
/// ancestor canonicalized (symlinks and `..` resolved), the rest appended.
/// `None` for a `..` in the part that doesn't exist yet, which can't be
/// resolved without it, and for an entry that is there but won't
/// canonicalize (a dangling or looping symlink): creating a file through a
/// dangling symlink creates its target, wherever it points.
pub(crate) fn resolve(path: &Path) -> Option<PathBuf> {
    let path = std::path::absolute(path).ok()?;
    let mut existing = path.as_path();
    let mut rest: Vec<&std::ffi::OsStr> = Vec::new();
    loop {
        if let Ok(canonical) = existing.canonicalize() {
            let mut out = canonical;
            for name in rest.iter().rev() {
                out.push(name);
            }
            return Some(out);
        }
        if existing.symlink_metadata().is_ok() {
            return None;
        }
        match existing.components().next_back() {
            Some(Component::Normal(name)) => rest.push(name),
            Some(Component::CurDir) => {}
            _ => return None,
        }
        existing = existing.parent()?;
    }
}

/// Whether `${localEnv:<var>}` may expand for the config at `dir`: always
/// outside a fleet root; under one only for a name (`var` up to a `:`) in
/// `allow.local-env`. An error finding the policy is a no.
pub fn local_env_allowed(dir: &Path, var: &str) -> bool {
    match Policy::for_dir(dir) {
        Ok(None) => true,
        Ok(Some(policy)) => policy.allows_local_env(var),
        Err(_) => false,
    }
}

/// The `${localEnv:…}` names `text` references (`NAME` of
/// `${localEnv:NAME}` and `${localEnv:NAME:default}`).
fn local_env_names(text: &str) -> Vec<&str> {
    let mut out = Vec::new();
    let mut rest = text;
    while let Some(start) = rest.find("${localEnv:") {
        let after = &rest[start + "${localEnv:".len()..];
        let end = after.find('}').unwrap_or(after.len());
        out.push(after[..end].split(':').next().unwrap_or_default());
        rest = &after[end..];
    }
    out
}

/// Every string in `value`, keys included.
fn strings<'a>(value: &'a toml::Value, out: &mut Vec<&'a str>) {
    match value {
        toml::Value::String(s) => out.push(s),
        toml::Value::Array(items) => items.iter().for_each(|v| strings(v, out)),
        toml::Value::Table(table) => table_strings(table, out),
        _ => {}
    }
}

fn table_strings<'a>(table: &'a toml::Table, out: &mut Vec<&'a str>) {
    for (key, value) in table {
        out.push(key);
        strings(value, out);
    }
}

/// A service spec's `-p` with no host address bound to loopback:
/// `5432` → `127.0.0.1::5432`, `8080:80` → `127.0.0.1:8080:80`; one with an
/// address is kept (and [`Policy::check_run`] judges it).
pub fn loopback_publish(spec: &str) -> String {
    if spec.starts_with('[') || spec.matches(':').count() >= 2 {
        return spec.to_string();
    }
    match spec.contains(':') {
        true => format!("127.0.0.1:{spec}"),
        false => format!("127.0.0.1::{spec}"),
    }
}

/// Whether a `-p` value's host side is bound to a loopback address.
fn loopback_bound(spec: &str) -> bool {
    spec.starts_with("127.0.0.1:") || spec.starts_with("[::1]:")
}

/// [`Policy::for_dir`] over `dir`, then `f` on the policy when there is one.
pub fn enforce(dir: &Path, f: impl FnOnce(&Policy) -> Result<(), Refusal>) -> Result<()> {
    if let Some(policy) = Policy::for_dir(dir)? {
        f(&policy)?;
    }
    Ok(())
}

/// [`enforce`] of [`Policy::check_run`] / [`Policy::check_build`] over an
/// argv about to be run for the config at `dir`.
pub fn enforce_argv(dir: &Path, argv: &[String]) -> Result<()> {
    enforce(dir, |p| match argv.first().map(String::as_str) {
        Some("build") => p.check_build(argv),
        _ => p.check_run(argv),
    })
}

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

    /// A temp `<data>/devsandbox` with `fleet/me@control_plane/cfg` and an `outside`
    /// dir next to it. Returns (data, fleet root, outside).
    fn fixture(tag: &str) -> (PathBuf, PathBuf, PathBuf) {
        let base = std::env::temp_dir().join(format!("dsv-policy-{tag}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&base);
        let data = base.join("data/devsandbox");
        let root = data.join("fleet/me@control_plane");
        std::fs::create_dir_all(root.join("cfg/repo")).unwrap();
        std::fs::create_dir_all(base.join("outside")).unwrap();
        (data.canonicalize().unwrap(), root.canonicalize().unwrap(), base.join("outside").canonicalize().unwrap())
    }

    fn policy(data: &Path, root: &Path, allow: Allow) -> Policy {
        Policy { root: root.to_path_buf(), data: data.to_path_buf(), allow, sockets: runtime_sockets(None, None, None, None) }
    }

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

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

    #[test]
    fn detects_fleet_roots_by_path() {
        let (data, root, outside) = fixture("detect");
        let cfg = root.join("cfg");
        assert_eq!(fleet_root_of(&data, &cfg).unwrap(), Some(root.clone()));
        assert_eq!(fleet_root_of(&data, &root).unwrap(), Some(root.clone()));
        assert_eq!(fleet_root_of(&data, &data.join("fleet")).unwrap(), Some(data.join("fleet")));
        assert_eq!(fleet_root_of(&data, &outside).unwrap(), None);
        assert_eq!(fleet_root_of(&data, &data).unwrap(), None);
        // A symlink into the fleet dir is under it; `..` out of it isn't.
        let link = outside.join("link");
        std::os::unix::fs::symlink(&cfg, &link).unwrap();
        assert_eq!(fleet_root_of(&data, &link).unwrap(), Some(root.clone()));
        assert_eq!(fleet_root_of(&data, &cfg.join("../../../..")).unwrap(), None);
        // A dir that doesn't exist is placed as written.
        assert_eq!(fleet_root_of(&data, &root.join("gone/x")).unwrap(), Some(root.clone()));
        assert_eq!(fleet_root_of(&data, &outside.join("gone")).unwrap(), None);
        assert!(fleet_root_of(&data, &root.join("gone/../x")).is_err());
        // No fleet dir: nothing is under it.
        let (data2, _, outside2) = fixture("detect-none");
        std::fs::remove_dir_all(data2.join("fleet")).unwrap();
        assert_eq!(fleet_root_of(&data2, &outside2).unwrap(), None);
        // `detect` reads `[allow]` off member.toml next to it.
        std::fs::write(data.join("member.toml"), "name = \"lap\"\n[allow]\nprivileged = true\ngpus = true\n").unwrap();
        let p = Policy::detect(&data, &cfg, Vec::new()).unwrap().unwrap();
        assert!(p.allow.privileged && p.allow.gpus && !p.allow.host_network);
        assert!(Policy::detect(&data, &outside, Vec::new()).unwrap().is_none());
        std::fs::write(data.join("member.toml"), "trust = \"worker\"\n").unwrap();
        assert_eq!(Policy::detect(&data, &cfg, Vec::new()).unwrap().unwrap().allow, Allow::default());
        // Under `full`, no policy at all.
        std::fs::write(data.join("member.toml"), "trust = \"full\"\n").unwrap();
        assert!(Policy::detect(&data, &cfg, Vec::new()).unwrap().is_none());
        std::fs::write(data.join("member.toml"), "trust = \"most\"\n").unwrap();
        assert!(Policy::detect(&data, &cfg, Vec::new()).is_err(), "a bad trust fails, not the defaults");
        std::fs::write(data.join("member.toml"), "[allow]\nprivileged = 1\n").unwrap();
        assert!(Policy::detect(&data, &cfg, Vec::new()).is_err(), "a bad [allow] fails, not the defaults");
        std::fs::write(data.join("member.toml"), "[allow]\nbinds = [\"rel\"]\n").unwrap();
        assert!(Policy::detect(&data, &cfg, Vec::new()).is_err());
    }

    #[test]
    fn local_env_names_and_loopback_publish() {
        assert_eq!(local_env_names("a${localEnv:A}b${localEnv:B:def}${containerEnv:C}${localEnv:D"), ["A", "B", "D"]);
        assert!(local_env_names("${localenv:A} $localEnv:B").is_empty());
        for (spec, want) in [
            ("5432", "127.0.0.1::5432"),
            ("5432/udp", "127.0.0.1::5432/udp"),
            ("8080:80", "127.0.0.1:8080:80"),
            ("8000-8010:8000-8010", "127.0.0.1:8000-8010:8000-8010"),
            ("127.0.0.1:8080:80", "127.0.0.1:8080:80"),
            ("0.0.0.0:8080:80", "0.0.0.0:8080:80"),
            ("[::1]:8080:80", "[::1]:8080:80"),
        ] {
            assert_eq!(loopback_publish(spec), want, "{spec}");
        }
        assert!(loopback_bound("127.0.0.1::5432") && loopback_bound("[::1]:1:1"));
        assert!(!loopback_bound("127.0.0.10:1:1") && !loopback_bound("5432") && !loopback_bound("[::]:1:1"));
    }

    #[test]
    fn the_refusal_names_the_item_and_its_allowance() {
        let r = Refusal::new("privileged", "privileged = true");
        assert_eq!(
            r.to_string(),
            "refused by this machine's worker policy: privileged (allow with privileged = true under [allow] in member.toml)"
        );
        assert!(Refusal::always("runtime flag `--cap-add`").to_string().ends_with("(no member.toml allowance permits it)"));
    }

    #[test]
    fn run_argv_items_are_refused_and_each_allowance_permits_its_own() {
        let (data, root, outside) = fixture("run");
        let inside = root.join("cfg/repo");
        let sv = root.join("cfg/shared-volumes");
        let sock = outside.join("docker.sock");
        let inside_s = inside.display().to_string();
        let outside_s = outside.display().to_string();
        let ws = format!("{inside_s}:/w");
        let seccomp = format!("seccomp={}", data.join("seccomp-0123.json").display());
        let fine: Vec<Vec<String>> = vec![
            vec!["-v".into(), ws.clone()],
            vec!["-v".into(), format!("{ws}:ro,z")],
            vec!["-v".into(), "named:/data".into()],
            vec!["--mount".into(), format!("type=bind,source={},target=/c", sv.join("cargo").display())],
            vec!["--mount".into(), format!("type=bind,source={},target=/commandhistory", sv.join("history/x").display())],
            vec!["--mount".into(), format!("type=bind,source={},target=/h", sv.join("skills/x").display())],
            vec!["--mount".into(), format!("type=bind,source={},target=/l", root.join("cfg/shared-files/r-1").display())],
            vec!["--mount".into(), format!("type=bind,source={inside_s},target=/x,readonly")],
            vec!["--mount".into(), format!("type=bind,source={inside_s}/new/dir,target=/x,relabel=shared")],
            vec!["--mount".into(), "type=volume,source=devsandbox-x-cache,target=/c".into()],
            vec!["--mount".into(), "type=volume,target=/anon".into()],
            vec!["--mount".into(), "type=tmpfs,target=/t".into()],
            vec!["--network".into(), "devsandbox-net-abc-x".into()],
            vec!["--network=devsandbox-net-abc".into()],
            vec!["--userns".into(), "keep-id:uid=1000,gid=1000".into()],
            vec!["--security-opt".into(), seccomp.clone()],
            vec!["--init".into(), "--restart".into(), "unless-stopped".into(), "-e".into(), "A=--privileged".into()],
            vec!["--user".into(), "root".into(), "-p".into(), "127.0.0.1::5432".into(), "--network-alias".into(), "db".into()],
            vec!["-p".into(), "127.0.0.1:5432:5432".into(), "--publish=[::1]:8080:80".into()],
        ];
        let none = policy(&data, &root, Allow::default());
        for extra in &fine {
            let extra: Vec<&str> = extra.iter().map(String::as_str).collect();
            assert_eq!(none.check_run(&run_with(&extra)), Ok(()), "{extra:?}");
        }
        // (args, the allowance that permits them; None: nothing does).
        let all_but = |f: &dyn Fn(&mut Allow)| {
            let mut a = Allow {
                privileged: true,
                runtime_socket: true,
                host_network: true,
                global_services: true,
                dispatcher: true,
                inbox: true,
                initialize_command: true,
                publish: true,
                gpus: true,
                local_env: vec!["X".into()],
                binds: vec![outside.clone()],
            };
            f(&mut a);
            a
        };
        type Set = fn(&mut Allow);
        let denied: Vec<(Vec<String>, &str, Option<(Set, Set)>)> = vec![
            (argv(&["--privileged"]), "privileged = true", Some((|a| a.privileged = true, |a| a.privileged = false))),
            (
                vec!["-v".into(), format!("{outside_s}:/o")],
                "binds = [",
                Some((|a| a.binds = vec![std::env::temp_dir()], |a| a.binds.clear())),
            ),
            (
                vec!["--mount".into(), format!("type=bind,source={},target=/s", sock.display())],
                "runtime-socket = true",
                Some((
                    |a| {
                        a.runtime_socket = true;
                        a.binds = vec![std::env::temp_dir()];
                    },
                    |a| a.runtime_socket = false,
                )),
            ),
            (
                vec!["--mount".into(), "type=bind,source=/var/run/docker.sock,target=/s".into()],
                "runtime-socket = true",
                None,
            ),
            (argv(&["--network", "host"]), "host-network = true", Some((|a| a.host_network = true, |a| a.host_network = false))),
            (argv(&["-p", "5432"]), "publish = true", Some((|a| a.publish = true, |a| a.publish = false))),
            (argv(&["--gpus", "all"]), "gpus = true", Some((|a| a.gpus = true, |a| a.gpus = false))),
            (argv(&["--gpus=all"]), "gpus = true", Some((|a| a.gpus = true, |a| a.gpus = false))),
            (argv(&["--device", "nvidia.com/gpu=all"]), "gpus = true", Some((|a| a.gpus = true, |a| a.gpus = false))),
            (argv(&["--device=nvidia.com/gpu=all"]), "gpus = true", Some((|a| a.gpus = true, |a| a.gpus = false))),
            (argv(&["-p", "8080:80"]), "publish = true", None),
            (argv(&["--publish=0.0.0.0:8080:80"]), "publish = true", None),
            (argv(&["-p", "[::]:8080:80"]), "publish = true", None),
            (argv(&["-p", "192.168.1.2:8080:80"]), "publish = true", None),
            // `..` and symlink escapes, nothing but `binds` permits them.
            (vec!["-v".into(), format!("{inside_s}/../../../../../../outside:/o")], "binds = [", None),
            (vec!["-v".into(), format!("{inside_s}/missing/../../x:/o")], "", None),
        ];
        for (extra, allowance, toggles) in &denied {
            let extra: Vec<&str> = extra.iter().map(String::as_str).collect();
            let err = none.check_run(&run_with(&extra)).unwrap_err();
            assert!(err.to_string().contains(allowance), "{extra:?}: {err}");
            if let Some((grant, revoke)) = toggles {
                // Its own allowance alone permits it …
                let mut only = Allow::default();
                grant(&mut only);
                assert_eq!(policy(&data, &root, only).check_run(&run_with(&extra)), Ok(()), "{extra:?}");
                // … and every other one doesn't.
                let rest = all_but(&|a| revoke(a));
                assert!(policy(&data, &root, rest).check_run(&run_with(&extra)).is_err(), "{extra:?}");
            }
        }
        // A symlink inside the root pointing out is outside.
        let link = inside.join("escape");
        std::os::unix::fs::symlink(&outside, &link).unwrap();
        let err = none.check_run(&run_with(&["-v", &format!("{}:/e", link.display())])).unwrap_err();
        assert!(err.to_string().contains("outside the fleet root"), "{err}");
        // So is one in `shared-volumes/`: those binds pass only as paths.
        std::fs::create_dir_all(&sv).unwrap();
        let link = sv.join("escape");
        std::os::unix::fs::symlink(&outside, &link).unwrap();
        let err = none.check_run(&run_with(&["--mount", &format!("type=bind,source={},target=/e", link.display())])).unwrap_err();
        assert!(err.to_string().contains("outside the fleet root"), "{err}");
        // A dangling one doesn't resolve (creating the source would create
        // its target), nor does a path through it.
        let dangling = sv.join("dangling");
        std::os::unix::fs::symlink(outside.join("missing.json"), &dangling).unwrap();
        for p in [dangling.clone(), dangling.join("x")] {
            let err = none.check_run(&run_with(&["--mount", &format!("type=bind,source={},target=/e", p.display())])).unwrap_err();
            assert!(err.to_string().contains("cannot resolve"), "{err}");
        }
        // A dir holding a runtime socket needs `runtime-socket` even when bound.
        let p = Policy { sockets: vec![sock.clone()], ..policy(&data, &root, Allow { binds: vec![outside.clone()], ..Default::default() }) };
        assert!(p.check_run(&run_with(&["-v", &format!("{outside_s}:/o")])).unwrap_err().to_string().contains("runtime-socket"));
        // Flags devsandbox never writes: refused, no allowance.
        let all = policy(&data, &root, all_but(&|_| {}));
        for extra in [
            &["--cap-add", "SYS_ADMIN"][..],
            &["--device", "/dev/kvm"],
            &["--device", "nvidia.com/gpu=0"],
            &["--device=/dev/nvidia0"],
            &["--gpus", "1"],
            &["--gpus=device=0"],
            &["--gpus", "all,capabilities=utility"],
            &["--pid", "host"],
            &["--ipc=host"],
            &["--userns", "host"],
            &["--security-opt", "seccomp=unconfined"],
            &["--security-opt", "apparmor=unconfined"],
            &["--security-opt", "seccomp=/tmp/seccomp-1.json"],
            &["--volumes-from", "other"],
            &["--network", "container:other"],
            &["--mount", "type=volume,source=v,target=/v,volume-opt=device=/"],
            &["--mount", "type=image,source=x,target=/i"],
            &["-v", "/x:/y:rshared"],
            &["-it"],
        ] {
            let err = all.check_run(&run_with(extra)).unwrap_err();
            assert_eq!(err.allow, None, "{extra:?}: {err}");
        }
        assert!(none.check_run(&argv(&["create", "alpine"])).is_err());
        assert!(none.check_run(&argv(&["run", "-d"])).is_err());
    }

    #[test]
    fn build_argv_paths_and_flags() {
        let (data, root, outside) = fixture("build");
        let none = policy(&data, &root, Allow::default());
        let cfg = root.join("cfg");
        let df = cfg.join("Dockerfile").display().to_string();
        let ctx = cfg.display().to_string();
        let ok = argv(&["build", "-t", "t", "-f", &df, "--build-arg", "A=1", "--target", "x", "--cache-from", "i", "--pull", &ctx]);
        assert_eq!(none.check_build(&ok), Ok(()));
        let out = outside.display().to_string();
        for (bad, allowance) in [
            (argv(&["build", "-t", "t", "-f", &df, &out]), Some("binds = [")),
            (argv(&["build", "-t", "t", "-f", &format!("{out}/Dockerfile"), &ctx]), Some("binds = [")),
            (argv(&["build", "-t", "t", "-f", &df, &format!("{ctx}/../../../../../outside")]), Some("binds = [")),
            (argv(&["build", "-t", "t", "--network", "host", "-f", &df, &ctx]), Some("host-network = true")),
            (argv(&["build", "-t", "t", "--build-context", &format!("x={out}"), "-f", &df, &ctx]), None),
            (argv(&["build", "-t", "t", "--secret", "id=a,src=/etc/shadow", "-f", &df, &ctx]), None),
            (argv(&["build", "-t", "t", "--ssh", "default", "-f", &df, &ctx]), None),
            (argv(&["build", "-t", "t", "-f", &df, &ctx, &ctx]), None),
            (argv(&["build", "-t", "t", "-f", &df, "-"]), None),
        ] {
            let err = none.check_build(&bad).unwrap_err();
            assert_eq!(err.allow.as_deref().map(|a| a.split('"').next().unwrap()), allowance, "{bad:?}: {err}");
        }
        let net = policy(&data, &root, Allow { host_network: true, ..Default::default() });
        assert_eq!(net.check_build(&argv(&["build", "--network=host", "-f", &df, &ctx])), Ok(()));
        let bound = policy(&data, &root, Allow { binds: vec![outside.clone()], ..Default::default() });
        assert_eq!(bound.check_build(&argv(&["build", "-f", &df, &out])), Ok(()));
    }

    #[test]
    fn config_items_are_refused_and_each_allowance_permits_its_own() {
        let (data, root, outside) = fixture("config");
        let cfg = root.join("cfg");
        std::fs::create_dir_all(cfg.join("features/ok")).unwrap();
        std::fs::write(cfg.join("features/ok/install.sh"), "").unwrap();
        std::fs::create_dir_all(cfg.join("features/leak")).unwrap();
        std::fs::write(outside.join("secret"), "").unwrap();
        std::os::unix::fs::symlink(outside.join("secret"), cfg.join("features/leak/install.sh")).unwrap();
        let rel_out = "../../../../../outside";
        let sandbox = |extra: &str| {
            let text = format!(
                "[services.g]\nimage = \"redis\"\nscope = \"global\"\n[services.i]\nimage = \"redis\"\n\
                 [services.gpu]\nimage = \"ollama\"\nhostRequirements.gpu = true\n\
                 [services.opt]\nimage = \"ollama\"\nhostRequirements.gpu = \"optional\"\n\
                 [sandbox.s]\nfolder = \"repo\"\nimage = \"alpine\"\n{extra}\n"
            );
            let config = Config::parse(&text).unwrap();
            let s = config.resolve_sandbox("s").unwrap();
            (config, s)
        };
        let none = policy(&data, &root, Allow::default());
        let check = |p: &Policy, extra: &str| {
            let (config, s) = sandbox(extra);
            p.check_config(&cfg, &config, &s)
        };
        for fine in [
            "",
            "services = [\"i\"]",
            "privileged = false",
            "hostRequirements.gpu = false",
            "hostRequirements.gpu = \"optional\"",
            "services = [\"opt\"]",
            "caches = [\"cargo\"]",
            "persist-shell-history = true",
            "worktree-link = [\".env\"]",
            "features = { \"./features/ok\" = {} }",
            "features = { \"ghcr.io/devcontainers/features/node:1\" = {} }",
            "mounts = [\"source=/etc,target=/e\"]", // a bind: the mounts' own check
        ] {
            assert_eq!(check(&none, fine), Ok(()), "{fine}");
        }
        let items: [(&str, &str, fn(&mut Allow)); 9] = [
            ("initializeCommand = \"true\"", "initialize-command = true", |a| a.initialize_command = true),
            ("dispatcher = {}", "dispatcher = true", |a| a.dispatcher = true),
            ("inbox = true", "inbox = true", |a| a.inbox = true),
            ("privileged = true", "privileged = true", |a| a.privileged = true),
            ("hostRequirements.gpu = true", "gpus = true", |a| a.gpus = true),
            ("hostRequirements.gpu = { cores = 2 }", "gpus = true", |a| a.gpus = true),
            ("services = [\"i\", \"gpu\"]", "gpus = true", |a| a.gpus = true),
            ("services = [\"i\", \"g\"]", "global-services = true", |a| a.global_services = true),
            ("features = { \"./features/leak\" = {} }", "binds = [", |a| a.binds = vec![std::env::temp_dir()]),
        ];
        for (i, (extra, allowance, grant)) in items.iter().enumerate() {
            let err = check(&none, extra).unwrap_err();
            assert!(err.to_string().contains(allowance), "{extra}: {err}");
            let mut own = Allow::default();
            grant(&mut own);
            assert_eq!(check(&policy(&data, &root, own), extra), Ok(()), "{extra}");
            for (j, (_, theirs, other)) in items.iter().enumerate() {
                if i != j && theirs != allowance {
                    let mut a = Allow::default();
                    other(&mut a);
                    assert!(check(&policy(&data, &root, a), extra).is_err(), "{extra} permitted by item {j}");
                }
            }
        }
        // `${localEnv:…}` anywhere (a template, a key, a service), by name.
        let gh = Allow { local_env: vec!["GH".into()], ..Default::default() };
        for extra in [
            "containerEnv = { T = \"${localEnv:GH}\" }",
            "containerEnv = { T = \"${localEnv:GH:fallback}\" }",
            "mounts = [\"source=${localEnv:GH}/x,target=/x\"]",
            "folders = { \"/x-${localEnv:GH}\" = \"repo\" }",
            "worktree-branch = \"b-${localEnv:GH}\"",
            "services = [\"e\"]",
            "extends = \"t\"",
        ] {
            let text = format!(
                "[template.t]\nremoteEnv = {{ A = \"${{localEnv:GH}}\" }}\n\
                 [services.e]\nimage = \"redis\"\nenv = {{ P = \"${{localEnv:GH}}\" }}\n\
                 [sandbox.s]\nfolder = \"repo\"\nimage = \"alpine\"\n{extra}\n"
            );
            let config = Config::parse(&text).unwrap();
            let s = config.resolve_sandbox("s").unwrap();
            let err = none.check_config(&cfg, &config, &s).unwrap_err();
            assert_eq!(err.allow.as_deref(), Some("local-env = [\"GH\"]"), "{extra}: {err}");
            assert!(err.to_string().contains("`${localEnv:GH}`"), "{err}");
            assert_eq!(policy(&data, &root, gh.clone()).check_config(&cfg, &config, &s), Ok(()), "{extra}");
            let other = Allow { local_env: vec!["GH2".into(), "G".into()], ..Default::default() };
            assert!(policy(&data, &root, other).check_config(&cfg, &config, &s).is_err(), "{extra}");
        }
        // An unrelated sandbox's reference doesn't count.
        let config = Config::parse(
            "[sandbox.s]\nfolder = \"repo\"\nimage = \"alpine\"\n[sandbox.o]\nfolder = \"repo\"\nimage = \"a\"\ncontainerEnv = { T = \"${localEnv:GH}\" }\n",
        )
        .unwrap();
        assert_eq!(none.check_config(&cfg, &config, &config.resolve_sandbox("s").unwrap()), Ok(()));
        let p = policy(&data, &root, gh);
        assert!(p.allows_local_env("GH") && p.allows_local_env("GH:default") && !p.allows_local_env("GHX"));
        // Outside a fleet root nothing changes.
        assert!(local_env_allowed(&outside, "ANY"));
        let err = check(&none, &format!("features = {{ \"{rel_out}\" = {{}} }}")).unwrap_err();
        assert!(err.to_string().contains("outside the fleet root"), "{err}");
        // `check_path`: the folder rule.
        assert_eq!(none.check_path(&cfg.join("repo"), "folder"), Ok(()));
        assert!(none.check_path(&cfg.join(rel_out), "folder").is_err());
    }
}