nono 0.67.1

Capability-based sandboxing library using Landlock (Linux) and Seatbelt (macOS)
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
//! Sandbox state persistence
//!
//! This module provides serialization of capability state for diagnostic purposes.

use crate::capability::{
    AccessMode, CapabilitySet, FsCapability, SocketScope, UnixSocketCapability, UnixSocketMode,
};
use crate::resource::ResourceLimits;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Serializable representation of sandbox state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SandboxState {
    /// Filesystem capabilities
    pub fs: Vec<FsCapState>,
    /// AF_UNIX socket capabilities (may be absent in states persisted
    /// by older nono builds; `#[serde(default)]` preserves backward compat).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub unix_sockets: Vec<UnixSocketCapState>,
    /// Whether network is blocked
    pub net_blocked: bool,
    /// Resource ceilings (currently memory). Absent in states from older nono
    /// builds; `#[serde(default)]` keeps those loadable. Plain numbers, so unlike
    /// paths they need no re-validation.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub resource_limits: Option<ResourceLimits>,
}

/// Serializable representation of a filesystem capability
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FsCapState {
    /// Original path as specified
    pub original: PathBuf,
    /// Resolved canonical path
    pub resolved: PathBuf,
    /// Access mode
    pub access: String,
    /// Whether this is a file (vs directory)
    pub is_file: bool,
}

/// Serializable representation of a [`UnixSocketCapability`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnixSocketCapState {
    /// Original path as specified
    pub original: PathBuf,
    /// Resolved canonical path
    pub resolved: PathBuf,
    /// Path matching scope for this socket grant.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub scope: Option<SocketScope>,
    /// Legacy state field from before `SocketScope`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub is_directory: Option<bool>,
    /// Mode string: "connect" or "connect+bind"
    pub mode: String,
}

impl SandboxState {
    /// Create state from a capability set
    #[must_use]
    pub fn from_caps(caps: &CapabilitySet) -> Self {
        Self {
            fs: caps
                .fs_capabilities()
                .iter()
                .map(|cap| FsCapState {
                    original: cap.original.clone(),
                    resolved: cap.resolved.clone(),
                    access: cap.access.to_string(),
                    is_file: cap.is_file,
                })
                .collect(),
            unix_sockets: caps
                .unix_socket_capabilities()
                .iter()
                .map(|cap| UnixSocketCapState {
                    original: cap.original.clone(),
                    resolved: cap.resolved.clone(),
                    scope: Some(cap.scope),
                    is_directory: None,
                    mode: cap.mode.to_string(),
                })
                .collect(),
            net_blocked: caps.is_network_blocked(),
            resource_limits: caps.resource_limits().copied(),
        }
    }

    /// Convert state back to a capability set
    ///
    /// Paths are re-validated through the standard constructors (`new_dir`/`new_file`)
    /// which canonicalize paths and verify existence. This prevents crafted JSON from
    /// injecting arbitrary paths that bypass validation.
    ///
    /// Returns an error if any path no longer exists or fails validation.
    pub fn to_caps(&self) -> crate::error::Result<CapabilitySet> {
        let mut caps = CapabilitySet::new();

        for fs_cap in &self.fs {
            let access = match fs_cap.access.as_str() {
                "read" => AccessMode::Read,
                "write" => AccessMode::Write,
                "read+write" => AccessMode::ReadWrite,
                other => {
                    return Err(crate::error::NonoError::ConfigParse(format!(
                        "invalid access mode in sandbox state: {other}"
                    )));
                }
            };

            // Re-validate through the standard constructors to ensure
            // path canonicalization and existence checks are applied.
            let cap = if fs_cap.is_file {
                FsCapability::new_file(&fs_cap.original, access)?
            } else {
                FsCapability::new_dir(&fs_cap.original, access)?
            };
            caps.add_fs(cap);
        }

        for sock in &self.unix_sockets {
            let mode = match sock.mode.as_str() {
                "connect" => UnixSocketMode::Connect,
                "connect+bind" => UnixSocketMode::ConnectBind,
                other => {
                    return Err(crate::error::NonoError::ConfigParse(format!(
                        "invalid unix socket mode in sandbox state: {other}"
                    )));
                }
            };

            // Reconstruct from the caller-supplied `original` so the
            // stored alias survives the roundtrip (macOS Seatbelt uses
            // it for dual-path emission when original != resolved).
            // Then validate that canonicalisation produced the same
            // `resolved` as was serialized. The check rejects two
            // failure modes with one test:
            //
            // - Filesystem drift between save and reload (symlink moved,
            //   ConnectBind pending path now exists, etc.).
            // - Crafted JSON smuggling: attacker sets an evil `original`
            //   and legit `resolved`; the reconstructed cap's actual
            //   resolved won't match the crafted one, so we reject.
            let scope = sock.scope.unwrap_or_else(|| {
                if sock.is_directory.unwrap_or(false) {
                    SocketScope::DirChildren
                } else {
                    SocketScope::File
                }
            });

            let cap = match scope {
                SocketScope::File => UnixSocketCapability::new_file(&sock.original, mode)?,
                SocketScope::DirChildren => UnixSocketCapability::new_dir(&sock.original, mode)?,
                SocketScope::DirSubtree => {
                    UnixSocketCapability::new_dir_subtree(&sock.original, mode)?
                }
            };
            if cap.resolved != sock.resolved {
                return Err(crate::error::NonoError::ConfigParse(format!(
                    "unix socket grant canonical path drifted at state reload: \
                     serialized resolved={}, actual resolved={}",
                    sock.resolved.display(),
                    cap.resolved.display(),
                )));
            }
            caps.add_unix_socket(cap);
        }

        caps.set_network_blocked(self.net_blocked);

        if let Some(limits) = self.resource_limits {
            caps = caps.with_resource_limits(limits);
        }

        Ok(caps)
    }

