nexo-core 0.1.12

Agent runtime: event bus, sessions, plugin trait, heartbeat, A2A delegation.
Documentation
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
//! Phase 81.22 — Plugin sandbox runner (bubblewrap-based on Linux).
//!
//! Wraps a subprocess plugin's spawn `Command` with a `bwrap` invocation
//! that enforces the plugin's `[plugin.sandbox]` manifest section: fs
//! read/write allowlist, network namespace policy (deny | host),
//! optional uid/gid drop. Discovery happens once at boot; per-spawn
//! cost is argv assembly only.
//!
//! macOS path: no-op + `tracing::warn!`. Native sandbox-exec
//! integration deferred to 81.22.macos.
//!
//! IRROMPIBLE refs:
//! - `research/src/agents/sandbox/validate-sandbox-security.ts:307-345`
//!   — `validateBindMounts` 2-pass shape (canonicalize → per-path
//!   denylist check) inspired the [`validate_resolved_path`] helper.
//! - `research/src/agents/sandbox/network-mode.ts:18-29` — coarse
//!   `host` rejection without operator opt-in is OpenClaw's pattern;
//!   we keep the same shape gated behind
//!   [`Self::host_net_capability_allowed`] (env-driven).
//! - claude-code-leak/: absent — process-isolation infra, no
//!   LLM-tool surface.

use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use nexo_plugin_manifest::{
    path_under_or_equals_denylist, PluginManifest, SandboxNetwork, SandboxSection,
    SANDBOX_DENYLIST_HOST_PATHS, SANDBOX_STATE_DIR_TOKEN,
};
use thiserror::Error;

/// Env var: when `1`, refuse to spawn any plugin without
/// `sandbox.enabled = true`. Capability inventory entry lives in
/// `crates/setup/src/capabilities.rs::INVENTORY` (Phase 81.22).
pub const ENV_SANDBOX_REQUIRE: &str = "NEXO_PLUGIN_SANDBOX_REQUIRE";

/// Env var: when `1`, permits manifests declaring
/// `sandbox.network = "host"`. Default-off because sharing the host
/// net namespace defeats most of the sandbox.
pub const ENV_SANDBOX_HOST_NET_ALLOW: &str = "NEXO_PLUGIN_SANDBOX_HOST_NET_ALLOW";

/// Default uid/gid bwrap maps the child to when `drop_user = true`.
/// 65534 is the standard `nobody:nogroup` shape on Debian/Ubuntu/Alpine.
const NOBODY_UID: u32 = 65534;
const NOBODY_GID: u32 = 65534;

/// Boot-time state for sandbox enforcement. Constructed once in
/// `wire_plugin_registry_with_runtime` and shared across all
/// subprocess plugin spawns.
#[derive(Debug, Clone)]
pub struct SandboxRunner {
    /// `Some(p)` when bwrap was found in PATH at boot. `None` on
    /// macOS (always) or Linux without bubblewrap installed.
    bwrap_path: Option<PathBuf>,

    /// `NEXO_PLUGIN_SANDBOX_HOST_NET_ALLOW` — when `false`,
    /// manifests declaring `network = "host"` fail validation
    /// host-side.
    host_net_capability_allowed: bool,

    /// `NEXO_PLUGIN_SANDBOX_REQUIRE` — when `true`, every
    /// subprocess plugin must have `sandbox.enabled = true`,
    /// otherwise spawn fails with [`SandboxError::SandboxRequiredButDisabled`].
    require_sandbox: bool,
}

impl SandboxRunner {
    /// Discovers `bwrap` once and caches the path. Reads env vars
    /// for capability flags. Idempotent.
    pub fn discover() -> Self {
        let bwrap_path = if cfg!(target_os = "linux") {
            which::which("bwrap").ok()
        } else {
            None
        };
        Self {
            bwrap_path,
            host_net_capability_allowed: env_flag(ENV_SANDBOX_HOST_NET_ALLOW),
            require_sandbox: env_flag(ENV_SANDBOX_REQUIRE),
        }
    }

