dekopon-shell 0.2.0

Sandboxed bash-flavored script interpreter whose commands dispatch to Dekopon capabilities
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
//! The fixed builtin table and the context builtins run against.
//!
//! Every builtin name in this table is separator-free: it contains no `.`, `-`, or `_`. Capability
//! fallback in [`crate::dispatch`] only fires for words that *do* contain a separator, so a builtin
//! and a capability can never collide on the same bare word. That is the collision-avoidance
//! mechanism, not a coincidence, and [`tests::builtin_names_can_never_collide_with_capabilities`]
//! asserts it.
//!
//! Builtins are either **text-shaped** or **value-shaped**:
//!
//! - text-shaped (`grep`, `sed`, `cut`, `sort`, `uniq`, `wc`, `base64`) accept a raw string or a
//!   JSON array of lines, newline-joining arrays on the way in and re-coercing line lists to arrays
//!   on the way out, so `curl ... | grep foo | wc -l` reads like bash;
//! - value-shaped (`jq`, `cap`, `curl`, `cat`) stay JSON-native end to end.

use std::collections::BTreeMap;

use serde_json::Value;

use crate::{
    CapabilityCallResult, CapabilityInvoker, ExitCode,
    limits::{Budget, LimitExceeded},
};

pub(crate) mod cap;
pub(crate) mod clock;
pub(crate) mod curl;
pub(crate) mod encode;
pub(crate) mod jq;
pub(crate) mod misc;
pub(crate) mod text;
pub(crate) mod xargs;

/// What one command produced.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct CommandResult {
    /// The structured value the command produced.
    pub value: Value,
    /// The command's exit status.
    pub status: ExitCode,
    /// Whether emitting this value should omit the usual trailing newline.
    pub suppress_newline: bool,
}

impl CommandResult {
    pub(crate) fn value(value: Value) -> Self {
        Self {
            value,
            status: ExitCode::SUCCESS,
            suppress_newline: false,
        }
    }

    pub(crate) fn status(status: ExitCode) -> Self {
        Self {
            value: Value::Null,
            status,
            suppress_newline: false,
        }
    }

    pub(crate) fn without_newline(mut self) -> Self {
        self.suppress_newline = true;
        self
    }
}

/// Why a command did not produce a result.
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum CommandFailure {
    /// Recoverable: the message is written to output, the status is recorded, the script continues.
    Status {
        /// Diagnostic written to the combined output.
        message: String,
        /// Exit status recorded in `$?`.
        status: ExitCode,
    },
    /// Terminal: the script stops and the interpreter reports this exit code.
    Fatal(FatalError),
}

impl CommandFailure {
    /// A usage or argument error. Exit code `2`, matching a shell syntax failure.
    pub(crate) fn usage(message: impl Into<String>) -> Self {
        Self::Status {
            message: message.into(),
            status: ExitCode::SYNTAX,
        }
    }

    /// A runtime failure inside a builtin. Exit code `1`.
    pub(crate) fn failed(message: impl Into<String>) -> Self {
        Self::Status {
            message: message.into(),
            status: ExitCode::FAILURE,
        }
    }
}

impl From<LimitExceeded> for CommandFailure {
    fn from(limit: LimitExceeded) -> Self {
        Self::Fatal(FatalError::Limit(limit))
    }
}

impl From<LimitExceeded> for FatalError {
    fn from(limit: LimitExceeded) -> Self {
        Self::Limit(limit)
    }
}

/// A failure that stops the whole script.
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum FatalError {
    /// A sandbox bound was exhausted.
    Limit(LimitExceeded),
    /// The script reached a construct this shell deliberately excludes.
    Unsupported(String),
}

/// Everything a builtin may touch.
pub(crate) struct BuiltinContext<'a> {
    /// The capability seam.
    pub invoker: &'a dyn CapabilityInvoker,
    /// Per-execution counters.
    pub budget: &'a mut Budget,
    /// Named in-memory buffers written by `>` and `>>`; never real paths.
    pub buffers: &'a mut BTreeMap<String, Value>,
    /// The capability `curl` assembles requests for, when the embedder configured one.
    pub curl_capability: Option<&'a str>,
    /// Whether `date` may read the host wall clock; see [`clock`].
    pub allow_clock: bool,
}

