openmoat-core 0.19.0

Policy model and decision engine for OpenMoat. Pure: no I/O, no OS calls.
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
//! Injected input (ADR-057): which call outputs are untrusted, what of them is
//! kept, and how a later call that acts on it is tightened.
//!
//! Only tightens: a later shell command or write whose network target, path
//! or command came from untrusted content needs review, unless the policy
//! allows that part of it outright (a host on the allow list, a project
//! file, `cargo build`). A fetch, a search, an MCP call or an unknown tool
//! is not checked: following a link or a search result is how reading works,
//! and the host's own prompt decides those (`fetch: defer`).

use super::{CompiledPolicy, EvalContext, UntrustedKind, checked_atoms};
use crate::action::{Action, AtomicAction};
use crate::injection::{
    self, InjectedLookup, MAX_COMMAND_LEN, MAX_TOKENS_PER_OUTPUT, MIN_COMMAND_LEN, Token, TokenKind,
};
use crate::programs::NoResolver;
use crate::realpath::PathResolver;
use crate::verdict::{Decision, Verdict};

/// Programs whose output is text from a forge's issues, pull requests and
/// comments, which anyone may write: their hosts are on the allow list
/// (`registries`), so they are named here.
const REMOTE_TEXT_PROGRAMS: [&str; 3] = ["gh", "glab", "tea"];

/// The longest source description kept with a token.
const MAX_SOURCE_LEN: usize = 200;

