treeship_core/capability/
mod.rs1use crate::statements::ActionStatement;
11use crate::trust::{TrustRootKind, TrustRootStore};
12
13pub 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
23pub 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
34pub 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
52pub 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 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}