bashkit 0.19.0

Awesomely fast virtual sandbox with bash and file system
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
//! Brace expansion (`{a,b,c}`, `{1..5}`).
//!
//! Split out of the monolithic interpreter module; these are pure
//! `&self` helpers driven from the word-expansion path.

use super::*;

impl Interpreter {
    /// Check if a string contains glob characters
    /// Expand brace patterns like {a,b,c} or {1..5}
    /// Returns a Vec of expanded strings, or a single-element Vec if no braces
    /// THREAT[TM-DOS-042]: Cap total expansion count to prevent combinatorial OOM.
    pub(super) fn expand_braces(&self, s: &str) -> Vec<String> {
        const MAX_BRACE_EXPANSION_TOTAL: usize = 100_000;
        let mut count = 0;
        let mut bytes = 0;
        self.expand_braces_capped(s, &mut count, &mut bytes, MAX_BRACE_EXPANSION_TOTAL, 0)
    }

    /// THREAT[TM-DOS-042]: The combinatorial `count` cap alone is insufficient
    /// because it is only incremented *after* each recursive call returns, so
    /// the first DFS path descends to the full nesting/sequence depth (one
    /// level per brace group) with `count` still zero. An input like
    /// `{a,b}{a,b}...` repeated tens of thousands of times (well under
    /// `max_input_bytes`) therefore stack-overflows the worker thread, and
    /// allocates O(depth * suffix) memory, before the count cap or execution
    /// timeout can fire. The `depth` cap bounds the descent up front;
    /// legitimate scripts never approach this many nested/sequential groups.
    fn expand_braces_capped(
        &self,
        s: &str,
        count: &mut usize,
        bytes: &mut usize,
        max: usize,
        recursion_depth: usize,
    ) -> Vec<String> {
        const MAX_BRACE_EXPANSION_DEPTH: usize = 100;
        if *count >= max
            || *bytes >= Self::MAX_EXPANSION_RESULT_BYTES
            || recursion_depth >= MAX_BRACE_EXPANSION_DEPTH
        {
            return vec![s.to_string()];
        }

        // Find the first brace that has a matching close brace
        let mut depth = 0;
        let mut brace_start = None;
        let mut brace_end = None;
        let chars: Vec<char> = s.chars().collect();

        let mut escaped = false;
        for (i, &ch) in chars.iter().enumerate() {
            if escaped {
                escaped = false;
                continue;
            }
            if ch == '\\' {
                escaped = true;
                continue;
            }
            match ch {
                '{' => {
                    if depth == 0 {
                        brace_start = Some(i);
                    }
                    depth += 1;
                }
                '}' => {
                    if depth > 0 {
                        depth -= 1;
                    }
                    if depth == 0 && brace_start.is_some() {
                        brace_end = Some(i);
                        break;
                    }
                }
                _ => {}
            }
        }

        // No valid brace pattern found
        let (start, end) = match (brace_start, brace_end) {
            (Some(s), Some(e)) => (s, e),
            // An unclosed `{` is literal; a later group may still close
            // (`{{a,b}` -> `{a {b`).
            (Some(open), None) => {
                return self.expand_after_literal_brace(
                    &chars,
                    open,
                    count,
                    bytes,
                    max,
                    recursion_depth,
                );
            }
            _ => return vec![s.to_string()],
        };

        let prefix: String = chars[..start].iter().collect();
        let suffix: String = chars[end + 1..].iter().collect();
        let brace_content: String = chars[start + 1..end].iter().collect();

        // Brace content with leading/trailing space is not expanded
        if brace_content.starts_with(' ') || brace_content.ends_with(' ') {
            return self.expand_after_literal_brace(
                &chars,
                start,
                count,
                bytes,
                max,
                recursion_depth,
            );
        }

        // Check for range expansion like {1..5} or {a..z}
        if let Some(range_result) = self.try_expand_range(&brace_content) {
            let mut results = Vec::new();
            for item in range_result {
                if *count >= max || *bytes >= Self::MAX_EXPANSION_RESULT_BYTES {
                    break;
                }
                let expanded = format!("{}{}{}", prefix, item, suffix);
                let sub =
                    self.expand_braces_capped(&expanded, count, bytes, max, recursion_depth + 1);
                *count += sub.len();
                *bytes += sub.iter().map(String::len).sum::<usize>();
                results.extend(sub);
            }
            return results;
        }

        // List expansion like {a,b,c}
        // Need to split by comma, but respect nested braces
        let items = self.split_brace_items(&brace_content);
        if items.len() <= 1 {
            // Not a valid brace expansion (e.g., just {foo}): the `{` stays
            // literal and later groups still expand (`{x}{a,b}` -> `{x}a {x}b`).
            return self.expand_after_literal_brace(
                &chars,
                start,
                count,
                bytes,
                max,
                recursion_depth,
            );
        }

        let mut results = Vec::new();
        for item in items {
            if *count >= max || *bytes >= Self::MAX_EXPANSION_RESULT_BYTES {
                break;
            }
            let expanded = format!("{}{}{}", prefix, item, suffix);
            let sub = self.expand_braces_capped(&expanded, count, bytes, max, recursion_depth + 1);
            *count += sub.len();
            *bytes += sub.iter().map(String::len).sum::<usize>();
            results.extend(sub);
        }

        results
    }

