mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Restrict MCP user verification and add workspace-scoped identity (#43524)
## Why Configured MCP servers must not be able to request user verification, even when they reuse the hosted apps server name. Local verification identity selection also needs to distinguish a user's workspace memberships. ## What changed - Advertise `userVerification` only to the host-owned apps MCP server and cancel verification requests from other servers before prompting the client. - Add `CodexAuth::get_chatgpt_account_user_id()` to read the access token's opaque `chatgpt_account_user_id` only when its workspace matches the selected account. Missing or malformed claims return no identity, without falling back to another user ID or breaking ordinary authentication. ## Testing Add coverage for capability filtering, rejection of configured servers including those using the hosted apps name, and account-user identity selection with malformed or mismatched claims. Add an MCP round-trip test showing that verification waits for a client response even in full access mode, returns the supplied proof, and discards response metadata. GitOrigin-RevId: 156e83d5a0302c013b85e564b46b4bfe858cbdb0
This commit is contained in:
1 parent
adee0b04fa
commit
f326857cf4
11 files changed
+587
-68
No files matched your search
@@ -0,0 +1,156 @@
|
||||
//! Local account-user identity selection must use the access token's selected workspace.
|
||||
|
||||
use super::*;
|
||||
use base64::Engine;
|
||||
use pretty_assertions::assert_eq;
|
||||
use serde_json::json;
|
||||
|
||||
fn access_token(auth_claims: Value) -> String {
|
||||
let payload = json!({"https://api.openai.com/auth": auth_claims});
|
||||
let payload = base64::engine::general_purpose::URL_SAFE_NO_PAD
|
||||
.encode(serde_json::to_vec(&payload).unwrap());
|
||||
format!("e30.{payload}.c2ln")
|
||||
}
|
||||
|
||||
fn managed_auth(access_token: String, selected_account: Option<&str>) -> CodexAuth {
|
||||
let auth = CodexAuth::create_dummy_chatgpt_auth_for_testing();
|
||||
let CodexAuth::Chatgpt(managed) = &auth else {
|
||||
panic!("expected managed ChatGPT auth");
|
||||
};
|
||||
let mut state = managed.state.auth_dot_json.lock().unwrap();
|
||||
let tokens = state.as_mut().unwrap().tokens.as_mut().unwrap();
|
||||
tokens.access_token = access_token;
|
||||
tokens.account_id = selected_account.map(str::to_owned);
|
||||
tokens.id_token.chatgpt_user_id = Some("different-id-token-user".to_string());
|
||||
drop(state);
|
||||
auth
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn account_user_id_comes_from_the_access_token_without_synthesis() {
|
||||
let auth = managed_auth(
|
||||
access_token(json!({
|
||||
"chatgpt_account_user_id": "opaque-membership",
|
||||
"chatgpt_account_id": "workspace-a",
|
||||
"chatgpt_user_id": "chatgpt-user",
|
||||
"user_id": "auth-user",
|
||||
})),
|
||||
Some("workspace-a"),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
auth.get_chatgpt_account_user_id(),
|
||||
Some("opaque-membership".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn account_user_id_rejects_missing_malformed_or_unscoped_claims() {
|
||||
for claims in [
|
||||
json!(null),
|
||||
json!({"user_id": "auth-user", "chatgpt_account_id": "workspace-a"}),
|
||||
json!({"chatgpt_user_id": "chatgpt-user", "chatgpt_account_id": "workspace-a"}),
|
||||
json!({"chatgpt_account_user_id": null, "chatgpt_account_id": "workspace-a"}),
|
||||
json!({"chatgpt_account_user_id": "", "chatgpt_account_id": "workspace-a"}),
|
||||
json!({"chatgpt_account_user_id": " membership ", "chatgpt_account_id": "workspace-a"}),
|
||||
json!({"chatgpt_account_user_id": 123, "chatgpt_account_id": "workspace-a"}),
|
||||
json!({"chatgpt_account_user_id": "membership"}),
|
||||
json!({"chatgpt_account_user_id": "membership", "chatgpt_account_id": "workspace-b"}),
|
||||
json!({"chatgpt_account_user_id": "membership", "chatgpt_account_id": 123}),
|
||||
] {
|
||||
let auth = managed_auth(access_token(claims.clone()), Some("workspace-a"));
|
||||
assert_eq!(auth.get_chatgpt_account_user_id(), None, "{claims}");
|
||||
}
|
||||
|
||||
for token in [
|
||||
"not-a-jwt".to_string(),
|
||||
"e30.!.c2ln".to_string(),
|
||||
format!(
|
||||
"{}.extra",
|
||||
access_token(json!({
|
||||
"chatgpt_account_user_id": "membership",
|
||||
"chatgpt_account_id": "workspace-a",
|
||||
}))
|
||||
),
|
||||
] {
|
||||
let auth = managed_auth(token, Some("workspace-a"));
|
||||
assert_eq!(auth.get_chatgpt_account_user_id(), None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn account_user_id_requires_a_selected_account() {
|
||||
for selected_account in [None, Some(""), Some(" workspace-a ")] {
|
||||
let auth = managed_auth(
|
||||
access_token(json!({
|
||||
"chatgpt_account_user_id": "membership",
|
||||
"chatgpt_account_id": "workspace-a",
|
||||
})),
|
||||
selected_account,
|
||||
);
|
||||
assert_eq!(auth.get_chatgpt_account_user_id(), None);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn external_account_user_id_must_match_the_selected_workspace() {
|
||||
let token = access_token(json!({
|
||||
"chatgpt_account_user_id": "membership-a",
|
||||
"chatgpt_account_id": "workspace-a",
|
||||
}));
|
||||
for (selected_account, expected) in [
|
||||
("workspace-a", Some("membership-a".to_string())),
|
||||
("workspace-b", None),
|
||||
] {
|
||||
let auth = CodexAuth::from_external_chatgpt_tokens(
|
||||
&token,
|
||||
selected_account,
|
||||
/*chatgpt_plan_type*/ None,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(auth.get_chatgpt_account_user_id(), expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_account_user_id_does_not_break_ordinary_external_auth() {
|
||||
let token = access_token(json!({
|
||||
"chatgpt_account_user_id": 123,
|
||||
"chatgpt_account_id": "workspace-a",
|
||||
"chatgpt_user_id": "chatgpt-user",
|
||||
}));
|
||||
let auth = CodexAuth::from_external_chatgpt_tokens(
|
||||
&token,
|
||||
"workspace-a",
|
||||
/*chatgpt_plan_type*/ None,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
(
|
||||
auth.get_chatgpt_account_user_id(),
|
||||
auth.get_chatgpt_user_id(),
|
||||
auth.get_account_id(),
|
||||
),
|
||||
(
|
||||
None,
|
||||
Some("chatgpt-user".to_string()),
|
||||
Some("workspace-a".to_string()),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn api_key_and_header_auth_do_not_expose_an_account_user_id() {
|
||||
let mut headers = http::HeaderMap::new();
|
||||
headers.insert(
|
||||
"chatgpt-account-id",
|
||||
http::HeaderValue::from_static("workspace-a"),
|
||||
);
|
||||
for auth in [
|
||||
CodexAuth::from_api_key("test-api-key"),
|
||||
CodexAuth::Headers(AuthHeaders::new(headers)),
|
||||
] {
|
||||
assert_eq!(auth.get_chatgpt_account_user_id(), None);
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,7 @@ use crate::default_client::create_client;
|
||||
use crate::default_client::create_default_auth_client;
|
||||
use crate::outbound_proxy::AuthRouteConfig;
|
||||
use crate::token_data::TokenData;
|
||||
use crate::token_data::parse_chatgpt_account_user_id;
|
||||
use crate::token_data::parse_chatgpt_jwt_claims;
|
||||
use crate::token_data::parse_jwt_expiration;
|
||||
use codex_config::ManagedAuthPolicy;
|
||||
@@ -631,6 +632,19 @@ impl CodexAuth {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the access token's opaque account-user identity only when its workspace
|
||||
/// matches the selected account. Missing claims never fall back to a user id.
|
||||
/// Unlike `get_chatgpt_user_id`, this identifies one workspace membership, so keys
|
||||
/// are not shared across a person's workspaces. Workload-identity exchange claims
|
||||
/// describe an agent identity and cannot select a human verification credential.
|
||||
/// This is local identity selection, not access-token or proof validation.
|
||||
pub fn get_chatgpt_account_user_id(&self) -> Option<String> {
|
||||
let tokens = self.get_current_token_data()?;
|
||||
parse_chatgpt_account_user_id(&tokens.access_token, tokens.account_id.as_deref()?)
|
||||
.ok()
|
||||
.flatten()
|
||||
}
|
||||
|
||||
/// Account-facing plan classification derived from the current auth.
|
||||
/// Returns a high-level `AccountPlanType` (e.g., Free/Plus/Pro/Team/…)
|
||||
/// for UI or product decisions based on the user's subscription.
|
||||
@@ -3070,3 +3084,7 @@ mod tests;
|
||||
#[cfg(test)]
|
||||
#[path = "change_state_tests.rs"]
|
||||
mod change_state_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "account_user_id_tests.rs"]
|
||||
mod account_user_id_tests;
|
||||
@@ -104,6 +104,18 @@ struct StandardJwtClaims {
|
||||
exp: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct AccountUserClaims {
|
||||
#[serde(rename = "https://api.openai.com/auth")]
|
||||
auth: Option<AccountUserAuthClaims>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct AccountUserAuthClaims {
|
||||
chatgpt_account_user_id: Option<String>,
|
||||
chatgpt_account_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum IdTokenInfoError {
|
||||
#[error("invalid ID token format")]
|
||||
@@ -134,6 +146,31 @@ pub fn parse_jwt_expiration(jwt: &str) -> Result<Option<DateTime<Utc>>, IdTokenI
|
||||
.and_then(|exp| DateTime::<Utc>::from_timestamp(exp, 0)))
|
||||
}
|
||||
|
||||
// Keep membership parsing separate from ordinary auth claims: a malformed optional
|
||||
// membership must disable local verification without breaking an otherwise valid login.
|
||||
// Both paths share decode_jwt_payload for the JWT envelope.
|
||||
pub(crate) fn parse_chatgpt_account_user_id(
|
||||
jwt: &str,
|
||||
account_id: &str,
|
||||
) -> Result<Option<String>, IdTokenInfoError> {
|
||||
if account_id.is_empty() || account_id.trim() != account_id {
|
||||
return Ok(None);
|
||||
}
|
||||
if jwt.split('.').count() != 3 {
|
||||
return Err(IdTokenInfoError::InvalidFormat);
|
||||
}
|
||||
let claims: AccountUserClaims = decode_jwt_payload(jwt)?;
|
||||
let Some(auth) = claims.auth else {
|
||||
return Ok(None);
|
||||
};
|
||||
if auth.chatgpt_account_id.as_deref() != Some(account_id) {
|
||||
return Ok(None);
|
||||
}
|
||||
Ok(auth
|
||||
.chatgpt_account_user_id
|
||||
.filter(|id| !id.is_empty() && id.trim() == id.as_str()))
|
||||
}
|
||||
|
||||
pub fn parse_chatgpt_jwt_claims(jwt: &str) -> Result<IdTokenInfo, IdTokenInfoError> {
|
||||
let claims: IdClaims = decode_jwt_payload(jwt)?;
|
||||
let email = claims
|
||||
|
||||
Reference in new issue
Block a user