impl BuiltinContext<'_> {
    /// Charges the capability-call budget and invokes one capability.
    ///
    /// The deadline is re-read on both sides of the call. A capability invocation is the single
    /// most expensive thing a script can do in wall-clock terms and the cheapest in steps — the
    /// default budget lets thirty-two of them run in ninety-six steps — so leaving the clock to the
    /// step counter alone let a script overrun its deadline by minutes and still report success.
    pub(crate) fn invoke_capability(
        &mut self,
        capability: &str,
        input: Value,
    ) -> Result<CommandResult, CommandFailure> {
        self.budget.charge_capability_call()?;
        self.budget.check_deadline()?;
        let result = self.invoker.invoke(capability, input);
        self.budget.check_deadline()?;
        let status = ExitCode::from_capability_result(&result);
        Ok(match result {
            CapabilityCallResult::Succeeded(output) => CommandResult {
                value: output,
                status,
                suppress_newline: false,
            },
            CapabilityCallResult::Denied { reason } => {
                return Err(CommandFailure::Status {
                    message: format!("{capability}: denied: {reason}"),
                    status,
                });
            }
            CapabilityCallResult::Failed { error } => {
                return Err(CommandFailure::Status {
                    message: format!("{capability}: failed: {error}"),
                    status,
                });
            }
            CapabilityCallResult::NotFound => {
                return Err(CommandFailure::Status {
                    message: format!("{capability}: capability not found"),
                    status,
                });
            }
        })
    }
}

/// One builtin command.
pub(crate) trait Builtin {
    /// The separator-free name this builtin is dispatched by.
    fn name(&self) -> &'static str;

    /// Runs the builtin with already-expanded argv and the piped input value, if any.
    fn run(
        &self,
        context: &mut BuiltinContext<'_>,
        arguments: &[String],
        input: Option<Value>,
    ) -> Result<CommandResult, CommandFailure>;
}

/// How a builtin is executed.
#[derive(Clone, Copy)]
pub(crate) enum BuiltinKind {
    /// Runs entirely inside the builtin.
    Simple(&'static dyn Builtin),
    /// `xargs` maps a command over a list, so the interpreter runs it re-entrantly.
    Xargs,
}

/// The complete builtin registry, in dispatch order.
const REGISTRY: &[&dyn Builtin] = &[
    &jq::Jq,
    &curl::Curl,
    &clock::Date,
    &misc::Sleep,
    &text::Grep,
    &text::Sed,
    &text::Cut,
    &text::Sort,
    &text::Uniq,
    &text::Wc,
    &encode::Base64,
    &misc::Echo,
    &misc::Printf,
    &misc::Test,
    &misc::TestBracket,
    &misc::True,
    &misc::False,
    &misc::Cat,
    &cap::Cap,
];

/// Looks one command word up in the builtin table.
pub(crate) fn lookup(name: &str) -> Option<BuiltinKind> {
    if name == xargs::NAME {
        return Some(BuiltinKind::Xargs);
    }
    REGISTRY
        .iter()
        .find(|builtin| builtin.name() == name)
        .map(|builtin| BuiltinKind::Simple(*builtin))
}

/// Returns every builtin name, for the namespace-disjointness invariant tests.
#[cfg(test)]
pub(crate) fn names() -> Vec<&'static str> {
    let mut names = REGISTRY
        .iter()
        .map(|builtin| builtin.name())
        .collect::<Vec<_>>();
    names.push(xargs::NAME);
    names.sort_unstable();
    names
}

/// Rejects a flag this shell does not implement, rather than accepting it as a no-op.
pub(crate) fn unsupported_flag(command: &str, flag: &str) -> CommandFailure {
    CommandFailure::usage(format!("{command}: option not yet supported: {flag}"))
}

#[cfg(test)]
pub(crate) mod test_support {
    //! Helpers letting each builtin be unit-tested without standing up the whole interpreter.

    use std::collections::BTreeMap;

    use serde_json::Value;

    use crate::{
        CapabilityCallResult, CapabilityInvoker,
        limits::{Budget, Limits},
    };

    use super::{Builtin, BuiltinContext, CommandFailure, CommandResult};

    /// An invoker that grants nothing, for builtins that never reach the capability seam.
    pub(crate) struct NoCapabilities;

