Skip to main content

capsule_runtime/
network_policy.rs

1use std::path::{Component as PathComponent, Path, PathBuf};
2
3use crate::{
4    containment::{PreopenReport, PreopenSurface},
5    errors::RuntimeError,
6};
7
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub(crate) struct NetworkAllowRule {
10    pub(crate) scheme: Option<String>,
11    pub(crate) host: String,
12    pub(crate) port: Option<u16>,
13}
14
15impl NetworkAllowRule {
16    pub(crate) fn parse(entry: &str) -> Result<Self, RuntimeError> {
17        let trimmed = entry.trim();
18        if trimmed.is_empty() {
19            return Err(RuntimeError::InvalidNetworkAllowEntry {
20                entry: entry.to_string(),
21                message: "entry must not be empty".to_string(),
22            });
23        }
24
25        if trimmed.contains("://") {
26            parse_url_allow_rule(trimmed)
27        } else {
28            parse_host_allow_rule(trimmed)
29        }
30    }
31
32    /// Whether `self` — a capsule-wide ceiling rule — already permits everything `narrower`
33    /// would permit. Used to clamp a per-artifact allow-list to the ceiling: a rule no
34    /// ceiling rule covers is dropped, never granted.
35    ///
36    /// An unset `scheme`/`port` on the ceiling side is a wildcard, so `example.com` covers
37    /// `https://example.com`. The reverse is not true: a bare `example.com` on the artifact
38    /// side spans both schemes and every port, so a ceiling of `https://example.com` does
39    /// not cover it and the entry is dropped. That asymmetry is deliberate — clamping errs
40    /// toward denial rather than silently keeping a broader rule than the ceiling states.
41    pub(crate) fn covers(&self, narrower: &NetworkAllowRule) -> bool {
42        if self.host != narrower.host {
43            return false;
44        }
45
46        if let Some(expected_scheme) = &self.scheme {
47            if narrower.scheme.as_ref() != Some(expected_scheme) {
48                return false;
49            }
50        }
51
52        match self.port {
53            Some(expected_port) => narrower.port == Some(expected_port),
54            None => true,
55        }
56    }
57
58    pub(crate) fn matches(&self, target: &RequestTarget) -> bool {
59        if self.host != target.host {
60            return false;
61        }
62
63        if let Some(expected_scheme) = &self.scheme {
64            if expected_scheme != &target.scheme {
65                return false;
66            }
67        }
68
69        match self.port {
70            Some(expected_port) => target.port == Some(expected_port),
71            None => true,
72        }
73    }
74}
75
76#[derive(Debug, Clone, PartialEq, Eq)]
77pub(crate) struct RequestTarget {
78    pub(crate) scheme: String,
79    pub(crate) host: String,
80    pub(crate) port: Option<u16>,
81}
82
83impl RequestTarget {
84    pub(crate) fn from_request(uri: &http::Uri, use_tls: bool) -> Option<Self> {
85        let authority = uri.authority()?;
86        let scheme = uri
87            .scheme_str()
88            .map(|s| s.to_ascii_lowercase())
89            .unwrap_or_else(|| if use_tls { "https" } else { "http" }.to_string());
90
91        let port = authority
92            .port_u16()
93            .or_else(|| default_port_for_scheme(&scheme));
94
95        Some(Self {
96            scheme,
97            host: authority.host().to_ascii_lowercase(),
98            port,
99        })
100    }
101}
102
103pub(crate) fn parse_network_allow_rules(
104    entries: &[String],
105) -> Result<Vec<NetworkAllowRule>, RuntimeError> {
106    entries
107        .iter()
108        .map(|entry| NetworkAllowRule::parse(entry))
109        .collect()
110}
111
112fn parse_url_allow_rule(entry: &str) -> Result<NetworkAllowRule, RuntimeError> {
113    let url = url::Url::parse(entry).map_err(|err| RuntimeError::InvalidNetworkAllowEntry {
114        entry: entry.to_string(),
115        message: format!("invalid URL: {err}"),
116    })?;
117
118    let scheme = url.scheme().to_ascii_lowercase();
119    if scheme != "http" && scheme != "https" {
120        return Err(RuntimeError::InvalidNetworkAllowEntry {
121            entry: entry.to_string(),
122            message: format!("unsupported URL scheme '{scheme}' (expected http or https)"),
123        });
124    }
125
126    if url.query().is_some() || url.fragment().is_some() || (url.path() != "" && url.path() != "/")
127    {
128        return Err(RuntimeError::InvalidNetworkAllowEntry {
129            entry: entry.to_string(),
130            message: "URL allow entries must not include path, query, or fragment".to_string(),
131        });
132    }
133
134    let host = url
135        .host_str()
136        .ok_or_else(|| RuntimeError::InvalidNetworkAllowEntry {
137            entry: entry.to_string(),
138            message: "URL allow entries must include a host".to_string(),
139        })?
140        .to_ascii_lowercase();
141
142    let port = url.port().or_else(|| default_port_for_scheme(&scheme));
143    Ok(NetworkAllowRule {
144        scheme: Some(scheme),
145        host,
146        port,
147    })
148}
149
150fn parse_host_allow_rule(entry: &str) -> Result<NetworkAllowRule, RuntimeError> {
151    let prefixed = format!("http://{entry}");
152    let parsed =
153        url::Url::parse(&prefixed).map_err(|err| RuntimeError::InvalidNetworkAllowEntry {
154            entry: entry.to_string(),
155            message: format!("invalid host allow entry: {err}"),
156        })?;
157
158    if parsed.path() != "/" || parsed.query().is_some() || parsed.fragment().is_some() {
159        return Err(RuntimeError::InvalidNetworkAllowEntry {
160            entry: entry.to_string(),
161            message: "host allow entries must be host or host:port only".to_string(),
162        });
163    }
164
165    let host = parsed
166        .host_str()
167        .ok_or_else(|| RuntimeError::InvalidNetworkAllowEntry {
168            entry: entry.to_string(),
169            message: "host allow entries must include a host".to_string(),
170        })?
171        .to_ascii_lowercase();
172
173    Ok(NetworkAllowRule {
174        scheme: None,
175        host,
176        port: parsed.port(),
177    })
178}
179
180fn default_port_for_scheme(scheme: &str) -> Option<u16> {
181    match scheme {
182        "http" => Some(80),
183        "https" => Some(443),
184        _ => None,
185    }
186}
187
188/// Which of the three shapes a workdir subtree cannot have a declared path broke.
189///
190/// Carried instead of a built error so one rule can serve declarations that report under
191/// different diagnostic codes; [`Self::message`] supplies the noun each of them names.
192#[derive(Debug, Clone, Copy, PartialEq, Eq)]
193pub(crate) enum WorkdirSubpathRejection {
194    Absolute,
195    Escapes,
196    Prefixed,
197}
198
199impl WorkdirSubpathRejection {
200    /// The operator-facing half of the message, with `noun` naming the declaration that was
201    /// rejected (`"scope"`, `"read-only path"`).
202    pub(crate) fn message(self, noun: &str) -> String {
203        match self {
204            Self::Absolute => format!("{noun} must be relative to the workdir"),
205            Self::Escapes => format!("{noun} cannot escape the workdir via '..'"),
206            Self::Prefixed => format!("{noun} must not contain absolute or prefixed components"),
207        }
208    }
209}
210
211/// Lower one manifest-declared workdir-relative path to its normalized form: `.` components
212/// dropped and `..` applied against what is already accumulated.
213///
214/// The single definition of what a workdir subtree may be, shared by
215/// `capabilities.filesystem.scope` and `capabilities.filesystem.read_only` so the two cannot drift
216/// into accepting different shapes. Purely lexical — no filesystem access — so it is safe on a
217/// path that does not exist yet.
218pub(crate) fn lower_workdir_subpath(raw: &str) -> Result<PathBuf, WorkdirSubpathRejection> {
219    let path = Path::new(raw);
220    if path.is_absolute() {
221        return Err(WorkdirSubpathRejection::Absolute);
222    }
223
224    let mut normalized = PathBuf::new();
225    for component in path.components() {
226        match component {
227            PathComponent::CurDir => {}
228            PathComponent::Normal(part) => normalized.push(part),
229            // A `..` that would pop past the workdir root escapes it, whatever follows.
230            PathComponent::ParentDir => {
231                if !normalized.pop() {
232                    return Err(WorkdirSubpathRejection::Escapes);
233                }
234            }
235            PathComponent::Prefix(_) | PathComponent::RootDir => {
236                return Err(WorkdirSubpathRejection::Prefixed);
237            }
238        }
239    }
240
241    Ok(normalized)
242}
243
244pub(crate) fn validate_filesystem_scope(scope: &str) -> Result<(), RuntimeError> {
245    lower_workdir_subpath(scope)
246        .map(|_| ())
247        .map_err(|rejection| RuntimeError::InvalidFilesystemScope {
248            scope: scope.to_string(),
249            message: rejection.message("scope"),
250        })
251}
252
253/// The entry's declared `capabilities.filesystem.scope`, validated as a relative, non-escaping
254/// path before it is returned.
255///
256/// The one place the declaration is read. Both grant derivations and [`preopen_reports`] go
257/// through it, so a scope that refuses a launch refuses the report that describes one.
258fn declared_filesystem_scope(
259    capabilities: &murmur_artifact::Capabilities,
260) -> Result<Option<String>, RuntimeError> {
261    let scope = capabilities
262        .filesystem
263        .as_ref()
264        .and_then(|filesystem| filesystem.scope.clone());
265    if let Some(scope) = scope.as_deref() {
266        validate_filesystem_scope(scope)?;
267    }
268    Ok(scope)
269}
270
271/// Whether the entry declares `capabilities.filesystem.records: true`.
272fn declares_records(capabilities: &murmur_artifact::Capabilities) -> bool {
273    capabilities
274        .filesystem
275        .as_ref()
276        .is_some_and(|filesystem| filesystem.records)
277}
278
279/// Resolve a validated filesystem `scope` to the directory a guest's preopen should target,
280/// creating it if it does not already exist.
281///
282/// Shared by `hooks.rs::build_wasi_ctx` and `runtime.rs::build_wasi_ctx` — both preopen
283/// `root.join(scope)` as `"."` once a grant declares a scope, and both must fail the same way
284/// (hard error naming the scope) rather than silently falling back to an unscoped preopen,
285/// which would widen the grant.
286pub(crate) fn resolve_scoped_dir(root: &Path, scope: &str) -> Result<PathBuf, RuntimeError> {
287    let scoped_dir = root.join(scope);
288    std::fs::create_dir_all(&scoped_dir).map_err(|err| {
289        RuntimeError::wasi(
290            scoped_dir.clone(),
291            format!("failed to create granted filesystem scope '{scope}': {err}"),
292        )
293    })?;
294    Ok(scoped_dir)
295}
296
297/// A single hook's capability grant, lowered from the **capsule operator's own** manifest
298/// entry for that hook (`murmur_artifact::RuntimeArtifact::capabilities`) at staging time.
299///
300/// [`Default`] is the default-deny state and is what a hook entry with no `capabilities:`
301/// block lowers to: an empty allow-rule list (every outbound request is denied by
302/// `NetworkPolicyHooks`, and no raw WASI socket capability is granted at all) and no
303/// filesystem scope (no preopened directory of any kind).
304///
305/// Deliberately narrower than [`crate::types::CapabilityPolicy`]: the capsule-wide policy
306/// carries shell/spawn/env/limits, none of which a per-hook grant governs.
307#[derive(Debug, Clone, Default, PartialEq, Eq)]
308pub(crate) struct HookCapabilityGrant {
309    /// Hosts this hook may reach through the gated wasi-http path. Empty = deny all.
310    pub(crate) network_allow_rules: Vec<NetworkAllowRule>,
311    /// Relative path under the hook's working directory to preopen. `None` = preopen
312    /// nothing, so the hook has no `wasi:filesystem` access whatsoever.
313    pub(crate) filesystem_scope: Option<String>,
314    /// `capabilities.filesystem.records: true`: the hook's `"."` is the session's records
315    /// directory's `hooks/`, read-write, in place of a [`Self::filesystem_scope`] (the manifest
316    /// refuses both on one entry).
317    pub(crate) filesystem_records: bool,
318    /// Host path of the created `<records dir>/hooks/`, filled by the staging path once it has
319    /// made it through the records directory's root. Left `None` by [`Self::derive`], on
320    /// [`Self::state_dir`]'s terms.
321    pub(crate) records_hooks_dir: Option<PathBuf>,
322    /// Whether this hook may read the in-scope task's input and result text through the
323    /// `murmur:task-io/read` host import. `false` = the import still links and every one of
324    /// its functions returns `not-granted`.
325    pub(crate) task_io_read: bool,
326    /// Whether this hook may read the capsule's durable conversation record through the
327    /// `murmur:conversation/read` host import. `false` = the import still links and
328    /// `read-messages` returns `not-granted`.
329    pub(crate) conversation_read: bool,
330    /// Resolved, validated `capabilities.state.store` name, with the capsule-name default already
331    /// applied. `None` = no `capabilities.state` block, so no durable store of any kind.
332    pub(crate) state_store: Option<String>,
333    /// Host path of the created store directory, filled by the staging path once
334    /// [`crate::state_store::ensure_state_store`] has made it. Left `None` by [`Self::derive`],
335    /// which stays pure — the same division [`ToolCapabilityGrant::dropped_network_entries`]
336    /// already uses, where lowering is unit-testable and only staging touches the filesystem.
337    pub(crate) state_dir: Option<PathBuf>,
338    /// This hook's operator-declared `config:` block, already lowered to compact JSON by
339    /// [`crate::artifact_config::lower_artifact_config`]. `Some` adds exactly one variable,
340    /// [`crate::artifact_config::ARTIFACT_CONFIG_ENV`], to the hook's environment; `None` — the
341    /// hook entry declaring no `config:` — adds nothing at all.
342    ///
343    /// Filled by the staging path rather than by [`Self::derive`], because config is declared
344    /// beside `capabilities:` and not inside it: the two are independent, and a hook may carry
345    /// either without the other. Carried on the grant because dispatch already looks a grant up by
346    /// artifact name, which is what scopes the value to the declaring hook alone.
347    pub(crate) config_json: Option<String>,
348}
349
350impl HookCapabilityGrant {
351    /// Lower an operator-declared `capabilities:` block into an enforceable grant,
352    /// validating both halves up front so a malformed grant fails staging rather than
353    /// surfacing as a confusing denial once the hook is already running.
354    ///
355    /// `None` (no block declared) yields [`HookCapabilityGrant::default`] — full
356    /// default-deny. Only `network`, `filesystem`, `state` and `task_io` are read; the other
357    /// sub-blocks a [`murmur_artifact::Capabilities`] can carry govern capsule-wide concerns
358    /// that a per-hook grant does not reach.
359    ///
360    /// `capsule_name` is what an undeclared `capabilities.state.store` defaults to. It is the
361    /// operator's own capsule name, on the same sourcing rule as the block itself.
362    pub(crate) fn derive(
363        capabilities: Option<&murmur_artifact::Capabilities>,
364        capsule_name: &str,
365    ) -> Result<Self, RuntimeError> {
366        let Some(capabilities) = capabilities else {
367            return Ok(Self::default());
368        };
369
370        let network_allow_rules = match capabilities.network.as_ref() {
371            Some(network) => parse_network_allow_rules(&network.allow)?,
372            None => Vec::new(),
373        };
374
375        let filesystem_scope = declared_filesystem_scope(capabilities)?;
376        let filesystem_records = declares_records(capabilities);
377
378        Ok(Self {
379            network_allow_rules,
380            filesystem_scope,
381            filesystem_records,
382            records_hooks_dir: None,
383            task_io_read: capabilities
384                .task_io
385                .as_ref()
386                .is_some_and(|task_io| task_io.read),
387            conversation_read: capabilities
388                .conversation
389                .as_ref()
390                .is_some_and(|conversation| conversation.read),
391            state_store: derive_state_store(capabilities, capsule_name)?,
392            state_dir: None,
393            config_json: None,
394        })
395    }
396}
397
398/// Resolve and validate the store name a `capabilities:` block asks for, applying the capsule-name
399/// default. `None` when the block declares no `state`, which is deny.
400///
401/// Shared by both grants because the rule is one rule: the name comes from the capsule operator's
402/// own manifest entry, the default is the operator's capsule name, and a name that is not a single
403/// usable path segment fails staging rather than surfacing later as a confusing denial.
404fn derive_state_store(
405    capabilities: &murmur_artifact::Capabilities,
406    capsule_name: &str,
407) -> Result<Option<String>, RuntimeError> {
408    let Some(state) = capabilities.state.as_ref() else {
409        return Ok(None);
410    };
411    let store = crate::state_store::resolve_store_name(state, capsule_name);
412    crate::state_store::validate_store_name(&store)?;
413    Ok(Some(store))
414}
415
416/// One tool's or driver's capability grant, lowered from the **capsule operator's own**
417/// manifest entry for that artifact (`murmur_artifact::RuntimeArtifact::capabilities`) at
418/// staging time and applied on the shared `invoke_tool_component` dispatch path.
419///
420/// The baseline is the opposite of [`HookCapabilityGrant`]'s, which is why they are two types: a
421/// hook derives its grant *from nothing* (absent block = deny), whereas a tool or driver derives
422/// it *from the capsule ceiling* (absent block = the capsule-wide policy, unchanged). Both fields
423/// are therefore `Option`-of-narrowing rather than plain values, and [`Default`] — what an entry
424/// with no `capabilities:` block yields — means "inherit everything".
425///
426/// # The wide filesystem default
427///
428/// [`Default`]'s `filesystem_scope: None` preopens the entire accessible workdir, read-write, for
429/// every tool and driver whose entry declares no `capabilities.filesystem.scope`. That default is
430/// chosen against **prompt injection steering an honest artifact**: the artifact's code does what
431/// its publisher wrote, and the hazard is a model that has read attacker-controlled text calling
432/// it with attacker-chosen arguments. Against that, the containment class, the network allow-list,
433/// the untrusted fence and `exports` are the mechanisms, and a per-artifact `scope` is the
434/// operator's tool for the entries where a narrower working surface is known.
435///
436/// It is **not** chosen against a hostile artifact. Malicious artifact code is outside murmur's
437/// documented threat model: no artifact trust model and no signing exist, so nothing here
438/// establishes that an artifact's code is what its publisher intended. Hash-pinning through
439/// `murmur.lock` establishes that an artifact has not changed since it was locked, and never that
440/// it was safe when it was locked.
441///
442/// The alternative — deny by default, narrow by declaration — is rejected because nothing in the
443/// system tells an operator what scope to write. A grant is read only from the operator's own
444/// manifest and never from the artifact's bundled one (the anti-self-escalation property every
445/// `derive` here rests on), so there is no declaration of need to migrate from. Under a narrow
446/// default the discoverable path to a working capsule is trial and error, and its terminal state
447/// is `scope: .` on every entry: this default restated, plus a migration that broke every existing
448/// capsule.
449#[derive(Debug, Clone, Default, PartialEq, Eq)]
450pub(crate) struct ToolCapabilityGrant {
451    /// Effective allow-rules for this artifact, already clamped to the ceiling. `None` =
452    /// no `capabilities.network` block, so the capsule-wide rules apply untouched.
453    /// `Some(vec![])` is a real narrowing to zero network access, and is what both an
454    /// explicit `allow: []` and an allow-list wholly outside the ceiling lower to.
455    pub(crate) network_allow_rules: Option<Vec<NetworkAllowRule>>,
456    /// Relative path under `accessible_workdir` to preopen as `"."` instead of the whole
457    /// workdir. `None` = no `capabilities.filesystem.scope`, so the artifact sees the entire
458    /// `accessible_workdir` — the wide default this type documents above.
459    pub(crate) filesystem_scope: Option<String>,
460    /// Declared `network.allow` entries dropped because no ceiling rule covers them. Held
461    /// (rather than warned about in `derive`) so lowering stays pure and unit-testable; the
462    /// staging path turns a non-empty list into a `W-SEC-007` warning.
463    pub(crate) dropped_network_entries: Vec<String>,
464    /// Resolved, validated `capabilities.state.store` name, with the capsule-name default already
465    /// applied. `None` = no `capabilities.state` block, so no durable store of any kind.
466    ///
467    /// The one axis on which a per-artifact block *widens* rather than narrows, and it can only
468    /// ever open one directory the capsule ceiling does not otherwise reach — never a workdir
469    /// path, and never another capsule's store.
470    pub(crate) state_store: Option<String>,
471    /// Host path of the created store directory, filled by the staging path once
472    /// [`crate::state_store::ensure_state_store`] has made it. Left `None` by [`Self::derive`],
473    /// which stays pure, on the same terms as `dropped_network_entries` above.
474    pub(crate) state_dir: Option<PathBuf>,
475    /// This artifact's operator-declared `config:` block, already lowered to compact JSON by
476    /// [`crate::artifact_config::lower_artifact_config`]. `Some` adds exactly one variable,
477    /// [`crate::artifact_config::ARTIFACT_CONFIG_ENV`], to this artifact's guest environment;
478    /// `None` adds nothing.
479    ///
480    /// The one field here that neither narrows nor widens: it grants no reach the ceiling did not
481    /// already allow, which is why an entry declaring `config:` and nothing else stages a grant
482    /// equal to [`Default`] in every other field. Filled by the staging path rather than by
483    /// [`Self::derive`], because `config:` is declared beside `capabilities:` and not inside it.
484    pub(crate) config_json: Option<String>,
485}
486
487impl ToolCapabilityGrant {
488    /// Lower an operator-declared `capabilities:` block into a grant that can only ever be
489    /// narrower than `ceiling_network_allow_rules` (the capsule-wide allow-list, already
490    /// parsed). Validating here rather than at dispatch means a malformed entry fails
491    /// staging instead of surfacing as a confusing denial mid-run.
492    ///
493    /// `None` (no block declared) yields [`ToolCapabilityGrant::default`] — inherit the
494    /// ceiling wholesale. Only `network`, `filesystem` and `state` are read; the other sub-blocks
495    /// a [`murmur_artifact::Capabilities`] can carry govern capsule-wide concerns that a
496    /// per-artifact grant does not reach (the caller warns `W-SEC-008` for those).
497    ///
498    /// `state` is the one member of that set that widens rather than narrows: it opens one
499    /// directory outside every workdir and touches nothing the ceiling governs. `capsule_name` is
500    /// what an undeclared `capabilities.state.store` defaults to.
501    pub(crate) fn derive(
502        capabilities: Option<&murmur_artifact::Capabilities>,
503        ceiling_network_allow_rules: &[NetworkAllowRule],
504        capsule_name: &str,
505    ) -> Result<Self, RuntimeError> {
506        let Some(capabilities) = capabilities else {
507            return Ok(Self::default());
508        };
509
510        let mut dropped_network_entries = Vec::new();
511        let network_allow_rules = match capabilities.network.as_ref() {
512            None => None,
513            Some(network) => {
514                let mut kept = Vec::new();
515                for entry in &network.allow {
516                    let rule = NetworkAllowRule::parse(entry)?;
517                    if ceiling_network_allow_rules
518                        .iter()
519                        .any(|ceiling_rule| ceiling_rule.covers(&rule))
520                    {
521                        kept.push(rule);
522                    } else {
523                        dropped_network_entries.push(entry.clone());
524                    }
525                }
526                Some(kept)
527            }
528        };
529
530        let filesystem_scope = declared_filesystem_scope(capabilities)?;
531
532        Ok(Self {
533            network_allow_rules,
534            filesystem_scope,
535            dropped_network_entries,
536            state_store: derive_state_store(capabilities, capsule_name)?,
537            state_dir: None,
538            config_json: None,
539        })
540    }
541}
542
543/// The filesystem surface each of the given artifacts will be preopened into, resolved but not
544/// opened, one entry per artifact that has a guest.
545///
546/// Lives here, beside the two grant types whose [`Default`]s it reports: [`ToolCapabilityGrant`]
547/// supplies the whole-workdir baseline a tool or driver falls back to, and
548/// [`HookCapabilityGrant`] the deny baseline a hook falls back to. Resolving the same question in
549/// a second place is how a report starts disagreeing with a launch.
550///
551/// Takes `(artifact name, role, that artifact's operator-declared capabilities)` triples rather
552/// than a concrete artifact type so the two callers that must agree can both satisfy it:
553/// `stage_session` holds [`crate::types::ArtifactRequest`]s, and `mur run --explain-scope` and
554/// `mur doctor` hold [`murmur_artifact::RuntimeArtifact`]s and have staged nothing.
555///
556/// [`murmur_artifact::ArtifactRuntime::Skill`] produces no entry: a skill is markdown staged into
557/// the workdir, no component is instantiated for it, and it holds no descriptor to report.
558///
559/// Every declared scope is read through [`declared_filesystem_scope`], so a scope a launch would
560/// refuse is refused here too. Nothing is created — [`resolve_scoped_dir`] is the staging path's
561/// job — so this stays usable from a read-only diagnostic.
562pub fn preopen_reports<'a, I>(artifacts: I) -> Result<Vec<PreopenReport>, RuntimeError>
563where
564    I: IntoIterator<
565        Item = (
566            &'a str,
567            &'a murmur_artifact::ArtifactRuntime,
568            Option<&'a murmur_artifact::Capabilities>,
569        ),
570    >,
571{
572    let mut reports = Vec::new();
573
574    for (name, runtime, capabilities) in artifacts {
575        if matches!(runtime, murmur_artifact::ArtifactRuntime::Skill) {
576            continue;
577        }
578
579        let scope = capabilities
580            .map(declared_filesystem_scope)
581            .transpose()?
582            .flatten();
583
584        let records = matches!(runtime, murmur_artifact::ArtifactRuntime::Hook)
585            && capabilities.is_some_and(declares_records);
586        let surface = match (runtime, scope.is_some()) {
587            // A declared scope resolves the same way for every role: `build_wasi_ctx` in both
588            // `runtime.rs` and `hooks.rs` preopens `<workdir>/<scope>` as `"."`.
589            (_, true) => PreopenSurface::ScopedSubtree,
590            (murmur_artifact::ArtifactRuntime::Hook, false) if records => {
591                PreopenSurface::RecordsHooks
592            }
593            (murmur_artifact::ArtifactRuntime::Hook, false) => PreopenSurface::Nothing,
594            (_, false) => PreopenSurface::WholeWorkdir,
595        };
596
597        // Unresolved until the caller knows whether the installed artifact is WASM or native:
598        // see `containment::resolve_runtime_dir_reaches`.
599        let runtime_dir_reach =
600            crate::runtime_dir::gets_runtime_dir_preopen(runtime, scope.is_some())
601                .then_some(crate::containment::RuntimeDirReach::Unresolved);
602        reports.push(PreopenReport {
603            artifact: name.to_string(),
604            role: runtime.as_str().to_string(),
605            scope,
606            surface,
607            runtime_dir_reach,
608        });
609    }
610
611    Ok(reports)
612}
613
614/// The rules `NetworkPolicyHooks` enforces for one tool/driver dispatch: the artifact's own
615/// clamped set when it declared a `capabilities.network` block, otherwise the capsule ceiling
616/// borrowed unchanged.
617///
618/// Takes the grant as an `Option` because that is how the dispatch path holds it — a name
619/// missing from the session's grant map (no per-artifact block, or an artifact pulled in at
620/// runtime) must land on the ceiling, not on an empty allow-list.
621pub(crate) fn effective_tool_network_rules<'a>(
622    grant: Option<&'a ToolCapabilityGrant>,
623    ceiling_network_allow_rules: &'a [NetworkAllowRule],
624) -> &'a [NetworkAllowRule] {
625    grant
626        .and_then(|grant| grant.network_allow_rules.as_deref())
627        .unwrap_or(ceiling_network_allow_rules)
628}
629
630#[cfg(test)]
631mod tests {
632    use super::*;
633    use murmur_artifact::{Capabilities, FilesystemCapabilities, NetworkCapabilities};
634
635    /// A `Capabilities` block carrying only the two sub-blocks a per-artifact grant reads.
636    fn capabilities_block(
637        network: Option<Vec<&str>>,
638        filesystem_scope: Option<&str>,
639    ) -> Capabilities {
640        Capabilities {
641            peer_fetch: None,
642            network: network.map(|allow| NetworkCapabilities {
643                allow: allow.into_iter().map(str::to_string).collect(),
644                unix_sockets: false,
645            }),
646            filesystem: filesystem_scope.map(|scope| FilesystemCapabilities {
647                scope: Some(scope.to_string()),
648                workdir_exec: false,
649                read_only: Vec::new(),
650                records: false,
651            }),
652            shell: None,
653            spawn: None,
654            env: None,
655            limits: None,
656            resources: None,
657            state: None,
658            task_io: None,
659            conversation: None,
660            plan: None,
661            install: None,
662            containment: None,
663        }
664    }
665
666    /// A hook entry with no `capabilities:` block gets nothing: no allow rules (so
667    /// `NetworkPolicyHooks` denies every request) and no scope (so nothing is preopened).
668    #[test]
669    fn hook_grant_defaults_to_deny_network_and_filesystem() {
670        let grant = HookCapabilityGrant::derive(None, "test-capsule").unwrap();
671
672        assert_eq!(grant, HookCapabilityGrant::default());
673        assert!(grant.network_allow_rules.is_empty());
674        assert!(grant.filesystem_scope.is_none());
675
676        let target = RequestTarget {
677            scheme: "https".to_string(),
678            host: "telemetry.example.com".to_string(),
679            port: Some(443),
680        };
681        assert!(!grant
682            .network_allow_rules
683            .iter()
684            .any(|rule| rule.matches(&target)));
685    }
686
687    /// A declared-but-empty `capabilities:` block is still full default-deny — declaring
688    /// the key must not, by itself, widen anything.
689    #[test]
690    fn hook_grant_empty_capabilities_block_grants_nothing() {
691        let grant =
692            HookCapabilityGrant::derive(Some(&capabilities_block(None, None)), "test-capsule")
693                .unwrap();
694
695        assert_eq!(grant, HookCapabilityGrant::default());
696    }
697
698    #[test]
699    fn hook_grant_network_allows_exactly_the_declared_host() {
700        let caps = capabilities_block(Some(vec!["https://telemetry.example.com"]), None);
701        let grant = HookCapabilityGrant::derive(Some(&caps), "test-capsule").unwrap();
702
703        let allowed = RequestTarget {
704            scheme: "https".to_string(),
705            host: "telemetry.example.com".to_string(),
706            port: Some(443),
707        };
708        let other = RequestTarget {
709            scheme: "https".to_string(),
710            host: "evil.example.com".to_string(),
711            port: Some(443),
712        };
713
714        assert!(grant
715            .network_allow_rules
716            .iter()
717            .any(|rule| rule.matches(&allowed)));
718        assert!(!grant
719            .network_allow_rules
720            .iter()
721            .any(|rule| rule.matches(&other)));
722        // A network grant alone never widens the filesystem.
723        assert!(grant.filesystem_scope.is_none());
724    }
725
726    #[test]
727    fn hook_grant_filesystem_scope_is_carried_through() {
728        let caps = capabilities_block(None, Some("hook-state"));
729        let grant = HookCapabilityGrant::derive(Some(&caps), "test-capsule").unwrap();
730
731        assert_eq!(grant.filesystem_scope.as_deref(), Some("hook-state"));
732        // A filesystem grant alone never widens the network.
733        assert!(grant.network_allow_rules.is_empty());
734    }
735
736    /// `capabilities.task_io.read` lowers into the grant on its own axis: `true` grants it,
737    /// `false` and an absent block do not, and neither ever widens network or filesystem.
738    #[test]
739    fn hook_grant_task_io_read_is_carried_through_independently() {
740        for (declared, expected) in [(None, false), (Some(false), false), (Some(true), true)] {
741            let caps = murmur_artifact::Capabilities {
742                task_io: declared.map(|read| murmur_artifact::TaskIoCapabilities { read }),
743                conversation: None,
744                plan: None,
745                ..capabilities_block(None, None)
746            };
747            let grant = HookCapabilityGrant::derive(Some(&caps), "test-capsule").unwrap();
748            assert_eq!(grant.task_io_read, expected, "declared: {declared:?}");
749            assert!(grant.network_allow_rules.is_empty());
750            assert!(grant.filesystem_scope.is_none());
751        }
752    }
753
754    /// `capabilities.conversation.read` lowers on its own axis, exactly as `task_io.read` does,
755    /// and neither ever widens network or filesystem.
756    #[test]
757    fn hook_grant_conversation_read_is_carried_through_independently() {
758        for (declared, expected) in [(None, false), (Some(false), false), (Some(true), true)] {
759            let caps = murmur_artifact::Capabilities {
760                conversation: declared
761                    .map(|read| murmur_artifact::ConversationCapabilities { read }),
762                ..capabilities_block(None, None)
763            };
764            let grant = HookCapabilityGrant::derive(Some(&caps), "test-capsule").unwrap();
765            assert_eq!(grant.conversation_read, expected, "declared: {declared:?}");
766            assert!(!grant.task_io_read, "one grant never implies the other");
767            assert!(grant.network_allow_rules.is_empty());
768            assert!(grant.filesystem_scope.is_none());
769        }
770    }
771
772    /// The store name defaults to the capsule's, and an explicit `store:` wins. Applied where the
773    /// grant is lowered, so a report and a preopen can never name different directories.
774    #[test]
775    fn state_store_defaults_to_the_capsule_name_and_an_explicit_name_wins() {
776        for (declared, expected) in [(None, "state-capsule"), (Some("shey"), "shey")] {
777            let caps = Capabilities {
778                state: Some(murmur_artifact::StateCapabilities {
779                    store: declared.map(str::to_string),
780                }),
781                ..capabilities_block(None, None)
782            };
783
784            let hook = HookCapabilityGrant::derive(Some(&caps), "state-capsule").unwrap();
785            let tool =
786                ToolCapabilityGrant::derive(Some(&caps), &ceiling(), "state-capsule").unwrap();
787
788            assert_eq!(hook.state_store.as_deref(), Some(expected));
789            assert_eq!(tool.state_store.as_deref(), Some(expected));
790            // `derive` stays pure: the directory is made by the staging path, not here.
791            assert!(hook.state_dir.is_none());
792            assert!(tool.state_dir.is_none());
793            // A state grant alone widens nothing else.
794            assert!(hook.filesystem_scope.is_none());
795            assert!(hook.network_allow_rules.is_empty());
796            assert!(tool.filesystem_scope.is_none());
797            assert!(tool.network_allow_rules.is_none());
798        }
799    }
800
801    /// Absent `capabilities.state` is deny for both roles, including on a block that declares
802    /// other things — declaring the key is what grants a store, never declaring the block.
803    #[test]
804    fn an_absent_state_block_grants_no_store_to_either_role() {
805        let caps = capabilities_block(Some(vec!["https://api.example.com"]), Some("cache"));
806
807        assert!(HookCapabilityGrant::derive(Some(&caps), "state-capsule")
808            .unwrap()
809            .state_store
810            .is_none());
811        assert!(
812            ToolCapabilityGrant::derive(Some(&caps), &ceiling(), "state-capsule")
813                .unwrap()
814                .state_store
815                .is_none()
816        );
817    }
818
819    /// A malformed store name fails lowering, so it fails staging — never a confusing denial once
820    /// a guest is already running. Refused identically for a hook and for a tool.
821    #[test]
822    fn a_malformed_store_name_fails_lowering_for_both_roles() {
823        for store in ["../escape", "/abs/path", "a/b", "", ".hidden"] {
824            let caps = Capabilities {
825                state: Some(murmur_artifact::StateCapabilities {
826                    store: Some(store.to_string()),
827                }),
828                ..capabilities_block(None, None)
829            };
830
831            for err in [
832                HookCapabilityGrant::derive(Some(&caps), "state-capsule").unwrap_err(),
833                ToolCapabilityGrant::derive(Some(&caps), &ceiling(), "state-capsule").unwrap_err(),
834            ] {
835                assert!(
836                    matches!(err, RuntimeError::InvalidStateStore { .. }),
837                    "store '{store}' must refuse as InvalidStateStore, got: {err}"
838                );
839            }
840        }
841    }
842
843    /// An empty capsule name is not a usable store name either, so a capsule that somehow has one
844    /// is refused rather than silently given `~/.murmur/state/`.
845    #[test]
846    fn an_empty_capsule_name_cannot_become_a_store_name() {
847        let caps = Capabilities {
848            state: Some(murmur_artifact::StateCapabilities { store: None }),
849            ..capabilities_block(None, None)
850        };
851
852        assert!(matches!(
853            ToolCapabilityGrant::derive(Some(&caps), &ceiling(), ""),
854            Err(RuntimeError::InvalidStateStore { .. })
855        ));
856    }
857
858    #[test]
859    fn hook_grant_rejects_escaping_filesystem_scope() {
860        for scope in ["../escape", "/etc"] {
861            let caps = capabilities_block(None, Some(scope));
862            let err = HookCapabilityGrant::derive(Some(&caps), "test-capsule").unwrap_err();
863            assert!(
864                matches!(err, RuntimeError::InvalidFilesystemScope { .. }),
865                "scope {scope} should fail staging, got: {err}"
866            );
867        }
868    }
869
870    #[test]
871    fn hook_grant_rejects_malformed_network_entry() {
872        let caps = capabilities_block(Some(vec!["ftp://files.example.com"]), None);
873        let err = HookCapabilityGrant::derive(Some(&caps), "test-capsule").unwrap_err();
874
875        assert!(
876            matches!(err, RuntimeError::InvalidNetworkAllowEntry { .. }),
877            "got: {err}"
878        );
879    }
880
881    /// The capsule ceiling used by the tool/driver narrowing tests: two hosts, one of which
882    /// a narrowed artifact is expected to lose access to.
883    fn ceiling() -> Vec<NetworkAllowRule> {
884        parse_network_allow_rules(&[
885            "https://api.example.com".to_string(),
886            "https://other.example.com".to_string(),
887        ])
888        .unwrap()
889    }
890
891    fn target(host: &str) -> RequestTarget {
892        RequestTarget {
893            scheme: "https".to_string(),
894            host: host.to_string(),
895            port: Some(443),
896        }
897    }
898
899    fn reaches(rules: &[NetworkAllowRule], host: &str) -> bool {
900        rules.iter().any(|rule| rule.matches(&target(host)))
901    }
902
903    /// The no-op invariant: a tool/driver entry with no `capabilities:` block keeps the whole
904    /// ceiling and the whole workdir, unchanged.
905    #[test]
906    fn tool_grant_without_entry_inherits_the_ceiling_unchanged() {
907        let ceiling = ceiling();
908        let grant = ToolCapabilityGrant::derive(None, &ceiling, "test-capsule").unwrap();
909
910        assert_eq!(grant, ToolCapabilityGrant::default());
911        assert!(grant.network_allow_rules.is_none());
912        assert!(grant.filesystem_scope.is_none());
913        assert!(grant.dropped_network_entries.is_empty());
914
915        let effective = effective_tool_network_rules(Some(&grant), &ceiling);
916        assert_eq!(effective, ceiling.as_slice());
917        assert!(reaches(effective, "api.example.com"));
918        assert!(reaches(effective, "other.example.com"));
919    }
920
921    /// A declared subset keeps exactly the declared host and loses the rest of the ceiling.
922    #[test]
923    fn tool_grant_subset_of_ceiling_is_kept_and_narrows() {
924        let ceiling = ceiling();
925        let caps = capabilities_block(Some(vec!["https://api.example.com"]), None);
926        let grant = ToolCapabilityGrant::derive(Some(&caps), &ceiling, "test-capsule").unwrap();
927
928        let effective = effective_tool_network_rules(Some(&grant), &ceiling);
929        assert!(reaches(effective, "api.example.com"));
930        assert!(!reaches(effective, "other.example.com"));
931        assert!(grant.dropped_network_entries.is_empty());
932    }
933
934    /// Narrowing can only subtract: a host the ceiling never allowed is dropped (and
935    /// reported for `W-SEC-007`), not granted.
936    #[test]
937    fn tool_grant_entry_outside_the_ceiling_is_dropped_not_granted() {
938        let ceiling = ceiling();
939        let caps = capabilities_block(
940            Some(vec!["https://api.example.com", "https://evil.example.com"]),
941            None,
942        );
943        let grant = ToolCapabilityGrant::derive(Some(&caps), &ceiling, "test-capsule").unwrap();
944
945        let effective = effective_tool_network_rules(Some(&grant), &ceiling);
946        assert!(reaches(effective, "api.example.com"));
947        assert!(!reaches(effective, "evil.example.com"));
948        assert_eq!(
949            grant.dropped_network_entries,
950            vec!["https://evil.example.com".to_string()]
951        );
952    }
953
954    /// A bare host is broader than a scheme-bound ceiling rule (it spans http too), so it is
955    /// dropped rather than silently widening the artifact past the ceiling.
956    #[test]
957    fn tool_grant_entry_broader_than_the_ceiling_rule_is_dropped() {
958        let ceiling = ceiling();
959        let caps = capabilities_block(Some(vec!["api.example.com"]), None);
960        let grant = ToolCapabilityGrant::derive(Some(&caps), &ceiling, "test-capsule").unwrap();
961
962        assert_eq!(grant.network_allow_rules.as_deref(), Some(&[][..]));
963        assert_eq!(
964            grant.dropped_network_entries,
965            vec!["api.example.com".to_string()]
966        );
967    }
968
969    /// An explicit empty allow-list is a real narrowing to zero network access — distinct
970    /// from the key being absent, which inherits the ceiling.
971    #[test]
972    fn tool_grant_empty_network_allow_denies_all_for_that_artifact() {
973        let ceiling = ceiling();
974        let caps = capabilities_block(Some(vec![]), None);
975        let grant = ToolCapabilityGrant::derive(Some(&caps), &ceiling, "test-capsule").unwrap();
976
977        let effective = effective_tool_network_rules(Some(&grant), &ceiling);
978        assert!(effective.is_empty());
979        assert!(!reaches(effective, "api.example.com"));
980        assert!(!reaches(effective, "other.example.com"));
981    }
982
983    /// A filesystem-only block leaves the network on the ceiling: narrowing one axis must
984    /// not implicitly clamp the other.
985    #[test]
986    fn tool_grant_filesystem_scope_is_carried_and_leaves_network_inherited() {
987        let ceiling = ceiling();
988        let caps = capabilities_block(None, Some("cache"));
989        let grant = ToolCapabilityGrant::derive(Some(&caps), &ceiling, "test-capsule").unwrap();
990
991        assert_eq!(grant.filesystem_scope.as_deref(), Some("cache"));
992        assert!(grant.network_allow_rules.is_none());
993        assert_eq!(
994            effective_tool_network_rules(Some(&grant), &ceiling),
995            ceiling.as_slice()
996        );
997    }
998
999    #[test]
1000    fn tool_grant_rejects_escaping_filesystem_scope() {
1001        for scope in ["../escape", "/etc"] {
1002            let caps = capabilities_block(None, Some(scope));
1003            let err =
1004                ToolCapabilityGrant::derive(Some(&caps), &ceiling(), "test-capsule").unwrap_err();
1005            assert!(
1006                matches!(err, RuntimeError::InvalidFilesystemScope { .. }),
1007                "scope {scope} should fail staging, got: {err}"
1008            );
1009        }
1010    }
1011
1012    #[test]
1013    fn tool_grant_rejects_malformed_network_entry() {
1014        let caps = capabilities_block(Some(vec!["ftp://files.example.com"]), None);
1015        let err = ToolCapabilityGrant::derive(Some(&caps), &ceiling(), "test-capsule").unwrap_err();
1016
1017        assert!(
1018            matches!(err, RuntimeError::InvalidNetworkAllowEntry { .. }),
1019            "got: {err}"
1020        );
1021    }
1022
1023    /// An empty ceiling already denies everything, so every declared entry drops — a
1024    /// per-artifact block can never be the thing that turns network access on.
1025    #[test]
1026    fn tool_grant_cannot_widen_an_empty_ceiling() {
1027        let caps = capabilities_block(Some(vec!["https://api.example.com"]), None);
1028        let grant = ToolCapabilityGrant::derive(Some(&caps), &[], "test-capsule").unwrap();
1029
1030        assert_eq!(grant.network_allow_rules.as_deref(), Some(&[][..]));
1031        assert_eq!(
1032            grant.dropped_network_entries,
1033            vec!["https://api.example.com".to_string()]
1034        );
1035    }
1036
1037    /// An unset scheme/port on the ceiling side is a wildcard the narrower rule fits under.
1038    #[test]
1039    fn ceiling_rule_with_wildcard_scheme_covers_a_scheme_bound_rule() {
1040        let ceiling = parse_network_allow_rules(&["api.example.com".to_string()]).unwrap();
1041        let caps = capabilities_block(Some(vec!["https://api.example.com"]), None);
1042        let grant = ToolCapabilityGrant::derive(Some(&caps), &ceiling, "test-capsule").unwrap();
1043
1044        assert!(grant.dropped_network_entries.is_empty());
1045        assert!(reaches(
1046            effective_tool_network_rules(Some(&grant), &ceiling),
1047            "api.example.com"
1048        ));
1049    }
1050
1051    #[test]
1052    fn filesystem_scope_validation_accepts_safe_relative_paths() {
1053        for scope in [".", "./workdir", "a/../b", "sandbox/subdir"] {
1054            assert!(
1055                validate_filesystem_scope(scope).is_ok(),
1056                "scope should be valid: {scope}"
1057            );
1058        }
1059    }
1060
1061    #[test]
1062    fn filesystem_scope_validation_rejects_escape_paths() {
1063        for scope in ["../x", "../../x", "/tmp"] {
1064            assert!(
1065                validate_filesystem_scope(scope).is_err(),
1066                "scope should be invalid: {scope}"
1067            );
1068        }
1069    }
1070
1071    #[test]
1072    fn network_allowlist_empty_denies_all_requests() {
1073        let rules = parse_network_allow_rules(&Vec::<String>::new()).unwrap();
1074
1075        let target = RequestTarget {
1076            scheme: "https".to_string(),
1077            host: "blocked.example.com".to_string(),
1078            port: Some(443),
1079        };
1080
1081        assert!(!rules.iter().any(|rule| rule.matches(&target)));
1082    }
1083
1084    #[test]
1085    fn network_allowlist_matches_allowed_host_with_default_port() {
1086        let rules =
1087            parse_network_allow_rules(&["https://allowed.example.com".to_string()]).unwrap();
1088        let target = RequestTarget {
1089            scheme: "https".to_string(),
1090            host: "allowed.example.com".to_string(),
1091            port: Some(443),
1092        };
1093
1094        assert!(rules.iter().any(|rule| rule.matches(&target)));
1095    }
1096
1097    #[test]
1098    fn network_allowlist_blocks_non_matching_host() {
1099        let rules =
1100            parse_network_allow_rules(&["https://allowed.example.com".to_string()]).unwrap();
1101        let target = RequestTarget {
1102            scheme: "https".to_string(),
1103            host: "blocked.example.com".to_string(),
1104            port: Some(443),
1105        };
1106
1107        assert!(!rules.iter().any(|rule| rule.matches(&target)));
1108    }
1109
1110    #[test]
1111    fn network_allowlist_normalizes_default_ports() {
1112        let rules = parse_network_allow_rules(&[
1113            "https://allowed.example.com".to_string(),
1114            "allowed-two.example.com:8080".to_string(),
1115        ])
1116        .unwrap();
1117
1118        let https_default = RequestTarget {
1119            scheme: "https".to_string(),
1120            host: "allowed.example.com".to_string(),
1121            port: Some(443),
1122        };
1123        let custom_port = RequestTarget {
1124            scheme: "http".to_string(),
1125            host: "allowed-two.example.com".to_string(),
1126            port: Some(8080),
1127        };
1128
1129        assert!(rules.iter().any(|rule| rule.matches(&https_default)));
1130        assert!(rules.iter().any(|rule| rule.matches(&custom_port)));
1131    }
1132
1133    /// The two opposite baselines, read off one manifest: a `runtime: tool` and a
1134    /// `runtime: driver` entry that declare nothing keep the whole accessible workdir, and a
1135    /// `runtime: hook` entry that declares nothing holds no descriptor at all.
1136    #[test]
1137    fn undeclared_entries_report_the_baseline_their_role_starts_from() {
1138        let tool = murmur_artifact::ArtifactRuntime::Tool;
1139        let driver = murmur_artifact::ArtifactRuntime::Driver;
1140        let hook = murmur_artifact::ArtifactRuntime::Hook;
1141
1142        let reports = preopen_reports(vec![
1143            ("notes-tool", &tool, None),
1144            ("anthropic-driver", &driver, None),
1145            ("telemetry-hook", &hook, None),
1146        ])
1147        .unwrap();
1148
1149        assert_eq!(
1150            reports,
1151            vec![
1152                PreopenReport {
1153                    artifact: "notes-tool".to_string(),
1154                    role: "tool".to_string(),
1155                    scope: None,
1156                    surface: PreopenSurface::WholeWorkdir,
1157                    runtime_dir_reach: Some(crate::containment::RuntimeDirReach::Unresolved),
1158                },
1159                PreopenReport {
1160                    artifact: "anthropic-driver".to_string(),
1161                    role: "driver".to_string(),
1162                    scope: None,
1163                    surface: PreopenSurface::WholeWorkdir,
1164                    runtime_dir_reach: None,
1165                },
1166                PreopenReport {
1167                    artifact: "telemetry-hook".to_string(),
1168                    role: "hook".to_string(),
1169                    scope: None,
1170                    surface: PreopenSurface::Nothing,
1171                    runtime_dir_reach: None,
1172                },
1173            ]
1174        );
1175    }
1176
1177    /// A declared scope resolves the same way whatever the role, and is carried verbatim so an
1178    /// operator reads back the line they wrote.
1179    #[test]
1180    fn a_declared_scope_narrows_every_role_to_a_subtree() {
1181        let tool = murmur_artifact::ArtifactRuntime::Tool;
1182        let hook = murmur_artifact::ArtifactRuntime::Hook;
1183        let tool_caps = capabilities_block(None, Some("cache"));
1184        let hook_caps = capabilities_block(None, Some("hook-state"));
1185
1186        let reports = preopen_reports(vec![
1187            ("notes-tool", &tool, Some(&tool_caps)),
1188            ("telemetry-hook", &hook, Some(&hook_caps)),
1189        ])
1190        .unwrap();
1191
1192        assert_eq!(
1193            reports,
1194            vec![
1195                PreopenReport {
1196                    artifact: "notes-tool".to_string(),
1197                    role: "tool".to_string(),
1198                    scope: Some("cache".to_string()),
1199                    surface: PreopenSurface::ScopedSubtree,
1200                    runtime_dir_reach: None,
1201                },
1202                PreopenReport {
1203                    artifact: "telemetry-hook".to_string(),
1204                    role: "hook".to_string(),
1205                    scope: Some("hook-state".to_string()),
1206                    surface: PreopenSurface::ScopedSubtree,
1207                    runtime_dir_reach: None,
1208                },
1209            ]
1210        );
1211    }
1212
1213    /// A skill is markdown staged into the workdir with no component behind it, so it produces no
1214    /// entry at all — not a `Nothing` entry, which would claim a guest was denied a descriptor.
1215    #[test]
1216    fn a_skill_produces_no_entry() {
1217        let skill = murmur_artifact::ArtifactRuntime::Skill;
1218        let tool = murmur_artifact::ArtifactRuntime::Tool;
1219
1220        let reports = preopen_reports(vec![
1221            ("notes-skill", &skill, None),
1222            ("notes-tool", &tool, None),
1223        ])
1224        .unwrap();
1225
1226        assert_eq!(reports.len(), 1);
1227        assert_eq!(reports[0].artifact, "notes-tool");
1228    }
1229
1230    /// The report runs the declared scope through the same validator the grant derivation does,
1231    /// so a scope that would refuse a launch refuses the report.
1232    #[test]
1233    fn an_escaping_scope_is_refused() {
1234        let tool = murmur_artifact::ArtifactRuntime::Tool;
1235        let caps = capabilities_block(None, Some("../escape"));
1236
1237        let err = preopen_reports(vec![("notes-tool", &tool, Some(&caps))]).unwrap_err();
1238
1239        assert!(
1240            matches!(
1241                err,
1242                RuntimeError::InvalidFilesystemScope { ref scope, .. } if scope == "../escape"
1243            ),
1244            "unexpected error: {err}"
1245        );
1246        assert!(
1247            ToolCapabilityGrant::derive(Some(&caps), &[], "test-capsule").is_err(),
1248            "the grant derivation must refuse what the report refuses"
1249        );
1250    }
1251
1252    /// The three surfaces carry stable kebab-case wire names, and the enum has exactly three
1253    /// variants: a guest holds the workdir, one subtree of it, or no descriptor.
1254    #[test]
1255    fn preopen_surfaces_serialize_to_stable_wire_names() {
1256        for (surface, wire) in [
1257            (PreopenSurface::WholeWorkdir, "whole-workdir"),
1258            (PreopenSurface::ScopedSubtree, "scoped-subtree"),
1259            (PreopenSurface::Nothing, "nothing"),
1260        ] {
1261            assert_eq!(
1262                serde_json::to_value(surface).unwrap(),
1263                serde_json::Value::String(wire.to_string())
1264            );
1265        }
1266    }
1267}