submilli-engine 0.3.0

Compiles Submilli programs, a TypeScript subset, to WebAssembly and runs them under capability policy
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
//! `submilli:skills` — read the skills a harness offers from inside a Submilli
//! program.
//!
//! An optional package: it exists only for an embedder that enables
//! [`OptionalPackage::Skills`](crate::stdlib::OptionalPackage::Skills). The
//! harness serves skills through [`StoreData::skill_provider`]; a runtime with
//! none refuses every op with a catchable error.
//!
//! **One capability.** `list()`, `load` and `readFile` are all `skill.load`,
//! filtered by `name`. `list()` gates each candidate with the same filter, so a
//! listing never offers a skill the caller would be denied.

pub mod declaration;

use std::sync::Arc;

use wasmtime::{FuncType, HeapType, Linker, RefType, StructType, Val, ValType};

use crate::runtime::StoreData;
use crate::runtime::call_log::{Payload, Side, record_payload};
use crate::runtime::decision::CallTicket;
use crate::runtime::fuel;
use crate::runtime::host::{
    abi_arg, abi_result, range_error, read_string_arg, register_host_fn_async,
    write_submilli_string_struct,
};
use crate::runtime::intrinsic_types::build_intrinsic_types;
use crate::runtime::skills::{Skill, SkillError, SkillInfo, SkillProvider};
use crate::stdlib::abi::{
    self, backing_struct, install_field_getters, nullable_object_field, string_field,
};
use crate::stdlib::shared::{
    check_security_call, permitted_candidates, preflight_listing, sanitize_description, truncated,
};

pub use declaration::package_declaration;

pub const MODULE_NAME: &str = crate::runtime::skills::SKILLS_MODULE_NAME;

/// The capability gating every op in this package.
pub const CAPABILITY: &str = "skill.load";

// Backing field indices (0 is the vtable). `Skill` extends `SkillInfo`'s layout.
const S_NAME: usize = 1;
const S_DESCRIPTION: usize = 2;
const S_CONTENT: usize = 3;

pub fn install(linker: &mut Linker<StoreData>) -> wasmtime::Result<()> {
    let engine = linker.engine().clone();
    let intr = build_intrinsic_types(&engine)?;
    let string = ValType::Ref(RefType::new(
        false,
        HeapType::ConcreteStruct(intr.string.clone()),
    ));
    let nullable_object = ValType::Ref(RefType::new(
        true,
        HeapType::ConcreteStruct(intr.object.clone()),
    ));
    let object = ValType::Ref(RefType::new(
        false,
        HeapType::ConcreteStruct(intr.object.clone()),
    ));
    let array = ValType::Ref(RefType::new(
        false,
        HeapType::ConcreteStruct(intr.array.clone()),
    ));

    register_host_fn_async(
        linker,
        MODULE_NAME,
        crate::mangle::package_symbol(MODULE_NAME, "list"),
        FuncType::new(&engine, [], [array]),
        /* deterministic = */ false,
        |caller, _params, results| {
            Box::pin(async move {
                *abi_result(results, 0)? = list(caller).await?;
                Ok(())
            })
        },
    )?;

    register_host_fn_async(
        linker,
        MODULE_NAME,
        crate::mangle::package_symbol(MODULE_NAME, "load"),
        // A `Skill` crosses as the universal `(ref null $Object)` lowering.
        FuncType::new(&engine, [string.clone()], [nullable_object.clone()]),
        /* deterministic = */ false,
        |caller, params, results| {
            Box::pin(async move {
                let name =
                    read_string_arg(&mut *caller, abi_arg(params, 0)?, "skills.load (name)")?;
                *abi_result(results, 0)? = load(caller, name).await?;
                Ok(())
            })
        },
    )?;

    register_host_fn_async(
        linker,
        MODULE_NAME,
        crate::mangle::package_symbol(MODULE_NAME, "readFile"),
        FuncType::new(&engine, [string.clone(), string.clone()], [string.clone()]),
        /* deterministic = */ false,
        |caller, params, results| {
            Box::pin(async move {
                let name =
                    read_string_arg(&mut *caller, abi_arg(params, 0)?, "skills.readFile (name)")?;
                let path =
                    read_string_arg(&mut *caller, abi_arg(params, 1)?, "skills.readFile (path)")?;
                *abi_result(results, 0)? = read_file(caller, name, path).await?;
                Ok(())
            })
        },
    )?;

    let info_rows = [
        ("name", S_NAME, string.clone()),
        ("description", S_DESCRIPTION, nullable_object),
    ];
    install_field_getters(
        linker,
        MODULE_NAME,
        "SkillInfo",
        &engine,
        &object,
        &info_rows,
    )?;
    let mut skill_rows = info_rows.to_vec();
    skill_rows.push(("content", S_CONTENT, string));
    install_field_getters(linker, MODULE_NAME, "Skill", &engine, &object, &skill_rows)
}

