openmoat 0.7.0

Policy checks for AI coding agents: decides allow, defer, ask or deny for every tool call from Claude Code, Codex and Cursor, configures Claude Code's and Codex'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
//! Reading JSON5 (<https://spec.json5.org/>), the format OpenClaw reads its
//! `openclaw.json` in (<https://docs.openclaw.ai/gateway/configuration>).
//!
//! OpenMoat reads JSON5 into a [`Value`] and never writes it: a caller that
//! edits a file writes plain JSON, which JSON5 readers read too. Comments have
//! no place in a [`Value`], so [`Parsed::comments`] says whether writing the
//! file back would drop some; callers then refuse. `Infinity` and `NaN` have no
//! JSON form and are refused. Duplicate keys keep the last value, as
//! JavaScript objects do. No dependency: the grammar is small, and a parser
//! here keeps reading in step with what the lock and the MCP pins see.

use anyhow::{Result, bail};
use serde_json::{Map, Number, Value};

/// Nesting deeper than this is refused rather than recursed into.
const MAX_DEPTH: usize = 128;

/// A JSON5 document read as JSON.
#[derive(Debug)]
pub struct Parsed {
    pub value: Value,
    /// Whether the text has a `//` or `/* */` comment.
    pub comments: bool,
}

/// Read `text` as one JSON5 value.
pub fn parse(text: &str) -> Result<Parsed> {
    let mut parser = Parser {
        chars: text.chars().collect(),
        pos: 0,
        comments: false,
    };
    parser.skip()?;
    let value = parser.value(0)?;
    parser.skip()?;
    if parser.pos < parser.chars.len() {
        bail!("{}: text after the end of the value", parser.at());
    }
    Ok(Parsed {
        value,
        comments: parser.comments,
    })
}

struct Parser {
    chars: Vec<char>,
    pos: usize,
    comments: bool,
}

impl Parser {
    fn peek(&self) -> Option<char> {
        self.chars.get(self.pos).copied()
    }

    fn peek_at(&self, ahead: usize) -> Option<char> {
        self.chars.get(self.pos + ahead).copied()
    }

    fn bump(&mut self) -> Option<char> {
        let c = self.peek();
        if c.is_some() {
            self.pos += 1;
        }
        c
    }

    /// `line:column` of the current position, for messages.
    fn at(&self) -> String {
        let before = &self.chars[..self.pos.min(self.chars.len())];
        let line = before.iter().filter(|c| **c == '\n').count() + 1;
        let column = before.iter().rev().take_while(|c| **c != '\n').count() + 1;
        format!("line {line}, column {column}")
    }

    /// Skip white space and comments.
    fn skip(&mut self) -> Result<()> {
        loop {
            match (self.peek(), self.peek_at(1)) {
                (Some(c), _) if is_space(c) => self.pos += 1,
                (Some('/'), Some('/')) => {
                    self.comments = true;
                    while self.peek().is_some_and(|c| !is_line_end(c)) {
                        self.pos += 1;
                    }
                }
                (Some('/'), Some('*')) => {
                    self.comments = true;
                    self.pos += 2;
                    loop {
                        match self.bump() {
                            None => bail!("a `/*` comment is not closed"),
                            Some('*') if self.peek() == Some('/') => {
                                self.pos += 1;
                                break;
                            }
                            Some(_) => {}
                        }
                    }
                }
                _ => return Ok(()),
            }
        }
    }

    fn value(&mut self, depth: usize) -> Result<Value> {
        if depth > MAX_DEPTH {
            bail!("{}: nested more than {MAX_DEPTH} levels deep", self.at());
        }
        match self.peek() {
            None => bail!("the text ends where a value should be"),
            Some('{') => self.object(depth),
            Some('[') => self.array(depth),
            Some(quote @ ('"' | '\'')) => {
                self.pos += 1;
                Ok(Value::String(self.string(quote)?))
            }
            Some(c) if c == '-' || c == '+' || c == '.' || c.is_ascii_digit() => self.number(),
            Some(c) if is_id_start(c) => {
                let at = self.at();
                match self.identifier()?.as_str() {
                    "null" => Ok(Value::Null),
                    "true" => Ok(Value::Bool(true)),
                    "false" => Ok(Value::Bool(false)),
                    word @ ("Infinity" | "NaN") => bail!("{at}: `{word}` has no JSON form"),
                    word => bail!("{at}: `{word}` is not a value"),
                }
            }
            Some(c) => bail!("{}: `{c}` cannot start a value", self.at()),
        }
    }