    /// Serialize state to JSON
    pub fn to_json(&self) -> crate::error::Result<String> {
        serde_json::to_string_pretty(self).map_err(|e| {
            crate::error::NonoError::ConfigParse(format!("Failed to serialize sandbox state: {e}"))
        })
    }

    /// Deserialize state from JSON
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        serde_json::from_str(json)
    }
}

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

    #[test]
    fn test_state_roundtrip() {
        let caps = CapabilitySet::new().block_network();
        let state = SandboxState::from_caps(&caps);

        assert!(state.net_blocked);
        assert!(state.fs.is_empty());

        let json = state.to_json().expect("serialize state");
        let restored = SandboxState::from_json(&json).expect("deserialize state");
        assert!(restored.net_blocked);
    }

    #[test]
    fn test_resource_limits_roundtrip() {
        use crate::resource::ResourceLimits;

        let caps = CapabilitySet::new().with_resource_limits(ResourceLimits {
            memory_bytes: Some(512 * 1024 * 1024),
        });
        let state = SandboxState::from_caps(&caps);
        assert_eq!(
            state.resource_limits.and_then(|l| l.memory_bytes),
            Some(512 * 1024 * 1024)
        );

        let json = state.to_json().expect("serialize state");
        let restored = SandboxState::from_json(&json).expect("deserialize state");
        let limits = restored.resource_limits.expect("limits survive roundtrip");
        assert_eq!(limits.memory_bytes, Some(512 * 1024 * 1024));

        // And back into a CapabilitySet.
        let caps2 = restored.to_caps().expect("to_caps");
        assert_eq!(caps2.resource_limits(), caps.resource_limits());
    }

    #[test]
    fn test_resource_limits_absent_in_legacy_state() {
        // A state JSON written before resource limits existed must still load.
        let json = r#"{ "fs": [], "net_blocked": false }"#;
        let state = SandboxState::from_json(json).expect("legacy state");
        assert!(state.resource_limits.is_none());
    }

    // ---- On-disk shape & backward-compat ----

    #[test]
    fn state_without_limits_omits_resource_limits_key() {
        let caps = CapabilitySet::new().block_network();
        let state = SandboxState::from_caps(&caps);
        assert!(state.resource_limits.is_none());

        // skip_serializing_if on the field: no limits => the key is absent in JSON,
        // which is exactly what a legacy reader expects to NOT find.
        let json = state.to_json().expect("serialize");
        let v: serde_json::Value = serde_json::from_str(&json).unwrap();
        let obj = v.as_object().expect("object");
        assert!(
            !obj.contains_key("resource_limits"),
            "with no limits the resource_limits key must be omitted, got {obj:?}"
        );
    }

    #[test]
    fn state_with_limits_serializes_inner_value() {
        use crate::resource::ResourceLimits;

        let caps = CapabilitySet::new()
            .block_network()
            .with_resource_limits(ResourceLimits {
                memory_bytes: Some(256 * 1024 * 1024),
            });
        let state = SandboxState::from_caps(&caps);

        // The serialized state must nest memory_bytes at resource_limits.memory_bytes
        // (the roundtrip test checks the value survives; this pins the JSON shape).
        let json = state.to_json().expect("serialize");
        let v: serde_json::Value = serde_json::from_str(&json).unwrap();
        let inner = v
            .get("resource_limits")
            .and_then(|r| r.get("memory_bytes"))
            .and_then(serde_json::Value::as_u64);
        assert_eq!(inner, Some(256 * 1024 * 1024));
    }

    #[test]
    fn legacy_state_defaults_resource_limits_and_unix_sockets() {
        // An older state JSON (predating these fields) has neither
        // resource_limits nor unix_sockets.
        // #[serde(default)] on both must fill them in (None / empty) without error
        // — the backward-compat guarantee for on-disk states from older builds.
        let json = r#"{ "fs": [], "net_blocked": true }"#;
        let state = SandboxState::from_json(json).expect("legacy state loads");
        assert!(state.resource_limits.is_none(), "absent field -> None");
        assert!(state.unix_sockets.is_empty(), "absent field -> empty vec");
        assert!(state.net_blocked);

        // Explicit null is equivalent to absent for the Option field.
        let json_null = r#"{ "fs": [], "net_blocked": false, "resource_limits": null }"#;
        let state_null = SandboxState::from_json(json_null).expect("null limits load");
        assert!(state_null.resource_limits.is_none());
    }

    #[test]
    fn test_to_caps_rejects_nonexistent_path() {
        let json = r#"{
            "fs": [{
                "original": "/nonexistent/crafted/path",
                "resolved": "/nonexistent/crafted/path",
                "access": "read+write",
                "is_file": false
            }],
            "net_blocked": false
        }"#;
        let state = SandboxState::from_json(json).unwrap();
        assert!(
            state.to_caps().is_err(),
            "to_caps must reject nonexistent paths"
        );
    }

    #[test]
    fn test_to_caps_rejects_invalid_access_mode() {
        let json = r#"{
            "fs": [{
                "original": "/tmp",
                "resolved": "/tmp",
                "access": "root-access",
                "is_file": false
            }],
            "net_blocked": false
        }"#;
        let state = SandboxState::from_json(json).unwrap();
        assert!(
            state.to_caps().is_err(),
            "to_caps must reject invalid access modes"
        );
    }

    #[test]
    fn test_unix_socket_state_roundtrip_preserves_original_and_resolved() {
        use tempfile::tempdir;
        let dir = tempdir().expect("tempdir");
        let sock = dir.path().join("a.sock");
        std::fs::write(&sock, b"").expect("stub");

        let caps = CapabilitySet::new()
            .allow_unix_socket(&sock, UnixSocketMode::Connect)
            .expect("grant");
        let state = SandboxState::from_caps(&caps);
        let restored = state.to_caps().expect("to_caps");

        let round = restored.unix_socket_capabilities();
        assert_eq!(round.len(), 1);
        let before = &caps.unix_socket_capabilities()[0];
        let after = &round[0];
        assert_eq!(after.resolved, before.resolved);
        assert_eq!(after.original, before.original);
        assert_eq!(after.mode, before.mode);
        assert_eq!(after.scope, before.scope);
    }

    #[test]
    fn test_unix_socket_state_legacy_is_directory_maps_to_dir_children() {
        use tempfile::tempdir;
        let dir = tempdir().expect("tempdir");
        let json = format!(
            r#"{{
            "fs": [],
            "unix_sockets": [{{
                "original": "{}",
                "resolved": "{}",
                "is_directory": true,
                "mode": "connect"
            }}],
            "net_blocked": false
        }}"#,
            dir.path().display(),
            dir.path().canonicalize().expect("canonicalize").display()
        );
        let state = SandboxState::from_json(&json).expect("state json");
        let caps = state.to_caps().expect("to_caps");
        let sockets = caps.unix_socket_capabilities();
        assert_eq!(sockets.len(), 1);
        assert_eq!(sockets[0].scope, SocketScope::DirChildren);
    }

    #[test]
    fn test_unix_socket_state_rejects_invalid_mode() {
        let json = r#"{
            "fs": [],
            "unix_sockets": [{
                "original": "/tmp",
                "resolved": "/tmp",
                "is_directory": true,
                "mode": "bind-only"
            }],
            "net_blocked": false
        }"#;
        let state = SandboxState::from_json(json).unwrap();
        assert!(
            state.to_caps().is_err(),
            "to_caps must reject unknown unix socket modes"
        );
    }
}