openmoat 0.27.0

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.
use openmoat_hosts::Host;
use serde_json::json;

use super::*;

fn server(name: &str, entry: Value) -> Server {
    Server {
        id: format!("/h/.claude.json#/mcpServers/{name}"),
        host: Host::EVERY[0],
        file: PathBuf::from("/h/.claude.json"),
        pointer: format!("/mcpServers/{name}"),
        name: name.to_owned(),
        entry,
        form: openmoat_hosts::McpCommandForm::CommandArgs,
        runs_in_host: false,
    }
}

fn scan(servers: Vec<Server>) -> Scan {
    Scan {
        servers,
        unreadable: Vec::new(),
        scanned: vec![PathBuf::from("/h/.claude.json")],
        ..Scan::default()
    }
}

fn pinned(servers: &[Server]) -> Pins {
    servers
        .iter()
        .map(|s| (s.id.clone(), ServerPin::of(s)))
        .collect()
}

#[test]
fn key_order_is_not_a_change() {
    let a = server(
        "gh",
        json!({"command": "npx", "args": ["gh"], "env": {"A": "1", "B": "2"}}),
    );
    let pins = pinned(&[a]);
    let b = server(
        "gh",
        json!({"env": {"B": "2", "A": "1"}, "args": ["gh"], "command": "npx"}),
    );
    assert_eq!(changes(&pins, &scan(vec![b])), Vec::<Change>::new());
}

