vtcode-skills 0.185.2

Skill types, discovery, loading, and validation for VT Code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use super::*;
use serial_test::serial;
use std::cell::Cell;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use tempfile::TempDir;

thread_local! {
    static TEST_FREE_WAS_CALLED: Cell<bool> = const { Cell::new(false) };
}

static TEST_EXECUTE_ACTIVE_CALLS: AtomicUsize = AtomicUsize::new(0);
static TEST_EXECUTE_MAX_CONCURRENCY: AtomicUsize = AtomicUsize::new(0);
static TEST_FREE_CALLS: AtomicUsize = AtomicUsize::new(0);

/// Test-only C ABI shim for the plugin loader.
///
/// # Safety
///
/// The loader calls it with pointers produced by `CString::into_raw` in this module.
#[allow(unsafe_code, reason = "test-only C ABI shim for the plugin loader")]
unsafe extern "C" fn test_free_string(ptr: *const c_char) {
    TEST_FREE_WAS_CALLED.with(|was_called| was_called.set(true));
    if !ptr.is_null() {
        // Safety: tests pass pointers produced by `CString::into_raw` in this module.
        let _ = unsafe { CString::from_raw(ptr.cast_mut()) };
    }
}

/// Counting deallocator: proves the bridge releases a plugin buffer exactly
/// once, including on error paths (no leak, no double-free).
///
/// # Safety
///
/// The caller must pass either null or a pointer produced by
/// `CString::into_raw` in this module; `from_raw` reclaims that allocation
/// exactly once.
#[allow(unsafe_code, reason = "test-only C ABI shim for the plugin loader")]
unsafe extern "C" fn test_free_counting(ptr: *const c_char) {
    TEST_FREE_CALLS.fetch_add(1, Ordering::SeqCst);
    if !ptr.is_null() {
        // Safety: tests pass pointers produced by `CString::into_raw` in this module.
        let _ = unsafe { CString::from_raw(ptr.cast_mut()) };
    }
}

fn create_test_plugin_dir() -> (TempDir, PathBuf) {
    let temp_dir = TempDir::new().unwrap();
    let plugin_dir = temp_dir.path().join("test-plugin");
    std::fs::create_dir(&plugin_dir).unwrap();
    (temp_dir, plugin_dir)
}

