Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua): the daemon ignores caller headers from loopback clients (#5887)

ddupontb7d1b29944af
diff --git a/libs/cua/crates/cua-daemon/src/server.rs b/libs/cua/crates/cua-daemon/src/server.rs
index fefd7791fe..2f3246e752 100644
--- a/libs/cua/crates/cua-daemon/src/server.rs
+++ b/libs/cua/crates/cua-daemon/src/server.rs
@@ -730,6 +730,17 @@ async fn accept_loop<L: Accept>(listener: L, app: BoxedApp, mut shutdown: watch:
conns.abort_all();
}
+/// Headers that name a caller other than this machine's user.
+pub const FORWARDED_CALLER_HEADERS: &[&str] = &["x-cua-host-caller", "x-cua-share-caller"];
+
+/// Removes [`FORWARDED_CALLER_HEADERS`] from a request that did not come
+/// through the daemon socket.
+pub fn strip_forwarded_caller(headers: &mut http::HeaderMap) {
+ for h in FORWARDED_CALLER_HEADERS {
+ headers.remove(*h);
+ }
+}
+
fn app(shared: Arc<Shared>, require_token: bool) -> BoxedApp {
let svc = Svc(shared.clone());
// The clients' limit, not gRPC's 4 MiB default: a tool call can carry
@@ -835,10 +846,15 @@ fn app(shared: Arc<Shared>, require_token: bool) -> BoxedApp {
let busy = Busy::new(shared.clone());
async move {
let _busy = busy;
- let req = req.map(AxumBody::new);
+ let mut req = req.map(AxumBody::new);
if require_token && !authorized(&req, &token) {
return Ok::<_, std::convert::Infallible>(unauthorized(&req));
}
+ if require_token {
+ // A call on the loopback listener is this machine's user:
+ // only the socket's peer forwards another caller.
+ strip_forwarded_caller(req.headers_mut());
+ }
let grpc_web = req
.headers()
.get(http::header::CONTENT_TYPE)
diff --git a/libs/cua/crates/cua-daemon/tests/loopback_caller.rs b/libs/cua/crates/cua-daemon/tests/loopback_caller.rs
new file mode 100644
index 0000000000..faa3418bdd
--- /dev/null
+++ b/libs/cua/crates/cua-daemon/tests/loopback_caller.rs
@@ -0,0 +1,68 @@
+//! A call on the loopback listener is this machine's user: a caller header
+//! the client sets is dropped, so the call is the owner's. Only the 0600
+//! socket's peer (the host's cua-spacesd) forwards another caller.
+//! Temp dirs and loopback only.
+
+use cua_daemon::server::{ServerConfig, start};
+use cua_daemon::{Runtime as DaemonRuntime, RuntimeConfig};
+use cua_proto::env::v1 as pb;
+use cua_proto::env::v1::host_snapshots_service_client::HostSnapshotsServiceClient;
+use std::time::Duration;
+
+#[tokio::test]
+async fn a_caller_header_on_the_loopback_listener_is_dropped() {
+ let dir = tempfile::tempdir().unwrap();
+ let runtime = DaemonRuntime::new(RuntimeConfig {
+ state_dir: Some(dir.path().join("sandboxes")),
+ spaces_home: Some(dir.path().to_path_buf()),
+ ..Default::default()
+ })
+ .unwrap();
+ let handle = start(
+ runtime,
+ ServerConfig {
+ socket_path: None,
+ loopback: Some(([127, 0, 0, 1], 0).into()),
+ token: "t0k3n".into(),
+ discovery_path: None,
+ bridge_ticket_ttl: Duration::from_secs(60),
+ },
+ )
+ .await
+ .unwrap();
+ let channel = tonic::transport::Endpoint::from_shared(handle.loopback_url.clone().unwrap())
+ .unwrap()
+ .connect()
+ .await
+ .unwrap();
+ let mut client = HostSnapshotsServiceClient::new(channel);
+
+ // Exporting a checkpoint is the owner's alone. With no such checkpoint
+ // the owner is told so; a forged editor would be told it is not allowed.
+ let export = |caller: Option<&str>| {
+ let mut req = tonic::Request::new(pb::ExportHostSnapshotRequest {
+ name: "nothing".into(),
+ ..Default::default()
+ });
+ req.metadata_mut()
+ .insert("authorization", "Bearer t0k3n".parse().unwrap());
+ if let Some(c) = caller {
+ req.metadata_mut()
+ .insert("x-cua-host-caller", c.parse().unwrap());
+ }
+ req
+ };
+ let forged = r#"{"account":"someone-else","role":"shared","via":"relay"}"#;
+ let status = client
+ .export_host_snapshot(export(Some(forged)))
+ .await
+ .unwrap_err();
+ assert_ne!(
+ status.code(),
+ tonic::Code::PermissionDenied,
+ "the forged caller was honored: {status}"
+ );
+ // The same call with no header is the owner's, so it fails alike.
+ let own = client.export_host_snapshot(export(None)).await.unwrap_err();
+ assert_eq!(status.code(), own.code());
+}