    /// Construction for tests with explicit knobs (no env reads).
    #[cfg(test)]
    pub fn for_test(
        bwrap_path: Option<PathBuf>,
        host_net_capability_allowed: bool,
        require_sandbox: bool,
    ) -> Self {
        Self {
            bwrap_path,
            host_net_capability_allowed,
            require_sandbox,
        }
    }

    pub fn bwrap_path(&self) -> Option<&Path> {
        self.bwrap_path.as_deref()
    }

    pub fn host_net_capability_allowed(&self) -> bool {
        self.host_net_capability_allowed
    }

    pub fn require_sandbox(&self) -> bool {
        self.require_sandbox
    }

    /// Wraps `cmd + args` according to the plugin's manifest sandbox
    /// section. Returns the resolved program/args ready to feed
    /// `Command::new(...).args(...)`. The optional `diagnostic`
    /// surfaces non-fatal warnings (macOS no-op, missing-bwrap
    /// degrade) so the host can `tracing::warn!` them per spawn.
    pub fn wrap_command(
        &self,
        manifest: &PluginManifest,
        state_dir: &Path,
        cmd: &str,
        args: &[String],
    ) -> Result<WrappedCommand, SandboxError> {
        let sandbox = &manifest.plugin.sandbox;
        let plugin_id = manifest.plugin.id.as_str();

        if !sandbox.enabled {
            if self.require_sandbox {
                return Err(SandboxError::SandboxRequiredButDisabled {
                    plugin_id: plugin_id.to_string(),
                });
            }
            return Ok(WrappedCommand::raw(cmd, args, None));
        }

        // Sandbox enabled: pre-check operator-opt-in for host net.
        if sandbox.network == SandboxNetwork::Host && !self.host_net_capability_allowed {
            return Err(SandboxError::HostNetRequestedButNotAllowed {
                plugin_id: plugin_id.to_string(),
            });
        }

        // Platform branches.
        if cfg!(target_os = "macos") {
            if self.require_sandbox {
                return Err(SandboxError::UnsupportedPlatformAndRequired {
                    plugin_id: plugin_id.to_string(),
                });
            }
            return Ok(WrappedCommand::raw(
                cmd,
                args,
                Some(format!(
                    "macOS — sandbox not enforced for plugin `{}` (native sandbox-exec deferred)",
                    plugin_id
                )),
            ));
        }

        // Linux without bwrap.
        let bwrap = match &self.bwrap_path {
            Some(p) => p,
            None => {
                if self.require_sandbox {
                    return Err(SandboxError::BwrapMissingButRequired {
                        plugin_id: plugin_id.to_string(),
                    });
                }
                return Ok(WrappedCommand::raw(
                    cmd,
                    args,
                    Some(format!(
                        "plugin `{}` declares sandbox but `bwrap` is not in PATH — running without sandbox",
                        plugin_id
                    )),
                ));
            }
        };

        // Linux + bwrap available — build argv.
        let bwrap_args = build_bwrap_args(plugin_id, sandbox, state_dir, cmd, args)?;
        Ok(WrappedCommand {
            program: bwrap.clone(),
            args: bwrap_args,
            diagnostic: None,
        })
    }
}

/// Final shape consumed by the spawn site. `program` may be the
/// plugin's raw command (sandbox disabled / unsupported platform /
/// missing bwrap) or `bwrap` itself.
#[derive(Debug, Clone)]
pub struct WrappedCommand {
    pub program: PathBuf,
    pub args: Vec<OsString>,
    pub diagnostic: Option<String>,
}

impl WrappedCommand {
    fn raw(cmd: &str, args: &[String], diagnostic: Option<String>) -> Self {
        Self {
            program: PathBuf::from(cmd),
            args: args.iter().map(OsString::from).collect(),
            diagnostic,
        }
    }
}

/// Convenience: wraps `Self` in `Arc` for the boot-time shared
/// runner.
pub fn shared_runner_from_env() -> Arc<SandboxRunner> {
    Arc::new(SandboxRunner::discover())
}

