mirror of
https://github.com/openai/codex.git
synced 2026-11-22 15:15:18 +00:00
Add strict automatic review for MCP elicitations (#36365)
## What changed - Recognize the `codex_strict_auto_review` MCP elicitation marker and route marked approval requests through the configured automatic reviewer. - Accept only canonical automatic-review approvals and fail closed without a user prompt when review is unavailable, denied, malformed, disallowed by policy, or requests persistence. - Validate Codex Apps approval metadata against the active MCP invocation and build the review request from the trusted invocation details. ## Testing - Add unit coverage for strict review decisions, malformed markers, reviewer failures, explicit policy denials, and lifecycle cleanup. - Add app-server round-trip coverage for approvals, denials, configuration constraints, spoofed metadata, persistent requests, and subsequent turns. GitOrigin-RevId: af73b45a9b63118f25f6429d95b3222c5dbb59fb
This commit is contained in:
1 parent
0d109f097c
commit
287e1020ae
8 files changed
+773
-27
No files matched your search
@@ -21,6 +21,8 @@ use async_channel::Sender;
|
||||
use codex_protocol::approvals::ElicitationRequest;
|
||||
use codex_protocol::approvals::ElicitationRequestEvent;
|
||||
use codex_protocol::mcp::RequestId as ProtocolRequestId;
|
||||
use codex_protocol::mcp_approval_meta::APPROVALS_REVIEWER_KEY;
|
||||
use codex_protocol::mcp_approval_meta::STRICT_AUTO_REVIEW_KEY;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
use codex_protocol::protocol::Event;
|
||||
@@ -32,10 +34,14 @@ use futures::future::BoxFuture;
|
||||
use futures::future::FutureExt;
|
||||
use rmcp::model::ElicitationAction;
|
||||
use rmcp::model::RequestId;
|
||||
use serde_json::Value;
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
static NEXT_ELICITATION_REQUEST_ID: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
const STRICT_AUTO_REVIEW_DECLINE_MESSAGE: &str =
|
||||
"Strict automated review failed. Do not proceed or ask the user for approval.";
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ElicitationReviewRequest {
|
||||
pub server_name: String,
|
||||
@@ -220,6 +226,53 @@ impl ElicitationRequestManager {
|
||||
reviewer,
|
||||
lifecycle,
|
||||
} = authority;
|
||||
|
||||
match elicitation
|
||||
.meta()
|
||||
.and_then(|meta| meta.get(STRICT_AUTO_REVIEW_KEY))
|
||||
{
|
||||
Some(Value::Bool(true)) => {
|
||||
if matches!(
|
||||
approval_policy,
|
||||
AskForApproval::Granular(config) if !config.allows_mcp_elicitations()
|
||||
) {
|
||||
return Ok(strict_auto_review_decline());
|
||||
}
|
||||
let Some(reviewer) = reviewer.as_ref() else {
|
||||
return Ok(strict_auto_review_decline());
|
||||
};
|
||||
let _active_elicitation =
|
||||
lifecycle.as_ref().map(ElicitationLifecycle::start);
|
||||
return Ok(
|
||||
match reviewer
|
||||
.review(ElicitationReviewRequest {
|
||||
server_name,
|
||||
request_id: id,
|
||||
elicitation,
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(Some(response))
|
||||
if response.action == ElicitationAction::Accept
|
||||
&& response.content == Some(serde_json::json!({}))
|
||||
&& response
|
||||
.meta
|
||||
.as_ref()
|
||||
.and_then(Value::as_object)
|
||||
.and_then(|meta| meta.get(APPROVALS_REVIEWER_KEY))
|
||||
.and_then(Value::as_str)
|
||||
== Some("auto_review") =>
|
||||
{
|
||||
response
|
||||
}
|
||||
Ok(Some(_)) | Ok(None) | Err(_) => strict_auto_review_decline(),
|
||||
},
|
||||
);
|
||||
}
|
||||
None | Some(Value::Bool(false)) => {}
|
||||
Some(_) => return Ok(strict_auto_review_decline()),
|
||||
}
|
||||
|
||||
if mcp_permission_prompt_is_auto_approved(
|
||||
approval_policy,
|
||||
&permission_profile,
|
||||
@@ -341,6 +394,16 @@ impl ElicitationRequestManager {
|
||||
}
|
||||
}
|
||||
|
||||
fn strict_auto_review_decline() -> ElicitationResponse {
|
||||
ElicitationResponse {
|
||||
action: ElicitationAction::Decline,
|
||||
content: None,
|
||||
meta: Some(serde_json::json!({
|
||||
"message": STRICT_AUTO_REVIEW_DECLINE_MESSAGE,
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn elicitation_is_rejected_by_policy(approval_policy: AskForApproval) -> bool {
|
||||
match approval_policy {
|
||||
AskForApproval::Never => true,
|
||||
@@ -364,3 +427,7 @@ fn can_auto_accept_elicitation(elicitation: &Elicitation) -> bool {
|
||||
Elicitation::Mcp(_) | Elicitation::OpenAiForm { .. } => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "elicitation_tests.rs"]
|
||||
mod tests;
|
||||
Reference in new issue
Block a user