cageforge-backend-api 0.7.0

Capability and execution contracts for Cageforge sandbox backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// SPDX-License-Identifier: Apache-2.0

//! Backend capability negotiation and preflight for Cageforge execution.
//!
//! This crate is the typed boundary between [`cageforge_command`] and
//! [`cageforge_policy_compose`] values and a native execution backend. It does
//! not implement native filesystem or network I/O, resolve DNS, or select an
//! operating-system sandbox. [`Sandbox`] and [`DynSandbox`] delegate execution
//! to the selected native backend while [`SandboxChild`] exposes its lifecycle.
//!
//! Start with [`BackendRequest`] and [`BackendCapabilities`]. A native backend
//! implements [`SandboxBackend`], advertises the capabilities it can enforce,
//! and calls [`BackendRequest::prepare_for`] before lowering the prepared
//! request to its operating-system API. The backend owns process launch and
//! lifecycle after this preflight boundary.

#![doc = include_str!("../README.md")]
#![deny(missing_docs)]

mod capability;
mod diagnostic;
mod execution;
mod requirements;

use std::collections::{BTreeMap, BTreeSet};
use std::ffi::OsString;
use std::fmt;
use std::marker::PhantomData;
use std::net::{IpAddr, SocketAddr};
use std::path::Path;
use std::sync::Arc;

use cageforge_command::{CommandRequest, CommandSpec, StdioSpec, TimeoutPolicy};
use cageforge_path::normalize_lexical_path;
use cageforge_policy::{
    ConnectionAuthorization, FilesystemDecision, NetworkDecision, PathResolutionContext,
    PathSelector, ResolvedNetworkTarget,
};
use cageforge_policy_compose::{
    EffectiveFilesystemLowering, EffectiveNetworkLowering, EffectivePathContext, EffectiveSandbox,
    EnvironmentInput,
};
mod model;

pub use diagnostic::{BackendDiagnostic, BackendDiagnosticMetadata};
pub use execution::{DynSandbox, Sandbox, SandboxChild, SandboxExecutionError};

pub use model::{
    BackendCapabilities, BackendCapability, BackendContractError, BackendIdentity, BackendRequest,
    PreparedBackendRequest, SandboxBackend,
};

impl BackendCapabilities {
    /// Creates an empty capability set.
    pub const fn new() -> Self {
        Self {
            capabilities: BTreeSet::new(),
        }
    }

    /// Creates a capability set from an iterable collection.
    pub fn from_capabilities<I>(capabilities: I) -> Self
    where
        I: IntoIterator<Item = BackendCapability>,
    {
        Self {
            capabilities: capabilities.into_iter().collect(),
        }
    }

    /// Returns a copy with one capability added.
    pub fn with(mut self, capability: BackendCapability) -> Self {
        self.capabilities.insert(capability);
        self
    }

    /// Returns whether this backend advertises a capability.
    pub fn supports(&self, capability: BackendCapability) -> bool {
        self.capabilities.contains(&capability)
    }

    /// Returns capabilities in deterministic order.
    pub fn iter(&self) -> impl Iterator<Item = &BackendCapability> {
        self.capabilities.iter()
    }
}

impl FromIterator<BackendCapability> for BackendCapabilities {
    fn from_iter<T: IntoIterator<Item = BackendCapability>>(iter: T) -> Self {
        Self::from_capabilities(iter)
    }
}

impl<'a> BackendRequest<'a> {
    /// Creates a backend request from a command and composed sandbox.
    pub const fn new(command: &'a CommandRequest, sandbox: &'a EffectiveSandbox) -> Self {
        Self { command, sandbox }
    }