fn write_plugin_metadata(plugin_dir: &Path, name: &str) {
    std::fs::write(
        plugin_dir.join("plugin.json"),
        format!(r#"{{"name":"{name}","description":"test","version":"1.0.0"}}"#),
    )
    .unwrap();
}

fn write_fake_library(plugin_dir: &Path, name: &str) -> PathBuf {
    let loader = PluginLoader::new();
    let library_path = plugin_dir.join(loader.library_filename(name));
    std::fs::write(&library_path, b"fake-library").unwrap();
    library_path
}

fn current_process_library() -> Library {
    #[cfg(unix)]
    {
        libloading::os::unix::Library::this().into()
    }
    #[cfg(windows)]
    {
        libloading::os::windows::Library::this()
            .expect("current process library")
            .into()
    }
}

fn update_max_concurrency(active_calls: usize) {
    let mut current_max = TEST_EXECUTE_MAX_CONCURRENCY.load(Ordering::SeqCst);
    while active_calls > current_max {
        match TEST_EXECUTE_MAX_CONCURRENCY.compare_exchange(
            current_max,
            active_calls,
            Ordering::SeqCst,
            Ordering::SeqCst,
        ) {
            Ok(_) => break,
            Err(observed) => current_max = observed,
        }
    }
}

/// # Safety
///
/// The loader calls this through the plugin ABI with a nul-terminated input
/// pointer that stays valid for the call (the shim ignores it). The returned
/// pointer is an owned `CString::into_raw` buffer that the loader releases with
/// the matching free symbol.
unsafe extern "C" fn test_execute_with_delay(_input: *const c_char) -> *const c_char {
    let active_calls = TEST_EXECUTE_ACTIVE_CALLS.fetch_add(1, Ordering::SeqCst) + 1;
    update_max_concurrency(active_calls);
    std::thread::sleep(Duration::from_millis(25));
    TEST_EXECUTE_ACTIVE_CALLS.fetch_sub(1, Ordering::SeqCst);

    CString::new(r#"{"success":true,"output":{},"error":null,"files":[]}"#)
        .unwrap()
        .into_raw()
}

/// Boundary adversarial inputs: a conforming plugin must never return null,
/// invalid UTF-8, or malformed JSON, but the bridge must fail closed (and still
/// release the buffer) rather than introducing a crash of its own.
///
/// # Safety
///
/// The loader calls this through the plugin ABI with a nul-terminated input
/// pointer valid for the call; it is ignored. This shim intentionally returns
/// null to exercise the bridge's fail-closed path, so no buffer is transferred.
#[allow(unsafe_code, reason = "test-only C ABI shim for the plugin loader")]
unsafe extern "C" fn test_execute_null(_input: *const c_char) -> *const c_char {
    std::ptr::null()
}

/// # Safety
///
/// The loader calls this through the plugin ABI with a nul-terminated input
/// pointer valid for the call; it is ignored. The returned pointer is an owned
/// nul-terminated `into_raw` buffer that the loader releases with the matching
/// free symbol.
#[allow(unsafe_code, reason = "test-only C ABI shim for the plugin loader")]
unsafe extern "C" fn test_execute_invalid_utf8(_input: *const c_char) -> *const c_char {
    // 0xFF is never valid UTF-8; the nul keeps it a well-formed C string.
    CString::from_vec_with_nul(vec![0xFF, 0x00]).unwrap().into_raw()
}

/// # Safety
///
/// The loader calls this through the plugin ABI with a nul-terminated input
/// pointer valid for the call; it is ignored. The returned pointer is an owned
/// `CString::into_raw` buffer that the loader releases with the matching free
/// symbol.
#[allow(unsafe_code, reason = "test-only C ABI shim for the plugin loader")]
unsafe extern "C" fn test_execute_malformed_json(_input: *const c_char) -> *const c_char {
    CString::new("not json").unwrap().into_raw()
}

/// # Safety
///
/// The loader calls this through the plugin ABI with a nul-terminated input
/// pointer valid for the call; it is ignored. The returned pointer is an owned
/// `CString::into_raw` buffer; the loader releases it with a matching free
/// symbol only when one was exported (one boundary test deliberately exports
/// none, so the buffer is intentionally leaked there).
#[allow(unsafe_code, reason = "test-only C ABI shim for the plugin loader")]
unsafe extern "C" fn test_execute_valid(_input: *const c_char) -> *const c_char {
    CString::new(r#"{"success":true,"output":{},"error":null,"files":[]}"#)
        .unwrap()
        .into_raw()
}

fn test_context() -> PluginContext {
    PluginContext {
        input: HashMap::new(),
        workspace_root: None,
        config: HashMap::new(),
    }
}

/// Shared builder: keeps the plugin literal in one place so the concurrency
/// tests and the boundary tests exercise the same construction path.
fn test_plugin(execute_fn: PluginExecuteFn, free_string_fn: Option<PluginFreeStringFn>) -> NativePlugin {
    NativePlugin {
        _library: current_process_library(),
        metadata: PluginMetadata {
            name: "boundary-test".to_string(),
            description: "test plugin".to_string(),
            version: "1.0.0".to_string(),
            author: None,
            abi_version: PLUGIN_ABI_VERSION,
            when_to_use: None,
            when_not_to_use: None,
            allowed_tools: None,
            thread_safe: false,
        },
        path: PathBuf::from("/tmp/boundary-test-plugin"),
        execute_fn,
        free_string_fn,
        execution_lock: Mutex::new(()),
        thread_safe: false,
    }
}

#[test]
fn test_validate_plugin_structure_missing_metadata() {
    let (_temp_dir, plugin_dir) = create_test_plugin_dir();
    let errors = validate_plugin_structure(&plugin_dir).unwrap();
    assert!(errors.iter().any(|e| e.contains("plugin.json")));
}

#[test]
fn test_validate_plugin_structure_missing_library() {
    let (_temp_dir, plugin_dir) = create_test_plugin_dir();
    std::fs::write(plugin_dir.join("plugin.json"), r#"{"name": "test", "description": "test", "version": "1.0.0"}"#)
        .unwrap();

    let errors = validate_plugin_structure(&plugin_dir).unwrap();
    assert!(errors.iter().any(|e| e.contains("dynamic library")));
}

#[test]
fn test_validate_plugin_structure_complete() {
    let (_temp_dir, plugin_dir) = create_test_plugin_dir();

    // Create valid plugin.json
    std::fs::write(plugin_dir.join("plugin.json"), r#"{"name": "test", "description": "test", "version": "1.0.0"}"#)
        .unwrap();

    // Create fake library file
    let lib_name = if cfg!(target_os = "macos") {
        "libtest.dylib"
    } else if cfg!(target_os = "linux") {
        "libtest.so"
    } else {
        "test.dll"
    };
    std::fs::write(plugin_dir.join(lib_name), b"fake").unwrap();

    let errors = validate_plugin_structure(&plugin_dir).unwrap();
    assert!(errors.is_empty());
}

#[test]
fn test_library_filename_platform() {
    let loader = PluginLoader::new();
    let filename = loader.library_filename("my-plugin");

    #[cfg(target_os = "macos")]
    assert_eq!(filename, "libmy-plugin.dylib");

    #[cfg(target_os = "linux")]
    assert_eq!(filename, "libmy-plugin.so");

    #[cfg(target_os = "windows")]
    assert_eq!(filename, "my-plugin.dll");
}

#[test]
fn test_ensure_non_null_c_string_ptr_rejects_null() {
    let err = ensure_non_null_c_string_ptr(std::ptr::null::<c_char>(), "Test pointer")
        .expect_err("null pointer should be rejected");
    assert!(err.to_string().contains("Test pointer returned null pointer"));
}

#[test]
fn test_decode_plugin_c_string_frees_plugin_buffer() {
    TEST_FREE_WAS_CALLED.with(|was_called| was_called.set(false));

    let raw = CString::new("{\"ok\":true}").expect("valid C string").into_raw();
    let ptr = NonNull::new(raw).expect("non-null raw pointer");

    let decoded = decode_plugin_c_string(ptr, Some(test_free_string), "Plugin result is not valid UTF-8")
        .expect("valid UTF-8 payload");

    assert_eq!(decoded, "{\"ok\":true}");
    TEST_FREE_WAS_CALLED.with(|was_called| assert!(was_called.get()));
}

#[test]
fn test_decode_plugin_c_string_invalid_utf8_still_frees_buffer() {
    TEST_FREE_WAS_CALLED.with(|was_called| was_called.set(false));

    let raw = CString::from_vec_with_nul(vec![0xFF, 0x00])
        .expect("valid nul-terminated C string")
        .into_raw();
    let ptr = NonNull::new(raw).expect("non-null raw pointer");

    let err = decode_plugin_c_string(ptr, Some(test_free_string), "Plugin payload is not valid UTF-8")
        .expect_err("invalid UTF-8 should fail decoding");

    assert!(err.to_string().contains("Plugin payload is not valid UTF-8"));
    TEST_FREE_WAS_CALLED.with(|was_called| assert!(was_called.get()));
}

#[test]
fn test_load_plugin_rejects_dotdot_escape_from_trusted_root() {
    let temp_dir = TempDir::new().unwrap();
    let trusted_root = temp_dir.path().join("trusted");
    let escaped_plugin_dir = temp_dir.path().join("escaped-plugin");
    std::fs::create_dir(&trusted_root).unwrap();
    std::fs::create_dir(&escaped_plugin_dir).unwrap();
    write_plugin_metadata(&escaped_plugin_dir, "escaped");
    write_fake_library(&escaped_plugin_dir, "escaped");

    let escaped_path = trusted_root.join("..").join("escaped-plugin");

    let mut loader = PluginLoader::new();
    loader.add_trusted_dir(trusted_root);

    let err = loader
        .load_plugin(&escaped_path)
        .expect_err("path traversal should be rejected");

    assert!(err.to_string().contains("trusted directory"));
}

#[cfg(unix)]
#[test]
fn test_load_plugin_rejects_symlinked_plugin_dir_escape() {
    use std::os::unix::fs::symlink;

    let temp_dir = TempDir::new().unwrap();
    let trusted_root = temp_dir.path().join("trusted");
    let real_plugin_dir = temp_dir.path().join("external-plugin");
    let symlinked_plugin_dir = trusted_root.join("linked-plugin");
    std::fs::create_dir(&trusted_root).unwrap();
    std::fs::create_dir(&real_plugin_dir).unwrap();
    write_plugin_metadata(&real_plugin_dir, "linked");
    write_fake_library(&real_plugin_dir, "linked");
    symlink(&real_plugin_dir, &symlinked_plugin_dir).unwrap();

    let mut loader = PluginLoader::new();
    loader.add_trusted_dir(trusted_root);

    let err = loader
        .load_plugin(&symlinked_plugin_dir)
        .expect_err("symlink escape should be rejected");

    assert!(err.to_string().contains("trusted directory"));
}

#[cfg(unix)]
#[test]
fn test_load_plugin_rejects_symlinked_library_escape() {
    use std::os::unix::fs::symlink;

    let temp_dir = TempDir::new().unwrap();
    let trusted_root = temp_dir.path().join("trusted");
    let plugin_dir = trusted_root.join("plugin");
    let external_dir = temp_dir.path().join("external");
    std::fs::create_dir(&trusted_root).unwrap();
    std::fs::create_dir(&plugin_dir).unwrap();
    std::fs::create_dir(&external_dir).unwrap();
    write_plugin_metadata(&plugin_dir, "escaped-lib");

    let external_library = write_fake_library(&external_dir, "escaped-lib");
    let linked_library = plugin_dir.join(PluginLoader::new().library_filename("escaped-lib"));
    symlink(&external_library, &linked_library).unwrap();

    let mut loader = PluginLoader::new();
    loader.add_trusted_dir(trusted_root);

    let err = loader
        .load_plugin(&plugin_dir)
        .expect_err("library symlink escape should be rejected");

    assert!(err.to_string().contains("trusted directory"));
}

#[test]
#[serial]
fn test_native_plugin_serializes_concurrent_execution() {
    TEST_EXECUTE_ACTIVE_CALLS.store(0, Ordering::SeqCst);
    TEST_EXECUTE_MAX_CONCURRENCY.store(0, Ordering::SeqCst);

    let plugin = Arc::new(test_plugin(test_execute_with_delay, Some(test_free_string)));
    let ctx = test_context();

    let handles = (0..4)
        .map(|_| {
            let plugin = Arc::clone(&plugin);
            let ctx = ctx.clone();
            std::thread::spawn(move || plugin.execute(&ctx).expect("plugin execution"))
        })
        .collect::<Vec<_>>();

    for handle in handles {
        let result = handle.join().expect("thread should complete");
        assert!(result.success);
    }

    assert_eq!(TEST_EXECUTE_MAX_CONCURRENCY.load(Ordering::SeqCst), 1);
}

#[test]
#[serial]
fn test_native_plugin_allows_parallel_execution() {
    TEST_EXECUTE_ACTIVE_CALLS.store(0, Ordering::SeqCst);
    TEST_EXECUTE_MAX_CONCURRENCY.store(0, Ordering::SeqCst);

    let mut plugin = test_plugin(test_execute_with_delay, Some(test_free_string));
    // OPT-IN to parallel execution; the shared builder defaults to serialized.
    plugin.thread_safe = true;
    plugin.metadata.thread_safe = true;
    let plugin = Arc::new(plugin);

    let ctx = test_context();

    let num_threads = 4;
    let handles = (0..num_threads)
        .map(|_| {
            let plugin = Arc::clone(&plugin);
            let ctx = ctx.clone();
            std::thread::spawn(move || plugin.execute(&ctx).expect("parallel plugin execution"))
        })
        .collect::<Vec<_>>();

    for handle in handles {
        let result = handle.join().expect("thread join");
        assert!(result.success);
    }

    // With thread_safe: true, we expect max concurrency > 1
    assert!(TEST_EXECUTE_MAX_CONCURRENCY.load(Ordering::SeqCst) > 1);
}

#[cfg(unix)]
#[test]
fn test_warn_if_insecure_trusted_dir_warns_on_writable_dir() {
    use std::os::unix::fs::PermissionsExt;

    let temp_dir = TempDir::new().unwrap();
    let writable_dir = temp_dir.path().join("writable");
    std::fs::create_dir(&writable_dir).unwrap();
    std::fs::set_permissions(&writable_dir, std::fs::Permissions::from_mode(0o777)).unwrap();

    warn_if_insecure_trusted_dir(&writable_dir);
}

#[cfg(unix)]
#[test]
fn test_warn_if_insecure_trusted_dir_silent_on_restricted_dir() {
    use std::os::unix::fs::PermissionsExt;

    let temp_dir = TempDir::new().unwrap();
    let restricted_dir = temp_dir.path().join("restricted");
    std::fs::create_dir(&restricted_dir).unwrap();
    std::fs::set_permissions(&restricted_dir, std::fs::Permissions::from_mode(0o755)).unwrap();

    warn_if_insecure_trusted_dir(&restricted_dir);
}

// --- FFI boundary: adversarial plugin responses ---

#[test]
#[serial]
fn test_execute_ffi_null_result_fails_closed_without_freeing() {
    TEST_FREE_CALLS.store(0, Ordering::SeqCst);
    let plugin = test_plugin(test_execute_null, Some(test_free_counting));

    let err = plugin.execute(&test_context()).expect_err("null result must be rejected");

    assert!(err.to_string().contains("returned null pointer"));
    // No buffer was returned, so the deallocator must not be called.
    assert_eq!(TEST_FREE_CALLS.load(Ordering::SeqCst), 0);
}

#[test]
#[serial]
fn test_execute_ffi_invalid_utf8_frees_buffer_exactly_once() {
    TEST_FREE_CALLS.store(0, Ordering::SeqCst);
    let plugin = test_plugin(test_execute_invalid_utf8, Some(test_free_counting));

    let err = plugin.execute(&test_context()).expect_err("invalid UTF-8 must be rejected");

    assert!(err.to_string().contains("not valid UTF-8"));
    assert_eq!(TEST_FREE_CALLS.load(Ordering::SeqCst), 1);
}

#[test]
#[serial]
fn test_execute_ffi_malformed_json_frees_buffer_exactly_once() {
    TEST_FREE_CALLS.store(0, Ordering::SeqCst);
    let plugin = test_plugin(test_execute_malformed_json, Some(test_free_counting));

    let err = plugin.execute(&test_context()).expect_err("malformed JSON must be rejected");

    assert!(err.to_string().contains("Failed to parse plugin result JSON"));
    assert_eq!(TEST_FREE_CALLS.load(Ordering::SeqCst), 1);
}

#[test]
#[serial]
fn test_execute_ffi_without_deallocator_still_decodes() {
    TEST_FREE_CALLS.store(0, Ordering::SeqCst);
    let plugin = test_plugin(test_execute_valid, None);

    let result = plugin
        .execute(&test_context())
        .expect("valid result decodes even without a deallocator");

    assert!(result.success);
    assert_eq!(TEST_FREE_CALLS.load(Ordering::SeqCst), 0);
}

#[test]
#[serial]
fn test_plugin_owned_c_string_frees_exactly_once_on_decode_error() {
    TEST_FREE_CALLS.store(0, Ordering::SeqCst);
    let raw = CString::from_vec_with_nul(vec![0xFF, 0x00]).expect("valid C string").into_raw();
    let owned = PluginOwnedCString::new(NonNull::new(raw).expect("non-null"), Some(test_free_counting));

    let err = owned.decode("bad payload").expect_err("invalid UTF-8 must be rejected");
    drop(owned);

    assert!(err.to_string().contains("bad payload"));
    assert_eq!(TEST_FREE_CALLS.load(Ordering::SeqCst), 1);
}