    fn object(&mut self, depth: usize) -> Result<Value> {
        self.pos += 1;
        let mut map = Map::new();
        loop {
            self.skip()?;
            if self.peek() == Some('}') {
                self.pos += 1;
                return Ok(Value::Object(map));
            }
            let key = match self.peek() {
                Some(quote @ ('"' | '\'')) => {
                    self.pos += 1;
                    self.string(quote)?
                }
                Some(c) if is_id_start(c) || c == '\\' => self.identifier()?,
                _ => bail!("{}: expected a key", self.at()),
            };
            self.skip()?;
            if self.bump() != Some(':') {
                bail!("{}: expected `:` after key `{key}`", self.at());
            }
            self.skip()?;
            let value = self.value(depth + 1)?;
            map.insert(key, value);
            self.skip()?;
            match self.bump() {
                Some(',') => {}
                Some('}') => return Ok(Value::Object(map)),
                _ => bail!("{}: expected `,` or `}}`", self.at()),
            }
        }
    }

    fn array(&mut self, depth: usize) -> Result<Value> {
        self.pos += 1;
        let mut items = Vec::new();
        loop {
            self.skip()?;
            if self.peek() == Some(']') {
                self.pos += 1;
                return Ok(Value::Array(items));
            }
            items.push(self.value(depth + 1)?);
            self.skip()?;
            match self.bump() {
                Some(',') => {}
                Some(']') => return Ok(Value::Array(items)),
                _ => bail!("{}: expected `,` or `]`", self.at()),
            }
        }
    }

    /// A string after its opening `quote`.
    fn string(&mut self, quote: char) -> Result<String> {
        let mut out = String::new();
        loop {
            match self.bump() {
                None => bail!("a string is not closed"),
                Some(c) if c == quote => return Ok(out),
                Some('\\') => self.escape(&mut out)?,
                Some('\n' | '\r') => bail!("{}: a line break inside a string", self.at()),
                Some(c) => out.push(c),
            }
        }
    }

    /// One escape after its `\`, appended to `out`.
    fn escape(&mut self, out: &mut String) -> Result<()> {
        let at = self.at();
        match self.bump() {
            None => bail!("a string is not closed"),
            Some('b') => out.push('\u{8}'),
            Some('f') => out.push('\u{c}'),
            Some('n') => out.push('\n'),
            Some('r') => out.push('\r'),
            Some('t') => out.push('\t'),
            Some('v') => out.push('\u{b}'),
            Some('0') if !self.peek().is_some_and(|c| c.is_ascii_digit()) => out.push('\0'),
            Some(c @ '0'..='9') => bail!("{at}: `\\{c}` is not an escape"),
            Some('x') => {
                let code = self.hex(2)?;
                out.push(char::from_u32(code).unwrap_or('\u{fffd}'));
            }
            Some('u') => out.push(self.unicode()?),
            // A line continuation: the escaped line break is left out.
            Some('\r') => {
                if self.peek() == Some('\n') {
                    self.pos += 1;
                }
            }
            Some('\n' | '\u{2028}' | '\u{2029}') => {}
            Some(c) => out.push(c),
        }
        Ok(())
    }

    /// The character of a `\u` escape (its `u` already read), a surrogate pair
    /// joined.
    fn unicode(&mut self) -> Result<char> {
        let at = self.at();
        let high = self.hex(4)?;
        if !(0xD800..0xDC00).contains(&high) {
            return char::from_u32(high).ok_or_else(|| lone(&at));
        }
        if self.bump() != Some('\\') || self.bump() != Some('u') {
            return Err(lone(&at));
        }
        let low = self.hex(4)?;
        if !(0xDC00..0xE000).contains(&low) {
            return Err(lone(&at));
        }
        char::from_u32(0x10000 + ((high - 0xD800) << 10) + (low - 0xDC00)).ok_or_else(|| lone(&at))
    }

    fn hex(&mut self, digits: usize) -> Result<u32> {
        let mut code = 0;
        for _ in 0..digits {
            match self.bump().and_then(|c| c.to_digit(16)) {
                Some(d) => code = code * 16 + d,
                None => bail!("{}: expected {digits} hexadecimal digits", self.at()),
            }
        }
        Ok(code)
    }

