openmoat 0.22.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.
//! Text the server sends the model outside a tool result (ADR-069): the
//! `instructions` of its `initialize` answer, which a host puts in the
//! model's prompt. It is untrusted content as a tool's result is, so each
//! session it reaches has it kept as one: its pieces for the cross-server flow
//! check (ADR-058), the fingerprints of what it names as injected input
//! (ADR-057), and a row that marks the session untrusted (ADR-020).
//!
//! The server's requests to the host (`sampling/createMessage`,
//! `elicitation/create`) carry text the host puts before the model or the
//! person too: each is kept for the session the proxy served last, and one
//! sent before any for the next session it serves.
//!
//! The server answers `initialize` before any session made a call, and a
//! proxy learns a session only from the hook decision a call matches. So the
//! text is kept for a session when the proxy first passes a call of it, before
//! that call reaches the server; every session the proxy serves gets it once.
//! A session that never calls a tool of the server is not marked: what its
//! model read is only the text a person accepted (`openmoat mcp accept`).

use std::collections::HashSet;
use std::sync::PoisonError;

use anyhow::{Context as _, Result, bail};
use openmoat_audit::{NewEvent, Ticket};
use openmoat_core::rule_id::MCP_SERVER_TEXT;
use openmoat_core::{Action, Decision, Verdict};
use serde_json::Value;

use super::{AUDIT_HOST, Awaiting, Gateway};

/// The most requests' texts kept for a session the proxy has not served yet.
const MAX_QUEUED: usize = 64;

/// What the server sent outside its tool results, and which sessions have it.
#[derive(Default)]
pub(super) struct Sent {
    /// The `instructions` the host was passed, if any.
    instructions: Option<String>,
    /// The host and session of each session that has them.
    credited: HashSet<(String, String)>,
    /// The texts of requests the server sent the host before the proxy served
    /// any session, with their method: the next session it serves gets them.
    queued: Vec<(String, String)>,
}

impl Gateway {
    /// Keep `text`, the `instructions` the relay passed to the host, for every
    /// session the proxy serves from now on.
    pub(super) fn keep_instructions(&self, text: &str) {
        let mut sent = self.sent.lock().unwrap_or_else(PoisonError::into_inner);
        sent.instructions = Some(text.to_owned()).filter(|t| !t.trim().is_empty());
        sent.credited.clear();
    }

    /// Before the first call of `ticket`'s session passes: keep what the
    /// server sent outside its results for that session.
    pub(super) fn credit(&self, ticket: &Ticket, cwd: Option<&str>) -> Result<()> {
        let key = (ticket.host.clone(), ticket.session_id.clone());
        let whose = (ticket.host.as_str(), ticket.session_id.as_str());
        let text = {
            let sent = self.sent.lock().unwrap_or_else(PoisonError::into_inner);
            if sent.credited.contains(&key) {
                return Ok(());
            }
            sent.instructions.clone()
        };
        if let Some(text) = text {
            self.untrusted(whose, cwd, "initialize", &text)
                .context("keeping the MCP server's instructions for this session")?;
        }
        let queued = std::mem::take(
            &mut self
                .sent
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .queued,
        );
        for (method, text) in queued {
            self.untrusted(whose, cwd, &method, &text)
                .context("keeping the MCP server's request for this session")?;
        }
        self.sent
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .credited
            .insert(key);
        Ok(())
    }

    /// Keep `text`, which the server sent the host in a `method` request, for
    /// the session the proxy served last, which the request is about; before
    /// any, for the next one it serves.
    pub(super) fn server_text(&self, method: &str, text: &str) -> Result<()> {
        if text.trim().is_empty() {
            return Ok(());
        }
        let served = self
            .served
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .clone();
        let Some((host, session)) = served else {
            let mut sent = self.sent.lock().unwrap_or_else(PoisonError::into_inner);
            if sent.queued.len() >= MAX_QUEUED {
                bail!("too many requests from the server before any session used it");
            }
            sent.queued.push((method.to_owned(), text.to_owned()));
            return Ok(());
        };
        self.untrusted((&host, &session), None, method, text)
            .context("keeping the MCP server's request for the session it serves")
    }

    /// Record the server's `method` request to the host as `decision`, under
    /// the session the proxy served last when there is one.
    pub(super) fn record_request(&self, method: &str, decision: &Decision) -> Result<()> {
        let store = self.store()?.with_secrets(self.known());
        let served = self
            .served
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .clone();
        let session = served.map_or_else(|| self.session.clone(), |(_, s)| s);
        store.record(&NewEvent {
            host: AUDIT_HOST,
            session_id: &session,
            call_id: None,
            cwd: None,
            tool: method,
            action: None,
            decision,
            latency_us: 0,
        })?;
        Ok(())
    }

    /// Keep `text`, which the server sent the model in `what` (an MCP method),
    /// as untrusted content of `host`'s `session`.
    fn untrusted(
        &self,
        (host, session): (&str, &str),
        cwd: Option<&str>,
        what: &str,
        text: &str,
    ) -> Result<()> {
        let name = format!("mcp__{}__{what}", self.server);
        let action = Action::mcp(name.clone());
        let store = self.store()?.with_secrets(self.known());
        let decision = Decision::single(
            Verdict::Allow,
            MCP_SERVER_TEXT,
            format!(
                "MCP server `{}` sent the agent text outside a tool result ({what}), which \
                 marks the session untrusted (recorded by {AUDIT_HOST})",
                self.server
            ),
        );
        store.record(&NewEvent {
            host,
            session_id: session,
            call_id: None,
            cwd,
            tool: &name,
            action: Some(&action),
            decision: &decision,
            latency_us: 0,
        })?;
        let awaiting = Awaiting {
            host: host.to_owned(),
            session: session.to_owned(),
            action,
            cwd: cwd.map(str::to_owned),
        };
        self.keep(&store, &awaiting, &Value::String(text.to_owned()))
    }
}