/// The capability check, run before anything reaches the harness.
fn gate(
    caller: &mut wasmtime::Caller<'_, StoreData>,
    name: &str,
) -> wasmtime::Result<Option<CallTicket>> {
    check_security_call(caller, CAPABILITY, serde_json::json!({ "name": name }))
}

/// `list()`: every skill the harness offers that the caller may load. A denied
/// candidate is left out, never reported. Whether a provider is wired is not
/// hidden here: a listing has no candidate to gate before asking it.
async fn list(caller: &mut wasmtime::Caller<'_, StoreData>) -> wasmtime::Result<Val> {
    preflight_listing(caller, CAPABILITY, &serde_json::json!({ "name": "" }))?;
    let provider = provider(caller).map_err(|error| throw("list", &error))?;
    let candidates = provider.list().await.map_err(|e| throw("list", &e))?;
    // A name `load` would refuse is no use to the program, so it is not offered.
    let loadable = candidates
        .into_iter()
        .filter(|skill| is_skill_name(&skill.name))
        .collect();
    let visible = permitted_candidates(
        caller,
        loadable,
        |skill| skill.name.as_str(),
        |caller, name| gate(caller, name).map(|_| ()),
    )?;
    let mut built = Vec::with_capacity(visible.len());
    for skill in visible {
        built.push(build_skill_info(caller, skill)?);
    }
    abi::new_array(caller, &built)
}

/// `load(name)`: gate, refuse what cannot be sent, record the request, then ask
/// the provider. A refusal before the provider is asked is recorded as the
/// call's error, with no request.
async fn load(caller: &mut wasmtime::Caller<'_, StoreData>, name: String) -> wasmtime::Result<Val> {
    let ticket = gate(caller, &name)?;
    if !is_skill_name(&name) {
        return Err(fail(
            caller,
            ticket,
            "load",
            &SkillError::InvalidName { name },
        ));
    }
    let provider = provider(caller).map_err(|error| fail(caller, ticket, "load", &error))?;
    record_payload(&*caller, ticket, Side::Request, || {
        Payload::meta(serde_json::json!({ "op": "load", "name": name }))
    });
    match provider.load(&name).await {
        // The policy decided on `name`; a provider that answered with another
        // skill would hand the program one the policy never saw.
        Ok(skill) if skill.name != name => {
            // The read happened, so it is paid for; its content is not kept.
            fuel::settle(&mut *caller, fuel::IO, skill.content.len() as u64)?;
            Err(fail(
                caller,
                ticket,
                "load",
                &SkillError::WrongSkill { name },
            ))
        }
        Ok(skill) => {
            record_text(caller, ticket, &skill.content);
            fuel::settle(&mut *caller, fuel::IO, skill.content.len() as u64)?;
            fuel::settle_result(caller, |caller| build_skill(caller, skill))
        }
        Err(error) => Err(fail(caller, ticket, "load", &error)),
    }
}

/// `readFile(name, path)`: in the order `load` follows, with the path checked
/// beside the name.
async fn read_file(
    caller: &mut wasmtime::Caller<'_, StoreData>,
    name: String,
    path: String,
) -> wasmtime::Result<Val> {
    let ticket = gate(caller, &name)?;
    if !is_skill_name(&name) {
        return Err(fail(
            caller,
            ticket,
            "readFile",
            &SkillError::InvalidName { name },
        ));
    }
    if !is_skill_path(&path) {
        return Err(fail(
            caller,
            ticket,
            "readFile",
            &SkillError::InvalidPath { path },
        ));
    }
    let provider = provider(caller).map_err(|error| fail(caller, ticket, "readFile", &error))?;
    record_payload(&*caller, ticket, Side::Request, || {
        Payload::meta(serde_json::json!({ "op": "readFile", "name": name, "path": path }))
    });
    match provider.read_file(&name, &path).await {
        Ok(text) => {
            record_text(caller, ticket, &text);
            fuel::settle(&mut *caller, fuel::IO, text.len() as u64)?;
            fuel::settle_result(caller, |caller| {
                let text = write_submilli_string_struct(caller, &text)?;
                Ok(Val::AnyRef(Some(text.to_anyref())))
            })
        }
        Err(error) => Err(fail(caller, ticket, "readFile", &error)),
    }
}

