openmoat 0.23.1

Policy checks for AI coding agents: decides allow, defer, review or deny for each tool call the hook of Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot CLI and seven more agents reports, configures Claude Code's, Codex's and Cursor's own OS sandboxes from the same policy, and keeps a local audit log.
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
//! Reading a unified diff: the files it changes, their modes, and the lines it
//! adds, from `git diff` or from any tool that writes the unified format.
//!
//! Input that is not a diff is an error, never an empty change: a check that
//! read nothing must not pass. Hunks are read by their line counts, so a
//! removed line that reads `--- x` is never taken for the next file's header.

use std::fmt;

use openmoat_core::diffscan::{Block, Change};

/// One file a diff changes.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct FileChange {
    /// The path before the change; `None` for a new file.
    pub old: Option<String>,
    /// The path after the change; `None` for a deleted file.
    pub new: Option<String>,
    /// git's file mode before (`100644`, `100755`, `120000` a symbolic link,
    /// `160000` a submodule), when the diff gives it.
    pub old_mode: Option<String>,
    /// git's file mode after, when the diff gives it.
    pub new_mode: Option<String>,
    /// The diff only says the file is binary: none of its content was read.
    pub binary: bool,
    /// Each added line with its line number in the new file.
    pub added: Vec<(usize, String)>,
    /// Each run of removed and added lines with no kept line between them,
    /// for the content checks that compare what a line was with what it is.
    pub blocks: Vec<Block>,
}

impl FileChange {
    /// The file as the content checks read it, under its new path (its old
    /// one when the change deletes it).
    pub fn content(&self) -> Option<Change<'_>> {
        Some(Change {
            path: self.new.as_deref().or(self.old.as_deref())?,
            added: &self.added,
            blocks: &self.blocks,
        })
    }

    /// Every path the change writes: the old one (removed or renamed away) and
    /// the new one, once each.
    pub fn paths(&self) -> impl Iterator<Item = &str> {
        let new = self
            .new
            .as_deref()
            .filter(|n| self.old.as_deref() != Some(*n));
        self.old.as_deref().into_iter().chain(new)
    }
}

/// Why the input is not a unified diff OpenMoat can read in full.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
    /// 1-based line of the input.
    pub line: usize,
    pub problem: &'static str,
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "line {}: {}", self.line, self.problem)
    }
}

impl std::error::Error for ParseError {}

/// Lines left in the hunk being read, the new file's next line number, and
/// whether the last line read was removed or added (a block is open).
struct Hunk {
    old: usize,
    new: usize,
    line: usize,
    changing: bool,
}

impl Hunk {
    /// The open block of `change`, opened at this line if the last line was kept.
    fn block<'c>(&mut self, change: &'c mut FileChange) -> Option<&'c mut Block> {
        if !self.changing {
            self.changing = true;
            let at = change.added.len();
            change.blocks.push(Block {
                at: self.line,
                removed: Vec::new(),
                added: at..at,
            });
        }
        change.blocks.last_mut()
    }
}

/// The file being read, with what its header said.
#[derive(Default)]
struct Current {
    change: FileChange,
    created: bool,
    deleted: bool,
    /// A hunk was read: a `---` line after it starts the next file.
    hunks: bool,
}

impl Current {
    fn finish(self) -> FileChange {
        let mut change = self.change;
        if self.created {
            change.old = None;
        }
        if self.deleted {
            change.new = None;
        }
        change
    }
}