#[derive(Debug, Error)]
pub enum SandboxError {
    #[error(
        "plugin `{plugin_id}`: sandbox required (NEXO_PLUGIN_SANDBOX_REQUIRE=1) but manifest sets sandbox.enabled = false"
    )]
    SandboxRequiredButDisabled { plugin_id: String },

    #[error(
        "plugin `{plugin_id}`: sandbox.network = \"host\" requires NEXO_PLUGIN_SANDBOX_HOST_NET_ALLOW=1"
    )]
    HostNetRequestedButNotAllowed { plugin_id: String },

    #[error(
        "plugin `{plugin_id}`: sandbox required but `bwrap` not found in PATH (apt install bubblewrap)"
    )]
    BwrapMissingButRequired { plugin_id: String },

    #[error(
        "plugin `{plugin_id}`: sandbox required but the current platform is unsupported (macOS sandbox path deferred)"
    )]
    UnsupportedPlatformAndRequired { plugin_id: String },

    #[error("plugin `{plugin_id}`: sandbox path resolution failed for `{path}`: {reason}")]
    PathResolutionFailed {
        plugin_id: String,
        path: String,
        reason: String,
    },

    #[error(
        "plugin `{plugin_id}`: resolved sandbox path `{path}` (from manifest entry `{original}`) hits denylisted host path `{denylisted}`"
    )]
    ResolvedPathHitsDenylist {
        plugin_id: String,
        original: String,
        path: String,
        denylisted: String,
    },
}

fn env_flag(name: &str) -> bool {
    matches!(
        std::env::var(name).ok().as_deref(),
        Some("1") | Some("true") | Some("TRUE") | Some("yes") | Some("YES")
    )
}

/// Builds the bwrap argv (everything AFTER the bwrap binary path).
/// Runs the post-validation denylist check on the resolved
/// `${state_dir}` path so allowlist entries that expand to a
/// denylisted target get caught at spawn time.
fn build_bwrap_args(
    plugin_id: &str,
    sandbox: &SandboxSection,
    state_dir: &Path,
    cmd: &str,
    cmd_args: &[String],
) -> Result<Vec<OsString>, SandboxError> {
    let mut argv: Vec<OsString> = Vec::new();

    // Process-lifecycle hardening (see bwrap(1)).
    argv.push("--die-with-parent".into());
    argv.push("--unshare-pid".into());
    argv.push("--unshare-uts".into());
    argv.push("--unshare-ipc".into());
    argv.push("--new-session".into());

    // Filesystem skeleton — read-only system dirs, fresh /proc + /tmp.
    argv.push("--proc".into());
    argv.push("/proc".into());
    argv.push("--dev".into());
    argv.push("/dev".into());
    argv.push("--tmpfs".into());
    argv.push("/tmp".into());

    for ro in &["/usr", "/bin", "/sbin", "/lib", "/lib64", "/etc/ssl"] {
        if Path::new(ro).exists() {
            argv.push("--ro-bind-try".into());
            argv.push(OsString::from(ro));
            argv.push(OsString::from(ro));
        }
    }
    // /etc/resolv.conf is host-managed; bind read-only when present
    // (host net mode needs it; deny-net mode tolerates absence).
    argv.push("--ro-bind-try".into());
    argv.push("/etc/resolv.conf".into());
    argv.push("/etc/resolv.conf".into());

    // Network policy.
    match sandbox.network {
        SandboxNetwork::Deny => {
            argv.push("--unshare-net".into());
        }
        SandboxNetwork::Host => {
            // No --unshare-net flag; child shares host's net namespace.
            // Operator opt-in already verified by caller.
        }
    }

    // User/group drop.
    if sandbox.drop_user {
        argv.push("--unshare-user".into());
        argv.push("--uid".into());
        argv.push(OsString::from(NOBODY_UID.to_string()));
        argv.push("--gid".into());
        argv.push(OsString::from(NOBODY_GID.to_string()));
    }

    // PATH skeleton — bwrap doesn't preserve env by default.
    argv.push("--setenv".into());
    argv.push("PATH".into());
    argv.push("/usr/bin:/bin".into());

    // Implicit: ro-bind the plugin command's parent dir so the
    // executable itself is reachable inside the sandbox. Without
    // this, the binary path (often in the plugin's bundle dir)
    // would not exist in the sandboxed view and bwrap exec fails
    // with ENOENT.
    if let Some(cmd_parent) = std::path::Path::new(cmd).parent() {
        if cmd_parent.exists() && !cmd_parent.as_os_str().is_empty() {
            argv.push("--ro-bind-try".into());
            argv.push(OsString::from(cmd_parent));
            argv.push(OsString::from(cmd_parent));
        }
    }

    // Read-only allowlist.
    for entry in &sandbox.fs_read_paths {
        let resolved = resolve_path(entry, state_dir);
        validate_resolved_path(plugin_id, entry, &resolved)?;
        argv.push("--ro-bind".into());
        argv.push(OsString::from(&resolved));
        argv.push(OsString::from(&resolved));
    }

    // Read-write allowlist (state_dir token expansion happens here).
    for entry in &sandbox.fs_write_paths {
        let resolved = resolve_path(entry, state_dir);
        validate_resolved_path(plugin_id, entry, &resolved)?;
        argv.push("--bind".into());
        argv.push(OsString::from(&resolved));
        argv.push(OsString::from(&resolved));
    }

    // The plugin command + its args go after `--`.
    argv.push("--".into());
    argv.push(OsString::from(cmd));
    for a in cmd_args {
        argv.push(OsString::from(a));
    }

    Ok(argv)
}

