Files
codex/codex-rs/rmcp-client/src/parent_socket_tests.rs
T
Charlie Marsh b0b5cfcab8 Connect plugin MCP servers over launcher-provided Unix sockets (#64828)
## Why

A launcher that already hosts an MCP server needs a way for Codex to
use it without starting a separate subprocess for the plugin.

## What changed

The stdio app server accepts socket bindings from its immediate parent
on Unix. A binding applies to a local plugin's stdio MCP server only
when the plugin ID, server name, and binding ID match a single entry.
Codex uses the stdio wire protocol over the socket and retains the
original declaration for policy checks.

Before sending MCP traffic, Codex requires a user-owned socket
directory with mode `0700` and verifies the listener's PID and UID.

## Testing

Add tests for binding scope and ambiguous matches, and for rejecting
unexpected listener PIDs, insecure directory permissions, symlinked
socket directories, and relative socket paths.

GitOrigin-RevId: 3d669dd59a0812b04727e4a1e458774007f054ba
2026-11-30 23:34:32 +00:00

87 lines
2.5 KiB
Rust

#![cfg(test)]
use super::*;
use pretty_assertions::assert_eq;
use std::fs;
use std::os::unix::fs::PermissionsExt;
use tokio::io::AsyncReadExt;
use tokio::net::UnixListener;
fn private_dir() -> io::Result<tempfile::TempDir> {
tempfile::Builder::new()
.permissions(fs::Permissions::from_mode(0o700))
.tempdir()
}
#[tokio::test]
async fn only_the_expected_listener_receives_an_mcp_stream() -> io::Result<()> {
let dir = private_dir()?;
let path = dir.path().join("mcp");
let listener = UnixListener::bind(&path)?;
let pid = std::process::id().cast_signed();
let mut socket = ParentSocket { path, pid: pid + 1 };
assert_eq!(
socket.connect().await.err().map(|error| error.kind()),
Some(io::ErrorKind::PermissionDenied)
);
let (mut peer, _) = listener.accept().await?;
assert_eq!(peer.read(&mut [0]).await?, 0);
socket.pid = pid;
socket.connect().await?;
Ok(())
}
#[tokio::test]
async fn rechecks_directory_permissions_before_connecting() -> io::Result<()> {
let dir = private_dir()?;
let socket = ParentSocket {
path: dir.path().join("mcp"),
pid: std::process::id().cast_signed(),
};
let listener = std::os::unix::net::UnixListener::bind(&socket.path)?;
listener.set_nonblocking(true)?;
socket.connect().await?;
listener.accept()?;
fs::set_permissions(dir.path(), fs::Permissions::from_mode(0o750))?;
assert_eq!(
socket.connect().await.err().map(|error| error.kind()),
Some(io::ErrorKind::PermissionDenied)
);
assert_eq!(
listener.accept().unwrap_err().kind(),
io::ErrorKind::WouldBlock
);
Ok(())
}
#[tokio::test]
async fn rejects_a_symlink_as_the_socket_directory() -> io::Result<()> {
let dir = private_dir()?;
let _listener = UnixListener::bind(dir.path().join("mcp"))?;
let link = dir.path().join("alias");
std::os::unix::fs::symlink(dir.path(), &link)?;
let socket = ParentSocket {
path: link.join("mcp"),
pid: std::process::id().cast_signed(),
};
assert_eq!(
socket.connect().await.err().map(|error| error.kind()),
Some(io::ErrorKind::PermissionDenied)
);
Ok(())
}
#[tokio::test]
async fn rejects_relative_socket_path() {
let socket = ParentSocket {
path: "mcp".into(),
pid: std::process::id().cast_signed(),
};
assert_eq!(
socket.connect().await.err().map(|error| error.kind()),
Some(io::ErrorKind::InvalidInput)
);
}