agentmux_srv/server/
dispatch_guard.rs1use std::sync::atomic::{AtomicBool, Ordering};
24use std::sync::{Arc, OnceLock};
25
26use crate::backend::rpc::action_access;
27use crate::backend::rpc::engine::{DispatchGuard, WshRpcEngine};
28use crate::backend::rpc_types::RpcContext;
29
30pub(crate) const DEV_SWITCH: &str = "AGENTMUX_DEV_OPEN_RPC";
32
33#[derive(Debug, Clone, Copy, Default)]
35pub(crate) struct Mode {
36 pub dev_open: bool,
38 pub headless: bool,
40 pub external_host: bool,
42 pub local_sign_in: bool,
44}
45
46#[derive(Debug, PartialEq, Eq)]
47pub(crate) enum Verdict {
48 Allow,
49 AllowUnproven,
51 Refuse(String),
52}
53
54pub(crate) fn decide(command: &str, data: &serde_json::Value, ctx: &RpcContext, mode: Mode) -> Verdict {
55 if mode.dev_open || action_access::is_open_for(command, data) {
56 return Verdict::Allow;
57 }
58 if !ctx.agent_id.is_empty() {
59 return if action_access::is_own_pane(command) {
60 Verdict::Allow
61 } else {
62 Verdict::Refuse(format!("FORBIDDEN: {command} is the AgentMux window's call; an agent reaches AgentMux with its MuxAPI tools"))
63 };
64 }
65 if ctx.operator_proven_now() {
66 return Verdict::Allow;
67 }
68 if mode.headless && !mode.external_host && !mode.local_sign_in && !ctx.operator_proven {
69 return Verdict::AllowUnproven;
70 }
71 match ctx.operator_only(&format!("{command} is the AgentMux window's call")) {
72 Err(refusal) => Verdict::Refuse(refusal),
73 Ok(()) => Verdict::Allow,
74 }
75}
76
77fn mode() -> Mode {
79 #[cfg(test)]
82 return Mode { dev_open: true, ..Mode::default() };
83 #[cfg(not(test))]
84 live_mode()
85}
86
87#[cfg(not(test))]
88fn live_mode() -> Mode {
89 static DEV_OPEN: OnceLock<bool> = OnceLock::new();
90 let dev_open = *DEV_OPEN.get_or_init(|| {
91 dev_open_from(agentmux_common::RuntimeMode::from_env(), std::env::var(DEV_SWITCH).ok().as_deref())
92 });
93 Mode {
94 dev_open,
95 headless: crate::headless::active(),
96 external_host: super::external_host::host().is_some(),
97 local_sign_in: super::local_session_routes::sign_in_exists(),
98 }
99}
100
101pub(crate) fn dev_open_from(runtime: Option<agentmux_common::RuntimeMode>, var: Option<&str>) -> bool {
103 matches!(runtime, Some(agentmux_common::RuntimeMode::Dev { .. })) && matches!(var, Some("1") | Some("true"))
104}
105
106pub(crate) fn install(engine: &Arc<WshRpcEngine>) {
108 engine.set_dispatch_guard(guard_with(mode));
109}
110
111fn guard_with(mode: impl Fn() -> Mode + Send + Sync + 'static) -> DispatchGuard {
112 let logged = AtomicBool::new(false);
113 Arc::new(move |command: &str, data: &serde_json::Value, ctx: &RpcContext| {
114 if ctx.agent_id.is_empty() && ctx.operator_proven_now() {
116 return Ok(());
117 }
118 match decide(command, data, ctx, mode()) {
119 Verdict::Allow => Ok(()),
120 Verdict::AllowUnproven => {
121 if !logged.swap(true, Ordering::Relaxed) {
122 tracing::warn!(client_type = %ctx.client_type, "a connection no host proved calls the window's commands, in a headless srv with no host to prove it");
123 }
124 Ok(())
125 }
126 Verdict::Refuse(refusal) => Err(refusal),
127 }
128 })
129}
130
131#[cfg(test)]
132#[path = "tests/dispatch_guard_tests.rs"]
133mod dispatch_guard_tests;