Skip to main content

treeship_core/capability/
mod.rs

1//! Pure capability-card verification primitives, shared by the CLI
2//! (`treeship verify-capability`) and the WASM verifier (browser receipt
3//! viewer) so both agree by construction. No I/O: callers supply the parsed
4//! card, the action statements, and the trust roots.
5//!
6//! See docs/specs/agent-capability-cards.md. The honest contract holds here
7//! too: this checks consistency over *captured* evidence (the actions the
8//! caller passes in), never completeness.
9
10use crate::statements::ActionStatement;
11use crate::trust::{TrustRootKind, TrustRootStore};
12
13/// `family.*` matches `family.write`; otherwise an exact match. A bare `*`
14/// matches anything.
15pub fn tool_matches(declared: &str, actual: &str) -> bool {
16    if let Some(prefix) = declared.strip_suffix('*') {
17        actual.starts_with(prefix)
18    } else {
19        declared == actual
20    }
21}
22
23/// A card is **key-bound** only when its `keyid` is the envelope signer AND
24/// that key is pinned under `AgentCert`. Anything else is self-asserted.
25pub fn is_key_bound(card_keyid: &str, signer_keyid: &str, trust: &TrustRootStore) -> bool {
26    !card_keyid.is_empty()
27        && signer_keyid == card_keyid
28        && trust
29            .roots()
30            .iter()
31            .any(|r| r.key_id == card_keyid && r.kind == TrustRootKind::AgentCert)
32}
33
34/// Is an action within a declared capability set? Checks the action label and
35/// the optional `meta.tool` against each declared capability (exact, or a
36/// `family.*` glob).
37pub fn action_in_scope(action: &ActionStatement, declared_tools: &[String]) -> bool {
38    let mut candidates: Vec<&str> = vec![action.action.as_str()];
39    if let Some(tool) = action
40        .meta
41        .as_ref()
42        .and_then(|m| m.get("tool"))
43        .and_then(|v| v.as_str())
44    {
45        candidates.push(tool);
46    }
47    candidates
48        .iter()
49        .any(|c| declared_tools.iter().any(|d| tool_matches(d, c)))
50}
51
52/// Extract the declared `capabilities.tools` from an agent_card.v1 payload.
53pub fn declared_tools(card_payload: &serde_json::Value) -> Vec<String> {
54    card_payload
55        .get("capabilities")
56        .and_then(|c| c.get("tools"))
57        .and_then(|t| t.as_array())
58        .map(|a| {
59            a.iter()
60                .filter_map(|t| t.as_str().map(str::to_string))
61                .collect()
62        })
63        .unwrap_or_default()
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69    use crate::trust::{TrustRoot, TrustRootKind, TrustRootStore};
70
71    #[test]
72    fn exact_and_glob_matching() {
73        assert!(tool_matches("file.write", "file.write"));
74        assert!(!tool_matches("file.write", "file.read"));
75        assert!(tool_matches("file.*", "file.write"));
76        assert!(!tool_matches("file.*", "db.query"));
77        assert!(tool_matches("*", "anything.at.all"));
78    }
79
80    fn root(key_id: &str, kind: TrustRootKind) -> TrustRoot {
81        TrustRoot {
82            key_id: key_id.into(),
83            public_key: "ed25519:AAAA".into(),
84            kind,
85            label: String::new(),
86            added_at: String::new(),
87        }
88    }
89
90    #[test]
91    fn key_bound_needs_signer_match_and_agentcert() {
92        let agentcert = TrustRootStore::with_roots(vec![root("key_x", TrustRootKind::AgentCert)]);
93        assert!(is_key_bound("key_x", "key_x", &agentcert));
94        assert!(!is_key_bound("key_x", "key_y", &agentcert));
95        assert!(!is_key_bound("", "", &agentcert));
96        let ship = TrustRootStore::with_roots(vec![root("key_x", TrustRootKind::Ship)]);
97        assert!(!is_key_bound("key_x", "key_x", &ship));
98        assert!(!is_key_bound("key_x", "key_x", &TrustRootStore::with_roots(vec![])));
99    }
100
101    #[test]
102    fn in_scope_checks_action_and_meta_tool() {
103        let mut a = ActionStatement::new("agent://x", "file.write");
104        assert!(action_in_scope(&a, &["file.*".to_string()]));
105        assert!(!action_in_scope(&a, &["db.query".to_string()]));
106        // meta.tool also counts
107        a.action = "tool.call".into();
108        a.meta = Some(serde_json::json!({ "tool": "db.query" }));
109        assert!(action_in_scope(&a, &["db.query".to_string()]));
110    }
111}