vtcode_skills/native_plugin/
mod.rs1#![allow(
2 unsafe_code,
3 reason = "Intentional compatibility, platform, or test-only suppression."
4)]
5
6use anyhow::{Context, Result, anyhow};
59use hashbrown::HashMap;
60use libloading::{Library, Symbol};
61use serde::{Deserialize, Serialize};
62use std::ffi::{CStr, CString};
63#[cfg(unix)]
64use std::os::unix::fs::PermissionsExt;
65use std::path::{Path, PathBuf};
66use std::ptr::NonNull;
67use std::sync::Mutex;
68use tracing::{debug, info, warn};
69use vtcode_commons::canonicalize;
70
71pub const PLUGIN_ABI_VERSION: u32 = 1;
74
75use std::os::raw::c_char;
76
77type PluginVersionFn = unsafe extern "C" fn() -> u32;
93type PluginMetadataFn = unsafe extern "C" fn() -> *const c_char;
94type PluginExecuteFn = unsafe extern "C" fn(*const c_char) -> *const c_char;
95type PluginFreeStringFn = unsafe extern "C" fn(*const c_char);
96
97#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct PluginContext {
100 input: HashMap<String, serde_json::Value>,
102 workspace_root: Option<String>,
104 config: HashMap<String, serde_json::Value>,
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct PluginResult {
111 success: bool,
113 output: HashMap<String, serde_json::Value>,
115 error: Option<String>,
117 files: Vec<String>,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct PluginMetadata {
124 pub name: String,
126 pub description: String,
128 version: String,
130 author: Option<String>,
132 abi_version: u32,
134 when_to_use: Option<String>,
136 when_not_to_use: Option<String>,
138 allowed_tools: Option<Vec<String>>,
140 #[serde(default)]
143 thread_safe: bool,
144}
145
146pub trait NativePluginTrait: Send + Sync + std::fmt::Debug {
148 fn metadata(&self) -> &PluginMetadata;
150
151 fn path(&self) -> &Path;
153
154 fn execute(&self, ctx: &PluginContext) -> Result<PluginResult>;
156}
157
158pub struct NativePlugin {
160 _library: Library,
170 metadata: PluginMetadata,
172 path: PathBuf,
174 execute_fn: PluginExecuteFn,
176 free_string_fn: Option<PluginFreeStringFn>,
178 execution_lock: Mutex<()>,
180 thread_safe: bool,
182}
183
184fn ensure_non_null_c_string_ptr(ptr: *const c_char, context: &'static str) -> Result<NonNull<c_char>> {
185 NonNull::new(ptr.cast_mut()).ok_or_else(|| anyhow!("{context} returned null pointer"))
186}
187
188fn decode_plugin_c_string(
189 ptr: NonNull<c_char>,
190 free_string_fn: Option<PluginFreeStringFn>,
191 utf8_error_context: &'static str,
192) -> Result<String> {
193 let raw_ptr = ptr.as_ptr() as *const c_char;
194 let decoded = unsafe { CStr::from_ptr(raw_ptr) }
201 .to_str()
202 .context(utf8_error_context)
203 .map(str::to_owned);
204
205 if let Some(free_fn) = free_string_fn {
206 unsafe { free_fn(raw_ptr) };
209 }
210
211 decoded
212}
213
214impl std::fmt::Debug for NativePlugin {
215 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
216 f.debug_struct("NativePlugin")
217 .field("metadata", &self.metadata)
218 .field("path", &self.path)
219 .finish()
220 }
221}
222
223fn canonicalize_existing_path(path: &Path, label: &str) -> Result<PathBuf> {
224 canonicalize(path).with_context(|| format!("Failed to resolve {label} '{}'", path.display()))
225}
226
227fn get_plugin_symbol<T: Copy>(library: &Library, name: &[u8]) -> Result<T> {
233 let symbol: Symbol<T> = unsafe { library.get(name) }.with_context(|| {
236 format!(
237 "Failed to load plugin symbol: {:?}",
238 String::from_utf8_lossy(name.strip_suffix(b"\0").unwrap_or(name))
239 )
240 })?;
241 Ok(*symbol)
242}
243
244fn normalize_trusted_dir(path: PathBuf) -> PathBuf {
245 canonicalize_existing_path(&path, "trusted plugin directory").unwrap_or_else(|_| {
246 if path.is_absolute() {
247 path
248 } else {
249 std::env::current_dir().map(|cwd| cwd.join(&path)).unwrap_or(path)
250 }
251 })
252}
253
254#[cfg(unix)]
255fn warn_if_insecure_trusted_dir(dir: &Path) {
256 if let Ok(metadata) = std::fs::metadata(dir) {
257 let mode = metadata.permissions().mode();
258 if mode & 0o022 != 0 {
259 let perm_octal = format!("{:o}", mode & 0o777);
260 warn!(
261 dir = ?dir,
262 mode = %perm_octal,
263 "trusted plugin directory is writable by group or others; a non-owner user could plant a malicious plugin via symlink or replacement"
264 );
265 }
266 }
267}
268
269#[cfg(not(unix))]
270fn warn_if_insecure_trusted_dir(_dir: &Path) {}
271
272impl NativePlugin {
273 fn new(library: Library, path: PathBuf) -> Result<Self> {
275 let version_fn = get_plugin_symbol::<PluginVersionFn>(&library, b"vtcode_plugin_version\0")?;
277
278 let abi_version = unsafe { version_fn() };
280 if abi_version != PLUGIN_ABI_VERSION {
281 return Err(anyhow!("Plugin ABI version mismatch: expected {PLUGIN_ABI_VERSION}, got {abi_version}"));
282 }
283
284 let free_string_fn = get_plugin_symbol::<PluginFreeStringFn>(&library, b"vtcode_plugin_free_string\0").ok();
286
287 let metadata_fn = get_plugin_symbol::<PluginMetadataFn>(&library, b"vtcode_plugin_metadata\0")?;
289
290 let metadata_ptr = ensure_non_null_c_string_ptr(unsafe { metadata_fn() }, "Plugin metadata function")?;
292 let metadata_json = decode_plugin_c_string(metadata_ptr, free_string_fn, "Plugin metadata is not valid UTF-8")?;
293
294 let metadata: PluginMetadata =
295 serde_json::from_str(&metadata_json).context("Failed to parse plugin metadata JSON")?;
296
297 let execute_fn = get_plugin_symbol::<PluginExecuteFn>(&library, b"vtcode_plugin_execute\0")?;
299
300 Ok(Self {
301 _library: library,
302 metadata: metadata.clone(), path,
304 execute_fn,
305 free_string_fn,
306 execution_lock: Mutex::new(()),
307 thread_safe: metadata.thread_safe, })
309 }
310
311 fn execute(&self, ctx: &PluginContext) -> Result<PluginResult> {
313 let input_json = serde_json::to_string(ctx).context("Failed to serialize plugin context")?;
314 let input_cstr = CString::new(input_json).context("Plugin context contains internal null bytes")?;
315
316 if !self.thread_safe {
320 let _execution_guard = self
321 .execution_lock
322 .lock()
323 .map_err(|_e| anyhow!("native plugin execution lock poisoned"))?;
324
325 self.execute_ffi(input_cstr)
326 } else {
327 self.execute_ffi(input_cstr)
329 }
330 }
331
332 fn execute_ffi(&self, input_cstr: CString) -> Result<PluginResult> {
339 let result_ptr =
344 ensure_non_null_c_string_ptr(unsafe { (self.execute_fn)(input_cstr.as_ptr()) }, "Plugin execute function")?;
345 let result_json = decode_plugin_c_string(result_ptr, self.free_string_fn, "Plugin result is not valid UTF-8")?;
346
347 let result: PluginResult = serde_json::from_str(&result_json).context("Failed to parse plugin result JSON")?;
348
349 Ok(result)
350 }
351}
352
353impl NativePluginTrait for NativePlugin {
354 fn metadata(&self) -> &PluginMetadata {
355 &self.metadata
356 }
357
358 fn path(&self) -> &Path {
359 &self.path
360 }
361
362 fn execute(&self, ctx: &PluginContext) -> Result<PluginResult> {
363 self.execute(ctx)
364 }
365}
366
367pub struct PluginLoader {
374 trusted_dirs: Vec<PathBuf>,
376}
377
378impl PluginLoader {
379 pub fn new() -> Self {
381 Self { trusted_dirs: Vec::new() }
382 }
383
384 pub fn add_trusted_dir(&mut self, path: PathBuf) -> &mut Self {
390 let path = normalize_trusted_dir(path);
391 if !self.trusted_dirs.contains(&path) {
392 warn_if_insecure_trusted_dir(&path);
393 self.trusted_dirs.push(path);
394 }
395 self
396 }
397
398 pub fn trusted_dirs(&self) -> &[PathBuf] {
400 &self.trusted_dirs
401 }
402
403 pub fn load_plugin(&self, plugin_path: &Path) -> Result<Box<dyn NativePluginTrait>> {
412 debug!("Loading native plugin from {:?}", plugin_path);
413
414 let plugin_path = self.ensure_trusted_path(plugin_path, "Plugin path")?;
415
416 let lib_path = self.find_library_file(&plugin_path)?;
418 let lib_path = self.ensure_trusted_path(&lib_path, "Plugin library path")?;
419
420 let library = unsafe { Library::new(&lib_path) }
431 .with_context(|| format!("Failed to load dynamic library at {lib_path:?}"))?;
432
433 let plugin = NativePlugin::new(library, plugin_path.clone())?;
434
435 info!(
436 "Loaded native plugin '{}' v{} from {:?}",
437 plugin.metadata.name, plugin.metadata.version, plugin_path
438 );
439
440 Ok(Box::new(plugin))
441 }
442
443 pub fn discover_plugins(&self) -> Result<Vec<Box<dyn NativePluginTrait>>> {
448 let mut plugins = Vec::new();
449
450 for dir in &self.trusted_dirs {
451 warn_if_insecure_trusted_dir(dir);
452 if !dir.exists() {
453 continue;
454 }
455
456 match self.discover_plugins_in_dir(dir) {
457 Ok(mut dir_plugins) => plugins.append(&mut dir_plugins),
458 Err(e) => {
459 warn!("Failed to discover plugins in {:?}: {}", dir, e);
460 }
461 }
462 }
463
464 Ok(plugins)
465 }
466
467 fn is_in_trusted_dir(&self, path: &Path) -> bool {
469 self.trusted_dirs.iter().any(|dir| path.starts_with(dir))
470 }
471
472 fn ensure_trusted_path(&self, path: &Path, label: &str) -> Result<PathBuf> {
473 let path = canonicalize_existing_path(path, label)?;
474 if self.is_in_trusted_dir(&path) {
475 Ok(path)
476 } else {
477 Err(anyhow!("{label} {path:?} is not in a trusted directory"))
478 }
479 }
480
481 fn find_library_file(&self, plugin_dir: &Path) -> Result<PathBuf> {
483 if !plugin_dir.is_dir() {
484 return Err(anyhow!("Plugin path is not a directory"));
485 }
486
487 let metadata_path = plugin_dir.join("plugin.json");
489 if !metadata_path.exists() {
490 return Err(anyhow!("No plugin.json found in {plugin_dir:?}"));
491 }
492
493 let lib_name = self.get_library_name_from_metadata(&metadata_path)?;
495
496 let lib_path = plugin_dir.join(&lib_name);
497 if lib_path.exists() {
498 return Ok(lib_path);
499 }
500
501 let alternatives = self.get_alternative_library_names(&lib_name);
503 for alt in alternatives {
504 let alt_path = plugin_dir.join(alt);
505 if alt_path.exists() {
506 return Ok(alt_path);
507 }
508 }
509
510 Err(anyhow!("No dynamic library found in {plugin_dir:?}. Expected one of: {lib_name}, or alternatives"))
511 }
512
513 fn get_library_name_from_metadata(&self, metadata_path: &Path) -> Result<String> {
515 let metadata_content = std::fs::read_to_string(metadata_path).context("Failed to read plugin metadata")?;
516 let metadata: serde_json::Value =
517 serde_json::from_str(&metadata_content).context("Invalid plugin metadata JSON")?;
518
519 let name = metadata["name"]
520 .as_str()
521 .ok_or_else(|| anyhow!("Plugin metadata missing 'name' field"))?;
522
523 Ok(self.library_filename(name))
524 }
525
526 fn get_alternative_library_names(&self, base_name: &str) -> Vec<String> {
528 let mut alternatives = Vec::new();
529
530 if let Some(stripped) = base_name.strip_prefix("lib") {
532 alternatives.push(stripped.to_string());
533 } else {
534 alternatives.push(format!("lib{base_name}"));
535 }
536
537 let base = base_name.strip_prefix("lib").unwrap_or(base_name);
539 #[cfg(target_os = "macos")]
540 {
541 alternatives.push(format!("{base}.dylib"));
542 alternatives.push(format!("lib{base}.dylib"));
543 }
544 #[cfg(target_os = "linux")]
545 {
546 alternatives.push(format!("{}.so", base));
547 alternatives.push(format!("lib{}.so", base));
548 }
549 #[cfg(target_os = "windows")]
550 {
551 alternatives.push(format!("{}.dll", base));
552 alternatives.push(format!("lib{}.dll", base));
553 }
554
555 alternatives
556 }
557
558 fn discover_plugins_in_dir(&self, dir: &Path) -> Result<Vec<Box<dyn NativePluginTrait>>> {
560 let mut plugins = Vec::new();
561
562 for entry in std::fs::read_dir(dir)? {
563 let entry = entry?;
564 let path = entry.path();
565
566 if path.is_dir() && path.join("plugin.json").exists() {
567 match self.load_plugin(&path) {
568 Ok(plugin) => plugins.push(plugin),
569 Err(e) => {
570 warn!("Failed to load plugin at {:?}: {}", path, e);
571 }
572 }
573 }
574 }
575
576 Ok(plugins)
577 }
578
579 pub fn library_filename(&self, name: &str) -> String {
581 #[cfg(target_os = "macos")]
582 {
583 format!("lib{name}.dylib")
584 }
585 #[cfg(target_os = "linux")]
586 {
587 format!("lib{}.so", name)
588 }
589 #[cfg(target_os = "windows")]
590 {
591 format!("{}.dll", name)
592 }
593 #[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
594 {
595 format!("lib{}", name)
596 }
597 }
598}
599
600impl Default for PluginLoader {
601 fn default() -> Self {
602 Self::new()
603 }
604}
605
606pub fn validate_plugin_structure(plugin_dir: &Path) -> Result<Vec<String>> {
608 let mut errors = Vec::new();
609
610 if !plugin_dir.join("plugin.json").exists() {
612 errors.push("Missing plugin.json".to_string());
613 }
614
615 let has_lib = std::fs::read_dir(plugin_dir)
617 .map(|entries| {
618 entries.filter_map(|e| e.ok()).any(|entry| {
619 let path = entry.path();
620 let ext = path.extension().and_then(|e| e.to_str());
621 matches!(ext, Some("dylib") | Some("so") | Some("dll"))
622 })
623 })
624 .unwrap_or(false);
625
626 if !has_lib {
627 errors.push("No dynamic library found (.dylib, .so, or .dll)".to_string());
628 }
629
630 if let Ok(content) = std::fs::read_to_string(plugin_dir.join("plugin.json")) {
632 if let Ok(metadata) = serde_json::from_str::<serde_json::Value>(&content) {
633 if metadata["name"].as_str().is_none() {
634 errors.push("plugin.json missing required 'name' field".to_string());
635 }
636 if metadata["description"].as_str().is_none() {
637 errors.push("plugin.json missing required 'description' field".to_string());
638 }
639 if metadata["version"].as_str().is_none() {
640 errors.push("plugin.json missing required 'version' field".to_string());
641 }
642 } else {
643 errors.push("Invalid JSON in plugin.json".to_string());
644 }
645 }
646
647 Ok(errors)
648}
649
650#[cfg(test)]
651mod tests;