ctb_workspace/
capabilities.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Capability set definitions for the workspace.
21//!
22//! This module defines the capability sets that control what each subprocess
23//! (runtime, network) is allowed to do via IPC.
24
25#[allow(
26    unused_imports,
27    clippy::wildcard_imports,
28    reason = "Standard workspace prelude"
29)]
30use crate::utilities::*;
31
32use std::collections::HashMap;
33
34use ctb_workspace_ipc::auth::capability::{
35    CapabilitySet, MethodRule, MethodSelector, ServiceName,
36};
37use ctb_workspace_ipc::services::parent::SERVICE_NAME as PARENT_SERVICE_NAME;
38use ctb_workspace_ipc::services::parent::api::{
39    METHOD_MESSAGE_PARENT, METHOD_PROXY_CALL, METHOD_REQUEST_SPAWN_CHILD,
40};
41use ctb_workspace_ipc::services::process::SERVICE_NAME as PROCESS_SERVICE_NAME;
42use ipc::ChildKind;
43
44/// Create capabilities for the runtime process.
45///
46/// Runtimes can:
47/// - Send messages to parent (any method)
48/// - Request full workspace shutdown (`shutdown_tree`)
49/// - Cannot access network service directly (for denial test)
50pub fn create_runtime_capabilities() -> CapabilitySet {
51    let mut allowed: HashMap<ServiceName, Vec<MethodRule>> = HashMap::new();
52
53    // Runtime can send messages to parent, request spawns, and make
54    // workspace-mediated proxy calls to its own children.
55    allowed.insert(
56        ServiceName(PARENT_SERVICE_NAME.to_string()),
57        vec![
58            MethodRule {
59                method: MethodSelector::Exact(METHOD_MESSAGE_PARENT.into()),
60                quotas: None,
61            },
62            MethodRule {
63                method: MethodSelector::Exact(
64                    METHOD_REQUEST_SPAWN_CHILD.into(),
65                ),
66                quotas: None,
67            },
68            MethodRule {
69                method: MethodSelector::Exact(METHOD_PROXY_CALL.into()),
70                quotas: None,
71            },
72        ],
73    );
74
75    // Runtime can request full workspace shutdown
76    allowed.insert(
77        ServiceName(PROCESS_SERVICE_NAME.to_string()),
78        vec![MethodRule {
79            method: MethodSelector::Exact("shutdown_tree".into()),
80            quotas: None,
81        }],
82    );
83
84    // Runtime CANNOT access network service directly (for denial test)
85
86    CapabilitySet {
87        allowed,
88        global_limits: None,
89    }
90}
91
92/// Create capabilities for the network service process.
93///
94/// Network service can:
95/// - Request shutdown of itself and its descendants (`shutdown_own_tree`)
96pub fn create_network_capabilities() -> CapabilitySet {
97    ctb_workspace_ipc::auth::capability::default_service_capabilities(
98        ChildKind::Network,
99    )
100}
101
102/// Create capabilities for the renderer process.
103pub fn create_io_capabilities() -> CapabilitySet {
104    let mut caps =
105        ctb_workspace_ipc::auth::capability::default_service_capabilities(
106            ChildKind::Io,
107        );
108
109    // IO needs to request spawns and make proxy calls to storage child services
110    caps.allowed.insert(
111        ServiceName(PARENT_SERVICE_NAME.to_string()),
112        vec![
113            MethodRule {
114                method: MethodSelector::Exact(METHOD_MESSAGE_PARENT.into()),
115                quotas: None,
116            },
117            MethodRule {
118                method: MethodSelector::Exact(
119                    METHOD_REQUEST_SPAWN_CHILD.into(),
120                ),
121                quotas: None,
122            },
123            MethodRule {
124                method: MethodSelector::Exact(METHOD_PROXY_CALL.into()),
125                quotas: None,
126            },
127        ],
128    );
129
130    caps.allowed
131        .entry(ServiceName("workspace".to_string()))
132        .or_default()
133        .extend([
134            MethodRule {
135                method: MethodSelector::Exact("get_crlite_artifact".into()),
136                quotas: None,
137            },
138            MethodRule {
139                method: MethodSelector::Exact("get_crlite_cache_status".into()),
140                quotas: None,
141            },
142            MethodRule {
143                method: MethodSelector::Exact(
144                    "get_crlite_manifest_json".into(),
145                ),
146                quotas: None,
147            },
148            MethodRule {
149                method: MethodSelector::Exact("get_update_status".into()),
150                quotas: None,
151            },
152        ]);
153
154    caps
155    // TODO: read stdin; keyboard, mouse, etc. if configured; input/output other local input (USB, serial, etc) if configured; listen on 0.0.0.0 if the webUI is configured to do so
156}
157
158/// Create capabilities for the renderer process.
159pub fn create_storage_capabilities() -> CapabilitySet {
160    ctb_workspace_ipc::auth::capability::default_service_capabilities(
161        ChildKind::Storage,
162    )
163    // TODO: read/write files, sandboxed to the workspace's data directory
164}
165
166/// Create capabilities for the renderer process.
167pub fn create_renderer_capabilities() -> CapabilitySet {
168    ctb_workspace_ipc::auth::capability::default_service_capabilities(
169        ChildKind::Renderer,
170    )
171}
172
173#[cfg(test)]
174#[allow(
175    clippy::panic,
176    clippy::expect_used,
177    clippy::unwrap_used,
178    clippy::unwrap_in_result,
179    clippy::panic_in_result_fn,
180    clippy::indexing_slicing,
181    clippy::arithmetic_side_effects,
182    reason = "Standard repository test boilerplate"
183)]
184mod tests {
185    use super::*;
186
187    #[crate::ctb_test]
188    fn runtime_capabilities_allow_parent_service() {
189        let caps = create_runtime_capabilities();
190        assert!(
191            caps.allowed
192                .contains_key(&ServiceName(PARENT_SERVICE_NAME.to_string()))
193        );
194    }
195
196    #[crate::ctb_test]
197    fn runtime_capabilities_allow_process_shutdown() {
198        let caps = create_runtime_capabilities();
199        let process_rules = caps
200            .allowed
201            .get(&ServiceName(PROCESS_SERVICE_NAME.to_string()));
202        assert!(process_rules.is_some());
203    }
204
205    #[crate::ctb_test]
206    fn runtime_capabilities_deny_network_service() {
207        use ctb_workspace_ipc::services::network::SERVICE_NAME as NETWORK_SERVICE_NAME;
208        let caps = create_runtime_capabilities();
209        assert!(
210            !caps
211                .allowed
212                .contains_key(&ServiceName(NETWORK_SERVICE_NAME.to_string()))
213        );
214    }
215
216    #[crate::ctb_test]
217    fn network_capabilities_allow_process_shutdown() {
218        let caps = create_network_capabilities();
219        let process_rules = caps
220            .allowed
221            .get(&ServiceName(PROCESS_SERVICE_NAME.to_string()));
222        assert!(process_rules.is_some());
223    }
224}