    /// Returns the command intent.
    pub const fn command(&self) -> &'a CommandRequest {
        self.command
    }

    /// Returns the effective sandbox constraint.
    pub const fn sandbox(&self) -> &'a EffectiveSandbox {
        self.sandbox
    }

    /// Performs common preflight using exactly the capabilities advertised by
    /// `backend` and the backend's runtime path context.
    ///
    /// This is the safe handoff entry point for native integrations. The
    /// capability check is defined by Cageforge and cannot be overridden by a
    /// backend implementation. The context is narrowed before return, and an
    /// effective working directory must be supplied by the runtime context and
    /// permitted by the effective filesystem policy. The returned value is
    /// bound to `B`, so a handoff prepared for one backend type cannot be passed
    /// to a native lowering method for another backend type by accident.
    pub fn prepare_for<B: SandboxBackend>(
        self,
        backend: &B,
        base_context: &PathResolutionContext,
    ) -> Result<PreparedBackendRequest<'a, B>, BackendContractError> {
        self.validate::<B>(&backend.capabilities(), backend.identity(), base_context)
    }

    /// Computes the capabilities required by this request.
    pub fn required_capabilities(&self) -> BackendCapabilities {
        let mut required = BackendCapabilities::new().with(BackendCapability::CommandExecution);
        requirements::add_command_capabilities(&mut required, self.command);
        requirements::add_filesystem_capabilities(&mut required, self.sandbox);
        requirements::add_network_capabilities(&mut required, self.sandbox);
        requirements::add_environment_capabilities(&mut required, self.sandbox);
        required
    }

    /// Validates this request against advertised backend capabilities.
    ///
    /// No process, filesystem, DNS, or socket operation is performed. The
    /// returned value only proves that the request's portable requirements are
    /// represented by the advertised capability set; native enforcement still
    /// belongs to the backend.
    fn validate<B: SandboxBackend>(
        self,
        capabilities: &BackendCapabilities,
        backend_identity: &BackendIdentity,
        base_context: &PathResolutionContext,
    ) -> Result<PreparedBackendRequest<'a, B>, BackendContractError> {
        if !self
            .sandbox
            .environment()
            .requested_matches(self.command.environment())
        {
            return Err(BackendContractError::CommandEnvironmentMismatch);
        }
        for capability in self.required_capabilities().iter().copied() {
            if !capabilities.supports(capability) {
                return Err(BackendContractError::UnsupportedCapability { capability });
            }
        }
        let path_context = self
            .sandbox
            .path_context(base_context)
            .map_err(|source| BackendContractError::InvalidRuntimeContext { source })?;
        if !path_context.executable_roots().is_empty()
            && !capabilities.supports(BackendCapability::FilesystemExecutableMapping)
        {
            return Err(BackendContractError::UnsupportedCapability {
                capability: BackendCapability::FilesystemExecutableMapping,
            });
        }
        let working_directory = match self.command.working_directory() {
            Some(path) if path.is_absolute() => normalize_lexical_path(path).into_owned(),
            Some(path) => {
                let current_directory = base_context.current_directory().ok_or_else(|| {
                    BackendContractError::WorkingDirectoryResolution {
                        path: path.to_path_buf(),
                    }
                })?;
                normalize_lexical_path(&current_directory.join(path)).into_owned()
            }
            None => base_context
                .current_directory()
                .map(normalize_lexical_path)
                .map(std::borrow::Cow::into_owned)
                .ok_or(BackendContractError::MissingRuntimeCurrentDirectory)?,
        };
        match self
            .sandbox
            .filesystem()
            .access_for_path(&working_directory, &path_context)
            .map_err(|source| BackendContractError::FilesystemEvaluation { source })?
        {
            FilesystemDecision::Read
            | FilesystemDecision::Write
            | FilesystemDecision::ExternallyEnforced => {}
            FilesystemDecision::Deny => {
                return Err(BackendContractError::WorkingDirectoryDenied {
                    path: working_directory.clone(),
                });
            }
        }
        Ok(PreparedBackendRequest {
            request: self,
            path_context,
            working_directory,
            capabilities: capabilities.clone(),
            backend_identity: backend_identity.clone(),
            backend: PhantomData,
        })
    }
}