/// The longest skill name, and the longest path, the engine passes to a
/// provider.
const MAX_SKILL_ARG_BYTES: usize = 4096;

/// A skill name: one segment that is not `.` or `..`, with no `/`, `\\` or NUL.
/// `:` is allowed, for namespaced names such as `plugin:skill`; a provider must
/// not read a name as a path.
fn is_skill_name(name: &str) -> bool {
    name.len() <= MAX_SKILL_ARG_BYTES
        && !name.contains(['/', '\\', '\0'])
        && !matches!(name, "" | "." | "..")
}

/// A path inside a skill: relative, `/`-separated, with no empty, `.` or `..`
/// segment, and none of the characters a provider could read as structure — a
/// backslash or `:` (Windows separators, drives and streams) or NUL.
fn is_skill_path(path: &str) -> bool {
    path.len() <= MAX_SKILL_ARG_BYTES
        && !path.contains(['\\', ':', '\0'])
        && path
            .split('/')
            .all(|segment| !matches!(segment, "" | "." | ".."))
}

fn record_text(caller: &wasmtime::Caller<'_, StoreData>, ticket: Option<CallTicket>, text: &str) {
    record_payload(caller, ticket, Side::Response, || {
        Payload::meta(serde_json::Value::Null)
            .with_owned_body(text.as_bytes().to_vec())
            .with_size(text.len() as u64)
    });
}

/// Record `error` as the call's response and turn it into the guest's error.
fn fail(
    caller: &wasmtime::Caller<'_, StoreData>,
    ticket: Option<CallTicket>,
    op: &str,
    error: &SkillError,
) -> wasmtime::Error {
    record_payload(caller, ticket, Side::Response, || {
        Payload::meta(serde_json::json!({ "call_error": call_error_record(error) }))
    });
    throw(op, error)
}

/// Clone the provider out of the store before any `await`. `load` and
/// `readFile` look it up after the capability check, so a denied caller cannot
/// learn whether a harness is wired.
fn provider(
    caller: &wasmtime::Caller<'_, StoreData>,
) -> Result<Arc<dyn SkillProvider>, SkillError> {
    caller
        .data()
        .skill_provider
        .clone()
        .ok_or(SkillError::NotConfigured)
}

/// A name or path outside the skills is an argument the program got wrong; every
/// other failure keeps the base error type.
fn throw(op: &str, error: &SkillError) -> wasmtime::Error {
    let message = format!("skills.{op}: {error}");
    if matches!(
        error,
        SkillError::InvalidName { .. } | SkillError::InvalidPath { .. }
    ) {
        range_error(message)
    } else {
        wasmtime::Error::msg(message)
    }
}

/// A failure as the call log keeps it: a stable kind and what it was about.
fn call_error_record(error: &SkillError) -> serde_json::Value {
    match error {
        SkillError::NotConfigured => serde_json::json!({ "kind": "not-configured" }),
        SkillError::NotFound { name } => serde_json::json!({ "kind": "not-found", "name": name }),
        SkillError::FileNotFound { name, path } => {
            serde_json::json!({ "kind": "file-not-found", "name": name, "path": path })
        }
        SkillError::InvalidName { name } => {
            serde_json::json!({ "kind": "invalid-name", "name": truncated(name) })
        }
        SkillError::WrongSkill { name } => {
            serde_json::json!({ "kind": "wrong-skill", "name": name })
        }
        SkillError::InvalidPath { path } => {
            serde_json::json!({ "kind": "invalid-path", "path": truncated(path) })
        }
        SkillError::Failed { message } => {
            serde_json::json!({ "kind": "failed", "message": message })
        }
    }
}

fn info_backing_struct(engine: &wasmtime::Engine) -> wasmtime::Result<StructType> {
    let intr = build_intrinsic_types(engine)?;
    backing_struct(
        engine,
        &intr,
        vec![string_field(&intr), nullable_object_field(&intr)],
    )
}

fn skill_backing_struct(engine: &wasmtime::Engine) -> wasmtime::Result<StructType> {
    let intr = build_intrinsic_types(engine)?;
    backing_struct(
        engine,
        &intr,
        vec![
            string_field(&intr),
            nullable_object_field(&intr),
            string_field(&intr),
        ],
    )
}

