mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
Send the installation ID header on authenticated Responses requests (#65183)
## What changed Add `x-codex-installation-id` to Responses API HTTP requests and WebSocket handshakes when an authorization header is attached and the destination is a trusted OpenAI endpoint. Use the persisted installation ID already sent in `client_metadata` and mark the header value as sensitive. ## Testing Cover header delivery over WebSocket and HTTP, retention across a trusted redirect, and omission for unauthenticated requests and third-party endpoints. GitOrigin-RevId: 3fa83816cc91584944d6023680ba0dd05fbca138
This commit is contained in:
committed by
copyberry
parent
0d17a47212
commit
f20d485ea2
@@ -1621,6 +1621,9 @@ impl ModelClient {
|
||||
) -> std::result::Result<ApiWebSocketConnection, ApiError> {
|
||||
let mut headers = self.build_websocket_headers(responses_metadata).await;
|
||||
headers.extend(responses_headers.clone());
|
||||
if auth_context.auth_header_attached {
|
||||
add_responses_installation_id(&mut headers, &api_provider, responses_metadata);
|
||||
}
|
||||
inject_span_w3c_trace_headers(&tracing::Span::current(), &mut headers);
|
||||
if let Some(affinity) = parent_routing_affinity {
|
||||
let uri = api_provider
|
||||
@@ -2177,6 +2180,7 @@ impl ResponsesHttpExecutor<'_> {
|
||||
pending_retry,
|
||||
)
|
||||
.map_err(|error| self.client.state.provider.map_api_error(error))?;
|
||||
let include_installation_id = request_auth_context.auth_header_attached;
|
||||
let (request_telemetry, sse_telemetry) = ModelClientSession::build_streaming_telemetry(
|
||||
session_telemetry,
|
||||
request_auth_context,
|
||||
@@ -2240,6 +2244,13 @@ impl ResponsesHttpExecutor<'_> {
|
||||
let request_session_telemetry =
|
||||
session_telemetry_for_request(session_telemetry, &request);
|
||||
options.extra_headers.extend(responses_headers);
|
||||
if include_installation_id {
|
||||
add_responses_installation_id(
|
||||
&mut options.extra_headers,
|
||||
&client_setup.api_provider,
|
||||
responses_metadata,
|
||||
);
|
||||
}
|
||||
let thread_id = self.client.state.thread_id.to_string();
|
||||
let kind = codex_extension_api::ModelRequestKind::Generation;
|
||||
crate::model_request::check_budget(&self.client.request_contributors, &thread_id, kind)
|
||||
@@ -2977,6 +2988,27 @@ fn is_guardian_reviewer(responses_headers: &ApiHeaderMap) -> bool {
|
||||
.is_some_and(|value| value == "reviewer")
|
||||
}
|
||||
|
||||
fn add_responses_installation_id(
|
||||
headers: &mut ApiHeaderMap,
|
||||
provider: &ApiProvider,
|
||||
metadata: &CodexResponsesMetadata,
|
||||
) {
|
||||
let destination = provider.url_for_path(RESPONSES_PATH);
|
||||
let trusted = url::Url::parse(&destination).is_ok_and(|mut url| {
|
||||
if url.scheme() == "wss" {
|
||||
let _ = url.set_scheme("https");
|
||||
}
|
||||
url.fragment().is_none() && codex_http_client::is_openai_api_url(url.as_str())
|
||||
});
|
||||
if trusted
|
||||
&& !metadata.installation_id.is_empty()
|
||||
&& let Ok(mut value) = HeaderValue::from_str(&metadata.installation_id)
|
||||
{
|
||||
value.set_sensitive(true);
|
||||
headers.insert(X_CODEX_INSTALLATION_ID_HEADER, value);
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds the extra headers attached to Responses API requests.
|
||||
///
|
||||
/// These headers implement Codex-specific conventions:
|
||||
|
||||
@@ -2151,6 +2151,10 @@ async fn chatgpt_auth_sends_correct_request() {
|
||||
assert_eq!(request_originator, originator().value);
|
||||
assert_eq!(request_authorization, "Bearer Access Token");
|
||||
assert_eq!(request_chatgpt_account_id, "account_id");
|
||||
assert_eq!(
|
||||
request.header(codex_core::X_CODEX_INSTALLATION_ID_HEADER),
|
||||
None
|
||||
);
|
||||
assert_codex_client_metadata(
|
||||
&request_body,
|
||||
installation_id.as_str(),
|
||||
|
||||
@@ -6,6 +6,7 @@ use base64::Engine;
|
||||
use codex_core::CodexThread;
|
||||
use codex_core::StartThreadOptions;
|
||||
use codex_core::TurnInputRequest;
|
||||
use codex_core::X_CODEX_INSTALLATION_ID_HEADER as INSTALLATION_HEADER;
|
||||
use codex_features::Feature;
|
||||
use codex_features::RuntimeAuthMode;
|
||||
use codex_history::InitialHistory;
|
||||
@@ -44,6 +45,104 @@ use wiremock::matchers::path;
|
||||
|
||||
const TEST_NAME: &str = "suite::runtime_auth::harness_shares_runtime_and_honors_thread_auth_config";
|
||||
|
||||
#[tokio::test]
|
||||
async fn responds_with_persisted_installation_id_on_oauth_requests() -> Result<()> {
|
||||
use wiremock::matchers::header;
|
||||
|
||||
skip_if_no_network!(Ok(()));
|
||||
const ID: &str = "11111111-1111-4111-8111-111111111111";
|
||||
const RESPONSES_PATH: &str = "/backend-api/codex/responses";
|
||||
const REDIRECT_URL: &str = "https://us.chatgpt.com/redirected/responses";
|
||||
let server = MockServer::start().await;
|
||||
if std::env::var_os(PROBE_SERVER).is_some() {
|
||||
for (base_url, authenticated) in [
|
||||
(codex_model_provider_info::CHATGPT_CODEX_BASE_URL, true),
|
||||
("https://chatgpt.com/unauthenticated", false),
|
||||
] {
|
||||
let builder = test_codex();
|
||||
let builder = if authenticated {
|
||||
builder.with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing())
|
||||
} else {
|
||||
builder.with_auth_manager(
|
||||
codex_login::test_support::auth_manager_from_optional_auth(/*auth*/ None),
|
||||
)
|
||||
};
|
||||
let test = builder
|
||||
.with_config(core_test_support::test_codex::configure_scenario_catalog)
|
||||
.with_config(move |config| {
|
||||
config.model_provider.base_url = Some(base_url.to_owned());
|
||||
config.model_provider.supports_websockets = true;
|
||||
// The test subprocess routes the native stack through its TLS proxy.
|
||||
config.respect_system_proxy = false;
|
||||
})
|
||||
.with_pre_build_hook(|home| {
|
||||
std::fs::write(home.join("installation_id"), ID).expect("seed installation ID");
|
||||
})
|
||||
.build(&server)
|
||||
.await?;
|
||||
test.codex.prewarm().await;
|
||||
test.submit_turn("hello").await?;
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
for (verb, response) in [
|
||||
("GET", ResponseTemplate::new(426)),
|
||||
(
|
||||
"POST",
|
||||
ResponseTemplate::new(307).insert_header("location", REDIRECT_URL),
|
||||
),
|
||||
] {
|
||||
let mut request = Mock::given(method(verb));
|
||||
if verb == "GET" {
|
||||
request = request.and(header("upgrade", "websocket"));
|
||||
}
|
||||
request
|
||||
.and(path(RESPONSES_PATH))
|
||||
.and(header("authorization", "Bearer Access Token"))
|
||||
.and(header(INSTALLATION_HEADER, ID))
|
||||
.respond_with(response)
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
}
|
||||
let sse = responses::sse(vec![responses::ev_completed("ok")]);
|
||||
let redirect =
|
||||
responses::mount_sse_once_match(&server, path("/redirected/responses"), sse.clone()).await;
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/unauthenticated/responses"))
|
||||
.and(header("upgrade", "websocket"))
|
||||
.and(|request: &wiremock::Request| {
|
||||
!request.headers.contains_key("authorization")
|
||||
&& !request.headers.contains_key(INSTALLATION_HEADER)
|
||||
})
|
||||
.respond_with(ResponseTemplate::new(426))
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
let unauthenticated =
|
||||
responses::mount_sse_once_match(&server, path("/unauthenticated/responses"), sse).await;
|
||||
run_harness_through_proxy(
|
||||
&server,
|
||||
"suite::runtime_auth::responds_with_persisted_installation_id_on_oauth_requests",
|
||||
/*realtime_target*/ None,
|
||||
)
|
||||
.await?;
|
||||
let redirect = redirect.single_request();
|
||||
let unauthenticated = unauthenticated.single_request();
|
||||
assert_eq!(redirect.header(INSTALLATION_HEADER).as_deref(), Some(ID));
|
||||
assert_eq!(unauthenticated.header(INSTALLATION_HEADER), None);
|
||||
assert_eq!(unauthenticated.header("authorization"), None);
|
||||
for request in [redirect, unauthenticated] {
|
||||
assert_eq!(
|
||||
request.body_json()["client_metadata"][INSTALLATION_HEADER],
|
||||
ID
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn harness_shares_runtime_and_honors_thread_auth_config() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
@@ -206,6 +305,24 @@ async fn harness_shares_runtime_and_honors_thread_auth_config() -> Result<()> {
|
||||
Some(label)
|
||||
);
|
||||
}
|
||||
match label {
|
||||
Some(
|
||||
"startup-disabled" | "catalog-dual" | "catalog-assertion" | "parent-dual"
|
||||
| "child-assertion",
|
||||
) => {
|
||||
let body = request.body_json();
|
||||
let installation_id = body["client_metadata"][INSTALLATION_HEADER]
|
||||
.as_str()
|
||||
.expect("persisted installation ID");
|
||||
assert_eq!(
|
||||
request.header(INSTALLATION_HEADER).as_deref(),
|
||||
Some(installation_id),
|
||||
"{label:?}"
|
||||
);
|
||||
}
|
||||
Some("third-party") => assert_eq!(request.header(INSTALLATION_HEADER), None),
|
||||
_ => {}
|
||||
}
|
||||
let auth = request.header("authorization").unwrap();
|
||||
let proof = if expected_auth == "AgentAssertion " {
|
||||
assert!(request.header("x-openai-agent-assertion").is_none());
|
||||
|
||||
Reference in New Issue
Block a user