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(spacesd): tickets record the relay assertion time; guests' media tickets last 60 s (CUA-1416) (#5018)

Francesco Bonaccic1b584d6b531
diff --git a/libs/cua-spacesd/crates/cua-spacesd-desktop/src/grpc/stream.rs b/libs/cua-spacesd/crates/cua-spacesd-desktop/src/grpc/stream.rs
index 35db19a84b..19b438ca90 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-desktop/src/grpc/stream.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-desktop/src/grpc/stream.rs
@@ -40,6 +40,10 @@ pub(crate) struct Stream(pub Arc<DesktopState>);
const DEFAULT_TICKET_TTL: Duration = Duration::from_secs(60);
const MAX_TICKET_TTL: Duration = Duration::from_secs(600);
+/// A guest's media ticket (a relayed caller other than the owner) only has
+/// to last until the attach: kept short so it can't outlive a revoke for
+/// long, whatever the client asks (CUA-1416). The guest reopens to refresh.
+const GUEST_TICKET_TTL: Duration = Duration::from_secs(60);
fn media_codec(codec: &VideoCodec) -> MediaCodec {
match codec {
@@ -287,6 +291,7 @@ impl StreamService for Stream {
) -> Result<Response<OpenMediaResponse>, Status> {
let principal = caller_principal(&request);
let viewer = cua_spacesd_server::caller(&request).viewer;
+ let relayed = cua_spacesd_server::caller(&request).account;
// A principal the server authenticated (relay-asserted or a viewer
// ticket), as opposed to one the caller merely declared.
let authenticated = cua_spacesd_server::caller(&request).principal;
@@ -340,6 +345,10 @@ impl StreamService for Stream {
.filter(|duration| !duration.is_zero())
.unwrap_or(DEFAULT_TICKET_TTL)
.min(MAX_TICKET_TTL);
+ let ttl = match &relayed {
+ Some(account) if account.role != "owner" => ttl.min(GUEST_TICKET_TTL),
+ _ => ttl,
+ };
// A window's paired audio source is its app's stream when the audio
// backend can capture per app; otherwise the desktop mix.
let paired_source = match (&request.audio, &target) {
@@ -425,10 +434,13 @@ impl StreamService for Stream {
.map_err(join_error)?
.map_err(status::media)?;
let session_id = opened.session.id().to_owned();
- let (ticket, expires_at) =
- self.0
- .ctx
- .mint_ticket(TicketScope::Media, &session_id, principal.as_ref(), ttl);
+ let (ticket, expires_at) = self.0.ctx.mint_ticket_issued(
+ TicketScope::Media,
+ &session_id,
+ principal.as_ref(),
+ relayed.as_ref().map(|a| a.issued_at),
+ ttl,
+ );
self.0
.ctx
.media_sessions()
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/src/auth.rs b/libs/cua-spacesd/crates/cua-spacesd-server/src/auth.rs
index 37c17bb3cf..6d13c3e40d 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/src/auth.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/src/auth.rs
@@ -174,6 +174,10 @@ struct TicketPayload {
p: String,
e: u64,
n: String,
+ /// The minting call's relay assertion `iat`, when relayed (the relay
+ /// refuses a ticket from before a revoke of `p`).
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ a: Option<u64>,
}
/// What an external credential authorized.
@@ -204,6 +208,10 @@ pub struct RelayCaller {
pub name: Option<String>,
/// `owner` or `shared` (an editor).
pub role: String,
+ /// The relay assertion's `iat` (seconds, relay clock). Tickets minted
+ /// for the call carry it, so the relay can refuse one that predates a
+ /// revoke of this account.
+ pub issued_at: u64,
}
/// The only calls a relayed caller may make on a machine that does not
@@ -478,6 +486,19 @@ impl Auth {
resource: &str,
principal_id: &str,
ttl: Duration,
+ ) -> (String, SystemTime) {
+ self.mint_ticket_issued(scope, resource, principal_id, None, ttl)
+ }
+
+ /// [`Self::mint_ticket`] for a relayed call: records its assertion's
+ /// `iat` (`issued`) in the ticket.
+ pub fn mint_ticket_issued(
+ &self,
+ scope: TicketScope,
+ resource: &str,
+ principal_id: &str,
+ issued: Option<u64>,
+ ttl: Duration,
) -> (String, SystemTime) {
let expires_at = SystemTime::now() + ttl.min(MAX_TICKET_TTL);
let exp_ms = expires_at
@@ -490,6 +511,7 @@ impl Auth {
p: principal_id.to_owned(),
e: exp_ms,
n: random_id(9),
+ a: issued,
};
let payload = serde_json::to_vec(&payload).expect("ticket payload serializes");
let engine = base64::engine::general_purpose::URL_SAFE_NO_PAD;
@@ -1203,6 +1225,33 @@ mod tests {
);
}
+ #[test]
+ fn a_relayed_ticket_records_the_assertion_time_and_still_validates() {
+ use base64::Engine as _;
+ let auth = Auth::new(Some("secret".into()));
+ let payload = |ticket: &str| -> serde_json::Value {
+ let body = ticket.split('.').nth(1).unwrap();
+ let raw = base64::engine::general_purpose::URL_SAFE_NO_PAD
+ .decode(body)
+ .unwrap();
+ serde_json::from_slice(&raw).unwrap()
+ };
+ let (relayed, _) = auth.mint_ticket_issued(
+ TicketScope::Media,
+ "m",
+ "bob",
+ Some(1_791_773_000),
+ Duration::from_secs(60),
+ );
+ assert_eq!(payload(&relayed)["a"], 1_791_773_000);
+ assert_eq!(payload(&relayed)["p"], "bob");
+ let claims = auth.validate_ticket(&relayed, TicketScope::Media).unwrap();
+ assert_eq!(claims.principal_id, "bob");
+ // A local caller's ticket carries no `a` (same wire form as before).
+ let (local, _) = auth.mint_ticket(TicketScope::Media, "m", "", Duration::from_secs(60));
+ assert!(payload(&local).get("a").is_none());
+ }
+
#[test]
fn ticket_expiry_is_enforced() {
let auth = Auth::new(None);
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/src/context.rs b/libs/cua-spacesd/crates/cua-spacesd-server/src/context.rs
index 5909c8c50e..69740207d8 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/src/context.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/src/context.rs
@@ -188,11 +188,24 @@ impl ServerContext {
resource: &str,
principal: Option<&Principal>,
ttl: Duration,
+ ) -> (String, SystemTime) {
+ self.mint_ticket_issued(scope, resource, principal, None, ttl)
+ }
+
+ /// [`Self::mint_ticket`] for a call the relay asserted at `issued`
+ /// (`RelayCaller::issued_at`), recorded in the ticket.
+ pub fn mint_ticket_issued(
+ &self,
+ scope: TicketScope,
+ resource: &str,
+ principal: Option<&Principal>,
+ issued: Option<u64>,
+ ttl: Duration,
) -> (String, SystemTime) {
let principal_id = principal.map(|p| p.id.as_str()).unwrap_or("");
self.inner
.auth
- .mint_ticket(scope, resource, principal_id, ttl)
+ .mint_ticket_issued(scope, resource, principal_id, issued, ttl)
}
/// Validates a ticket string for `scope`.
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/src/host_spaces.rs b/libs/cua-spacesd/crates/cua-spacesd-server/src/host_spaces.rs
index 4bebd2a77c..3997affd6a 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/src/host_spaces.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/src/host_spaces.rs
@@ -475,6 +475,7 @@ mod tests {
email: Some("[email protected]".into()),
name: None,
role: "owner".into(),
+ ..Default::default()
}),
..Default::default()
});
@@ -513,6 +514,7 @@ mod tests {
email: Some("[email protected]".into()),
name: Some(name.into()),
role: "owner".into(),
+ ..Default::default()
}),
..Default::default()
});
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/src/relay_account.rs b/libs/cua-spacesd/crates/cua-spacesd-server/src/relay_account.rs
index c56f8d5a17..c62dd888f2 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/src/relay_account.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/src/relay_account.rs
@@ -263,6 +263,7 @@ impl ExternalAuthenticator for RelayAssertionAuth {
email: claims.email.clone(),
name: claims.name.clone(),
role: if owner { "owner" } else { "shared" }.into(),
+ issued_at: claims.iat,
}),
})
})())
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/src/services/system.rs b/libs/cua-spacesd/crates/cua-spacesd-server/src/services/system.rs
index 06360cd773..d8d25d4d36 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/src/services/system.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/src/services/system.rs
@@ -1023,10 +1023,11 @@ impl SystemService for SystemServiceImpl {
)
})?;
let principal = grant.principal();
- let (ticket, expires_at) = self.ctx.mint_ticket(
+ let (ticket, expires_at) = self.ctx.mint_ticket_issued(
crate::auth::TicketScope::Viewer,
&resource,
Some(&principal),
+ caller.account.as_ref().map(|a| a.issued_at),
ttl,
);
// Hints for the page (the server enforces the grant either way).
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/src/services/tunnel.rs b/libs/cua-spacesd/crates/cua-spacesd-server/src/services/tunnel.rs
index c4531b16e9..baf24a2cba 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/src/services/tunnel.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/src/services/tunnel.rs
@@ -240,7 +240,9 @@ impl TunnelService for TunnelServiceImpl {
&self,
request: Request<ForwardRequest>,
) -> Result<GrpcResponse<ForwardResponse>, Status> {
- let principal = caller(&request).principal;
+ let caller = caller(&request);
+ let issued = caller.account.as_ref().map(|a| a.issued_at);
+ let principal = caller.principal;
let body = request.into_inner();
if body.port == 0 || body.port > u16::MAX as u32 {
return Err(crate::error::invalid("port must be 1-65535"));
@@ -263,10 +265,13 @@ impl TunnelService for TunnelServiceImpl {
.filter(|d| !d.is_zero())
.unwrap_or(FORWARD_DEFAULT_TTL);
let id = format!("fwd-{}", crate::util::random_id(9));
- let (ticket, expires_at) =
- self.state
- .ctx
- .mint_ticket(TicketScope::Tunnel, &id, principal.as_ref(), ttl);
+ let (ticket, expires_at) = self.state.ctx.mint_ticket_issued(
+ TicketScope::Tunnel,
+ &id,
+ principal.as_ref(),
+ issued,
+ ttl,
+ );
self.state.gc_forwards();
self.state.forwards.lock().expect("forwards").insert(
id.clone(),
@@ -333,7 +338,9 @@ impl TunnelService for TunnelServiceImpl {
&self,
request: Request<StartHotspotRequest>,
) -> Result<GrpcResponse<StartHotspotResponse>, Status> {
- let principal = caller(&request).principal;
+ let caller = caller(&request);
+ let issued = caller.account.as_ref().map(|a| a.issued_at);
+ let principal = caller.principal;
let body = request.into_inner();
let port = match body.socks_port {
0 => 1080,
@@ -404,10 +411,13 @@ impl TunnelService for TunnelServiceImpl {
let ttl = duration(body.ticket_ttl.as_ref())
.filter(|d| !d.is_zero())
.unwrap_or(HOTSPOT_DEFAULT_TTL);
- let (ticket, _) =
- self.state
- .ctx
- .mint_ticket(TicketScope::Hotspot, &id, principal.as_ref(), ttl);
+ let (ticket, _) = self.state.ctx.mint_ticket_issued(
+ TicketScope::Hotspot,
+ &id,
+ principal.as_ref(),
+ issued,
+ ttl,
+ );
*self.state.hotspot.lock().expect("hotspot") = Some(Arc::new(Hotspot {
id: id.clone(),
socks_address,
diff --git a/libs/cua-spacesd/crates/cua-spacesd-server/src/services/volume.rs b/libs/cua-spacesd/crates/cua-spacesd-server/src/services/volume.rs
index e1e4754494..b1ac3c0390 100644
--- a/libs/cua-spacesd/crates/cua-spacesd-server/src/services/volume.rs
+++ b/libs/cua-spacesd/crates/cua-spacesd-server/src/services/volume.rs
@@ -681,7 +681,9 @@ impl VolumeService for VolumeServiceImpl {
&self,
request: Request<AttachVolumeRequest>,
) -> Result<GrpcResponse<AttachVolumeResponse>, Status> {
- let principal = caller(&request).principal;
+ let caller = caller(&request);
+ let issued = caller.account.as_ref().map(|a| a.issued_at);
+ let principal = caller.principal;
let body = request.into_inner();
let backend = backend()
.map_err(|reason| status(Code::FailedPrecondition, ErrorReason::Unspecified, reason))?;
@@ -736,10 +738,13 @@ impl VolumeService for VolumeServiceImpl {
let ttl = duration(body.ticket_ttl.as_ref())
.filter(|d| !d.is_zero())
.unwrap_or(VOLUME_DEFAULT_TTL);
- let (ticket, _) =
- self.state
- .ctx
- .mint_ticket(TicketScope::Volume, &id, principal.as_ref(), ttl);
+ let (ticket, _) = self.state.ctx.mint_ticket_issued(
+ TicketScope::Volume,
+ &id,
+ principal.as_ref(),
+ issued,
+ ttl,
+ );
*self.state.current.lock().expect("volume") = Some(Arc::new(Session {
id: id.clone(),
hub,