agentmux_srv/server/
capture_mask_route.rs1use serde::Deserialize;
8
9use super::*;
10use crate::sagas::capture_mask;
11
12#[derive(Debug, Deserialize)]
13pub(super) struct CaptureMaskRequest {
14 capture_id: String,
15 phase: String,
16}
17
18fn refusal(req: &CaptureMaskRequest, agent: bool, container: bool) -> Option<(StatusCode, &'static str)> {
20 if !agent {
21 return Some((StatusCode::FORBIDDEN, "the capture mask is for an agent known by its token"));
22 }
23 if container {
24 return Some((
25 StatusCode::FORBIDDEN,
26 "a container agent can't capture the host's windows: use UI screenshot of your own pane, or of a browser pane you opened",
27 ));
28 }
29 let id_ok = !req.capture_id.is_empty() && req.capture_id.len() <= 64 && req.capture_id.chars().all(|c| c.is_ascii_alphanumeric() || c == '-');
30 if !id_ok || !matches!(req.phase.as_str(), "begin" | "end") {
31 return Some((StatusCode::BAD_REQUEST, "capture_mask needs a capture_id and a phase of begin or end"));
32 }
33 None
34}
35
36pub(super) async fn handle_capture_mask(
37 State(state): State<AppState>,
38 grant: Option<axum::Extension<crate::backend::container::credential::ContainerGrant>>,
39 caller: Option<axum::Extension<super::caller::Caller>>,
40 Json(req): Json<CaptureMaskRequest>,
41) -> impl IntoResponse {
42 let agent = caller.as_ref().and_then(|c| c.0.uid()).is_some();
43 if let Some((status, why)) = refusal(&req, agent, grant.is_some()) {
44 return (status, Json(json!({ "error": why }))).into_response();
45 }
46 let masks = capture_mask::ask(&state, &req.capture_id, &req.phase, capture_mask::WAIT_FOR).await;
47 (StatusCode::OK, Json(json!({ "masks": masks }))).into_response()
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 fn req(id: &str, phase: &str) -> CaptureMaskRequest {
55 CaptureMaskRequest { capture_id: id.into(), phase: phase.into() }
56 }
57
58 #[test]
59 fn only_a_host_agent_asks_and_a_container_is_told_what_to_do_instead() {
60 assert_eq!(refusal(&req("c1", "begin"), false, false).map(|r| r.0), Some(StatusCode::FORBIDDEN));
61 let (status, why) = refusal(&req("c1", "begin"), true, true).unwrap();
62 assert_eq!(status, StatusCode::FORBIDDEN);
63 assert!(why.contains("UI screenshot of your own pane"), "{why}");
64 assert!(refusal(&req("c1", "begin"), true, false).is_none());
65 assert!(refusal(&req("c1", "end"), true, false).is_none());
66 }
67
68 #[test]
69 fn a_bad_id_or_phase_is_refused() {
70 for (id, phase) in [("", "begin"), ("c 1", "begin"), (&"x".repeat(65)[..], "end"), ("c1", "middle")] {
71 assert_eq!(refusal(&req(id, phase), true, false).map(|r| r.0), Some(StatusCode::BAD_REQUEST), "{id:?} {phase:?}");
72 }
73 }
74}