Files
codex/codex-rs/cli/src/mcp_cmd_tests.rs
T
willwang-openai 83b56bc5ad Support client secrets for pre-registered MCP OAuth clients (#47891)
## Why

MCP OAuth configuration supported a pre-registered client ID but could not supply a client secret for token exchange or refresh.

## What changed

- Add `oauth.client_secret` and `codex mcp add --oauth-client-secret`, requiring a nonempty secret and client ID. Persist the secret in server configuration and accept `clientSecret` in plugin configuration.
- Pass configured credentials through CLI, app-server, and plugin login flows and token refresh. Require a new login when a configured confidential client's ID differs from stored credentials.
- Redact secrets in debug output and keep them out of authorization URLs and persisted OAuth token records.
- Invalidate cached OAuth connections when the configured client ID or secret changes.

## Testing

Add coverage for configuration validation and round trips, CLI argument redaction and subsequent login, app-server login, connection invalidation, and refresh with `client_secret_basic` and `client_secret_post` in both refresh modes. Verify secret exclusion from token records and reject mismatched client IDs before contacting the provider.

GitOrigin-RevId: 85bb0e410e9e68e6cd68eb6359badfc7eac78147
2026-09-24 17:26:18 +00:00

63 lines
1.8 KiB
Rust

//! Verifies MCP client-secret argument requirements and redacted CLI diagnostics.
use clap::Parser;
use pretty_assertions::assert_eq;
use super::McpCli;
use super::McpSubcommand;
#[test]
fn oauth_client_secret_is_redacted_in_parsed_command_debug() {
let cli = McpCli::try_parse_from([
"mcp",
"add",
"private",
"--url",
"https://example.com/mcp",
"--oauth-client-id",
"registered-client",
"--oauth-client-secret",
"cli-secret-marker",
])
.expect("parse confidential client arguments");
let debug = format!("{cli:?}");
assert!(!debug.contains("cli-secret-marker"));
assert!(debug.contains("oauth_client_secret: Some(<redacted>)"));
let McpSubcommand::Add(add) = cli.subcommand else {
panic!("expected MCP add");
};
let http = add.transport_args.streamable_http.expect("HTTP arguments");
assert_eq!(
http.oauth_client_secret
.as_ref()
.map(|secret| secret.as_str()),
Some("cli-secret-marker")
);
}
#[test]
fn oauth_client_secret_requires_url_and_client_id_without_disclosure() {
for args in [
vec!["--url", "https://example.com/mcp"],
vec!["--oauth-client-id", "registered-client"],
] {
let error = McpCli::try_parse_from(
[
"mcp",
"add",
"private",
"--oauth-client-secret",
"cli-secret-marker",
]
.into_iter()
.chain(args),
)
.expect_err("client secret requires both HTTP URL and client ID");
assert_eq!(
error.kind(),
clap::error::ErrorKind::MissingRequiredArgument
);
assert!(!error.to_string().contains("cli-secret-marker"));
}
}