fn build_skill_info(
    caller: &mut wasmtime::Caller<'_, StoreData>,
    skill: SkillInfo,
) -> wasmtime::Result<Val> {
    let name = write_submilli_string_struct(caller, &skill.name)?;
    let description = listing_description(caller, skill.description.as_deref())?;
    let ty = info_backing_struct(caller.engine())?;
    abi::new_backing(
        caller,
        ty,
        &[Val::AnyRef(Some(name.to_anyref())), description],
    )
}

fn build_skill(
    caller: &mut wasmtime::Caller<'_, StoreData>,
    skill: Skill,
) -> wasmtime::Result<Val> {
    let name = write_submilli_string_struct(caller, &skill.name)?;
    let description = optional_text(caller, skill.description.as_deref())?;
    let content = write_submilli_string_struct(caller, &skill.content)?;
    let ty = skill_backing_struct(caller.engine())?;
    abi::new_backing(
        caller,
        ty,
        &[
            Val::AnyRef(Some(name.to_anyref())),
            description,
            Val::AnyRef(Some(content.to_anyref())),
        ],
    )
}

/// A listing shows a description as one bounded line, because it steers which
/// skill a program picks.
fn listing_description(
    caller: &mut wasmtime::Caller<'_, StoreData>,
    text: Option<&str>,
) -> wasmtime::Result<Val> {
    optional_text(caller, text.and_then(sanitize_description).as_deref())
}

/// `text` as a string, or `undefined` when absent.
fn optional_text(
    caller: &mut wasmtime::Caller<'_, StoreData>,
    text: Option<&str>,
) -> wasmtime::Result<Val> {
    match text {
        Some(text) => Ok(Val::AnyRef(Some(
            write_submilli_string_struct(caller, text)?.to_anyref(),
        ))),
        None => crate::runtime::prelude::undefined::value(caller),
    }
}

#[cfg(test)]
mod path_tests {
    use super::{MAX_SKILL_ARG_BYTES, is_skill_name, is_skill_path};
    use crate::stdlib::shared::truncated;

    #[test]
    fn only_relative_paths_inside_the_skill_are_accepted() {
        let longest = "a".repeat(MAX_SKILL_ARG_BYTES);
        for ok in [
            "SKILL.md",
            "templates/report.md",
            "a/b/c.txt",
            ".hidden",
            "notes/résumé ✓.md",
            "%2e%2e/x",
            longest.as_str(),
        ] {
            assert!(is_skill_path(ok), "{ok}");
        }
        let too_long = "a".repeat(MAX_SKILL_ARG_BYTES + 1);
        // 4095 ASCII bytes and a two-byte character: 4097 bytes, 4096 chars.
        let straddling = format!("{}é", "a".repeat(MAX_SKILL_ARG_BYTES - 1));
        for bad in [
            "",
            "/etc/passwd",
            "a//b",
            "./a",
            "a/.",
            "../x",
            "a/../b",
            "a/",
            "a\\b",
            "a\0b",
            "a:b",
            "C:/x",
            "C:x",
            "SKILL.md:stream",
            too_long.as_str(),
            straddling.as_str(),
        ] {
            assert!(!is_skill_path(bad), "{bad:?}");
        }
    }

    #[test]
    fn a_skill_name_is_one_segment_and_may_be_namespaced() {
        let longest = "n".repeat(MAX_SKILL_ARG_BYTES);
        for ok in [
            "code-review",
            "code.review",
            "plugin:skill",
            "C:",
            longest.as_str(),
        ] {
            assert!(is_skill_name(ok), "{ok}");
        }
        let too_long = "n".repeat(MAX_SKILL_ARG_BYTES + 1);
        for bad in [
            "",
            ".",
            "..",
            "../other",
            "a/b",
            "a\\b",
            "a\0b",
            too_long.as_str(),
        ] {
            assert!(!is_skill_name(bad), "{bad:?}");
        }
    }

    #[test]
    fn an_echoed_value_is_cut_on_a_character_boundary() {
        assert_eq!(truncated("short"), "short");
        let exactly = "a".repeat(200);
        assert_eq!(truncated(&exactly), exactly.as_str());
        // Three-byte characters: byte 200 falls inside one, so the cut is at 198.
        let cut = truncated(&"€".repeat(100)).into_owned();
        assert_eq!(cut, format!("{}…", "€".repeat(66)));
    }
}