impl CompiledPolicy<'_> {
    /// What the output of `action`, run in `cwd`, is, when it is content an
    /// attacker may control (`fetch https://docs.example/page`): what session
    /// taint calls untrusted (a fetch, a web search, an MCP result, an unknown
    /// tool; a metadata tool too, which does not taint the session, since a
    /// page sets the title and URL a tab list shows), and a shell command that
    /// reads from a network host the policy does not allow or from a forge's
    /// command-line client (`gh issue view`). `None` for everything else: a
    /// read of a file, a local command.
    #[must_use]
    pub fn output_source(
        &self,
        action: &Action,
        cwd: &str,
        paths: &dyn PathResolver,
    ) -> Option<String> {
        let ctx = EvalContext {
            cwd: cwd.to_owned(),
            ..self.ctx.clone()
        };
        let (atoms, _) = checked_atoms(action, &ctx, self.opens_as(), paths);
        if let Some(exposed) = atoms.iter().find(|atom| UntrustedKind::of(atom).is_some()) {
            let source = match action.shown() {
                Action::Fetch { url } => format!("the page fetched from {url}"),
                Action::WebSearch { query } => format!("the web search results for \"{query}\""),
                Action::McpTool { name, .. } => format!("the result of MCP tool {name}"),
                Action::UnknownTool { tool, .. } => format!("the output of tool {tool}"),
                Action::Shell { command } => format!("the output of `{command}`"),
                _ => exposed.describe(),
            };
            return Some(cut(source));
        }
        let Action::Shell { command } = action.shown() else {
            return None;
        };
        let reads_remote = atoms.iter().any(|atom| match atom {
            AtomicAction::Net { host } | AtomicAction::Fetch { host } => {
                !crate::host::is_loopback(host) && !self.allows(atom)
            }
            AtomicAction::Shell { argv } => argv
                .first()
                .is_some_and(|p| REMOTE_TEXT_PROGRAMS.contains(&program_name(p))),
            _ => false,
        });
        reads_remote.then(|| cut(format!("the output of `{command}`")))
    }

    /// The tokens of `output`, the output of `source` (an action
    /// [`Self::output_source`] names), worth keeping: hosts other than the
    /// ones `source` itself reached and than loopback, absolute paths outside
    /// the project, and commands of its code spans as the classifier reads
    /// them. Hosts are kept in their canonical spelling
    /// (`host::canonical`, ADR-063 §4), as a later call's hosts are
    /// compared. De-duplicated, at most [`MAX_TOKENS_PER_OUTPUT`]: URL hosts
    /// first, then commands, paths, and words that look like hosts.
    #[must_use]
    pub fn injected_tokens(&self, source: &Action, cwd: &str, output: &str) -> Vec<Token> {
        let found = injection::candidates(output);
        let ctx = EvalContext {
            cwd: cwd.to_owned(),
            ..self.ctx.clone()
        };
        let (source_atoms, _) = checked_atoms(source, &ctx, self.opens_as(), &NoResolver);
        // Never kept, so never a parent a later call links to (ADR-063 §4):
        // `docs.github.com` named by a page on `github.com` links nothing new.
        let own_hosts: Vec<String> = source_atoms
            .iter()
            .filter_map(host_of)
            .map(crate::host::canonical)
            .collect();
        let host_token = |kind: TokenKind| {
            let own_hosts = &own_hosts;
            move |host: String| {
                let text = crate::host::canonical(&host);
                let foreign = !own_hosts.contains(&text) && !crate::host::is_loopback(&host);
                foreign.then_some(Token { kind, text })
            }
        };
        let commands = found
            .commands
            .iter()
            .flat_map(|line| self.commands_of(line, &ctx));
        let paths = found.paths.iter().filter_map(|raw| {
            let path = crate::paths::resolve(raw, &ctx.home, ctx.project.as_deref(), None)?;
            (path.len() > 1 && !self.in_project(&path)).then(|| Token {
                kind: TokenKind::Path,
                text: self.path_key(&path),
            })
        });
        let mut tokens: Vec<Token> = Vec::new();
        let all = found
            .url_hosts
            .into_iter()
            .filter_map(host_token(TokenKind::UrlHost))
            .chain(commands)
            .chain(paths)
            .chain(
                found
                    .word_hosts
                    .into_iter()
                    .filter_map(host_token(TokenKind::WordHost)),
            );
        for token in all {
            if tokens.len() >= MAX_TOKENS_PER_OUTPUT {
                break;
            }
            if !tokens.contains(&token) {
                tokens.push(token);
            }
        }
        tokens
    }

    /// `decision` for `action`, tightened by the tokens of untrusted content
    /// `seen` knows: a shell command or a write needs review when a host it
    /// reaches, a file it writes or a command it runs came from that content,
    /// and the policy does not allow that atom. A command counts only when the
    /// call also reaches a host or a file outside the project that the policy
    /// does not allow. Never loosens; a deny stays.
    #[must_use]
    pub fn with_injected(
        &self,
        decision: Decision,
        action: &Action,
        paths: &dyn PathResolver,
        seen: &dyn InjectedLookup,
    ) -> Decision {
        let checked = matches!(
            action.shown(),
            Action::Shell { .. } | Action::FsWrite { .. } | Action::Patch { .. }
        );
        if !checked || decision.verdict == Verdict::Deny {
            return decision;
        }
        let (atoms, _) = self.atoms(action, paths);
        // A command copied from the content counts only when it reaches past
        // what the policy allows outside the project: a host or a file. Docs
        // tell people to run `git push origin main`; that is daily work.
        let reaches_outside = atoms.iter().any(|atom| match atom {
            AtomicAction::Net { .. } | AtomicAction::Fetch { .. } => !self.allows(atom),
            AtomicAction::FsRead { path } | AtomicAction::FsWrite { path } => {
                !self.in_project(path) && !self.allows(atom)
            }
            _ => false,
        });
        let found = atoms
            .iter()
            .filter(|atom| reaches_outside || !matches!(atom, AtomicAction::Shell { .. }))
            .filter_map(|atom| self.injected(atom, seen))
            .collect();
        planted(found).into_iter().fold(decision, |mut acc, next| {
            acc.merge(next);
            acc
        })
    }

    /// The part of `atom` that came from untrusted content, unless the policy
    /// allows the atom; `None` when no part did.
    fn injected(&self, atom: &AtomicAction, seen: &dyn InjectedLookup) -> Option<Found> {
        let (token, source) = match atom {
            AtomicAction::Net { host } | AtomicAction::Fetch { host } => {
                let kinds = [TokenKind::UrlHost, TokenKind::WordHost];
                probe(seen, host_tokens(host, &kinds))
                    .or_else(|| self.parent_source(atom, host, seen))
            }
            // a word that only looks like a host (`app.run`) is compared with
            // hosts of URLs alone, which prose does not invent, and not with
            // their parents: it is not a request to any of them
            AtomicAction::NetWord { host } => probe(seen, host_tokens(host, &[TokenKind::UrlHost])),
            AtomicAction::FsWrite { path } => probe(
                seen,
                [Token {
                    kind: TokenKind::Path,
                    text: self.path_key(path),
                }],
            ),
            // a command is compared as the classifier reads it, which also
            // yields the command a wrapper runs (`sudo`, `env`, `nohup`,
            // `sh -c`) and leaves `VAR=value` out, at keep and at check alike
            AtomicAction::Shell { argv } => probe(
                seen,
                command_text(argv).map(|text| Token {
                    kind: TokenKind::Command,
                    text,
                }),
            ),
            _ => None,
        }?;
        if self.allows(atom) {
            return None;
        }
        let what = match token.kind {
            TokenKind::UrlHost | TokenKind::WordHost => "host",
            TokenKind::Path => "path",
            TokenKind::Command => "command",
        };
        Some(Found {
            part: format!("the {what} `{}`", token.text),
            source,
        })
    }

    /// The kept URL host a parent domain of `host` matches, and its source:
    /// a kept URL host links a request to any of its subdomains (ADR-063 §4).
    /// One probe per parent, nearest first; a parent the policy allows on its
    /// own (`github.com`) links nothing, so a page naming an allowed site
    /// does not tighten every call to its subdomains. Word hosts are compared
    /// exactly: a name in prose is weaker evidence than a URL (ADR-057 §4).
    fn parent_source(
        &self,
        atom: &AtomicAction,
        host: &str,
        seen: &dyn InjectedLookup,
    ) -> Option<(Token, String)> {
        let host = crate::host::canonical(host);
        crate::host::parents(&host).find_map(|parent| {
            let token = Token {
                kind: TokenKind::UrlHost,
                text: parent.to_owned(),
            };
            let source = seen.source_of(&token)?;
            (!self.allows(&with_host(atom, parent))).then_some((token, source))
        })
    }

    /// Whether the policy decides `atom` `allow` on its own.
    fn allows(&self, atom: &AtomicAction) -> bool {
        self.evaluate_atomic(atom)
            .is_some_and(|d| d.verdict == Verdict::Allow)
    }

    /// The commands `line` runs, as [`Self::with_injected`] compares them.
    fn commands_of(&self, line: &str, ctx: &EvalContext) -> Vec<Token> {
        let action = Action::Shell {
            command: line.to_owned(),
        };
        let (atoms, _) = checked_atoms(&action, ctx, self.opens_as(), &NoResolver);
        atoms
            .iter()
            .filter_map(|atom| match atom {
                AtomicAction::Shell { argv } => command_text(argv),
                _ => None,
            })
            .map(|text| Token {
                kind: TokenKind::Command,
                text,
            })
            .collect()
    }

    /// Whether `path` is the project or below it, under either spelling.
    fn in_project(&self, path: &str) -> bool {
        let key = self.path_key(path);
        [&self.ctx.project, &self.ctx.real_project]
            .into_iter()
            .flatten()
            .any(|root| {
                let root = self.path_key(root);
                let root = root.trim_end_matches('/');
                key == root
                    || key
                        .strip_prefix(root)
                        .is_some_and(|rest| rest.starts_with('/'))
            })
    }

    /// `path` as tokens compare it: lowercase where file names ignore case.
    fn path_key(&self, path: &str) -> String {
        if self.ctx.case_insensitive_paths {
            path.to_lowercase()
        } else {
            path.to_owned()
        }
    }
}

