use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use super::manifest::ResolvedCapability;
pub const REGISTRY_FILE: &str = "registry.json";
pub const REGISTRY_SCHEMA_VERSION: u32 = 1;
#[derive(Debug)]
pub enum RegistryError {
Io(std::io::Error),
Json(String),
BadSchemaVersion(u32),
}
impl std::fmt::Display for RegistryError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Io(e) => write!(f, "registry io: {e}"),
Self::Json(e) => write!(f, "registry.json is corrupt: {e}"),
Self::BadSchemaVersion(v) => write!(f, "registry schemaVersion {v} is not supported"),
}
}
}
impl std::error::Error for RegistryError {}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "form", rename_all = "lowercase")]
pub enum ToolPin {
Wasm {
identity: String,
module: String,
wasm_sha256: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
capabilities: Vec<ResolvedCapability>,
},
Script {
identity: String,
run: Vec<String>,
},
Mcp {
label: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
command: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
env: BTreeMap<String, String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
url: Option<String>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
headers: BTreeMap<String, String>,
},
}
impl ToolPin {
pub fn identity(&self, plugin: &str) -> String {
match self {
Self::Wasm { identity, .. } | Self::Script { identity, .. } => identity.clone(),
Self::Mcp { label, .. } => format!("plugin.{plugin}.{label}"),
}
}
pub fn form(&self) -> &'static str {
match self {
Self::Wasm { .. } => "wasm",
Self::Script { .. } => "script",
Self::Mcp { .. } => "mcp",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PluginPin {
pub name: String,
pub version: String,
pub source: String,
pub manifest_sha256: String,
pub tools: Vec<ToolPin>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PackageStatus {
Trusted,
Tampered { reason: String },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ToolStatus {
Ready,
Broken { detail: String },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VerifyIssue {
ManifestHashMismatch,
ToolSetChanged { detail: String },
WasmHashMismatch { identity: String },
MissingModule { identity: String, path: String },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VerifyReport {
pub plugin: String,
pub package: PackageStatus,
pub tools: Vec<(String, ToolStatus)>,
pub issues: Vec<VerifyIssue>,
}
impl VerifyReport {
pub fn tampered(&self) -> bool {
self.package != PackageStatus::Trusted
}
pub fn user_message(&self) -> Option<String> {
match &self.package {
PackageStatus::Tampered { reason } => Some(format!(
"plugin `{}` has been modified ({reason}); run `plugin add` again to re-review",
self.plugin
)),
PackageStatus::Trusted => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PluginRegistry {
pub schema_version: u32,
pub plugins: BTreeMap<String, PluginPin>,
}
impl Default for PluginRegistry {
fn default() -> Self {
Self::new()
}
}
impl PluginRegistry {
pub fn new() -> Self {
Self {
schema_version: REGISTRY_SCHEMA_VERSION,
plugins: BTreeMap::new(),
}
}
pub fn load(path: &Path) -> Result<Self, RegistryError> {
match std::fs::read_to_string(path) {
Ok(raw) => {
let registry: Self =
serde_json::from_str(&raw).map_err(|e| RegistryError::Json(e.to_string()))?;
if registry.schema_version != REGISTRY_SCHEMA_VERSION {
return Err(RegistryError::BadSchemaVersion(registry.schema_version));
}
Ok(registry)
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Self::new()),
Err(e) => Err(RegistryError::Io(e)),
}
}
pub fn save(&self, path: &Path) -> Result<(), RegistryError> {
if let Some(dir) = path.parent()
&& !dir.as_os_str().is_empty()
{
std::fs::create_dir_all(dir).map_err(RegistryError::Io)?;
}
let raw =
serde_json::to_string_pretty(self).map_err(|e| RegistryError::Json(e.to_string()))?;
std::fs::write(path, raw).map_err(RegistryError::Io)
}
pub fn path_in(plugins_root: &Path) -> PathBuf {
plugins_root.join(REGISTRY_FILE)
}
pub fn get(&self, name: &str) -> Option<&PluginPin> {
self.plugins.get(name)
}
pub fn insert(&mut self, pin: PluginPin) {
self.plugins.insert(pin.name.clone(), pin);
}
pub fn remove(&mut self, name: &str) -> Option<PluginPin> {
self.plugins.remove(name)
}
pub fn len(&self) -> usize {
self.plugins.len()
}
pub fn is_empty(&self) -> bool {
self.plugins.is_empty()
}
pub fn verify(&self, plugin_dir: &Path) -> VerifyReport {
let Some(pin) = plugin_dir
.file_name()
.and_then(|n| n.to_str())
.and_then(|n| self.plugins.get(n))
else {
let name = plugin_dir
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
return VerifyReport {
plugin: name,
package: PackageStatus::Tampered {
reason: "not installed (no registry pin)".into(),
},
tools: Vec::new(),
issues: vec![VerifyIssue::ManifestHashMismatch],
};
};
self.verify_pin(pin, plugin_dir)
}
pub fn verify_plugin(&self, name: &str, plugin_dir: &Path) -> VerifyReport {
let Some(pin) = self.plugins.get(name) else {
return VerifyReport {
plugin: name.to_string(),
package: PackageStatus::Tampered {
reason: "not installed (no registry pin)".into(),
},
tools: Vec::new(),
issues: vec![VerifyIssue::ManifestHashMismatch],
};
};
self.verify_pin(pin, plugin_dir)
}
fn verify_pin(&self, pin: &PluginPin, plugin_dir: &Path) -> VerifyReport {
let mut issues = Vec::new();
let mut tool_status: BTreeMap<String, ToolStatus> = BTreeMap::new();
let manifest_path = plugin_dir.join("plugin.json");
let manifest_raw = std::fs::read(&manifest_path).ok();
let manifest_ok = match &manifest_raw {
Some(raw) if hex_sha256(raw) == pin.manifest_sha256 => true,
Some(_) => {
issues.push(VerifyIssue::ManifestHashMismatch);
false
}
None => {
issues.push(VerifyIssue::ManifestHashMismatch);
false
}
};
if manifest_ok
&& let Some(raw) = &manifest_raw
&& let Ok(text) = std::str::from_utf8(raw)
&& let Ok(manifest) = super::manifest::PluginManifest::from_json(text)
{
let manifest_keys: Vec<String> = manifest
.tools
.iter()
.map(|t| t.name().to_string())
.collect();
let pin_keys: Vec<String> = pin
.tools
.iter()
.map(|t| match t {
ToolPin::Wasm { identity, .. } | ToolPin::Script { identity, .. } => {
identity.rsplit('.').next().unwrap_or_default().to_string()
}
ToolPin::Mcp { label, .. } => label.clone(),
})
.collect();
if manifest_keys != pin_keys {
issues.push(VerifyIssue::ToolSetChanged {
detail: format!("manifest tools {manifest_keys:?} vs pinned {pin_keys:?}"),
});
}
}
for tool in &pin.tools {
let (identity, status) = match tool {
ToolPin::Wasm {
identity,
module,
wasm_sha256,
..
} => {
let path = Path::new(module);
if !path.is_file() {
issues.push(VerifyIssue::MissingModule {
identity: identity.clone(),
path: module.clone(),
});
(
identity.clone(),
ToolStatus::Broken {
detail: format!("module missing: {module}"),
},
)
} else {
match std::fs::read(path) {
Ok(bytes) if hex_sha256(&bytes) == *wasm_sha256 => {
(identity.clone(), ToolStatus::Ready)
}
Ok(_) => {
issues.push(VerifyIssue::WasmHashMismatch {
identity: identity.clone(),
});
(
identity.clone(),
ToolStatus::Broken {
detail: "wasm hash mismatch".into(),
},
)
}
Err(e) => (
identity.clone(),
ToolStatus::Broken {
detail: format!("module unreadable: {e}"),
},
),
}
}
}
ToolPin::Script { identity, run } => {
let missing = std::fs::metadata(&run[0])
.map(|m| !m.is_file())
.unwrap_or(true);
if missing {
(
identity.clone(),
ToolStatus::Broken {
detail: format!("script missing: {}", run[0]),
},
)
} else {
(identity.clone(), ToolStatus::Ready)
}
}
ToolPin::Mcp { label, .. } => {
(format!("plugin.{}.{}", pin.name, label), ToolStatus::Ready)
}
};
tool_status.insert(identity, status);
}
let tampered = issues.iter().any(|i| {
matches!(
i,
VerifyIssue::ManifestHashMismatch
| VerifyIssue::ToolSetChanged { .. }
| VerifyIssue::WasmHashMismatch { .. }
)
});
let package = if tampered {
PackageStatus::Tampered {
reason: match issues.first() {
Some(VerifyIssue::ManifestHashMismatch) => "manifest changed".into(),
Some(VerifyIssue::ToolSetChanged { detail }) => {
format!("tool set changed ({detail})")
}
Some(VerifyIssue::WasmHashMismatch { identity }) => {
format!("wasm replaced ({identity})")
}
_ => "integrity check failed".into(),
},
}
} else {
PackageStatus::Trusted
};
let tools = pin
.tools
.iter()
.map(|t| {
let identity = t.identity(&pin.name);
let status = if tampered {
ToolStatus::Broken {
detail: "plugin tampered; re-run plugin add".into(),
}
} else {
tool_status.remove(&identity).unwrap_or(ToolStatus::Ready)
};
(identity, status)
})
.collect();
VerifyReport {
plugin: pin.name.clone(),
package,
tools,
issues,
}
}
}
pub fn hex_sha256(bytes: &[u8]) -> String {
use sha2::{Digest, Sha256};
let digest = Sha256::digest(bytes);
digest.iter().map(|b| format!("{b:02x}")).collect()
}
#[cfg(test)]
mod tests {
use super::*;
fn pin_with_wasm(
name: &str,
manifest_raw: &str,
wasm_path: &str,
wasm_bytes: &[u8],
) -> PluginPin {
PluginPin {
name: name.into(),
version: "0.1.0".into(),
source: "path".into(),
manifest_sha256: hex_sha256(manifest_raw.as_bytes()),
tools: vec![ToolPin::Wasm {
identity: format!("plugin.{name}.search"),
module: wasm_path.into(),
wasm_sha256: hex_sha256(wasm_bytes),
capabilities: vec![ResolvedCapability::FsRead {
paths: vec!["/tmp/data".into()],
}],
}],
}
}
fn manifest_raw() -> String {
serde_json::json!({
"schemaVersion": 1, "name": "my-tools", "version": "0.1.0",
"tools": [{"name": "search", "form": "wasm", "module": "wasm/search.wasm", "description": "d"}]
})
.to_string()
}
#[test]
fn registry_round_trips_json() {
let mut reg = PluginRegistry::new();
reg.insert(pin_with_wasm(
"my-tools",
&manifest_raw(),
"/abs/search.wasm",
b"wasm!",
));
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(REGISTRY_FILE);
reg.save(&path).unwrap();
let raw = std::fs::read_to_string(&path).unwrap();
let parsed: serde_json::Value = serde_json::from_str(&raw).unwrap();
assert_eq!(parsed["schema_version"], 1);
assert_eq!(parsed["plugins"]["my-tools"]["version"], "0.1.0");
assert_eq!(parsed["plugins"]["my-tools"]["tools"][0]["form"], "wasm");
assert_eq!(
parsed["plugins"]["my-tools"]["tools"][0]["capabilities"][0]["paths"][0],
"/tmp/data"
);
let back = PluginRegistry::load(&path).unwrap();
assert_eq!(back, reg);
}
#[test]
fn load_missing_is_fresh_but_corrupt_is_error() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(REGISTRY_FILE);
assert!(PluginRegistry::load(&path).unwrap().is_empty());
std::fs::write(&path, "{broken").unwrap();
assert!(matches!(
PluginRegistry::load(&path),
Err(RegistryError::Json(_))
));
}
#[test]
fn verify_trusted_when_untouched() {
let dir = tempfile::tempdir().unwrap();
let plugin_dir = dir.path().join("my-tools");
std::fs::create_dir_all(plugin_dir.join("wasm")).unwrap();
std::fs::write(plugin_dir.join("plugin.json"), manifest_raw()).unwrap();
std::fs::write(plugin_dir.join("wasm/search.wasm"), b"wasm!").unwrap();
let mut reg = PluginRegistry::new();
reg.insert(pin_with_wasm(
"my-tools",
&manifest_raw(),
plugin_dir.join("wasm/search.wasm").to_str().unwrap(),
b"wasm!",
));
let report = reg.verify(&plugin_dir);
assert!(!report.tampered());
assert_eq!(report.package, PackageStatus::Trusted);
assert_eq!(
report.tools,
vec![("plugin.my-tools.search".into(), ToolStatus::Ready)]
);
}
#[test]
fn verify_tampered_on_manifest_edit() {
let dir = tempfile::tempdir().unwrap();
let plugin_dir = dir.path().join("my-tools");
std::fs::create_dir_all(plugin_dir.join("wasm")).unwrap();
std::fs::write(plugin_dir.join("wasm/search.wasm"), b"wasm!").unwrap();
let tampered = serde_json::json!({
"schemaVersion": 1, "name": "my-tools", "version": "0.1.0",
"tools": [
{"name": "search", "form": "wasm", "module": "wasm/search.wasm", "description": "d",
"capabilities": [{"type": "fs:read", "paths": ["/"]}]},
{"name": "evil", "form": "script", "description": "d", "run": ["scripts/x.sh"]}
]
})
.to_string();
std::fs::write(plugin_dir.join("plugin.json"), &tampered).unwrap();
let mut reg = PluginRegistry::new();
reg.insert(pin_with_wasm(
"my-tools",
&manifest_raw(),
plugin_dir.join("wasm/search.wasm").to_str().unwrap(),
b"wasm!",
));
let report = reg.verify(&plugin_dir);
assert!(report.tampered());
let msg = report.user_message().unwrap();
assert!(msg.contains("plugin add"), "{msg}");
assert!(
report
.tools
.iter()
.all(|(_, s)| matches!(s, ToolStatus::Broken { .. }))
);
}
#[test]
fn verify_tampered_on_wasm_swap() {
let dir = tempfile::tempdir().unwrap();
let plugin_dir = dir.path().join("my-tools");
std::fs::create_dir_all(plugin_dir.join("wasm")).unwrap();
std::fs::write(plugin_dir.join("plugin.json"), manifest_raw()).unwrap();
let wasm_path = plugin_dir.join("wasm/search.wasm");
std::fs::write(&wasm_path, b"EVIL!!").unwrap();
let mut reg = PluginRegistry::new();
reg.insert(pin_with_wasm(
"my-tools",
&manifest_raw(),
wasm_path.to_str().unwrap(),
b"wasm!",
));
let report = reg.verify(&plugin_dir);
assert!(report.tampered());
assert!(report.user_message().unwrap().contains("plugin add"));
}
#[test]
fn verify_missing_wasm_disables_one_tool_not_package() {
let dir = tempfile::tempdir().unwrap();
let plugin_dir = dir.path().join("my-tools");
std::fs::create_dir_all(plugin_dir.join("wasm")).unwrap();
std::fs::write(plugin_dir.join("plugin.json"), manifest_raw()).unwrap();
let mut reg = PluginRegistry::new();
reg.insert(pin_with_wasm(
"my-tools",
&manifest_raw(),
plugin_dir.join("wasm/search.wasm").to_str().unwrap(),
b"wasm!",
));
let report = reg.verify(&plugin_dir);
assert!(!report.tampered(), "missing is corruption, not tampering");
assert_eq!(report.package, PackageStatus::Trusted);
assert!(matches!(
&report.tools[0],
(id, ToolStatus::Broken { detail }) if id == "plugin.my-tools.search" && detail.contains("missing")
));
assert!(matches!(
report.issues[0],
VerifyIssue::MissingModule { .. }
));
}
#[test]
fn verify_unpinned_plugin_is_tampered() {
let dir = tempfile::tempdir().unwrap();
let plugin_dir = dir.path().join("stranger");
std::fs::create_dir_all(&plugin_dir).unwrap();
std::fs::write(plugin_dir.join("plugin.json"), manifest_raw()).unwrap();
let reg = PluginRegistry::new();
let report = reg.verify(&plugin_dir);
assert!(report.tampered());
}
#[test]
fn remove_clears_registry_entry() {
let mut reg = PluginRegistry::new();
reg.insert(pin_with_wasm(
"my-tools",
&manifest_raw(),
"/abs/s.wasm",
b"w",
));
assert_eq!(reg.len(), 1);
let removed = reg.remove("my-tools").unwrap();
assert_eq!(removed.name, "my-tools");
assert!(reg.is_empty());
assert!(reg.remove("my-tools").is_none());
}
}