base/paths/sensitive/config_root.rs
1//! A configuration root placed in a directory, as the sandbox backends guard
2//! it: shut to writes, or — the instance's own — open only in the working
3//! entries of its `state/`, the same ones the gate leaves out of
4//! configuration.
5
6use std::path::PathBuf;
7
8use super::is_working_entry;
9use crate::paths::layout::{entry, STATE_ROOT_WORKING_DIRS};
10
11/// A configuration root in a directory, as [`super::SensitivePaths::config_roots_in`]
12/// gives it.
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct ConfigRootAt {
15 pub path: PathBuf,
16 /// `path` with its symbolic links resolved.
17 pub resolved: PathBuf,
18 /// The instance's own project directory: held even when absent, the
19 /// working entries of its `state/` left writable.
20 pub own: bool,
21}
22
23impl ConfigRootAt {
24 /// Whether a sandboxed command may write nothing in this root but what
25 /// the policy itself names writable: a foreign root, every entry of
26 /// which the gate takes for configuration
27 /// ([`super::SensitivePaths::names_configuration`]).
28 pub fn shut_to_writes(&self) -> bool {
29 !self.own
30 }
31
32 /// `<root>/state`, as given.
33 pub fn state_dir(&self) -> PathBuf {
34 self.path.join(entry::STATE)
35 }
36
37 /// Whether a sandboxed command may write the entry `name` directly in
38 /// this root's `state/`: in the instance's own, its working space
39 /// ([`super::SensitivePaths::is_engine_working_entry`]), which is all the gate
40 /// leaves out of configuration there; in a foreign one none
41 /// ([`Self::shut_to_writes`]). Nothing directly in the root itself, nor
42 /// `state/` as a whole.
43 pub fn writable_state_entry(&self, name: &str) -> bool {
44 !self.shut_to_writes() && is_working_entry(name)
45 }
46
47 /// The working entries of this root's `state/` as the root resolves,
48 /// whether or not they exist: what [`Self::writable_state_entry`] admits.
49 pub fn working_entries(&self) -> Vec<PathBuf> {
50 STATE_ROOT_WORKING_DIRS
51 .iter()
52 .filter(|name| self.writable_state_entry(name))
53 .map(|name| self.resolved.join(entry::STATE).join(name))
54 .collect()
55 }
56}