/// The text a command is compared by: its words joined by spaces, when it has
/// arguments and is long enough to be distinctive.
fn command_text(argv: &[String]) -> Option<String> {
    let text = argv.join(" ");
    (argv.len() >= 2 && (MIN_COMMAND_LEN..=MAX_COMMAND_LEN).contains(&text.len())).then_some(text)
}

/// One part of a call that came from untrusted content, worded as the reason
/// names it (the host `x`), and where it came from.
struct Found {
    part: String,
    source: String,
}

/// One review per source of `found`, naming each part once: "the command `x`,
/// the host `h` and the path `p` came from …". A planted command makes several
/// atoms (the command, the host it reaches, the file it writes), and the
/// person reads one sentence for them, not one per atom.
fn planted(found: Vec<Found>) -> Vec<Decision> {
    let mut sources: Vec<(String, Vec<String>)> = Vec::new();
    for Found { part, source } in found {
        match sources.iter_mut().find(|(known, _)| *known == source) {
            Some((_, parts)) if !parts.contains(&part) => parts.push(part),
            Some(_) => {}
            None => sources.push((source, vec![part])),
        }
    }
    sources
        .into_iter()
        .map(|(source, parts)| {
            Decision::single(
                Verdict::Review,
                injection::INJECTED_RULE,
                format!(
                    "{} came from {source}, untrusted content this session read; the call may \
                     follow an instruction planted there",
                    listed(&parts)
                ),
            )
        })
        .collect()
}