#[test]
fn a_changed_command_is_named_and_env_values_are_never_shown() {
    let pins = pinned(&[server(
        "gh",
        json!({"command": "npx", "env": {"TOKEN": "secret-one"}}),
    )]);
    let now = server(
        "gh",
        json!({"command": "sh", "env": {"TOKEN": "secret-two", "X": "y"}}),
    );
    let found = changes(&pins, &scan(vec![now]));
    assert_eq!(found.len(), 1);
    let text = found[0].describe();
    assert!(text.contains(r#"command: "npx" → "sh""#), "{text}");
    assert!(
        text.contains("env.TOKEN: changed (value not shown)"),
        "{text}"
    );
    assert!(text.contains("env.X: added (value not shown)"), "{text}");
    assert!(!text.contains("secret"), "{text}");
    let stored = serde_json::to_string(&pins).unwrap();
    assert!(!stored.contains("secret-one"), "{stored}");
    assert!(found[0].blocks());
}

#[test]
fn added_and_removed_servers_are_changes_and_apply_accepts_them() {
    let old = server("old", json!({"command": "a"}));
    let mut pins = pinned(&[old]);
    let new = server("new", json!({"url": "https://mcp.example"}));
    let found = changes(&pins, &scan(vec![new]));
    let kinds: Vec<(&str, &Kind)> = found.iter().map(|c| (c.name.as_str(), &c.kind)).collect();
    assert_eq!(kinds, [("new", &Kind::Added), ("old", &Kind::Removed)]);
    assert!(found[0].blocks() && !found[1].blocks());
    apply(&mut pins, &found);
    assert_eq!(pins.len(), 1);
    assert_eq!(
        changes(
            &pins,
            &scan(vec![server("new", json!({"url": "https://mcp.example"}))])
        ),
        Vec::<Change>::new()
    );
}

#[test]
fn pins_of_files_not_scanned_stay() {
    let pins = pinned(&[server("gh", json!({"command": "a"}))]);
    let elsewhere = Scan {
        scanned: vec![PathBuf::from("/p/.mcp.json")],
        ..Scan::default()
    };
    assert_eq!(changes(&pins, &elsewhere), Vec::<Change>::new());
}

#[test]
fn a_redacted_token_change_still_counts() {
    let pins = pinned(&[server(
        "gh",
        json!({"args": ["--token", "ghp_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"]}),
    )]);
    let now = server(
        "gh",
        json!({"args": ["--token", "ghp_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"]}),
    );
    let found = changes(&pins, &scan(vec![now]));
    assert_eq!(found.len(), 1);
    assert!(
        !found[0].describe().contains("ghp_b"),
        "{}",
        found[0].describe()
    );
}

/// A host whose user-level reader names `form`, found by its spec.
fn reading(form: McpCommandForm) -> Host {
    Host::ALL
        .into_iter()
        .find(|h| {
            h.spec()
                .mcp
                .as_ref()
                .is_some_and(|m| m.user_reader.command_form() == form)
        })
        .unwrap()
}

/// `server` read by `host` in its own form.
fn read_by(host: Host, name: &str, entry: Value) -> Server {
    let mut found = server(name, entry);
    found.host = host;
    found.form = host.spec().mcp.as_ref().unwrap().user_reader.command_form();
    found
}

/// The variables each form gives its server: `env`, OpenCode's
/// `environment`, goose's `envs`. A secret there is never stored in the
/// lock's copy and never shown, new or changed.
#[test]
fn a_secret_under_each_form_s_variables_is_never_stored_or_shown() {
    let secret = "first-secret-value";
    for (form, base) in [
        (McpCommandForm::CommandArgs, json!({"command": "npx"})),
        (
            McpCommandForm::CommandList,
            json!({"type": "local", "command": ["npx"]}),
        ),
        (
            McpCommandForm::CmdArgs,
            json!({"type": "stdio", "cmd": "npx"}),
        ),
    ] {
        let host = reading(form);
        let key = read_by(host, "gh", json!({})).form.env_key();
        let mut entry = base;
        entry[key] = json!({"API_KEY": secret});
        let added = changes(
            &Pins::new(),
            &scan(vec![read_by(host, "gh", entry.clone())]),
        );
        let said = added[0].describe();
        assert!(!said.contains(secret), "{key}: {said}");
        let mut pins = Pins::new();
        apply(&mut pins, &added);
        let stored = serde_json::to_string(&pins).unwrap();
        assert!(!stored.contains(secret), "{key}: {stored}");

        entry[key] = json!({"API_KEY": "second-secret-value"});
        let found = changes(&pins, &scan(vec![read_by(host, "gh", entry)]));
        let said = found[0].describe();
        assert!(
            said.contains(&format!("{key}.API_KEY: changed (value not shown)")),
            "{said}"
        );
        assert!(!said.contains("secret-value"), "{key}: {said}");
    }
}

/// An older version kept OpenCode's and goose's values in clear in the
/// copy it shows. The pin still trusts exactly what it trusted, so the
/// same definition needs no new acceptance; a planted change is still
/// held and shows no value; and every save writes the copy without them.
#[test]
fn a_value_an_older_lock_kept_in_clear_is_never_shown_and_saved_away() {
    for (form, key, entry) in [
        (
            McpCommandForm::CommandList,
            "environment",
            json!({"type": "local", "command": ["npx"], "environment": {"API_KEY": "kept-secret-value"}}),
        ),
        (
            McpCommandForm::CmdArgs,
            "envs",
            json!({"type": "stdio", "cmd": "npx", "envs": {"API_KEY": "kept-secret-value"}}),
        ),
    ] {
        let host = reading(form);
        let now = read_by(host, "db", entry.clone());
        let mut old = ServerPin::of(&now);
        old.shown = entry.clone();
        let mut pins = Pins::from([(now.id.clone(), old.clone())]);
        assert_eq!(clear_values(&pins), 1, "{key}");
        assert_eq!(
            changes(&pins, &scan(vec![now])),
            Vec::<Change>::new(),
            "{key}: nothing to accept again"
        );

        let mut planted = entry.clone();
        planted[key]["NODE_OPTIONS"] = json!("--require planted-secret-value");
        let found = changes(&pins, &scan(vec![read_by(host, "db", planted)]));
        assert!(found[0].blocks(), "{key}");
        let said = found[0].describe();
        assert!(
            said.contains(&format!("{key}.NODE_OPTIONS: added")),
            "{said}"
        );
        assert!(!said.contains("secret-value"), "{key}: {said}");
        assert!(!said.contains("API_KEY"), "unchanged, so not named: {said}");

        let dir = tempfile::tempdir().unwrap();
        let policy = dir.path().join("policy.yaml");
        std::fs::write(&policy, "version: 1\n").unwrap();
        let path = dir.path().join("policy.lock");
        let mut lock =
            crate::integrity::Lock::pin(std::path::Path::new("/x/openmoat"), &[policy]).unwrap();
        lock.mcp_servers = Some(pins.clone());
        lock.save(&path).unwrap();
        let text = std::fs::read_to_string(&path).unwrap();
        assert!(!text.contains("kept-secret-value"), "{key}: {text}");
        let saved = crate::integrity::Lock::load(&path)
            .unwrap()
            .mcp_servers
            .unwrap();
        assert_eq!(
            saved[&old_id(&pins)].digest,
            old.digest,
            "trusts what it did"
        );
        assert_eq!(clear_values(&saved), 0);
        assert_eq!(scrub(&mut pins), 1);
        assert_eq!(pins, saved);
    }
}

fn old_id(pins: &Pins) -> String {
    pins.keys().next().unwrap().clone()
}

/// What `openmoat mcp accept` shows before it asks is where a person
/// decides to trust: an escape sequence or a hidden character in a name, a
/// key or an argument never reaches the screen, new or changed.
#[test]
fn what_a_person_is_asked_to_accept_holds_no_escape_sequence() {
    let name = "gh\u{1b}]8;;https://evil\u{7}";
    let entry = json!({"command": "npx", "args": ["\u{202e}lmth.exe", "\u{1b}[2J"]});
    let added = changes(&Pins::new(), &scan(vec![server(name, entry)]));
    let mut pins = Pins::new();
    apply(&mut pins, &added);
    let changed = json!({"command": "sh", "args": ["\u{202e}x", "\u{1b}[31m"],
        "k\u{1b}[1m": 1});
    let found = changes(&pins, &scan(vec![server(name, changed)]));
    for said in [added[0].describe(), found[0].describe()] {
        for bad in ['\u{1b}', '\u{7}', '\u{202e}'] {
            assert!(!said.contains(bad), "{bad:?} in {said:?}");
        }
        assert!(
            said.contains("MCP server `gh?]8;;https://evil?`"),
            "{said:?}"
        );
    }
}

/// A key or a variable's name holding line breaks cannot add lines of its own
/// before the question, push the real change off the screen or forge another
/// server's: the screen has exactly the lines the real differences make.
#[test]
fn a_key_with_line_breaks_cannot_forge_lines_on_the_accept_screen() {
    let pins = pinned(&[server("gh", json!({"command": "npx", "env": {"A": "1"}}))]);
    let forged = "\n\n\n\nMCP server `db` in /h/.claude.json was removed\r";
    let now = server(
        "gh",
        json!({"command": "evil", "env": {"A": "1", format!("N{forged}"): "x"},
            format!("zz{forged}"): 1}),
    );
    let found = changes(&pins, &scan(vec![now]));
    let said = found[0].describe();
    assert!(!said.contains('\r'), "{said:?}");
    let lines: Vec<&str> = said.lines().collect();
    assert_eq!(
        lines.len(),
        4,
        "the server, then command, the new variable and the new key: {said}"
    );
    assert!(lines[0].starts_with("MCP server `gh`"), "{said}");
    assert!(lines[1].contains(r#"command: "npx" → "evil""#), "{said}");
    assert!(lines[2].contains("env.N????MCP server"), "{said}");
    assert!(
        lines[3].trim_start().starts_with("zz????MCP server"),
        "{said}"
    );
}

#[test]
fn tool_names_match_their_server_only() {
    assert!(tool_of("mcp__github__get_issue", "github"));
    assert!(!tool_of("mcp__github__get_issue", "git"));
    assert!(tool_of("mcp__my_server__run", "my.server"));
    assert!(!tool_of("mcp__other__run", "my.server"));
}

fn release(version: &str) -> crate::mcp::provenance::Provenance {
    crate::mcp::provenance::Provenance {
        ecosystem: "npm".to_owned(),
        package: "postmark-mcp".to_owned(),
        version: Some(version.to_owned()),
        integrity: Some(format!("sha512-{version}")),
    }
}

fn with_release(servers: Vec<Server>, version: &str) -> Scan {
    let mut found = scan(servers);
    for server in &found.servers {
        found.provenance.insert(server.id.clone(), release(version));
    }
    found
}

#[test]
fn a_new_package_release_holds_an_unchanged_definition() {
    let gh = server(
        "mail",
        json!({"command": "npx", "args": ["-y", "postmark-mcp"]}),
    );
    let accepted = with_release(vec![gh.clone()], "1.0.15");
    let mut pins = Pins::new();
    let first = changes(&pins, &accepted);
    apply(&mut pins, &first);
    assert_eq!(
        pins.values().next().unwrap().provenance,
        Some(release("1.0.15"))
    );
    assert_eq!(changes(&pins, &accepted), Vec::<Change>::new());
    // The definition is the same text; npx now runs 1.0.16 (postmark-mcp).
    let bumped = changes(&pins, &with_release(vec![gh.clone()], "1.0.16"));
    assert_eq!(bumped.len(), 1);
    assert!(bumped[0].blocks());
    assert!(bumped[0].describe().contains("1.0.15") && bumped[0].describe().contains("1.0.16"));
    // A release that cannot be read now holds nothing.
    assert_eq!(changes(&pins, &scan(vec![gh])), Vec::<Change>::new());
}

#[test]
fn a_pin_without_a_release_records_one_without_holding() {
    let gh = server(
        "mail",
        json!({"command": "npx", "args": ["-y", "postmark-mcp"]}),
    );
    let pins = pinned(std::slice::from_ref(&gh));
    let found = changes(&pins, &with_release(vec![gh], "1.0.15"));
    assert_eq!(found.len(), 1);
    assert!(matches!(found[0].kind, Kind::Recorded(_)) && !found[0].blocks());
    assert_eq!(
        found[0].accept.as_ref().unwrap().provenance,
        Some(release("1.0.15"))
    );
}