Skip to main content

mermaid_runtime/
plugin.rs

1use std::io::Write;
2use std::path::{Path, PathBuf};
3use std::process::{Command, Stdio};
4use std::time::{Duration, Instant};
5
6use anyhow::{Context, Result};
7use serde::{Deserialize, Serialize};
8use sha2::{Digest, Sha256};
9
10use crate::{NewPluginInstall, PluginInstallRecord, RuntimeStore, data_dir};
11
12/// A plugin hook that runs longer than this is killed. Hooks are fire-and-forget
13/// observers, so a runaway one must never hang the caller (the TUI event loop,
14/// the daemon, or the CLI) — this bound is what makes that guarantee.
15const HOOK_TIMEOUT: Duration = Duration::from_secs(30);
16
17#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
18pub struct PluginManifest {
19    pub name: String,
20    #[serde(default)]
21    pub version: Option<String>,
22    #[serde(default)]
23    pub description: Option<String>,
24    #[serde(default)]
25    pub skills: Vec<String>,
26    #[serde(default)]
27    pub agents: Vec<String>,
28    #[serde(default)]
29    pub hooks: Vec<String>,
30    #[serde(default)]
31    pub mcp: Vec<String>,
32    /// Capabilities the plugin *declares* it uses (e.g. "network", "filesystem").
33    /// ADVISORY ONLY — surfaced at install/enable time for informed consent, not
34    /// enforced. A plugin hook is a native child process running with the user's
35    /// privileges; the runtime cannot confine it to this list without OS-level
36    /// sandboxing, so the field documents intent rather than granting a sandbox.
37    /// The real boundary is the explicit `mermaid plugin enable` decision.
38    #[serde(default)]
39    pub capabilities: Vec<String>,
40    #[serde(default)]
41    pub prompts: Vec<String>,
42    #[serde(default)]
43    pub bin: Vec<String>,
44}
45
46/// A summary of what a plugin declares it will do, shown before install/enable.
47/// These are advisory disclosures for informed consent, not an enforced sandbox
48/// (see [`PluginManifest::capabilities`]).
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct PluginCapabilityPreview {
51    pub name: String,
52    pub declared_capabilities: Vec<String>,
53    pub capabilities_toml: Option<toml::Value>,
54    pub hooks: Vec<String>,
55    pub mcp: Vec<String>,
56    pub bin: Vec<String>,
57}
58
59pub fn validate_plugin_manifest(manifest: &PluginManifest, root: &Path) -> Result<()> {
60    anyhow::ensure!(!manifest.name.trim().is_empty(), "plugin name is required");
61    ensure_relative_paths("skills", &manifest.skills, root)?;
62    ensure_relative_paths("agents", &manifest.agents, root)?;
63    ensure_relative_paths("hooks", &manifest.hooks, root)?;
64    ensure_relative_paths("mcp", &manifest.mcp, root)?;
65    ensure_relative_paths("prompts", &manifest.prompts, root)?;
66    ensure_relative_paths("bin", &manifest.bin, root)?;
67    Ok(())
68}
69
70pub fn install_plugin_from_path(path: &Path) -> Result<PluginInstallRecord> {
71    let (_manifest_path, root, manifest) = load_plugin_manifest(path)?;
72    validate_plugin_manifest(&manifest, &root)?;
73    let manifest_json = serde_json::to_string_pretty(&manifest)?;
74    let store = RuntimeStore::open_default()?;
75    let record = store.plugins().install(NewPluginInstall {
76        id: Some(manifest.name.clone()),
77        name: manifest.name,
78        source: root.display().to_string(),
79        version: manifest.version,
80        // Installed plugins are DISABLED by default. A hook runs native code
81        // with the user's privileges, so activation is a separate, explicit
82        // decision (`mermaid plugin enable <id>`) — never a side effect of
83        // install. This is the meaningful boundary in a hook system.
84        enabled: false,
85        manifest_json,
86    })?;
87    write_plugin_lockfile()?;
88    Ok(record)
89}
90
91pub fn plugin_capability_preview(path: &Path) -> Result<PluginCapabilityPreview> {
92    let (_manifest_path, root, manifest) = load_plugin_manifest(path)?;
93    validate_plugin_manifest(&manifest, &root)?;
94    let capabilities_path = root.join("capabilities.toml");
95    let capabilities_toml = if capabilities_path.exists() {
96        let raw = std::fs::read_to_string(&capabilities_path)
97            .with_context(|| format!("failed to read {}", capabilities_path.display()))?;
98        Some(toml::from_str(&raw)?)
99    } else {
100        None
101    };
102    Ok(PluginCapabilityPreview {
103        name: manifest.name,
104        declared_capabilities: manifest.capabilities,
105        capabilities_toml,
106        hooks: manifest.hooks,
107        mcp: manifest.mcp,
108        bin: manifest.bin,
109    })
110}
111
112pub fn write_plugin_lockfile() -> Result<PathBuf> {
113    let store = RuntimeStore::open_default()?;
114    let plugins = store.plugins().list()?;
115    let path = data_dir()?.join("plugins.lock.json");
116    if let Some(parent) = path.parent() {
117        std::fs::create_dir_all(parent)?;
118    }
119    // Atomic write so a crash can't leave a truncated lockfile.
120    crate::write_atomic(&path, &serde_json::to_vec_pretty(&plugins)?)?;
121    Ok(path)
122}
123
124pub fn run_plugin_hooks(event: &str, payload: &serde_json::Value) -> Result<()> {
125    let store = RuntimeStore::open_default()?;
126    let payload_bytes = std::sync::Arc::new(serde_json::to_string(payload)?.into_bytes());
127    for plugin in store.plugins().list()? {
128        // The enabled flag is the trust boundary: a plugin runs native hook
129        // code only after an explicit `plugin enable`. Declared capabilities are
130        // advisory and intentionally not consulted here — they cannot constrain
131        // a native child process.
132        if !plugin.enabled {
133            continue;
134        }
135        let Ok(manifest) = serde_json::from_str::<PluginManifest>(&plugin.manifest_json) else {
136            tracing::warn!(plugin = %plugin.name, "skipping plugin with unparseable manifest");
137            continue;
138        };
139        // Canonicalize the root so a symlink inside it can't be used to escape.
140        let Ok(root) = std::fs::canonicalize(&plugin.source) else {
141            tracing::warn!(plugin = %plugin.name, "plugin source missing; skipping hooks");
142            continue;
143        };
144        for hook in manifest.hooks {
145            // Resolve the hook through symlinks and verify containment on the
146            // CANONICAL path (the old lexical `starts_with` could be escaped
147            // by a symlink inside the root pointing outside it).
148            let Ok(canonical_hook) = std::fs::canonicalize(root.join(&hook)) else {
149                continue; // missing hook: nothing to run
150            };
151            if !canonical_hook.starts_with(&root) {
152                tracing::warn!(plugin = %plugin.name, hook = %hook, "plugin hook escapes root; skipping");
153                continue;
154            }
155            // Execute with a SCRUBBED environment (clear + minimal allowlist)
156            // so provider API keys and MERMAID_DAEMON_TOKEN never leak into
157            // plugin-provided code.
158            let spawn = Command::new(&canonical_hook)
159                .env_clear()
160                .env("PATH", std::env::var_os("PATH").unwrap_or_default())
161                .env("HOME", std::env::var_os("HOME").unwrap_or_default())
162                .env("MERMAID_HOOK_EVENT", event)
163                .env("MERMAID_PLUGIN_NAME", &plugin.name)
164                .stdin(Stdio::piped())
165                .stdout(Stdio::null())
166                .stderr(Stdio::null())
167                .spawn();
168            let mut child = match spawn {
169                Ok(child) => child,
170                Err(err) => {
171                    tracing::warn!(plugin = %plugin.name, error = %err, "failed to spawn plugin hook");
172                    continue; // isolate: one bad hook must not abort the rest
173                },
174            };
175            // Write the payload on a detached thread, then let stdin drop so the
176            // hook sees EOF. Two failure modes are bounded here: a hook that
177            // reads stdin-to-EOF (the drop unblocks it), AND a hook that never
178            // reads stdin while the payload exceeds the pipe buffer (~64 KiB;
179            // checkpoint payloads embed the full file list) — a synchronous
180            // `write_all` would block forever there, and the timeout below only
181            // bounds the WAIT. The thread unblocks when the child exits or is
182            // killed on timeout (closing the pipe), so we never join it.
183            if let Some(mut stdin) = child.stdin.take() {
184                let payload = std::sync::Arc::clone(&payload_bytes);
185                std::thread::spawn(move || {
186                    let _ = stdin.write_all(&payload);
187                });
188            }
189            wait_hook_bounded(&mut child, &plugin.name, &hook, HOOK_TIMEOUT);
190        }
191    }
192    Ok(())
193}
194
195/// Wait for a plugin hook to exit, killing it if it overruns `timeout`.
196/// Synchronous — the runtime crate has no async runtime; callers that must not
197/// block an executor (the `effect/` loop) wrap `run_plugin_hooks` in
198/// `spawn_blocking`.
199fn wait_hook_bounded(child: &mut std::process::Child, plugin: &str, hook: &str, timeout: Duration) {
200    let deadline = Instant::now() + timeout;
201    loop {
202        match child.try_wait() {
203            Ok(Some(status)) => {
204                if !status.success() {
205                    tracing::warn!(plugin = %plugin, hook = %hook, %status, "plugin hook failed");
206                }
207                return;
208            },
209            Ok(None) => {
210                if Instant::now() >= deadline {
211                    let _ = child.kill();
212                    let _ = child.wait();
213                    tracing::warn!(plugin = %plugin, hook = %hook, "plugin hook timed out; killed");
214                    return;
215                }
216                std::thread::sleep(Duration::from_millis(20));
217            },
218            Err(err) => {
219                tracing::warn!(plugin = %plugin, error = %err, "plugin hook wait failed");
220                return;
221            },
222        }
223    }
224}
225
226fn load_plugin_manifest(path: &Path) -> Result<(PathBuf, PathBuf, PluginManifest)> {
227    let resolved = resolve_plugin_source(path)?;
228    let manifest_path = if resolved.is_dir() {
229        resolved.join("plugin.toml")
230    } else {
231        resolved
232    };
233    let root = manifest_path
234        .parent()
235        .context("plugin manifest must have a parent directory")?
236        .to_path_buf();
237    let raw = std::fs::read_to_string(&manifest_path)
238        .with_context(|| format!("failed to read {}", manifest_path.display()))?;
239    let manifest: PluginManifest = toml::from_str(&raw)
240        .with_context(|| format!("failed to parse {}", manifest_path.display()))?;
241    Ok((manifest_path, root, manifest))
242}
243
244fn resolve_plugin_source(path: &Path) -> Result<PathBuf> {
245    if path.exists() {
246        return Ok(path.to_path_buf());
247    }
248    let source = path.to_string_lossy();
249    // Only an EXPLICIT git URL is treated as a remote source. The old bare
250    // `owner/repo` → github.com expansion turned any short string into a
251    // network fetch of attacker-named code; require the full URL instead.
252    let is_git_url = source.starts_with("https://")
253        || source.starts_with("git@")
254        || source.starts_with("ssh://")
255        || source.ends_with(".git");
256    if !is_git_url {
257        return Ok(path.to_path_buf());
258    }
259
260    // Fetching remote plugin code is a privileged operation — gate it behind
261    // an explicit opt-in so it can't be triggered silently (e.g. via the
262    // daemon's local fallback). Operators who want it set the env var.
263    anyhow::ensure!(
264        std::env::var("MERMAID_ALLOW_PLUGIN_FETCH").is_ok_and(|v| v == "1" || v == "true"),
265        "refusing to fetch remote plugin source {source:?}: set MERMAID_ALLOW_PLUGIN_FETCH=1 to allow, \
266         or clone it yourself and install from the local path",
267    );
268
269    let git_source = source.to_string();
270    let dest = data_dir()?
271        .join("plugins")
272        .join("sources")
273        .join(crate::hex_lower(&Sha256::digest(git_source.as_bytes())));
274    // Harden git: no credential prompts, no repo-provided hooks, no external
275    // transports (`ext::` RCE), so materializing the source can't itself run
276    // attacker code.
277    const HARDENING: [&str; 4] = [
278        "-c",
279        "core.hooksPath=/dev/null",
280        "-c",
281        "protocol.ext.allow=never",
282    ];
283    if dest.exists() {
284        let _ = Command::new("git")
285            .env("GIT_TERMINAL_PROMPT", "0")
286            .args(HARDENING)
287            .arg("-C")
288            .arg(&dest)
289            .args(["pull", "--ff-only"])
290            .status();
291    } else {
292        if let Some(parent) = dest.parent() {
293            std::fs::create_dir_all(parent)?;
294        }
295        let status = Command::new("git")
296            .env("GIT_TERMINAL_PROMPT", "0")
297            .args(HARDENING)
298            .args(["clone", "--depth", "1"])
299            .arg(&git_source)
300            .arg(&dest)
301            .status()
302            .with_context(|| format!("failed to clone plugin source {}", git_source))?;
303        anyhow::ensure!(status.success(), "git clone failed for {}", git_source);
304    }
305    Ok(dest)
306}
307
308fn ensure_relative_paths(kind: &str, paths: &[String], root: &Path) -> Result<()> {
309    for path in paths {
310        let rel = Path::new(path);
311        anyhow::ensure!(
312            !rel.is_absolute() && !path.contains(".."),
313            "{} path must stay inside plugin root: {}",
314            kind,
315            path
316        );
317        let full = root.join(rel);
318        anyhow::ensure!(
319            full.exists(),
320            "{} path does not exist under plugin root: {}",
321            kind,
322            path
323        );
324    }
325    Ok(())
326}
327
328#[cfg(test)]
329mod tests {
330    use crate::*;
331
332    #[test]
333    fn manifest_rejects_parent_escape() {
334        let root = std::env::temp_dir();
335        let manifest = PluginManifest {
336            name: "bad".to_string(),
337            version: None,
338            description: None,
339            skills: vec!["../x".to_string()],
340            agents: vec![],
341            hooks: vec![],
342            mcp: vec![],
343            capabilities: vec![],
344            prompts: vec![],
345            bin: vec![],
346        };
347        assert!(validate_plugin_manifest(&manifest, &root).is_err());
348    }
349
350    #[test]
351    fn manifest_round_trips_capabilities_field() {
352        let toml_src = r#"
353            name = "demo"
354            capabilities = ["network", "filesystem"]
355        "#;
356        let manifest: PluginManifest = toml::from_str(toml_src).expect("parse manifest");
357        assert_eq!(manifest.capabilities, vec!["network", "filesystem"]);
358        // Serializes back under the new key, and the old key is gone.
359        let json = serde_json::to_string(&manifest).expect("serialize");
360        assert!(json.contains("\"capabilities\""));
361        assert!(!json.contains("\"permissions\""));
362    }
363
364    #[test]
365    fn hook_overrunning_timeout_is_killed() {
366        use std::time::{Duration, Instant};
367        // A hook that sleeps far past the timeout must be killed, and
368        // wait_hook_bounded must return promptly (no permanent hang).
369        #[cfg(unix)]
370        let mut child = std::process::Command::new("sh")
371            .arg("-c")
372            .arg("sleep 10")
373            .spawn()
374            .expect("spawn sleep");
375        #[cfg(windows)]
376        let mut child = std::process::Command::new("cmd")
377            .args(["/C", "ping -n 11 127.0.0.1 >NUL"])
378            .spawn()
379            .expect("spawn ping");
380        let start = Instant::now();
381        super::wait_hook_bounded(&mut child, "test", "hook", Duration::from_millis(150));
382        assert!(
383            start.elapsed() < Duration::from_secs(3),
384            "should return promptly after killing the overrunning hook"
385        );
386    }
387
388    #[test]
389    fn hook_that_exits_quickly_returns_without_kill() {
390        use std::time::{Duration, Instant};
391        #[cfg(unix)]
392        let mut child = std::process::Command::new("true")
393            .spawn()
394            .expect("spawn true");
395        #[cfg(windows)]
396        let mut child = std::process::Command::new("cmd")
397            .args(["/C", "exit 0"])
398            .spawn()
399            .expect("spawn exit");
400        let start = Instant::now();
401        super::wait_hook_bounded(&mut child, "test", "hook", Duration::from_secs(30));
402        assert!(start.elapsed() < Duration::from_secs(5));
403    }
404}