mirror of
https://github.com/openai/codex.git
synced 2026-10-21 17:48:44 +00:00
## Why Destination restrictions and policy revocation must remain effective during redirects, response body reads, and established WebSocket traffic. Policy denials must also survive error handling so callers do not retry them or report a revoked operation as successful. ## What changed - Route managed HTTP clients through a shared `RequestBuilder` and policy-aware execution, checking each redirect destination before route resolution and retaining a network permit while consuming response bodies. - Guard WebSocket connection setup, reads, and writes with revocable permits, including independent wakeups for split readers and writers. - Preserve `TransportError::Policy` through HTTP, SSE, and realtime error handling, and treat policy denials as non-retryable. - Keep the supplied network policy in plugin startup HTTP requests and propagate response body failures from backend and plugin requests. ## Testing Add regression coverage for rejection before connecting, revocation during redirect routing and streamed body reads, split WebSocket revocation, realtime writer error propagation, and revoked plugin upload responses. GitOrigin-RevId: fba36700444c98a8364c09b4dc5452c4d78a90fb
96 lines
3.7 KiB
Rust
96 lines
3.7 KiB
Rust
//! Unavailable policy rejects requests before a socket is opened.
|
|
|
|
use super::*;
|
|
use pretty_assertions::assert_eq;
|
|
|
|
#[tokio::test]
|
|
async fn unavailable_policy_does_not_connect() {
|
|
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
|
listener.set_nonblocking(true).unwrap();
|
|
let url = format!("https://{}/", listener.local_addr().unwrap());
|
|
let controller = crate::NetworkPolicyController::default();
|
|
let factory = HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault)
|
|
.with_network_policy(controller.policy());
|
|
let client = factory.build_client(&url, ClientRouteClass::Api).unwrap();
|
|
assert!(matches!(
|
|
client.get(&url).send().await,
|
|
Err(RouteAwareRequestError::Policy(
|
|
NetworkPolicyDenied::Unavailable
|
|
))
|
|
));
|
|
assert_eq!(
|
|
listener.accept().unwrap_err().kind(),
|
|
std::io::ErrorKind::WouldBlock
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn account_change_cancels_redirect_route_selection() {
|
|
let (address, server) = super::tests::spawn_response_server(vec![
|
|
"HTTP/1.1 302 Found\r\nLocation: /final\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
|
|
.into(),
|
|
]);
|
|
let controller = crate::NetworkPolicyController::default();
|
|
let policy = controller.policy();
|
|
controller.publish(policy.revision(), crate::DestinationPolicy::Unrestricted);
|
|
let pool = RouteAwareClientPool::new(
|
|
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault)
|
|
.with_network_policy(policy.clone()),
|
|
ClientRouteClass::Api,
|
|
);
|
|
let request = reqwest::Request::new(
|
|
Method::GET,
|
|
reqwest::Url::parse(&format!("http://{address}/start")).unwrap(),
|
|
);
|
|
let result = tokio::time::timeout(
|
|
std::time::Duration::from_secs(/*secs*/ 5),
|
|
pool.send_with_resolver(request, |url| {
|
|
let policy = policy.clone();
|
|
async move {
|
|
if url.ends_with("/final") {
|
|
policy.invalidate();
|
|
return std::future::pending().await;
|
|
}
|
|
Ok(OutboundProxyRoute::Direct)
|
|
}
|
|
}),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert!(matches!(
|
|
result,
|
|
Err(RouteAwareRequestError::Policy(NetworkPolicyDenied::Revoked))
|
|
));
|
|
assert_eq!(server.join().unwrap().len(), 1);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn managed_redirect_failure_is_tracked_before_proxy_retry() {
|
|
let unused = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
|
let dead_address = unused.local_addr().unwrap();
|
|
drop(unused);
|
|
let (address, server) = super::tests::spawn_response_server(vec![format!(
|
|
"HTTP/1.1 307 Temporary Redirect\r\nLocation: http://{dead_address}/token\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
|
|
)]);
|
|
let controller = crate::NetworkPolicyController::default();
|
|
let policy = controller.policy();
|
|
controller.publish(policy.revision(), crate::DestinationPolicy::Unrestricted);
|
|
let redirected = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(/*v*/ false));
|
|
let pool = RouteAwareClientPool::with_builder(
|
|
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault)
|
|
.with_network_policy(policy)
|
|
.with_system_proxy_fallback(),
|
|
ClientRouteClass::Auth,
|
|
HttpClientBuilder::new().with_redirect_tracking(redirected.clone()),
|
|
);
|
|
let result = pool
|
|
.post(format!("http://{address}/token"))
|
|
.body("code=one-time-code")
|
|
.timeout(std::time::Duration::from_secs(/*secs*/ 5))
|
|
.send()
|
|
.await;
|
|
assert!(result.is_err());
|
|
assert!(redirected.load(std::sync::atomic::Ordering::Relaxed));
|
|
assert_eq!(server.join().unwrap().len(), 1);
|
|
}
|