/// The files `text` changes. Lines may end in `\n` or `\r\n`.
pub fn parse(text: &str) -> Result<Vec<FileChange>, ParseError> {
    let mut files = Vec::new();
    let mut current: Option<Current> = None;
    let mut hunk: Option<Hunk> = None;
    let mut last = 0;
    // The `\n` that ends the last line starts no line of its own: read as one,
    // the empty text after it was a kept empty line, and a diff cut off after
    // a hunk header (`@@ -1 +1 @@\n`) read as complete.
    let lines = text.strip_suffix('\n').unwrap_or(text);
    for (index, raw) in lines.split('\n').enumerate() {
        let number = index + 1;
        last = number;
        let line = raw.strip_suffix('\r').unwrap_or(raw);
        if let Some(open) = hunk.as_mut().filter(|h| h.old > 0 || h.new > 0) {
            let change = current.as_mut().map(|c| &mut c.change);
            read_hunk_line(open, line, change).map_err(|problem| ParseError {
                line: number,
                problem,
            })?;
            continue;
        }
        hunk = None;
        if line.starts_with('\\') {
            // `\ No newline at end of file`, after a hunk's last line
            continue;
        }
        if let Some(rest) = line.strip_prefix("diff --git ") {
            files.extend(current.take().map(Current::finish));
            let (old, new) = header_paths(rest);
            current = Some(Current {
                change: FileChange {
                    old,
                    new,
                    ..FileChange::default()
                },
                ..Current::default()
            });
            continue;
        }
        if let Some(rest) = line.strip_prefix("--- ") {
            // A plain unified diff has no `diff --git` line: `---` starts a file.
            if current.as_ref().is_none_or(|c| c.hunks) {
                files.extend(current.take().map(Current::finish));
            }
            let file = current.get_or_insert_with(Current::default);
            file.change.old = file_path(rest, "a/");
            file.created |= file.change.old.is_none();
            continue;
        }
        let Some(file) = current.as_mut() else {
            // text before the first file: a commit message, `git format-patch` mail headers
            continue;
        };
        if let Some(rest) = line.strip_prefix("+++ ") {
            file.change.new = file_path(rest, "b/");
            file.deleted |= file.change.new.is_none();
        } else if line.starts_with("@@ ") {
            hunk = Some(hunk_header(line).ok_or(ParseError {
                line: number,
                problem: "a hunk header OpenMoat cannot read",
            })?);
            file.hunks = true;
        } else {
            read_extended_header(file, line);
        }
    }
    if hunk.is_some_and(|h| h.old > 0 || h.new > 0) {
        return Err(ParseError {
            line: last,
            problem: "the diff ends inside a hunk",
        });
    }
    files.extend(current.map(Current::finish));
    if files.is_empty() && !text.trim().is_empty() {
        return Err(ParseError {
            line: 1,
            problem: "no changed file found; the input is not a unified diff",
        });
    }
    Ok(files)
}

/// One line inside a hunk: added, removed or kept.
fn read_hunk_line(
    hunk: &mut Hunk,
    line: &str,
    change: Option<&mut FileChange>,
) -> Result<(), &'static str> {
    let taken = |left: &mut usize| -> Result<(), &'static str> {
        *left = left
            .checked_sub(1)
            .ok_or("a hunk has more lines than its header says")?;
        Ok(())
    };
    match line.chars().next() {
        Some('+') => {
            taken(&mut hunk.new)?;
            if let Some(change) = change {
                // open the block first: it starts at this line's index
                hunk.block(change);
                change.added.push((hunk.line, line[1..].to_owned()));
                let end = change.added.len();
                if let Some(block) = change.blocks.last_mut() {
                    block.added.end = end;
                }
            }
            hunk.line = hunk.line.saturating_add(1);
        }
        Some('-') => {
            taken(&mut hunk.old)?;
            if let Some(block) = change.and_then(|c| hunk.block(c)) {
                block.removed.push(line[1..].to_owned());
            }
        }
        // An empty line is a kept empty line whose leading space was trimmed.
        Some(' ') | None => {
            taken(&mut hunk.old)?;
            taken(&mut hunk.new)?;
            hunk.line = hunk.line.saturating_add(1);
            hunk.changing = false;
        }
        Some('\\') => {}
        Some(_) => return Err("a hunk has fewer lines than its header says"),
    }
    Ok(())
}

/// The mode, rename, copy and binary lines of a git header.
fn read_extended_header(file: &mut Current, line: &str) {
    let mode = |rest: &str| rest.trim().to_owned();
    if let Some(rest) = line.strip_prefix("new file mode ") {
        file.change.new_mode = Some(mode(rest));
        file.created = true;
    } else if let Some(rest) = line.strip_prefix("deleted file mode ") {
        file.change.old_mode = Some(mode(rest));
        file.deleted = true;
    } else if let Some(rest) = line.strip_prefix("old mode ") {
        file.change.old_mode = Some(mode(rest));
    } else if let Some(rest) = line.strip_prefix("new mode ") {
        file.change.new_mode = Some(mode(rest));
    } else if let Some(rest) = line.strip_prefix("index ") {
        // `index <old>..<new> <mode>`: the mode when it did not change
        if let Some((_, unchanged)) = rest.split_once(' ') {
            file.change.old_mode.get_or_insert_with(|| mode(unchanged));
            file.change.new_mode.get_or_insert_with(|| mode(unchanged));
        }
    } else if let Some(rest) = line
        .strip_prefix("rename from ")
        .or_else(|| line.strip_prefix("copy from "))
    {
        file.change.old = Some(unquote(rest));
    } else if let Some(rest) = line
        .strip_prefix("rename to ")
        .or_else(|| line.strip_prefix("copy to "))
    {
        file.change.new = Some(unquote(rest));
    } else if line.starts_with("Binary files ") || line == "GIT binary patch" {
        file.change.binary = true;
    }
}

