openmoat 0.22.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.
//! The temp directory of one `moat run`: a new directory of its own (mode
//! `0700`) inside the system's, which the sandbox lets commands write and the
//! agent gets as `TMPDIR`. The shared temp directory (`/tmp` on Linux) holds
//! other programs' state, other sessions' files and Kerberos tickets; it is
//! no longer granted, so nothing the agent's commands write lands there and
//! nothing they leave there survives the run. Removed when the run ends.
//!
//! Under `--isolate` the whole temp directory is an empty `tmpfs` already,
//! private to the session, so this is not used there.

use std::path::{Path, PathBuf};

use anyhow::{Context as _, Result};

use crate::context::path_string;
use crate::guard_relay::random_hex;

/// The variable programs take their temp directory from.
pub const TMPDIR: &str = "TMPDIR";

/// The longest path a Unix socket can be bound at on Linux: `sun_path` holds
/// 108 bytes, the last a NUL.
#[cfg(any(target_os = "linux", test))]
const SOCKET_PATH_MAX: usize = 107;

/// Where the directory `name` holding the Unix socket `socket` goes: the
/// system's temp directory `temp`, or `/tmp` when a path that long could not be
/// bound (a `TMPDIR` deep in a build tree). The directory is private (`0700`)
/// and made only if nothing has the name, so `/tmp` serves as well.
#[cfg(any(target_os = "linux", test))]
pub fn socket_parent(temp: &Path, name: &str, socket: &str) -> PathBuf {
    let bytes = temp.join(name).join(socket).as_os_str().len();
    if bytes <= SOCKET_PATH_MAX {
        temp.to_path_buf()
    } else {
        PathBuf::from("/tmp")
    }
}

/// The run's own temp directory; removed with everything in it when dropped.
#[derive(Debug)]
pub struct RunTemp {
    dir: PathBuf,
}

impl RunTemp {
    /// A new directory `openmoat-run-<random>` in the system's temp directory,
    /// resolved. Fails rather than reuse one that exists.
    pub fn create() -> Result<Self> {
        let system =
            std::fs::canonicalize(std::env::temp_dir()).context("resolving the temp directory")?;
        Self::create_in(&system)
    }

    fn create_in(system: &Path) -> Result<Self> {
        let name = format!("openmoat-run-{}", random_hex(8)?.as_str());
        let dir = system.join(name);
        let builder = std::fs::DirBuilder::new();
        // Owner-only where modes exist; elsewhere the temp directory's ACL holds.
        #[cfg(unix)]
        let builder = {
            let mut builder = builder;
            std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700);
            builder
        };
        builder
            .create(&dir)
            .with_context(|| format!("creating the run's temp directory {}", dir.display()))?;
        Ok(Self { dir })
    }

    /// Its path, slash-separated.
    pub fn path(&self) -> String {
        path_string(&self.dir)
    }
}

impl Drop for RunTemp {
    fn drop(&mut self) {
        // `remove_dir_all` does not follow links a command left inside.
        let _ = std::fs::remove_dir_all(&self.dir);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A temp directory deep in a build tree left the socket too long to
    /// bind: `openmoat run` refused on Linux for that reason alone.
    #[test]
    fn a_socket_too_long_to_bind_moves_to_the_short_temp_directory() {
        let (name, socket) = ("moat-run-12345", "proxy.sock");
        let short = Path::new("/tmp");
        assert_eq!(socket_parent(short, name, socket), short);
        // Exactly what fits, and one byte more.
        let fixed = format!("/{name}/{socket}").len();
        let fits = format!("/{}", "d".repeat(SOCKET_PATH_MAX - fixed - 1));
        assert_eq!(
            Path::new(&fits).join(name).join(socket).as_os_str().len(),
            SOCKET_PATH_MAX
        );
        assert_eq!(
            socket_parent(Path::new(&fits), name, socket),
            Path::new(&fits)
        );
        let long = format!("{fits}d");
        assert_eq!(socket_parent(Path::new(&long), name, socket), short);
    }

    #[test]
    fn each_run_gets_a_new_private_directory_removed_afterwards() {
        let base = tempfile::tempdir().unwrap();
        let first = RunTemp::create_in(base.path()).unwrap();
        let second = RunTemp::create_in(base.path()).unwrap();
        assert_ne!(first.path(), second.path());
        let dir = PathBuf::from(first.path());
        assert!(dir.starts_with(base.path()));
        assert!(
            dir.file_name()
                .and_then(|n| n.to_str())
                .is_some_and(|n| n.starts_with("openmoat-run-"))
        );
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt as _;
            let mode = std::fs::metadata(&dir).unwrap().permissions().mode();
            assert_eq!(mode & 0o777, 0o700);
        }
        std::fs::write(dir.join("left.txt"), "x").unwrap();
        drop(first);
        assert!(!dir.exists(), "removed with what is inside");
        assert!(PathBuf::from(second.path()).is_dir());
    }
}