    /// An unquoted key or keyword: letters, `$`, `_`, digits after the first,
    /// and `\u` escapes.
    fn identifier(&mut self) -> Result<String> {
        let mut out = String::new();
        loop {
            match self.peek() {
                Some('\\') => {
                    self.pos += 1;
                    if self.bump() != Some('u') {
                        bail!("{}: only `\\u` escapes may appear in a key", self.at());
                    }
                    out.push(self.unicode()?);
                }
                Some(c) if is_id_start(c) || (!out.is_empty() && is_id_part(c)) => {
                    self.pos += 1;
                    out.push(c);
                }
                _ => break,
            }
        }
        if out.is_empty() {
            bail!("{}: expected a key", self.at());
        }
        Ok(out)
    }

    fn number(&mut self) -> Result<Value> {
        let at = self.at();
        let mut text = String::new();
        match self.peek() {
            Some('-') => {
                text.push('-');
                self.pos += 1;
            }
            Some('+') => self.pos += 1,
            _ => {}
        }
        if self.peek().is_some_and(is_id_start) {
            let word = self.identifier()?;
            bail!("{at}: `{word}` has no JSON form or is not a number");
        }
        let hex = self.peek() == Some('0') && matches!(self.peek_at(1), Some('x' | 'X'));
        let number = if hex {
            self.pos += 2;
            self.hex_number(&text, &at)?
        } else {
            self.decimal(text, &at)?
        };
        if self.peek().is_some_and(is_id_part) {
            bail!("{at}: a number runs into other text");
        }
        Ok(Value::Number(number))
    }

    fn hex_number(&mut self, sign: &str, at: &str) -> Result<Number> {
        let start = self.pos;
        let mut value: u64 = 0;
        while let Some(d) = self.peek().and_then(|c| c.to_digit(16)) {
            self.pos += 1;
            value = value
                .checked_mul(16)
                .and_then(|v| v.checked_add(u64::from(d)))
                .ok_or_else(|| anyhow::Error::msg(format!("{at}: number too large")))?;
        }
        if self.pos == start {
            bail!("{at}: `0x` without digits");
        }
        if sign.is_empty() {
            return Ok(Number::from(value));
        }
        let negative = i64::try_from(value)
            .ok()
            .and_then(i64::checked_neg)
            .ok_or_else(|| anyhow::Error::msg(format!("{at}: number too large")))?;
        Ok(Number::from(negative))
    }

    /// Digits with an optional fraction and exponent; JSON5 allows `.5` and
    /// `5.`, which are written out as `0.5` and `5.0` for JSON.
    fn decimal(&mut self, mut text: String, at: &str) -> Result<Number> {
        let int = self.digits();
        if int.len() > 1 && int.starts_with('0') {
            bail!("{at}: a number with a leading zero");
        }
        let fraction = if self.peek() == Some('.') {
            self.pos += 1;
            Some(self.digits())
        } else {
            None
        };
        if int.is_empty() && fraction.as_deref().is_none_or(str::is_empty) {
            bail!("{at}: a number without digits");
        }
        text.push_str(if int.is_empty() { "0" } else { &int });
        if let Some(fraction) = fraction {
            text.push('.');
            text.push_str(if fraction.is_empty() { "0" } else { &fraction });
        }
        if matches!(self.peek(), Some('e' | 'E')) {
            self.pos += 1;
            text.push('e');
            if let Some(sign @ ('+' | '-')) = self.peek() {
                self.pos += 1;
                text.push(sign);
            }
            let exponent = self.digits();
            if exponent.is_empty() {
                bail!("{at}: an exponent without digits");
            }
            text.push_str(&exponent);
        }
        text.parse::<Number>()
            .map_err(|e| anyhow::Error::msg(format!("{at}: {e}")))
    }

    fn digits(&mut self) -> String {
        let mut out = String::new();
        while let Some(c) = self.peek().filter(char::is_ascii_digit) {
            self.pos += 1;
            out.push(c);
        }
        out
    }
}

fn lone(at: &str) -> anyhow::Error {
    anyhow::Error::msg(format!("{at}: a `\\u` escape names half a character"))
}

/// JSON5 white space and line breaks; the byte order mark counts as space.
fn is_space(c: char) -> bool {
    c.is_whitespace() || c == '\u{feff}'
}

fn is_line_end(c: char) -> bool {
    matches!(c, '\n' | '\r' | '\u{2028}' | '\u{2029}')
}

fn is_id_start(c: char) -> bool {
    c.is_alphabetic() || c == '$' || c == '_'
}

fn is_id_part(c: char) -> bool {
    is_id_start(c) || c.is_alphanumeric() || c == '\u{200c}' || c == '\u{200d}'
}

#[cfg(test)]
mod tests;