impl BackendIdentity {
    /// Creates a new backend-instance identity.
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        Self(Arc::new(()))
    }
}

impl fmt::Debug for BackendIdentity {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("BackendIdentity")
            .finish_non_exhaustive()
    }
}

impl PartialEq for BackendIdentity {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

impl Eq for BackendIdentity {}

impl<'a, B: SandboxBackend> Clone for PreparedBackendRequest<'a, B> {
    fn clone(&self) -> Self {
        Self {
            request: self.request,
            path_context: self.path_context.clone(),
            working_directory: self.working_directory.clone(),
            capabilities: self.capabilities.clone(),
            backend_identity: self.backend_identity.clone(),
            backend: PhantomData,
        }
    }
}

impl<'a, B: SandboxBackend> fmt::Debug for PreparedBackendRequest<'a, B> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PreparedBackendRequest")
            .field("request", &self.request)
            .field("path_context", &self.path_context)
            .field("working_directory", &self.working_directory)
            .field("capabilities", &self.capabilities)
            .finish()
    }
}

impl<'a, B: SandboxBackend> PreparedBackendRequest<'a, B> {
    fn ensure_backend(&self, backend: &B) -> Result<(), BackendContractError> {
        if self.backend_identity != *backend.identity() {
            Err(BackendContractError::BackendIdentityMismatch)
        } else if self.capabilities != backend.capabilities() {
            Err(BackendContractError::BackendCapabilitiesMismatch)
        } else {
            Ok(())
        }
    }

