Skip to main content

agentmux_srv/server/app_api/
browser_grants.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! `browsergrants.*`: the person's standing browser grants
5//! (docs/specs/SPEC_BROWSER_WEB_TERMINAL_TYPING_2026_10_23.md ยง3), for the
6//! Settings screen: list them with the presets, give an agent powers on a
7//! site, switch a console preset on, change a grant's powers, revoke one.
8//!
9//! A standing grant lets an agent act on a site without asking, so only the
10//! user's own window, proven by the host, may write one: an agent holding
11//! srv's auth key gets an error, and no agent route reaches `browser_standing`.
12
13use super::*;
14use crate::backend::rpc_types::{
15    BrowserGrantPreset, BrowserGrantRow, BrowserGrantsResult, CommandBrowserGrantsPermissionRevokeData, CommandBrowserGrantsPermissionSetData, SitePermissionRow, CommandBrowserGrantsListData, CommandBrowserGrantsPauseAllData, CommandBrowserGrantsPowersData,
16    CommandBrowserGrantsPresetData, CommandBrowserGrantsRevokeData, CommandBrowserGrantsSetData, COMMAND_BROWSER_GRANTS_LIST,
17    COMMAND_BROWSER_GRANTS_PAUSE_ALL, COMMAND_BROWSER_GRANTS_PERMISSION_REVOKE, COMMAND_BROWSER_GRANTS_PERMISSION_SET, COMMAND_BROWSER_GRANTS_POWERS, COMMAND_BROWSER_GRANTS_PRESET, COMMAND_BROWSER_GRANTS_REVOKE, COMMAND_BROWSER_GRANTS_SET,
18};
19use crate::server::browser_standing as standing;
20
21pub fn register(engine: &Arc<WshRpcEngine>, state: &AppState) {
22    engine.register_typed(COMMAND_BROWSER_GRANTS_LIST, move |_req: CommandBrowserGrantsListData, ctx| async move {
23        operator(&ctx)?;
24        Ok(snapshot())
25    });
26
27    let st = state.clone();
28    engine.register_typed(COMMAND_BROWSER_GRANTS_SET, move |req: CommandBrowserGrantsSetData, ctx| {
29        let state = st.clone();
30        async move {
31            operator(&ctx)?;
32            standing::upsert(&state, &req.agent, std::slice::from_ref(&req.site), &req.powers, false)?;
33            Ok(snapshot())
34        }
35    });
36
37    let st = state.clone();
38    engine.register_typed(COMMAND_BROWSER_GRANTS_PRESET, move |req: CommandBrowserGrantsPresetData, ctx| {
39        let state = st.clone();
40        async move {
41            operator(&ctx)?;
42            standing::enable_preset(&state, &req.preset, &req.agent)?;
43            Ok(snapshot())
44        }
45    });
46
47    let st = state.clone();
48    engine.register_typed(COMMAND_BROWSER_GRANTS_REVOKE, move |req: CommandBrowserGrantsRevokeData, ctx| {
49        let state = st.clone();
50        async move {
51            operator(&ctx)?;
52            standing::revoke(&state, &req.id)?;
53            Ok(snapshot())
54        }
55    });
56
57    let st = state.clone();
58    engine.register_typed(COMMAND_BROWSER_GRANTS_PERMISSION_SET, move |req: CommandBrowserGrantsPermissionSetData, ctx| {
59        let state = st.clone();
60        async move {
61            operator(&ctx)?;
62            crate::server::browser_site_permissions::upsert(&state, &req.agent, &req.site, &req.kinds)?;
63            Ok(snapshot())
64        }
65    });
66
67    let st = state.clone();
68    engine.register_typed(COMMAND_BROWSER_GRANTS_PERMISSION_REVOKE, move |req: CommandBrowserGrantsPermissionRevokeData, ctx| {
69        let state = st.clone();
70        async move {
71            operator(&ctx)?;
72            crate::server::browser_site_permissions::revoke(&state, &req.id)?;
73            Ok(snapshot())
74        }
75    });
76
77    let st = state.clone();
78    engine.register_typed(COMMAND_BROWSER_GRANTS_POWERS, move |req: CommandBrowserGrantsPowersData, ctx| {
79        let state = st.clone();
80        async move {
81            operator(&ctx)?;
82            standing::set_powers(&state, &req.id, &req.powers)?;
83            Ok(snapshot())
84        }
85    });
86
87    let st = state.clone();
88    engine.register_typed(COMMAND_BROWSER_GRANTS_PAUSE_ALL, move |req: CommandBrowserGrantsPauseAllData, ctx| {
89        let state = st.clone();
90        async move {
91            let by = pause_all_actor(&ctx, req.paused)?;
92            Ok(crate::server::browser_pause_all::set(&state, req.paused, by.as_deref()))
93        }
94    });
95}
96
97/// Who is turning "Pause all agent browsing" on or off, for the record: None is the person, from
98/// their own window. Anyone may turn it on. Turning it off is the person's, or an agent's as the
99/// sensitive operation `resume_agent_browsing` (reviewed by the super agent, with the person's
100/// veto), which runs it as that agent: a direct request from an agent is refused and says so.
101fn pause_all_actor(ctx: &RpcContext, paused: bool) -> Result<Option<String>, String> {
102    if ctx.operator_only("").is_ok() {
103        return Ok(None);
104    }
105    if !paused {
106        return Err(
107            "FORBIDDEN: only the person lifts a pause from their own window. To lift it as an agent, ask with the Sensitive tool: \
108             request, op resume_agent_browsing, with a reason. The super agent reviews it, the person sees a countdown and can veto it"
109                .to_string(),
110        );
111    }
112    Ok(Some(if ctx.agent_id.is_empty() { "another connection".to_string() } else { ctx.agent_id.clone() }))
113}
114
115fn operator(ctx: &RpcContext) -> Result<(), String> {
116    ctx.operator_only("only the user gives an agent standing powers on a site, or changes or revokes them")
117}
118
119fn snapshot() -> BrowserGrantsResult {
120    BrowserGrantsResult {
121        rows: standing::list()
122            .into_iter()
123            .map(|r| BrowserGrantRow { id: r.id, agent: r.agent, origins: r.origins, powers: r.powers, granted_at_ms: r.granted_at_ms })
124            .collect(),
125        presets: standing::PRESETS
126            .iter()
127            .map(|p| BrowserGrantPreset {
128                id: p.id.to_string(),
129                label: p.label.to_string(),
130                sites: p.sites.iter().map(|s| s.to_string()).collect(),
131                note: p.note.to_string(),
132            })
133            .collect(),
134        powers: crate::server::browser_powers::granted_by_the_person(),
135        permission_kinds: crate::server::browser_site_permissions::KINDS.iter().map(|k| k.to_string()).collect(),
136        permissions: crate::server::browser_site_permissions::list()
137            .into_iter()
138            .map(|r| SitePermissionRow { id: r.id, agent: r.agent, site: r.site, kinds: r.kinds, granted_at_ms: r.granted_at_ms })
139            .collect(),
140        pause_all: crate::server::browser_pause_all::status(),
141    }
142}
143
144#[cfg(test)]
145mod tests {
146    use super::*;
147
148    #[test]
149    fn an_agent_connection_cannot_list_or_write_grants() {
150        let agent = RpcContext { agent_id: "naki-2".to_string(), ..Default::default() };
151        assert!(operator(&agent).is_err());
152        assert!(operator(&RpcContext::operator_for_tests()).is_ok());
153    }
154
155    #[test]
156    fn anyone_may_pause_all_browsing_and_only_the_person_lifts_it_directly() {
157        let person = RpcContext::operator_for_tests();
158        let agent = RpcContext { agent_id: "naki-2".to_string(), ..Default::default() };
159        let other = RpcContext::default();
160        assert_eq!(pause_all_actor(&person, true), Ok(None));
161        assert_eq!(pause_all_actor(&person, false), Ok(None));
162        assert_eq!(pause_all_actor(&agent, true), Ok(Some("naki-2".to_string())));
163        assert_eq!(pause_all_actor(&other, true), Ok(Some("another connection".to_string())));
164        let refused = pause_all_actor(&agent, false).unwrap_err();
165        assert!(refused.contains("resume_agent_browsing"), "{refused}");
166        assert!(pause_all_actor(&other, false).is_err());
167    }
168
169    #[test]
170    fn the_snapshot_offers_every_preset_and_the_powers_beyond_clicks() {
171        let s = snapshot();
172        assert_eq!(s.presets.len(), standing::PRESETS.len());
173        assert_eq!(s.powers, vec!["terminal", "terminal_ask", "enter_space", "uploads"]);
174    }
175}