/// `a`, `a and b`, `a, b and c`.
fn listed(parts: &[String]) -> String {
    match parts {
        [] => String::new(),
        [one] => one.clone(),
        [init @ .., last] => format!("{} and {last}", init.join(", ")),
    }
}

/// The first of `tokens` the session kept, with its source.
fn probe(
    seen: &dyn InjectedLookup,
    tokens: impl IntoIterator<Item = Token>,
) -> Option<(Token, String)> {
    tokens
        .into_iter()
        .find_map(|token| seen.source_of(&token).map(|source| (token, source)))
}

/// The tokens a call's `host` is compared with, one per kind of `kinds`: in
/// its canonical spelling ([`crate::host::canonical`]), which tokens are kept
/// in, then as written (lowercase, no trailing dot) when that differs, which
/// rows kept before ADR-063 hold for an address spelled another way.
fn host_tokens(host: &str, kinds: &[TokenKind]) -> Vec<Token> {
    let canonical = crate::host::canonical(host);
    let written = host.trim_end_matches('.').to_ascii_lowercase();
    let spellings = if written == canonical {
        vec![canonical]
    } else {
        vec![canonical, written]
    };
    spellings
        .iter()
        .flat_map(|text| {
            kinds.iter().map(|kind| Token {
                kind: *kind,
                text: text.clone(),
            })
        })
        .collect()
}

/// `atom`, a `Net` or `Fetch` atom, reaching `host` instead.
fn with_host(atom: &AtomicAction, host: &str) -> AtomicAction {
    let host = host.to_owned();
    if matches!(atom, AtomicAction::Fetch { .. }) {
        AtomicAction::Fetch { host }
    } else {
        AtomicAction::Net { host }
    }
}

/// The host a network atom reaches.
fn host_of(atom: &AtomicAction) -> Option<&str> {
    match atom {
        AtomicAction::Net { host }
        | AtomicAction::Fetch { host }
        | AtomicAction::NetWord { host } => Some(host),
        _ => None,
    }
}

/// The file name of a program word (`/usr/bin/gh` → `gh`).
fn program_name(word: &str) -> &str {
    word.rsplit(['/', '\\']).next().unwrap_or(word)
}

/// `text`, cut to [`MAX_SOURCE_LEN`] characters.
fn cut(text: String) -> String {
    if text.chars().count() <= MAX_SOURCE_LEN {
        return text;
    }
    let mut short: String = text.chars().take(MAX_SOURCE_LEN).collect();
    short.push('…');
    short
}

#[cfg(test)]
mod tests;