Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
// SPDX-License-Identifier: FSL-1.1-MIT
// Copyright (c) 2026 Cua AI, Inc.
//! Real machine join through a forward proxy. Environment is confined to a child
//! test process; no production injection switch or global environment mutation.
use std::{process::Command, time::Duration};
use cua_relay::{
client::{JoinConfig, SessionEnd},
server::{Relay, RelayConfig},
};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{TcpListener, TcpStream},
};
use tokio_util::sync::CancellationToken;
const TOKEN: &str = "machine-secret-canary";
const ID: &str = "proxy-machine";
const CHILD: &str = "CUA_RELAY_PROXY_TEST_CHILD";
/// Binds the fake proxy inside the child and points `var` at it before
/// anything reads the variable. The parent never picks a port for the child
/// to bind later: a port freed in between can be taken by a test running in
/// parallel ("Address already in use"). The variable is set in the child
/// only, before its runtime starts.
fn bind_proxy(var: &str) -> std::net::TcpListener {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
listener.set_nonblocking(true).unwrap();
std::env::set_var(var, format!("http://{}", listener.local_addr().unwrap()));
listener
}
#[test]
fn machine_join_uses_http_proxy() {
if std::env::var_os(CHILD).is_none() {
let mut child = Command::new(std::env::current_exe().unwrap());
child.args(["--exact", "machine_join_uses_http_proxy", "--nocapture"]);
for key in [
"HTTP_PROXY",
"http_proxy",
"HTTPS_PROXY",
"https_proxy",
"ALL_PROXY",
"all_proxy",
"NO_PROXY",
"no_proxy",
"REQUEST_METHOD",
] {
child.env_remove(key);
}
let result = child.env(CHILD, "1").output().unwrap();
assert!(
result.status.success(),
"{}\n{}",
String::from_utf8_lossy(&result.stdout),
String::from_utf8_lossy(&result.stderr)
);
return;
}
let proxy = bind_proxy("HTTP_PROXY");
tokio::runtime::Runtime::new().unwrap().block_on(async {
let relay = Relay::new(RelayConfig { tokens: vec![TOKEN.into()], ..RelayConfig::default() });
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let serving = relay.clone();
let relay_task = tokio::spawn(async move { serving.serve(listener, std::future::pending()).await });
let proxy = TcpListener::from_std(proxy).unwrap();
let proxy_task = tokio::spawn(async move {
let (mut inbound, _) = proxy.accept().await.unwrap();
let mut head = Vec::new();
while !head.ends_with(b"\r\n\r\n") {
head.push(inbound.read_u8().await.unwrap());
assert!(head.len() < 4096);
}
let head = String::from_utf8(head).unwrap();
assert!(head.starts_with("CONNECT relay.invalid:80 HTTP/1.1\r\n"), "{head}");
assert!(!head.contains(TOKEN));
assert!(!head.to_ascii_lowercase().contains("authorization"));
let mut upstream = TcpStream::connect(addr).await.unwrap();
// Every status/header boundary may be fragmented by the network.
for byte in b"HTTP/1.1 200 Connection established\r\n\r\n" {
inbound.write_all(&[*byte]).await.unwrap();
tokio::task::yield_now().await;
}
tokio::io::copy_bidirectional(&mut inbound, &mut upstream).await.unwrap();
});
let local = TcpListener::bind("127.0.0.1:0").await.unwrap();
let local_addr = local.local_addr().unwrap();
let local_task = tokio::spawn(async move {
axum::serve(local, axum::Router::new().route("/health", axum::routing::get(|| async { "through-machine" }))).await.unwrap();
});
let config = JoinConfig::new("ws://relay.invalid".into(), TOKEN.into(), ID.into(), local_addr);
let stop = CancellationToken::new();
let stopped = stop.clone();
let mut join = tokio::spawn(async move { cua_relay::client::session(&config, &stopped).await });
let online = async {
while !relay.machine_ids().contains(&ID.to_string()) {
tokio::time::sleep(Duration::from_millis(10)).await;
}
};
tokio::select! {
ended = &mut join => panic!("join ended before becoming online: {ended:?}"),
result = tokio::time::timeout(Duration::from_secs(5), online) => result.expect("proxy join never became online"),
}
let response = reqwest::Client::builder().no_proxy().build().unwrap()
.get(format!("http://{addr}/m/{ID}/health")).bearer_auth(TOKEN)
.send().await.unwrap();
assert_eq!(response.status(), 200);
assert_eq!(response.text().await.unwrap(), "through-machine");
stop.cancel();
assert!(matches!(join.await.unwrap(), SessionEnd::Shutdown));
// Established-session mux task lifetime is existing client behavior.
// Partial CONNECT cancellation is covered in transport's unit tests.
proxy_task.abort();
local_task.abort();
relay_task.abort();
});
}
#[test]
fn proxy_tunnel_keeps_native_tls_verification() {
if std::env::var_os(CHILD).is_none() {
let mut child = Command::new(std::env::current_exe().unwrap());
child.args([
"--exact",
"proxy_tunnel_keeps_native_tls_verification",
"--nocapture",
]);
for key in [
"HTTP_PROXY",
"http_proxy",
"HTTPS_PROXY",
"https_proxy",
"ALL_PROXY",
"all_proxy",
"NO_PROXY",
"no_proxy",
"REQUEST_METHOD",
] {
child.env_remove(key);
}
let result = child.env(CHILD, "1").output().unwrap();
assert!(
result.status.success(),
"{}\n{}",
String::from_utf8_lossy(&result.stdout),
String::from_utf8_lossy(&result.stderr)
);
return;
}
let proxy = bind_proxy("HTTPS_PROXY");
tokio::runtime::Runtime::new().unwrap().block_on(async {
let proxy = TcpListener::from_std(proxy).unwrap();
// This is a public test-only identity, never installed in a trust store.
let identity = native_tls::Identity::from_pkcs8(
include_bytes!("fixtures/untrusted-cert.pem"),
include_bytes!("fixtures/untrusted-key.pem"),
)
.unwrap();
let tls =
tokio_native_tls::TlsAcceptor::from(native_tls::TlsAcceptor::new(identity).unwrap());
let proxy_task = tokio::spawn(async move {
let (mut stream, _) = proxy.accept().await.unwrap();
let mut head = Vec::new();
while !head.ends_with(b"\r\n\r\n") {
head.push(stream.read_u8().await.unwrap());
}
assert!(String::from_utf8(head)
.unwrap()
.starts_with("CONNECT relay.invalid:443 HTTP/1.1\r\n"));
stream.write_all(b"HTTP/1.1 200 OK\r\n\r\n").await.unwrap();
// Schannel can finish the server handshake before the client
// rejects its certificate. The client must send no application
// data, regardless of when the server observes the rejection.
if let Ok(mut stream) = tls.accept(stream).await {
let mut byte = [0];
let read = tokio::time::timeout(Duration::from_secs(2), stream.read(&mut byte))
.await
.expect("rejected TLS connection remained open");
assert!(
matches!(read, Ok(0) | Err(_)),
"client sent application data to an untrusted origin"
);
}
});
let config = JoinConfig::new(
"wss://relay.invalid/?origin-secret-canary".into(),
TOKEN.into(),
ID.into(),
"127.0.0.1:1".parse().unwrap(),
);
let end = tokio::time::timeout(
Duration::from_secs(5),
cua_relay::client::session(&config, &CancellationToken::new()),
)
.await
.unwrap();
assert!(
matches!(&end, SessionEnd::Lost(reason) if reason == "origin TLS failed"),
"{end:?}"
);
assert!(!format!("{end:?}").contains("canary"));
tokio::time::timeout(Duration::from_secs(2), proxy_task)
.await
.expect("TLS fixture server did not finish")
.unwrap();
});
}
#[test]
#[allow(clippy::result_large_err)] // tungstenite callback fixes the error response type.
fn reconnect_through_proxy_rereads_token_and_checks_pinned_keys() {
if std::env::var_os(CHILD).is_none() {
let mut child = Command::new(std::env::current_exe().unwrap());
child.args([
"--exact",
"reconnect_through_proxy_rereads_token_and_checks_pinned_keys",
"--nocapture",
]);
for key in [
"HTTP_PROXY",
"http_proxy",
"HTTPS_PROXY",
"https_proxy",
"ALL_PROXY",
"all_proxy",
"NO_PROXY",
"no_proxy",
"REQUEST_METHOD",
] {
child.env_remove(key);
}
let result = child.env(CHILD, "1").output().unwrap();
assert!(
result.status.success(),
"{}\n{}",
String::from_utf8_lossy(&result.stdout),
String::from_utf8_lossy(&result.stderr)
);
return;
}
let proxy = bind_proxy("HTTP_PROXY");
tokio::runtime::Runtime::new().unwrap().block_on(async {
use base64::Engine as _;
use cua_relay::assertion::{RelayKey, JWKS_HEADER, OWNER_HEADER};
let proxy = TcpListener::from_std(proxy).unwrap();
let files = tempfile::tempdir().unwrap();
let token_path = files.path().join("token");
std::fs::write(&token_path, "first-token").unwrap();
let mut config = JoinConfig::new(
"ws://relay.invalid".into(),
"stale-token".into(),
ID.into(),
"127.0.0.1:1".parse().unwrap(),
);
config.relay_token_file = Some(token_path.clone());
config.max_backoff = Duration::from_millis(10);
let pinned = RelayKey::generate();
config
.account
.pin_jwks_json(&pinned.jwks().to_string())
.unwrap();
let account = config.account.clone();
let header =
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(pinned.jwks().to_string());
let stop = CancellationToken::new();
let stopped = stop.clone();
let run = tokio::spawn(cua_relay::client::run(config, stopped));
let test = async {
for token in ["first-token", "second-token"] {
let (mut stream, _) = proxy.accept().await.unwrap();
let mut head = Vec::new();
while !head.ends_with(b"\r\n\r\n") {
head.push(stream.read_u8().await.unwrap());
}
assert!(!String::from_utf8(head).unwrap().contains("token"));
stream.write_all(b"HTTP/1.1 200 OK\r\n\r\n").await.unwrap();
let mut socket = tokio_tungstenite::accept_hdr_async(
stream,
|request: &http::Request<()>, mut response: http::Response<()>| {
assert_eq!(
request.headers()["authorization"],
format!("Bearer {token}")
);
assert_eq!(request.headers()[cua_relay::MACHINE_ID_HEADER], ID);
response
.headers_mut()
.insert(JWKS_HEADER, header.parse().unwrap());
response
.headers_mut()
.insert(OWNER_HEADER, "proxy-test-owner".parse().unwrap());
Ok(response)
},
)
.await
.unwrap();
std::fs::write(&token_path, "second-token").unwrap();
// Wait until session processed the key-bearing 101 before closing.
while account.owner().as_deref() != Some("proxy-test-owner") {
tokio::task::yield_now().await;
}
socket.close(None).await.unwrap();
}
};
tokio::time::timeout(Duration::from_secs(5), test)
.await
.unwrap();
stop.cancel();
tokio::time::timeout(Duration::from_secs(1), run)
.await
.unwrap()
.unwrap();
});
}