Skip to main content

openmoat_core/
policy.rs

1//! Policy file model, loading and linting (docs/POLICY.md).
2
3use std::collections::{BTreeMap, BTreeSet};
4
5/// Fallback verdicts when no rule matches: one verdict for everything, or a map
6/// from action kind (`shell`, `fs.read`, `fs.write`, `net`, `fetch`,
7/// `web.search`, `tool.unknown`, `env.read`, `env.set`, `mcp`, or `"*"`) to a verdict; a `fetch`
8/// without its own entry takes the `net` one. This is how "outbound network is
9/// deny-by-default" is expressed, because deny rules are absolute and cannot
10/// be punched through by allow rules (ADR-002).
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12#[serde(untagged)]
13pub enum Defaults {
14    /// The same verdict for every kind.
15    All(Verdict),
16    /// A verdict per kind name (`net`, `fs.read`, …), with `*` as the fallback.
17    PerKind(BTreeMap<String, Verdict>),
18}
19
20impl Defaults {
21    /// Verdict and synthetic rule id (`default` or `default.<kind>`) for a kind.
22    #[must_use]
23    pub fn for_kind(&self, kind: Kind) -> (Verdict, String) {
24        match self {
25            Self::All(v) => (*v, "default".to_owned()),
26            Self::PerKind(map) => kind
27                .rule_kinds()
28                .iter()
29                .find_map(|k| map.get(k.as_str()).map(|v| (*v, format!("default.{k}"))))
30                .unwrap_or_else(|| {
31                    (
32                        map.get("*").copied().unwrap_or(Verdict::Ask),
33                        "default".to_owned(),
34                    )
35                }),
36        }
37    }
38
39    /// The default for an atom no rule matched: [`Self::for_kind`], except that a
40    /// word that only looks like a host is no network evidence and gets
41    /// [`Self::unclassified`] (ADR-025).
42    #[must_use]
43    pub fn for_atom(&self, atom: &crate::AtomicAction) -> (Verdict, String) {
44        match atom {
45            crate::AtomicAction::NetWord { .. } => (self.unclassified(), "default".to_owned()),
46            _ => self.for_kind(atom.kind()),
47        }
48    }
49
50    /// What OpenMoat gives a call it cannot classify, or a word that only looks
51    /// like a host: `defer` under a catch-all (`"*"`) of `allow` or `defer`, else
52    /// `ask` (ADR-025). Never `allow`, and never a deny nothing named.
53    #[must_use]
54    pub fn unclassified(&self) -> Verdict {
55        let catch_all = match self {
56            Self::All(v) => *v,
57            Self::PerKind(map) => map.get("*").copied().unwrap_or(Verdict::Ask),
58        };
59        catch_all.clamp(Verdict::Defer, Verdict::Ask)
60    }
61}
62
63use serde::{Deserialize, Serialize};
64use thiserror::Error;
65
66use crate::kind::Kind;
67use crate::pattern::{GlobPattern, ShellPattern};
68use crate::secret::{self, Secret};
69use crate::taint::{self, TaintSettings};
70use crate::verdict::Verdict;
71
72mod hook_only;
73
74/// The policy schema version (`version:`) this build reads; any other is an error.
75pub const SUPPORTED_VERSION: u32 = 1;
76
77/// Why a policy file cannot be used. Any of these keeps `guard` from deciding.
78///
79/// Messages include their cause, so no variant also exposes it as `source`:
80/// a chained report (`{:#}`) would print it twice.
81#[derive(Debug, Error)]
82#[non_exhaustive]
83pub enum PolicyError {
84    /// The file is not valid YAML or does not fit the schema.
85    #[error("policy parse error: {0}")]
86    Parse(crate::yaml::Error),
87    /// The `version` key names a schema this build does not know.
88    #[error("unsupported policy version {found}; this build supports {supported}")]
89    Version {
90        /// The version in the file.
91        found: u32,
92        /// The version this build supports.
93        supported: u32,
94    },
95    /// Two rule groups share an id.
96    #[error("duplicate rule id `{0}`")]
97    DuplicateId(String),
98    /// A rule group lists no patterns.
99    #[error("rule `{0}` has no patterns")]
100    EmptyRule(String),
101    /// A pattern is empty (or only `!`/`$`).
102    #[error("empty pattern")]
103    EmptyPattern,
104    /// A glob does not compile.
105    #[error("bad glob `{pattern}`: {error}")]
106    BadGlob {
107        /// The pattern as written.
108        pattern: String,
109        /// The glob compiler's explanation.
110        error: globset::Error,
111    },
112    /// A shell pattern does not tokenise (unbalanced quotes, a here-document).
113    #[error("bad shell pattern `{pattern}` (unbalanced quotes?)")]
114    BadShellPattern {
115        /// The pattern as written.
116        pattern: String,
117    },
118    /// A rule group or `executables` entry is invalid; `problem` says why.
119    #[error("rule `{rule}`: {problem}")]
120    Rule {
121        /// Rule id, or `executables.<name>`.
122        rule: String,
123        /// What is wrong.
124        problem: String,
125    },
126}
127
128/// One named group of patterns. A group matches when **any** of its patterns
129/// matches the atomic action of the corresponding kind.
130#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
131#[serde(deny_unknown_fields)]
132pub struct RuleGroup {
133    /// Stable identifier shown in decisions and the audit log.
134    pub id: String,
135    /// Text prepended to the reason when this group decides.
136    #[serde(default, skip_serializing_if = "Option::is_none")]
137    pub reason: Option<String>,
138    /// Shell patterns (token prefixes, `*`, `!`, trailing `$`).
139    #[serde(default, skip_serializing_if = "Vec::is_empty")]
140    pub shell: Vec<String>,
141    /// Path globs for file reads.
142    #[serde(default, rename = "fs.read", skip_serializing_if = "Vec::is_empty")]
143    pub fs_read: Vec<String>,
144    /// Path globs for file writes.
145    #[serde(default, rename = "fs.write", skip_serializing_if = "Vec::is_empty")]
146    pub fs_write: Vec<String>,
147    /// Host globs for any network access, fetches included.
148    #[serde(default, skip_serializing_if = "Vec::is_empty")]
149    pub net: Vec<String>,
150    /// Host globs for read-only fetches by a host's fetch tool only.
151    #[serde(default, skip_serializing_if = "Vec::is_empty")]
152    pub fetch: Vec<String>,
153    /// Variable-name globs for reads.
154    #[serde(default, rename = "env.read", skip_serializing_if = "Vec::is_empty")]
155    pub env_read: Vec<String>,
156    /// Variable-name globs for sets.
157    #[serde(default, rename = "env.set", skip_serializing_if = "Vec::is_empty")]
158    pub env_set: Vec<String>,
159    /// MCP tool-name globs.
160    #[serde(default, skip_serializing_if = "Vec::is_empty")]
161    pub mcp: Vec<String>,
162    /// Applied by the hook only (ADR-034): OS layers leave this group's
163    /// `fs.read` and `fs.write` paths to the other rules, so a build tool a
164    /// sandboxed command runs can still read them. `deny` groups only, never
165    /// with `net` or `fetch`.
166    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
167    pub hook_only: bool,
168}
169
170impl RuleGroup {
171    /// The patterns this group lists for `kind`.
172    #[must_use]
173    pub fn patterns(&self, kind: Kind) -> &[String] {
174        match kind {
175            Kind::Shell => &self.shell,
176            Kind::FsRead => &self.fs_read,
177            Kind::FsWrite => &self.fs_write,
178            Kind::Net => &self.net,
179            Kind::Fetch => &self.fetch,
180            // A query or an unknown tool names no host or path to match; only
181            // `defaults` decide them.
182            Kind::WebSearch | Kind::UnknownTool => &[],
183            Kind::EnvRead => &self.env_read,
184            Kind::EnvSet => &self.env_set,
185            Kind::Mcp => &self.mcp,
186        }
187    }
188
189    fn is_empty(&self) -> bool {
190        Kind::ALL.iter().all(|k| self.patterns(*k).is_empty())
191    }
192}
193
194#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
195#[serde(deny_unknown_fields)]
196pub struct Scope {
197    #[serde(default = "default_roots")]
198    pub project_roots: Vec<String>,
199}
200
201fn default_roots() -> Vec<String> {
202    vec![".".to_owned()]
203}
204
205impl Default for Scope {
206    fn default() -> Self {
207        Self {
208            project_roots: default_roots(),
209        }
210    }
211}
212
213#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
214#[serde(rename_all = "lowercase")]
215pub enum ApprovalChannel {
216    Terminal,
217    Telegram,
218}
219
220#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
221#[serde(rename_all = "lowercase")]
222pub enum RememberScope {
223    Once,
224    Session,
225    Permanent,
226}
227
228#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
229#[serde(deny_unknown_fields)]
230pub struct Approval {
231    #[serde(default = "default_channel")]
232    pub channel: ApprovalChannel,
233    #[serde(default = "default_remember")]
234    pub remember: RememberScope,
235    #[serde(default = "default_timeout")]
236    pub timeout_s: u32,
237}
238
239fn default_channel() -> ApprovalChannel {
240    ApprovalChannel::Terminal
241}
242fn default_remember() -> RememberScope {
243    RememberScope::Session
244}
245fn default_timeout() -> u32 {
246    300
247}
248
249impl Default for Approval {
250    fn default() -> Self {
251        Self {
252            channel: default_channel(),
253            remember: default_remember(),
254            timeout_s: default_timeout(),
255        }
256    }
257}
258
259/// The policy document (`policy.yaml`), as written by users.
260#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
261#[serde(deny_unknown_fields)]
262pub struct Policy {
263    /// Schema version; only 1 is supported.
264    pub version: u32,
265    /// Verdict when no rule matches.
266    #[serde(default = "default_verdict")]
267    pub defaults: Defaults,
268    /// Reserved for multi-root projects; parsed so existing files stay valid,
269    /// not yet consulted (`policy lint` warns).
270    #[serde(default, skip_serializing_if = "Option::is_none")]
271    pub scope: Option<Scope>,
272    /// Evaluated first; a match is final.
273    #[serde(default)]
274    pub deny: Vec<RuleGroup>,
275    /// Evaluated after `deny`.
276    #[serde(default)]
277    pub allow: Vec<RuleGroup>,
278    /// Evaluated after `allow`.
279    #[serde(default)]
280    pub ask: Vec<RuleGroup>,
281    /// Evaluated after `ask`: calls the agent's own permission flow decides
282    /// (ADR-025). Absent in policies written before it existed.
283    #[serde(default, skip_serializing_if = "Vec::is_empty")]
284    pub defer: Vec<RuleGroup>,
285    /// Reserved for the kernel's own approval prompt; parsed so existing files
286    /// stay valid, not yet consulted (`policy lint` warns).
287    #[serde(default, skip_serializing_if = "Option::is_none")]
288    pub approval: Option<Approval>,
289    /// Basename → allowed absolute paths (defeats PATH poisoning).
290    #[serde(default)]
291    pub executables: BTreeMap<String, Vec<String>>,
292    /// What the operating-system layers OpenMoat configures need beyond the rules
293    /// (ADR-018). Absent in policies written before it existed.
294    #[serde(default, skip_serializing_if = "Option::is_none")]
295    pub sandbox: Option<SandboxSettings>,
296    /// Secrets `moat proxy` keeps from the agent and injects for one host each.
297    #[serde(default, skip_serializing_if = "Vec::is_empty")]
298    pub secrets: Vec<Secret>,
299    /// What session taint protects beyond the built-in list. Absent in older policies.
300    #[serde(default, skip_serializing_if = "Option::is_none")]
301    pub taint: Option<TaintSettings>,
302    /// Trust a repository policy only when its detached signature
303    /// (`policy.yaml.sig`) verifies against a key the person trusted (ADR-031):
304    /// a signed one needs no `moat trust`, and an unsigned one is not trusted even
305    /// after `moat trust`. Off by default: then only `moat trust` lets a
306    /// repository policy allow.
307    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
308    pub require_signed_repo_policy: bool,
309    /// How `moat mcp proxy` treats tool definitions (ADR-032). Absent in older policies.
310    #[serde(default, skip_serializing_if = "Option::is_none")]
311    pub mcp: Option<McpSettings>,
312    /// Ask rules merged in from a repository policy ([`crate::RepoPolicy`],
313    /// ADR-022) or a policy pack (ADR-031). Tried after `deny` and before
314    /// `allow`, so a repository can make an action the user allows ask. Never
315    /// read from or written to a file, and not lowered to OS layers, which are
316    /// generated from the user policy alone.
317    #[serde(skip)]
318    pub repo_ask: Vec<RuleGroup>,
319}
320
321/// The `sandbox:` section (ADR-018, ADR-019).
322#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
323#[serde(deny_unknown_fields)]
324pub struct SandboxSettings {
325    /// Paths (directories or files) the OS layers let commands read although no
326    /// allow rule covers them: system and toolchain directories every build
327    /// reads. The hook decision ignores them, and deny rules win over them in
328    /// every layer. Absolute or `~/…`, never a glob.
329    #[serde(default)]
330    pub read_roots: Vec<String>,
331    /// The loopback port `moat proxy` listens on and the host sandboxes send
332    /// their commands' traffic to; absent, the CLI's default.
333    #[serde(default, skip_serializing_if = "Option::is_none")]
334    pub proxy_port: Option<std::num::NonZeroU16>,
335    /// Let `moat proxy` forward a TLS `ClientHello` that carries an encrypted
336    /// server name (ECH, ESNI). Off, such a tunnel is refused: the server acts
337    /// on the encrypted name, which may be another site than the allowed host.
338    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
339    pub proxy_allow_ech: bool,
340    /// Let `moat proxy` serve a request that carries no session credential,
341    /// with no session (ADR-044). Off, such a request gets 407: a session's
342    /// taint and connection share cannot be escaped by sending no credential,
343    /// and other local processes cannot use the proxy. `moat run` ignores it.
344    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
345    pub proxy_serve_without_credential: bool,
346    /// Days a package version must have been published before the package
347    /// managers `moat run` starts install it (ADR-030); `0` turns this off,
348    /// absent is 7 days ([`Policy::package_min_release_age_days`]).
349    #[serde(default, skip_serializing_if = "Option::is_none")]
350    pub package_min_release_age_days: Option<u16>,
351}
352
353/// The `mcp:` section (ADR-032).
354#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
355#[serde(deny_unknown_fields)]
356pub struct McpSettings {
357    /// Pin every tool `moat mcp proxy` sees from a server it has no pins for,
358    /// as it is, instead of holding them until `moat mcp accept`. Off by
359    /// default: a server that is malicious on first contact would get its
360    /// tool descriptions pinned as good.
361    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
362    pub trust_first_sight: bool,
363    /// How an MCP call that only opens or reads one web page is decided
364    /// (ADR-047): `fetch` (absent) as a `fetch` of its host, like `WebFetch`;
365    /// `net` as a `net` connection, which the default policy denies for
366    /// unlisted hosts.
367    #[serde(default, skip_serializing_if = "Option::is_none")]
368    pub browser_open: Option<BrowserOpen>,
369}
370
371/// `mcp.browser_open`: the kind a read-only page open of an MCP tool is decided as.
372#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
373#[serde(rename_all = "lowercase")]
374pub enum BrowserOpen {
375    /// A `fetch` of the page's host (the default).
376    Fetch,
377    /// A `net` connection to the page's host, as any other MCP URL argument.
378    Net,
379}
380
381/// The release age `moat run` asks of new package versions when the policy
382/// does not say (ADR-030).
383pub const DEFAULT_PACKAGE_MIN_RELEASE_AGE_DAYS: u16 = 7;
384
385impl Policy {
386    /// The days a package version must have been published before `moat run`
387    /// lets npm and uv install it; `0` is off.
388    #[must_use]
389    pub fn package_min_release_age_days(&self) -> u16 {
390        self.sandbox
391            .as_ref()
392            .and_then(|s| s.package_min_release_age_days)
393            .unwrap_or(DEFAULT_PACKAGE_MIN_RELEASE_AGE_DAYS)
394    }
395
396    /// Whether `moat mcp proxy` pins a new server's tools without a person
397    /// accepting them (`mcp.trust_first_sight`, ADR-032).
398    #[must_use]
399    pub fn trust_mcp_first_sight(&self) -> bool {
400        self.mcp.as_ref().is_some_and(|m| m.trust_first_sight)
401    }
402
403    /// Whether an MCP call that only opens a web page is decided as a `fetch`
404    /// (`mcp.browser_open`, ADR-047): yes unless the policy says `net`.
405    #[must_use]
406    pub fn browser_open_is_fetch(&self) -> bool {
407        self.mcp.as_ref().and_then(|m| m.browser_open) != Some(BrowserOpen::Net)
408    }
409}
410
411fn default_verdict() -> Defaults {
412    Defaults::All(Verdict::Ask)
413}
414
415impl Policy {
416    /// Parse and lint a policy document. Pure: takes the YAML text.
417    pub fn parse(yaml: &str) -> Result<Self, PolicyError> {
418        let policy: Self = crate::yaml::from_str(yaml).map_err(PolicyError::Parse)?;
419        policy.lint()?;
420        Ok(policy)
421    }
422
423    /// Structural validation independent of any environment.
424    pub fn lint(&self) -> Result<(), PolicyError> {
425        if self.version != SUPPORTED_VERSION {
426            return Err(PolicyError::Version {
427                found: self.version,
428                supported: SUPPORTED_VERSION,
429            });
430        }
431        let mut ids = BTreeSet::new();
432        let groups = self.deny.iter().chain(&self.repo_ask);
433        for group in groups
434            .chain(&self.allow)
435            .chain(&self.ask)
436            .chain(&self.defer)
437        {
438            if group.id.trim().is_empty() {
439                return Err(PolicyError::Rule {
440                    rule: "<unnamed>".into(),
441                    problem: "missing id".into(),
442                });
443            }
444            if !ids.insert(group.id.clone()) {
445                return Err(PolicyError::DuplicateId(group.id.clone()));
446            }
447            if group.is_empty() {
448                return Err(PolicyError::EmptyRule(group.id.clone()));
449            }
450            for kind in Kind::ALL {
451                for p in group.patterns(kind) {
452                    let compiled = if kind == Kind::Shell {
453                        ShellPattern::compile(p).map(drop)
454                    } else {
455                        GlobPattern::compile(p, false).map(drop)
456                    };
457                    compiled.map_err(|e| rule_err(&group.id, &e))?;
458                }
459            }
460        }
461        hook_only::check(self)?;
462        for (name, paths) in &self.executables {
463            if paths.is_empty() {
464                return Err(PolicyError::Rule {
465                    rule: format!("executables.{name}"),
466                    problem: "no paths listed".into(),
467                });
468            }
469            if let Some(bad) = paths.iter().find(|p| !crate::paths::is_absolute(p)) {
470                return Err(PolicyError::Rule {
471                    rule: format!("executables.{name}"),
472                    problem: format!("`{bad}` is not an absolute path"),
473                });
474            }
475        }
476        for root in self.sandbox.iter().flat_map(|s| &s.read_roots) {
477            lint_read_root(root)?;
478        }
479        taint::check(self.taint.as_ref())?;
480        secret::check(&self.secrets)
481    }
482
483    /// Number of rule groups in `deny`, `allow` and `ask`.
484    #[must_use]
485    pub fn rule_count(&self) -> (usize, usize, usize) {
486        (self.deny.len(), self.allow.len(), self.ask.len())
487    }
488}
489
490/// An OS layer takes a read root literally, and a root that is the disk or the
491/// home directory would undo every default deny.
492fn lint_read_root(root: &str) -> Result<(), PolicyError> {
493    let trimmed = root.trim_end_matches(['/', '\\']);
494    let problem = if !(crate::paths::is_absolute(root) || root.starts_with("~/")) {
495        "must be absolute or start with `~/`"
496    } else if root.contains(['*', '?', '[', ']', '{', '}', '!', '$']) {
497        "must be a plain path, not a glob or a variable"
498    } else if root.split(['/', '\\']).any(|s| s == "." || s == "..") {
499        "must not contain `.` or `..` segments"
500    } else if trimmed == "~" {
501        "must not be the home directory"
502    } else if trimmed.is_empty() || (trimmed.len() == 2 && trimmed.ends_with(':')) {
503        "must not be a filesystem root"
504    } else {
505        return Ok(());
506    };
507    Err(PolicyError::Rule {
508        rule: "sandbox.read_roots".to_owned(),
509        problem: format!("`{root}` {problem}"),
510    })
511}
512
513fn rule_err(rule: &str, e: &PolicyError) -> PolicyError {
514    PolicyError::Rule {
515        rule: rule.to_owned(),
516        problem: e.to_string(),
517    }
518}
519
520#[cfg(test)]
521mod tests;