    impl CapabilityInvoker for NoCapabilities {
        fn granted(&self) -> Vec<String> {
            Vec::new()
        }

        fn invoke(&self, _capability: &str, _input: Value) -> CapabilityCallResult {
            CapabilityCallResult::NotFound
        }
    }

    /// Runs one builtin under default limits with no capabilities and no buffers.
    pub(crate) fn run_builtin(
        builtin: &dyn Builtin,
        arguments: &[&str],
        input: Option<Value>,
    ) -> Result<CommandResult, CommandFailure> {
        let mut buffers = BTreeMap::new();
        run_builtin_with(
            builtin,
            arguments,
            input,
            Limits::default(),
            None,
            &mut buffers,
        )
    }

    /// Runs one builtin against a caller-supplied invoker.
    pub(crate) fn run_builtin_with_invoker(
        builtin: &dyn Builtin,
        arguments: &[&str],
        invoker: &dyn CapabilityInvoker,
    ) -> Result<CommandResult, CommandFailure> {
        let mut budget = Budget::start(Limits::default());
        let mut buffers = BTreeMap::new();
        let mut context = BuiltinContext {
            invoker,
            budget: &mut budget,
            buffers: &mut buffers,
            curl_capability: None,
            allow_clock: false,
        };
        let arguments = arguments
            .iter()
            .map(|argument| (*argument).to_owned())
            .collect::<Vec<_>>();
        builtin.run(&mut context, &arguments, None)
    }

    /// Runs one builtin with the wall clock enabled, for the `date` builtin's own tests.
    pub(crate) fn run_builtin_with_clock(
        builtin: &dyn Builtin,
        arguments: &[&str],
    ) -> Result<CommandResult, CommandFailure> {
        let invoker = NoCapabilities;
        let mut budget = Budget::start(Limits::default());
        let mut buffers = BTreeMap::new();
        let mut context = BuiltinContext {
            invoker: &invoker,
            budget: &mut budget,
            buffers: &mut buffers,
            curl_capability: None,
            allow_clock: true,
        };
        let arguments = arguments
            .iter()
            .map(|argument| (*argument).to_owned())
            .collect::<Vec<_>>();
        builtin.run(&mut context, &arguments, None)
    }

    /// Runs one builtin with explicit limits, curl capability, and buffer store.
    pub(crate) fn run_builtin_with(
        builtin: &dyn Builtin,
        arguments: &[&str],
        input: Option<Value>,
        limits: Limits,
        curl_capability: Option<&str>,
        buffers: &mut BTreeMap<String, Value>,
    ) -> Result<CommandResult, CommandFailure> {
        let invoker = NoCapabilities;
        let mut budget = Budget::start(limits);
        let mut context = BuiltinContext {
            invoker: &invoker,
            budget: &mut budget,
            buffers,
            curl_capability,
            allow_clock: false,
        };
        let arguments = arguments
            .iter()
            .map(|argument| (*argument).to_owned())
            .collect::<Vec<_>>();
        builtin.run(&mut context, &arguments, input)
    }
}

#[cfg(test)]
mod tests {
    use super::{lookup, names, xargs};

    #[test]
    fn builtin_names_can_never_collide_with_capabilities() {
        // Capability fallback only fires for words containing `.`, `-`, or `_`. Keeping every
        // builtin name separator-free is what makes that disjointness total rather than likely.
        for name in names() {
            assert!(
                !name.contains(['.', '-', '_']),
                "builtin {name:?} contains a capability-identifier separator"
            );
        }
    }

    #[test]
    fn the_registry_covers_every_documented_builtin() {
        let expected = [
            "[",
            "base64",
            "cap",
            "cat",
            "curl",
            "cut",
            "date",
            "echo",
            "false",
            "grep",
            "jq",
            "printf",
            "sed",
            "sleep",
            "sort",
            "test",
            "true",
            "uniq",
            "wc",
            xargs::NAME,
        ];
        let mut expected = expected.to_vec();
        expected.sort_unstable();
        assert_eq!(names(), expected);
        for name in expected {
            assert!(lookup(name).is_some(), "{name} must resolve");
        }
        assert!(lookup("definitely-not-a-builtin").is_none());
    }
}