    /// The group opening at `start` is not a brace expansion: keep text up to
    /// and including that `{` and expand whatever follows it.
    fn expand_after_literal_brace(
        &self,
        chars: &[char],
        start: usize,
        count: &mut usize,
        bytes: &mut usize,
        max: usize,
        recursion_depth: usize,
    ) -> Vec<String> {
        let head: String = chars[..=start].iter().collect();
        let rest: String = chars[start + 1..].iter().collect();
        self.expand_braces_capped(&rest, count, bytes, max, recursion_depth + 1)
            .into_iter()
            .map(|tail| format!("{head}{tail}"))
            .collect()
    }

    /// Brace-expand a word before any other expansion, as bash does.
    ///
    /// Only unquoted literal text takes part: every other part (variables,
    /// command substitutions, quoted segments) is an opaque atom, so text an
    /// expansion produces is never brace-expanded (`y='{a,b}'; echo $y`
    /// prints `{a,b}`) and `{1..$n}` stays literal. Atoms are carried through
    /// the string-based expander as private-use placeholder characters and
    /// mapped back to the original parts afterwards.
    ///
    /// Backslashes stay escapes, as in the lexer's quoted-glob convention
    /// (`a"{"b,c}{1,2}` arrives as `a\{b,c}{1,2}`). Fully quoted words never
    /// brace-expand.
    pub(super) fn brace_expand_word(&self, word: &Word) -> Option<Vec<Word>> {
        if word.quoted && !word.has_unquoted_glob {
            return None;
        }
        let quoted_at = |i: usize| word.part_quoted.get(i).copied().unwrap_or(word.quoted);
        let has_brace =
            word.parts.iter().enumerate().any(
                |(i, p)| matches!(p, WordPart::Literal(t) if !quoted_at(i) && t.contains('{')),
            );
        if !has_brace {
            return None;
        }
        let mut text = String::new();
        for (i, part) in word.parts.iter().enumerate() {
            match part {
                WordPart::Literal(t) if !quoted_at(i) => {
                    for ch in t.chars() {
                        if is_brace_placeholder(ch) {
                            // Cannot encode this word; leave it unexpanded.
                            return None;
                        }
                        text.push(ch);
                    }
                }
                _ => text.push(placeholder_for(i)?),
            }
        }
        let expanded = self.expand_braces(&text);
        if expanded.len() == 1 && expanded[0] == text {
            return None;
        }
        let mut words = Vec::with_capacity(expanded.len());
        for item in expanded {
            // An unquoted brace item that expands to nothing is dropped
            // (`echo {,a}` prints `a`).
            if item.is_empty() {
                continue;
            }
            let mut parts = Vec::new();
            let mut part_quoted = Vec::new();
            let mut lit = String::new();
            for ch in item.chars() {
                if let Some(i) = placeholder_index(ch) {
                    if !lit.is_empty() {
                        parts.push(WordPart::Literal(std::mem::take(&mut lit)));
                        part_quoted.push(false);
                    }
                    parts.push(word.parts[i].clone());
                    part_quoted.push(quoted_at(i));
                } else {
                    lit.push(ch);
                }
            }
            if !lit.is_empty() {
                parts.push(WordPart::Literal(lit));
                part_quoted.push(false);
            }
            words.push(Word {
                parts,
                quoted: word.quoted,
                has_unquoted_glob: word.has_unquoted_glob,
                part_quoted,
                raw: None,
            });
        }
        Some(words)
    }