    /// Returns the validated executable and argv values.
    ///
    /// The working directory is intentionally exposed separately through
    /// [`Self::working_directory`]. A backend must not recover or inherit the
    /// original optional cwd from a raw [`CommandRequest`] after preflight.
    pub fn command_spec(&self, backend: &B) -> Result<&'a CommandSpec, BackendContractError> {
        self.ensure_backend(backend)?;
        Ok(self.request.command().command())
    }

    /// Returns the validated effective sandbox.
    pub fn sandbox(&self, backend: &B) -> Result<&'a EffectiveSandbox, BackendContractError> {
        self.ensure_backend(backend)?;
        Ok(self.request.sandbox())
    }

    /// Returns all filesystem constraint layers required for native lowering.
    ///
    /// The backend must enforce every layer in the returned view. This is
    /// distinct from the combined decision helpers: a native sandbox builder
    /// needs the concrete rules, protected paths, and glob settings, while
    /// the view prevents it from selecting only the requested or ceiling
    /// side.
    pub fn filesystem_lowering(
        &self,
        backend: &B,
    ) -> Result<EffectiveFilesystemLowering<'_>, BackendContractError> {
        self.ensure_backend(backend)?;
        Ok(self.request.sandbox().filesystem().lowering())
    }

    /// Returns all network constraint layers required for native lowering.
    ///
    /// These rules configure enforcement only. Actual connections must still
    /// use [`Self::authorize_connection`] with a resolved target and exact
    /// socket address.
    pub fn network_lowering(
        &self,
        backend: &B,
    ) -> Result<EffectiveNetworkLowering<'_>, BackendContractError> {
        self.ensure_backend(backend)?;
        Ok(self.request.sandbox().network().lowering())
    }

    /// Returns the runtime path context that was narrowed and checked during
    /// [`BackendRequest::prepare_for`].
    pub fn path_context(&self, backend: &B) -> Result<&EffectivePathContext, BackendContractError> {
        self.ensure_backend(backend)?;
        Ok(&self.path_context)
    }

    /// Returns the effective working directory resolved during preflight.
    ///
    /// This is always present. When the command did not specify an explicit
    /// directory, it is the runtime current directory supplied in the path
    /// context and checked against the effective filesystem policy.
    pub fn working_directory(&self, backend: &B) -> Result<&Path, BackendContractError> {
        self.ensure_backend(backend)?;
        Ok(&self.working_directory)
    }

    /// Returns the validated standard-stream routing.
    pub fn stdio(&self, backend: &B) -> Result<StdioSpec, BackendContractError> {
        self.ensure_backend(backend)?;
        Ok(self.request.command().stdio())
    }

    /// Returns the validated timeout intent.
    pub fn timeout_policy(&self, backend: &B) -> Result<TimeoutPolicy, BackendContractError> {
        self.ensure_backend(backend)?;
        Ok(self.request.command().timeout_policy())
    }

    /// Applies the effective environment to a backend-selected input base.
    ///
    /// A backend must construct [`EnvironmentInput::core`] only after it has
    /// selected the platform's conservative core environment.
    pub fn apply_environment(
        &self,
        backend: &B,
        input: EnvironmentInput,
    ) -> Result<BTreeMap<OsString, OsString>, BackendContractError> {
        self.ensure_backend(backend)?;
        self.request
            .sandbox()
            .environment()
            .apply_to(input)
            .map_err(|source| BackendContractError::EnvironmentPreparation { source })
    }

    /// Evaluates one absolute path against both effective filesystem policies.
    pub fn filesystem_access_for_path(
        &self,
        backend: &B,
        path: &Path,
    ) -> Result<FilesystemDecision, BackendContractError> {
        self.ensure_backend(backend)?;
        self.request
            .sandbox()
            .filesystem()
            .access_for_path(path, &self.path_context)
            .map_err(|source| BackendContractError::FilesystemEvaluation { source })
    }

    /// Evaluates one symbolic filesystem selector against both effective
    /// policies and the narrowed runtime context.
    ///
    /// The context must come from [`Self::path_context`]. A selector that has
    /// no effective runtime paths is denied, so a backend cannot accidentally
    /// replace a workspace-root ceiling with a broader context.
    pub fn filesystem_access_for(
        &self,
        backend: &B,
        selector: &PathSelector,
    ) -> Result<FilesystemDecision, BackendContractError> {
        self.ensure_backend(backend)?;
        self.request
            .sandbox()
            .filesystem()
            .access_for(selector, &self.path_context)
            .map_err(|source| BackendContractError::FilesystemEvaluation { source })
    }

    /// Evaluates a resolved hostname and all addresses captured for it.
    ///
    /// This is a policy query, not connection authorization. A backend must
    /// call [`Self::authorize_connection`] immediately before connecting.
    pub fn network_decision_for_domain_with_resolved_ips(
        &self,
        backend: &B,
        domain: &str,
        resolved_ips: &[IpAddr],
    ) -> Result<NetworkDecision, BackendContractError> {
        self.ensure_backend(backend)?;
        self.request
            .sandbox()
            .network()
            .decision_for_domain_with_resolved_ips(domain, resolved_ips)
            .map_err(|source| BackendContractError::NetworkEvaluation { source })
    }

    /// Authorizes the exact socket address the backend is about to connect to.
    pub fn authorize_connection(
        &self,
        backend: &B,
        target: &ResolvedNetworkTarget,
        connected: SocketAddr,
    ) -> Result<ConnectionAuthorization, BackendContractError> {
        self.ensure_backend(backend)?;
        self.request
            .sandbox()
            .network()
            .authorize_connection(target, connected)
            .map_err(|source| BackendContractError::NetworkEvaluation { source })
    }

    /// Evaluates one Unix socket path against both effective network policies.
    pub fn network_decision_for_unix_socket(
        &self,
        backend: &B,
        socket: &Path,
    ) -> Result<NetworkDecision, BackendContractError> {
        self.ensure_backend(backend)?;
        self.request
            .sandbox()
            .network()
            .decision_for_unix_socket(socket)
            .map_err(|source| BackendContractError::NetworkEvaluation { source })
    }
}