/// `@@ -a[,b] +c[,d] @@`: the lines the hunk removes and adds, and where it starts.
fn hunk_header(line: &str) -> Option<Hunk> {
    let mut parts = line.strip_prefix("@@ ")?.split(' ');
    let range = |part: Option<&str>, sign: char| -> Option<(usize, usize)> {
        let range = part?.strip_prefix(sign)?;
        match range.split_once(',') {
            Some((start, count)) => Some((start.parse().ok()?, count.parse().ok()?)),
            None => Some((range.parse().ok()?, 1)),
        }
    };
    let (_, old) = range(parts.next(), '-')?;
    let (start, new) = range(parts.next(), '+')?;
    (parts.next()? == "@@").then_some(Hunk {
        old,
        new,
        line: start,
        changing: false,
    })
}

/// The path of a `---` or `+++` line: `None` for `/dev/null`, `prefix`
/// removed, and anything after a tab (a timestamp; git adds a tab after a
/// name with a space) dropped.
fn file_path(rest: &str, prefix: &str) -> Option<String> {
    let name = if rest.starts_with('"') {
        unquote(rest)
    } else {
        rest.split('\t').next().unwrap_or(rest).to_owned()
    };
    if name == "/dev/null" {
        return None;
    }
    Some(
        name.strip_prefix(prefix)
            .map_or(name.clone(), str::to_owned),
    )
}

/// The two paths of `diff --git a/<old> b/<new>`. Unquoted names may hold
/// spaces, so when both are the same name (every change but a rename or
/// copy, whose own lines then give the names) the line is split in half.
fn header_paths(rest: &str) -> (Option<String>, Option<String>) {
    let strip = |name: String, prefix: &str| name.strip_prefix(prefix).map(str::to_owned);
    if rest.starts_with('"') {
        let (old, tail) = quoted(rest);
        let new = unquote(tail.trim_start());
        return (strip(old, "a/"), strip(new, "b/"));
    }
    let len = rest.len();
    if rest.starts_with("a/") && len >= 5 && (len - 5).is_multiple_of(2) {
        let half = (len - 5) / 2;
        if let (Some(old), Some(sep), Some(new)) = (
            rest.get(2..2 + half),
            rest.get(2 + half..5 + half),
            rest.get(5 + half..),
        ) && sep == " b/"
            && old == new
        {
            return (Some(old.to_owned()), Some(new.to_owned()));
        }
    }
    match rest.split_once(" \"").or_else(|| rest.split_once(" b/")) {
        Some((old, _)) if rest.contains(" \"") => (
            strip(old.to_owned(), "a/"),
            strip(unquote(&rest[old.len() + 1..]), "b/"),
        ),
        Some((old, new)) => (strip(old.to_owned(), "a/"), Some(new.to_owned())),
        None => (None, None),
    }
}

/// A C-style quoted name as git writes it (`"a/x\ty"`, octal for other bytes),
/// or `text` as it is when it is not quoted.
pub fn unquote(text: &str) -> String {
    if text.starts_with('"') {
        quoted(text).0
    } else {
        text.to_owned()
    }
}

/// The quoted name at the start of `text`, and what follows its closing quote.
fn quoted(text: &str) -> (String, &str) {
    let body = &text[1..];
    let mut bytes = Vec::new();
    let mut chars = body.char_indices();
    while let Some((i, c)) = chars.next() {
        match c {
            '"' => return (String::from_utf8_lossy(&bytes).into_owned(), &body[i + 1..]),
            '\\' => {
                let Some((_, escaped)) = chars.next() else {
                    break;
                };
                let byte = match escaped {
                    'a' => 0x07,
                    'b' => 0x08,
                    't' => b'\t',
                    'n' => b'\n',
                    'v' => 0x0b,
                    'f' => 0x0c,
                    'r' => b'\r',
                    '0'..='7' => {
                        let mut value = escaped.to_digit(8).unwrap_or_default();
                        for _ in 0..2 {
                            let next = chars.clone().next().and_then(|(_, d)| d.to_digit(8));
                            if let Some(d) = next {
                                value = value * 8 + d;
                                chars.next();
                            }
                        }
                        u8::try_from(value).unwrap_or(b'?')
                    }
                    other => {
                        let mut buf = [0; 4];
                        bytes.extend_from_slice(other.encode_utf8(&mut buf).as_bytes());
                        continue;
                    }
                };
                bytes.push(byte);
            }
            other => {
                let mut buf = [0; 4];
                bytes.extend_from_slice(other.encode_utf8(&mut buf).as_bytes());
            }
        }
    }
    // no closing quote: the text is the name
    (text.to_owned(), "")
}