fn resolve_path(entry: &str, state_dir: &Path) -> String {
    if entry.contains(SANDBOX_STATE_DIR_TOKEN) {
        entry.replace(SANDBOX_STATE_DIR_TOKEN, &state_dir.to_string_lossy())
    } else {
        entry.to_string()
    }
}

/// Spawn-time guard: re-runs the denylist check on the post-token
/// expansion path. The manifest validator ran this against the
/// pre-expansion string with a sentinel placeholder; here we
/// catch the case where `state_dir` itself happens to live under
/// a denylisted path (operator misconfig).
fn validate_resolved_path(
    plugin_id: &str,
    original: &str,
    resolved: &str,
) -> Result<(), SandboxError> {
    if !resolved.starts_with('/') {
        return Err(SandboxError::PathResolutionFailed {
            plugin_id: plugin_id.to_string(),
            path: original.to_string(),
            reason: "resolved path is not absolute".into(),
        });
    }
    if let Some(denylisted) = path_under_or_equals_denylist(resolved, SANDBOX_DENYLIST_HOST_PATHS) {
        return Err(SandboxError::ResolvedPathHitsDenylist {
            plugin_id: plugin_id.to_string(),
            original: original.to_string(),
            path: resolved.to_string(),
            denylisted: denylisted.to_string(),
        });
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use nexo_plugin_manifest::{PluginManifest, SandboxNetwork};

    fn manifest_with_sandbox(toml: &str) -> PluginManifest {
        let full = format!(
            r#"
[plugin]
id = "p"
version = "0.1.0"
name = "p"
description = "test plugin"
min_nexo_version = ">=0.0.0"

{}
"#,
            toml
        );
        PluginManifest::from_str(&full).expect("valid TOML")
    }

    fn fresh_runner() -> SandboxRunner {
        SandboxRunner::for_test(Some(PathBuf::from("/usr/bin/bwrap")), false, false)
    }

    #[test]
    fn discover_sets_bwrap_path_when_present() {
        let r = SandboxRunner::for_test(Some("/usr/bin/bwrap".into()), false, false);
        assert_eq!(r.bwrap_path(), Some(Path::new("/usr/bin/bwrap")));
    }

    #[test]
    fn discover_handles_missing_bwrap() {
        let r = SandboxRunner::for_test(None, false, false);
        assert!(r.bwrap_path().is_none());
    }

    #[test]
    fn env_flag_recognizes_truthy_values() {
        // pure helper — set + read.
        std::env::set_var("NEXO_PLUGIN_SANDBOX_TEST_FLAG", "1");
        assert!(env_flag("NEXO_PLUGIN_SANDBOX_TEST_FLAG"));
        std::env::set_var("NEXO_PLUGIN_SANDBOX_TEST_FLAG", "yes");
        assert!(env_flag("NEXO_PLUGIN_SANDBOX_TEST_FLAG"));
        std::env::set_var("NEXO_PLUGIN_SANDBOX_TEST_FLAG", "0");
        assert!(!env_flag("NEXO_PLUGIN_SANDBOX_TEST_FLAG"));
        std::env::remove_var("NEXO_PLUGIN_SANDBOX_TEST_FLAG");
    }

    #[test]
    fn wrap_disabled_passes_command_through_raw() {
        let m = manifest_with_sandbox("[plugin.sandbox]\nenabled = false");
        let runner = fresh_runner();
        let wrapped = runner
            .wrap_command(&m, Path::new("/state"), "/bin/echo", &["hi".into()])
            .unwrap();
        assert_eq!(wrapped.program, PathBuf::from("/bin/echo"));
        assert_eq!(wrapped.args, vec![OsString::from("hi")]);
        assert!(wrapped.diagnostic.is_none());
    }

    #[test]
    fn wrap_disabled_with_strict_mode_errors() {
        let m = manifest_with_sandbox("[plugin.sandbox]\nenabled = false");
        let runner = SandboxRunner::for_test(Some("/usr/bin/bwrap".into()), false, true);
        let err = runner
            .wrap_command(&m, Path::new("/state"), "/bin/echo", &[])
            .unwrap_err();
        assert!(
            matches!(err, SandboxError::SandboxRequiredButDisabled { .. }),
            "{err:?}"
        );
    }

    #[test]
    fn wrap_host_network_without_capability_errors() {
        let m = manifest_with_sandbox("[plugin.sandbox]\nenabled = true\nnetwork = \"host\"");
        let runner = fresh_runner();
        let err = runner
            .wrap_command(&m, Path::new("/state"), "/bin/echo", &[])
            .unwrap_err();
        assert!(
            matches!(err, SandboxError::HostNetRequestedButNotAllowed { .. }),
            "{err:?}"
        );
    }

    #[test]
    fn wrap_host_network_with_capability_passes() {
        let m = manifest_with_sandbox("[plugin.sandbox]\nenabled = true\nnetwork = \"host\"");
        let runner = SandboxRunner::for_test(Some("/usr/bin/bwrap".into()), true, false);
        // On macOS the platform branch may downgrade to raw — we
        // only assert the host-net check passed (no error).
        let res = runner.wrap_command(&m, Path::new("/state"), "/bin/echo", &[]);
        assert!(res.is_ok(), "{res:?}");
    }

    #[test]
    fn wrap_bwrap_missing_warns_in_non_strict_mode() {
        let m = manifest_with_sandbox(
            "[plugin.sandbox]\nenabled = true\nfs_read_paths = [\"/etc/ssl\"]",
        );
        let runner = SandboxRunner::for_test(None, false, false);
        if cfg!(target_os = "macos") {
            // macOS branch fires first — separate test covers
            // that path.
            return;
        }
        let wrapped = runner
            .wrap_command(&m, Path::new("/state"), "/bin/echo", &[])
            .unwrap();
        assert_eq!(wrapped.program, PathBuf::from("/bin/echo"));
        assert!(wrapped.diagnostic.is_some());
        let diag = wrapped.diagnostic.unwrap();
        assert!(diag.contains("bwrap"), "{diag}");
    }

    #[test]
    fn wrap_bwrap_missing_with_strict_errors() {
        let m = manifest_with_sandbox("[plugin.sandbox]\nenabled = true");
        let runner = SandboxRunner::for_test(None, false, true);
        if cfg!(target_os = "macos") {
            // strict + macos hits UnsupportedPlatformAndRequired
            // first; assertion in dedicated macos test.
            return;
        }
        let err = runner
            .wrap_command(&m, Path::new("/state"), "/bin/echo", &[])
            .unwrap_err();
        assert!(
            matches!(err, SandboxError::BwrapMissingButRequired { .. }),
            "{err:?}"
        );
    }

    #[test]
    fn build_bwrap_args_includes_unshare_net_for_deny_mode() {
        let sandbox = SandboxSection {
            enabled: true,
            network: SandboxNetwork::Deny,
            fs_read_paths: vec![],
            fs_write_paths: vec![],
            drop_user: false,
        };
        let argv = build_bwrap_args("p", &sandbox, Path::new("/state"), "/bin/echo", &[]).unwrap();
        assert!(
            argv.iter().any(|a| *a == OsString::from("--unshare-net")),
            "deny mode must include --unshare-net: {argv:?}"
        );
    }

    #[test]
    fn build_bwrap_args_omits_unshare_net_for_host_mode() {
        let sandbox = SandboxSection {
            enabled: true,
            network: SandboxNetwork::Host,
            fs_read_paths: vec![],
            fs_write_paths: vec![],
            drop_user: false,
        };
        let argv = build_bwrap_args("p", &sandbox, Path::new("/state"), "/bin/echo", &[]).unwrap();
        assert!(
            !argv.iter().any(|a| *a == OsString::from("--unshare-net")),
            "host mode must omit --unshare-net: {argv:?}"
        );
    }

    #[test]
    fn build_bwrap_args_includes_uid_gid_for_drop_user() {
        let sandbox = SandboxSection {
            enabled: true,
            network: SandboxNetwork::Deny,
            fs_read_paths: vec![],
            fs_write_paths: vec![],
            drop_user: true,
        };
        let argv = build_bwrap_args("p", &sandbox, Path::new("/state"), "/bin/echo", &[]).unwrap();
        // Find `--uid` followed by 65534.
        let uid_pos = argv.iter().position(|a| *a == OsString::from("--uid"));
        assert!(uid_pos.is_some());
        let uid_val = &argv[uid_pos.unwrap() + 1];
        assert_eq!(uid_val, &OsString::from("65534"));
    }

    #[test]
    fn build_bwrap_args_expands_state_dir_token_in_write_paths() {
        let sandbox = SandboxSection {
            enabled: true,
            network: SandboxNetwork::Deny,
            fs_read_paths: vec![],
            fs_write_paths: vec!["${state_dir}".into(), "${state_dir}/cache".into()],
            drop_user: false,
        };
        let argv = build_bwrap_args(
            "p",
            &sandbox,
            Path::new("/var/lib/nexo/plugins/p"),
            "/bin/echo",
            &[],
        )
        .unwrap();
        // After expansion, --bind <state_dir> <state_dir> appears.
        let bind_count = argv
            .iter()
            .filter(|a| **a == OsString::from("--bind"))
            .count();
        assert_eq!(bind_count, 2);
        assert!(argv
            .iter()
            .any(|a| a == &OsString::from("/var/lib/nexo/plugins/p")));
        assert!(argv
            .iter()
            .any(|a| a == &OsString::from("/var/lib/nexo/plugins/p/cache")));
    }

    #[test]
    fn build_bwrap_args_appends_command_after_double_dash() {
        let sandbox = SandboxSection::default();
        let mut s = sandbox;
        s.enabled = true;
        let argv = build_bwrap_args(
            "p",
            &s,
            Path::new("/state"),
            "/usr/bin/python3",
            &["-c".into(), "print(1)".into()],
        )
        .unwrap();
        let dd_pos = argv
            .iter()
            .position(|a| *a == OsString::from("--"))
            .expect("argv must contain --");
        assert_eq!(argv[dd_pos + 1], OsString::from("/usr/bin/python3"));
        assert_eq!(argv[dd_pos + 2], OsString::from("-c"));
        assert_eq!(argv[dd_pos + 3], OsString::from("print(1)"));
    }

    #[test]
    fn validate_resolved_path_rejects_denylist_after_state_dir_expansion() {
        // state_dir resolves to a denylisted path — operator
        // misconfig caught at spawn time.
        let err = validate_resolved_path("p", "${state_dir}", "/etc/shadow").unwrap_err();
        assert!(
            matches!(err, SandboxError::ResolvedPathHitsDenylist { .. }),
            "{err:?}"
        );
    }
}