aKode fase 1: niente sync plugin OpenAI, CDN pets, CIMD chatgpt.com, npm registry; pulizia codice morto
This commit is contained in:
@@ -7,7 +7,6 @@ use rmcp::transport::AuthorizationRequest;
|
||||
use rmcp::transport::AuthorizationSession;
|
||||
use rmcp::transport::auth::OAuthHttpClient;
|
||||
use rmcp::transport::auth::OAuthState;
|
||||
use url::Url;
|
||||
|
||||
use crate::oauth::validate_authorization_server_endpoints;
|
||||
use crate::oauth_callback::McpOAuthCallbackMode;
|
||||
@@ -18,10 +17,12 @@ use crate::oauth_callback::validate_callback_redirect;
|
||||
/// OAuth client-registration strategy for one interactive HTTP MCP login.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub enum McpOAuthClientRegistration {
|
||||
/// Prefer a supported native CIMD and otherwise use advertised DCR.
|
||||
/// Use the advertised Dynamic Client Registration endpoint.
|
||||
// aKode: upstream preferred a ChatGPT-hosted CIMD here; only DCR is used now.
|
||||
#[default]
|
||||
Auto,
|
||||
/// Require a ChatGPT-hosted Codex public native Client ID Metadata Document.
|
||||
/// Client ID Metadata Document registration.
|
||||
// aKode: always rejected; upstream pointed at a ChatGPT-hosted metadata document.
|
||||
Cimd,
|
||||
/// Require the authorization server's Dynamic Client Registration endpoint.
|
||||
Dcr,
|
||||
@@ -42,6 +43,11 @@ pub(crate) async fn start_authorization(
|
||||
callback_id: &str,
|
||||
client_registration: McpOAuthClientRegistration,
|
||||
) -> Result<PreparedOAuthLogin> {
|
||||
// aKode: CIMD is disabled because its client identifier was a ChatGPT-hosted document.
|
||||
if client_registration == McpOAuthClientRegistration::Cimd {
|
||||
bail!("MCP OAuth CIMD client registration is not supported; use dynamic client registration");
|
||||
}
|
||||
|
||||
let mut auth_manager =
|
||||
AuthorizationManager::new_with_oauth_http_client(server_url, http_client).await?;
|
||||
auth_manager.set_allow_missing_issuer(true);
|
||||
@@ -50,79 +56,18 @@ pub(crate) async fn start_authorization(
|
||||
let authorization_server_issuer = metadata.issuer.clone();
|
||||
let callback_mode = callback_mode(&metadata)?;
|
||||
|
||||
let cimd_advertised = metadata
|
||||
.additional_fields
|
||||
.get("client_id_metadata_document_supported")
|
||||
.and_then(serde_json::Value::as_bool)
|
||||
.unwrap_or(false);
|
||||
let public_client_auth_supported = metadata
|
||||
.additional_fields
|
||||
.get("token_endpoint_auth_methods_supported")
|
||||
.and_then(serde_json::Value::as_array)
|
||||
.is_some_and(|methods| methods.iter().any(|method| method.as_str() == Some("none")));
|
||||
|
||||
let uses_shared_callback = callback_mode == McpOAuthCallbackMode::IssuerBound;
|
||||
let redirect_uri = if uses_shared_callback {
|
||||
redirect_uri.to_string()
|
||||
} else {
|
||||
append_callback_id_to_redirect_uri(redirect_uri, callback_id)?
|
||||
};
|
||||
let parsed_redirect_uri = Url::parse(&redirect_uri)?;
|
||||
let expected_callback_path = if uses_shared_callback {
|
||||
"/callback".to_string()
|
||||
} else {
|
||||
format!("/callback/{callback_id}")
|
||||
};
|
||||
let native_redirect_supported = parsed_redirect_uri.scheme() == "http"
|
||||
&& matches!(
|
||||
parsed_redirect_uri.host_str(),
|
||||
Some("127.0.0.1" | "localhost")
|
||||
)
|
||||
&& parsed_redirect_uri.port().is_some_and(|port| port > 0)
|
||||
&& parsed_redirect_uri.path() == expected_callback_path
|
||||
&& parsed_redirect_uri.query().is_none()
|
||||
&& parsed_redirect_uri.fragment().is_none()
|
||||
&& parsed_redirect_uri.username().is_empty()
|
||||
&& parsed_redirect_uri.password().is_none();
|
||||
validate_callback_redirect(&redirect_uri, callback_id, callback_mode)?;
|
||||
// MCP 2026-07-28 priority: pre-registered clients never reach this path; offer
|
||||
// advertised CIMD here and otherwise let rmcp fall back to DCR.
|
||||
// https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/client-registration
|
||||
let offer_cimd = match client_registration {
|
||||
McpOAuthClientRegistration::Auto => {
|
||||
cimd_advertised && native_redirect_supported && public_client_auth_supported
|
||||
}
|
||||
McpOAuthClientRegistration::Cimd => {
|
||||
if !cimd_advertised || !public_client_auth_supported {
|
||||
bail!(
|
||||
"MCP authorization server does not advertise CIMD with token endpoint auth method `none`"
|
||||
);
|
||||
}
|
||||
if !native_redirect_supported {
|
||||
bail!(
|
||||
"MCP OAuth CIMD requires an ephemeral loopback callback at `{expected_callback_path}`"
|
||||
);
|
||||
}
|
||||
true
|
||||
}
|
||||
McpOAuthClientRegistration::Dcr => false,
|
||||
};
|
||||
|
||||
// Pre-registered clients never reach this path; rmcp uses the advertised DCR endpoint.
|
||||
auth_manager.set_metadata(metadata);
|
||||
let mut request = AuthorizationRequest::new(redirect_uri.clone())
|
||||
let request = AuthorizationRequest::new(redirect_uri.clone())
|
||||
.with_scopes(scopes.iter().copied())
|
||||
.with_client_name("Codex");
|
||||
if offer_cimd {
|
||||
// CIMD is an active IETF Internet-Draft: this HTTPS client identifier resolves
|
||||
// to its self-referential JSON metadata document.
|
||||
// https://datatracker.ietf.org/doc/draft-ietf-oauth-client-id-metadata-document/
|
||||
let client_metadata_url = if uses_shared_callback {
|
||||
"https://chatgpt.com/oauth/codex/client.json".to_string()
|
||||
} else {
|
||||
format!("https://chatgpt.com/oauth/codex/{callback_id}/client.json")
|
||||
};
|
||||
request = request.with_client_metadata_url(client_metadata_url);
|
||||
}
|
||||
let session = AuthorizationSession::new(auth_manager, request)
|
||||
.await
|
||||
.map_err(|(_auth_manager, error)| error)?;
|
||||
|
||||
@@ -127,68 +127,28 @@ async fn authorization(
|
||||
Ok((state, query))
|
||||
}
|
||||
|
||||
// aKode: CIMD (a ChatGPT-hosted client metadata document) is never offered; automatic
|
||||
// registration always uses DCR, even when the server advertises CIMD support.
|
||||
#[tokio::test]
|
||||
async fn automatic_cimd_uses_stable_or_callback_specific_identity() -> Result<()> {
|
||||
for (host, supports_issuer, expected_client_id, expected_redirect) in [
|
||||
async fn automatic_registration_uses_dcr_even_when_cimd_is_advertised() -> Result<()> {
|
||||
for (host, supports_issuer, expected_redirect) in [
|
||||
(
|
||||
"127.0.0.1",
|
||||
false,
|
||||
"https://chatgpt.com/oauth/codex/abc123ABC_-x/client.json",
|
||||
"http://127.0.0.1:43123/callback/abc123ABC_-x",
|
||||
),
|
||||
(
|
||||
"localhost",
|
||||
false,
|
||||
"https://chatgpt.com/oauth/codex/abc123ABC_-x/client.json",
|
||||
"http://localhost:43123/callback/abc123ABC_-x",
|
||||
),
|
||||
(
|
||||
"127.0.0.1",
|
||||
true,
|
||||
"https://chatgpt.com/oauth/codex/client.json",
|
||||
"http://127.0.0.1:43123/callback",
|
||||
),
|
||||
("127.0.0.1", true, "http://127.0.0.1:43123/callback"),
|
||||
] {
|
||||
let server = oauth_server(json!({
|
||||
"authorization_response_iss_parameter_supported": supports_issuer,
|
||||
}))
|
||||
.await;
|
||||
let redirect = format!("http://{host}:43123/callback");
|
||||
let (mut state, query) =
|
||||
let (_, query) =
|
||||
authorization(&server, &redirect, McpOAuthClientRegistration::Auto).await?;
|
||||
assert_eq!(query["client_id"], expected_client_id);
|
||||
assert_eq!(query["client_id"], "dcr-client");
|
||||
assert_eq!(query["redirect_uri"], expected_redirect);
|
||||
assert_eq!(query["code_challenge_method"], "S256");
|
||||
assert_eq!(query["scope"], "read offline_access");
|
||||
|
||||
state
|
||||
.handle_callback_with_issuer(
|
||||
"valid-authorization-code",
|
||||
&query["state"],
|
||||
supports_issuer
|
||||
.then(|| format!("{}/mcp", server.uri()))
|
||||
.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
let token_requests = requests_to(&server, "/token").await;
|
||||
assert_eq!(token_requests.len(), 1);
|
||||
let request = &token_requests[0];
|
||||
let body: HashMap<_, _> = url::form_urlencoded::parse(&request.body)
|
||||
.into_owned()
|
||||
.collect();
|
||||
assert_eq!(body["client_id"], expected_client_id);
|
||||
assert_eq!(body["redirect_uri"], expected_redirect);
|
||||
assert_eq!(body["grant_type"], "authorization_code");
|
||||
assert!(body.contains_key("code_verifier"));
|
||||
assert!(!body.contains_key("client_secret"));
|
||||
assert!(!request.headers.contains_key("authorization"));
|
||||
assert!(requests_to(&server, "/register").await.is_empty());
|
||||
assert_eq!(
|
||||
requests_to(&server, "/.well-known/oauth-authorization-server/mcp")
|
||||
.await
|
||||
.len(),
|
||||
1
|
||||
);
|
||||
assert_eq!(requests_to(&server, "/register").await.len(), 1);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -510,57 +470,21 @@ async fn resource_headers_follow_same_origin_registration_redirect_and_sdk_auth_
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_cimd_metadata_and_redirects_fail_without_dynamic_registration() {
|
||||
async fn explicit_cimd_is_rejected_without_contacting_the_server() {
|
||||
let valid = "http://127.0.0.1:43123/callback/abc123ABC_-x";
|
||||
for (metadata, redirect, expected_error) in [
|
||||
(
|
||||
json!({
|
||||
"authorization_response_iss_parameter_supported": true,
|
||||
"issuer": null,
|
||||
}),
|
||||
valid,
|
||||
"issuer-bound callbacks require an authorization server issuer",
|
||||
),
|
||||
(
|
||||
json!({"token_endpoint_auth_methods_supported": ["private_key_jwt"]}),
|
||||
valid,
|
||||
"token endpoint auth method `none`",
|
||||
),
|
||||
(
|
||||
json!({}),
|
||||
"http://127.0.0.1.evil.example:43123/callback/abc123ABC_-x",
|
||||
"ephemeral loopback callback",
|
||||
),
|
||||
(
|
||||
json!({}),
|
||||
"http://127.0.0.1/callback/abc123ABC_-x",
|
||||
"ephemeral loopback callback",
|
||||
),
|
||||
(
|
||||
json!({}),
|
||||
"http://127.0.0.1:43123/callback/wrong-id",
|
||||
"ephemeral loopback callback",
|
||||
),
|
||||
(
|
||||
json!({}),
|
||||
"http://127.0.0.1:43123/callback/abc123ABC_-x?unexpected=true",
|
||||
"ephemeral loopback callback",
|
||||
),
|
||||
(
|
||||
json!({}),
|
||||
"http://[::1]:43123/callback/abc123ABC_-x",
|
||||
"ephemeral loopback callback",
|
||||
),
|
||||
] {
|
||||
let server = oauth_server(metadata).await;
|
||||
let error = authorization(&server, redirect, McpOAuthClientRegistration::Cimd)
|
||||
let server = oauth_server(json!({})).await;
|
||||
let error = authorization(&server, valid, McpOAuthClientRegistration::Cimd)
|
||||
.await
|
||||
.err()
|
||||
.expect("CIMD should be rejected");
|
||||
assert!(error.to_string().contains("CIMD client registration is not supported"));
|
||||
assert!(
|
||||
server
|
||||
.received_requests()
|
||||
.await
|
||||
.err()
|
||||
.expect("invalid CIMD metadata or callback should fail");
|
||||
assert!(error.to_string().contains(expected_error));
|
||||
assert!(requests_to(&server, "/register").await.is_empty());
|
||||
assert!(requests_to(&server, "/token").await.is_empty());
|
||||
}
|
||||
.expect("mock server should record requests")
|
||||
.is_empty()
|
||||
);
|
||||
|
||||
let server = oauth_server(json!({"registration_endpoint": null})).await;
|
||||
let error = authorization(&server, valid, McpOAuthClientRegistration::Dcr)
|
||||
|
||||
Reference in New Issue
Block a user