    /// Try to expand a range like 1..5, a..z, or 1..10..2
    /// THREAT[TM-DOS-041]: Cap range size to prevent OOM from {1..999999999}
    pub(super) fn try_expand_range(&self, content: &str) -> Option<Vec<String>> {
        /// Maximum number of elements in a brace range expansion
        const MAX_BRACE_RANGE: u64 = 10_000;

        // Check for .. separator: accept {start..end} or {start..end..step}
        let parts: Vec<&str> = content.split("..").collect();
        if parts.len() != 2 && parts.len() != 3 {
            return None;
        }

        let start = parts[0];
        let end = parts[1];

        // Try numeric range
        if let (Ok(start_num), Ok(end_num)) = (start.parse::<i64>(), end.parse::<i64>()) {
            // Parse optional step (default: 1 or -1 based on direction)
            let step: i64 = if parts.len() == 3 {
                match parts[2].parse::<i64>() {
                    // bash treats a zero step as 1.
                    Ok(0) => 1,
                    Ok(s) => s,
                    Err(_) => return None,
                }
            } else if start_num <= end_num {
                1
            } else {
                -1
            };

            let abs_step = step.unsigned_abs() as u128;
            let abs_diff = (end_num as i128 - start_num as i128).unsigned_abs();
            let range_size = abs_diff / abs_step + 1;
            if range_size > MAX_BRACE_RANGE as u128 {
                return None; // Treat as literal — too large
            }

            let mut results = Vec::new();
            // Bash behavior: direction is determined by start/end. Keep stepping in i128 so
            // huge but valid i64 steps cannot overflow after the precomputed range cap passes.
            let step_magnitude = step.unsigned_abs() as i128;
            let effective_step = if start_num <= end_num {
                step_magnitude
            } else {
                -step_magnitude
            };

            // `{01..10}`: a leading zero on either end pads every item to the
            // wider end's length (the `-` counts toward the width).
            let zero_padded = |n: &str| {
                let digits = n.strip_prefix('-').unwrap_or(n);
                digits.len() > 1 && digits.starts_with('0')
            };
            let width = if zero_padded(start) || zero_padded(end) {
                start.len().max(end.len())
            } else {
                0
            };
            let mut i = start_num as i128;
            let end = end_num as i128;
            if effective_step > 0 {
                while i <= end {
                    results.push(format!("{i:0width$}"));
                    i += effective_step;
                }
            } else {
                while i >= end {
                    results.push(format!("{i:0width$}"));
                    i += effective_step;
                }
            }
            return Some(results);
        }

        // Try character range (single chars only)
        if start.len() == 1 && end.len() == 1 {
            let start_char = start.chars().next().unwrap();
            let end_char = end.chars().next().unwrap();

            if start_char.is_ascii_alphabetic() && end_char.is_ascii_alphabetic() {
                let step: i64 = if parts.len() == 3 {
                    match parts[2].parse::<i64>() {
                        Ok(0) => 1,
                        Ok(s) => s,
                        Err(_) => return None,
                    }
                } else {
                    1
                };
                let abs_step = step.unsigned_abs();

                let mut results = Vec::new();
                let start_byte = u64::from(start_char as u8);
                let end_byte = u64::from(end_char as u8);

                if start_byte <= end_byte {
                    let mut b = start_byte;
                    while b <= end_byte {
                        results.push(((b as u8) as char).to_string());
                        b = match b.checked_add(abs_step) {
                            Some(v) => v,
                            None => break,
                        };
                    }
                } else {
                    let mut b = start_byte;
                    while b >= end_byte {
                        results.push(((b as u8) as char).to_string());
                        b = match b.checked_sub(abs_step) {
                            Some(v) => v,
                            None => break,
                        };
                    }
                }
                return Some(results);
            }
        }

        None
    }

    /// Split brace content by commas, respecting nested braces
    fn split_brace_items(&self, content: &str) -> Vec<String> {
        let mut items = Vec::new();
        let mut current = String::new();
        let mut depth = 0;
        let mut escaped = false;

        for ch in content.chars() {
            if escaped {
                escaped = false;
                current.push(ch);
                continue;
            }
            match ch {
                '\\' => {
                    escaped = true;
                    current.push(ch);
                }
                '{' => {
                    depth += 1;
                    current.push(ch);
                }
                '}' => {
                    depth -= 1;
                    current.push(ch);
                }
                ',' if depth == 0 => {
                    items.push(current);
                    current = String::new();
                }
                _ => {
                    current.push(ch);
                }
            }
        }
        items.push(current);

        items
    }
}

/// Opaque word part `i` is carried as `BASE + 1 + i`.
const BRACE_PLACEHOLDER_BASE: u32 = 0xF0000;
/// Words with more parts than this are not brace-expanded.
const BRACE_MAX_PARTS: u32 = 0xFFF0;

fn is_brace_placeholder(ch: char) -> bool {
    (BRACE_PLACEHOLDER_BASE..=BRACE_PLACEHOLDER_BASE + BRACE_MAX_PARTS).contains(&(ch as u32))
}

fn placeholder_for(i: usize) -> Option<char> {
    let i = u32::try_from(i).ok().filter(|i| *i < BRACE_MAX_PARTS)?;
    char::from_u32(BRACE_PLACEHOLDER_BASE + 1 + i)
}

fn placeholder_index(ch: char) -> Option<usize> {
    let c = ch as u32;
    (c > BRACE_PLACEHOLDER_BASE && c <= BRACE_PLACEHOLDER_BASE + BRACE_MAX_PARTS)
        .then(|| (c - BRACE_PLACEHOLDER_BASE - 1) as usize)
}