mirror of
https://github.com/openai/codex.git
synced 2026-11-17 14:58:36 +00:00
Add dynamic HTTP header helpers for MCP servers (#38245)
## What changed - Add `http_headers_helper` configuration for local streamable HTTP MCP servers. The configured shell command runs once per connection and returns a JSON object of headers that is cached across requests. - Apply helper headers to MCP startup and OAuth flows while restricting them to the server origin, stopping redirects, rejecting reserved or duplicate headers, and enforcing output and execution limits. - Reject helpers for remote or managed-disabled servers, use the local environment working directory, and redact helper commands from `codex mcp list` and `codex mcp get` output. ## Testing - Cover configuration validation, helper lifecycle and output parsing, origin isolation, OAuth discovery and token refresh, managed requirements, environment selection, and CLI redaction. GitOrigin-RevId: 84e0e26ce75520b0869d37c72b1678e033bd6818
This commit is contained in:
1 parent
8d4d57387a
commit
379cb68444
38 files changed
+1285
-72
No files matched your search
@@ -112,6 +112,17 @@ struct EchoArgs {
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut args = std::env::args_os().skip(1);
|
||||
match args.next().as_deref() {
|
||||
Some(value) if value == std::ffi::OsStr::new("--http-headers-helper") => {
|
||||
if std::env::var_os("MCP_TEST_AMBIENT_SECRET").is_some() {
|
||||
return Err("helper inherited ambient secret".into());
|
||||
}
|
||||
println!(r#"{{"Proxy-Authorization":"Bearer gateway-token"}}"#);
|
||||
return Ok(());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
let bind_addr = parse_bind_addr()?;
|
||||
let post_failure_state = PostFailureState::default();
|
||||
const MAX_BIND_RETRIES: u32 = 20;
|
||||
@@ -209,6 +220,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
} else {
|
||||
router
|
||||
};
|
||||
let router = if let Ok(token) = std::env::var("MCP_EXPECT_GATEWAY_BEARER") {
|
||||
let expected = Arc::new(format!("Bearer {token}"));
|
||||
router.layer(middleware::from_fn_with_state(
|
||||
expected,
|
||||
require_gateway_bearer,
|
||||
))
|
||||
} else {
|
||||
router
|
||||
};
|
||||
|
||||
axum::serve(listener, router).await?;
|
||||
task::yield_now().await;
|
||||
@@ -412,6 +432,25 @@ async fn require_bearer(
|
||||
}
|
||||
}
|
||||
|
||||
async fn require_gateway_bearer(
|
||||
State(expected): State<Arc<String>>,
|
||||
request: Request<Body>,
|
||||
next: Next,
|
||||
) -> Result<Response, StatusCode> {
|
||||
if !request.uri().path().starts_with("/mcp") {
|
||||
return Ok(next.run(request).await);
|
||||
}
|
||||
if request
|
||||
.headers()
|
||||
.get("proxy-authorization")
|
||||
.is_some_and(|value| value.as_bytes() == expected.as_bytes())
|
||||
{
|
||||
Ok(next.run(request).await)
|
||||
} else {
|
||||
Err(StatusCode::UNAUTHORIZED)
|
||||
}
|
||||
}
|
||||
|
||||
async fn arm_session_post_failure(
|
||||
State(state): State<PostFailureState>,
|
||||
Json(request): Json<ArmSessionPostFailureRequest>,
|
||||
|
||||
Reference in new issue
Block a user