silicube 0.3.1

A library for sandboxed code execution
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
use std::collections::HashMap;

use serde::{Deserialize, Deserializer, Serialize, de};

use crate::config::ConfigError;
use crate::types::{MountConfig, ResourceLimits};

const INVALID_FILE_EXT_CHARS: [char; 2] = ['/', '.'];

/// Default PATH for sandbox execution
pub const DEFAULT_SANDBOX_PATH: &str = "/usr/bin:/bin";

/// Configuration for a programming language
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Language {
    /// Human-readable name for the language (e.g., "C++20 (GCC)")
    pub name: String,

    /// File extension
    pub extension: FileExtension,

    /// Compilation configuration (None for interpreted languages)
    #[serde(default)]
    pub compile: Option<CompileConfig>,

    /// Execution configuration
    pub run: RunConfig,
}

impl Language {
    /// Check if the language is compiled
    pub fn is_compiled(&self) -> bool {
        self.compile.is_some()
    }

    /// Get the source file name for this language
    pub fn source_name(&self) -> String {
        if let Some(ref compile) = self.compile {
            compile.source_name.clone()
        } else {
            format!("main.{}", self.extension)
        }
    }

    /// Expand placeholders in the given command
    pub fn expand_command(command: &[String], source: &str, binary: &str) -> Vec<String> {
        command
            .iter()
            .map(|arg| {
                arg.replace("{source}", source)
                    .replace("{output}", binary)
                    .replace("{binary}", binary)
            })
            .collect()
    }
}

/// File extension without dot (e.g., "cpp")
#[derive(Debug, Clone, Serialize)]
pub struct FileExtension(String);

impl FileExtension {
    pub fn new(extension: &str) -> Result<Self, ConfigError> {
        let contains_invalid = extension
            .chars()
            .any(|c| INVALID_FILE_EXT_CHARS.contains(&c));
        if contains_invalid {
            return Err(ConfigError::InvalidFileExtChars);
        }
        Ok(Self(extension.to_owned()))
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl<'de> Deserialize<'de> for FileExtension {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        FileExtension::new(&s).map_err(|_| {
            de::Error::invalid_value(
                de::Unexpected::Str(&s),
                &"a file extension without '/' or '.' characters",
            )
        })
    }
}

impl std::fmt::Display for FileExtension {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Configuration for the compilation step
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompileConfig {
    /// Command and arguments with placeholders
    /// Placeholders: {source}, {binary}
    pub command: Vec<String>,

    /// Source file name in the sandbox (e.g., "main.cpp")
    pub source_name: String,

    /// Output binary name (e.g., "main")
    pub output_name: String,

    /// Environment variables to set during compilation
    #[serde(default)]
    pub env: HashMap<String, String>,

    /// Resource limits for compilation (overrides defaults)
    #[serde(default)]
    pub limits: Option<ResourceLimits>,
}

/// Configuration for the execution step
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RunConfig {
    /// Command and arguments with placeholders
    /// Placeholders: {source}, {binary}
    pub command: Vec<String>,

    /// Environment Variables to set
    #[serde(default)]
    pub env: HashMap<String, String>,

    /// Directory mounts
    #[serde(default)]
    pub mounts: Vec<MountConfig>,

    /// PATH environment variable for the sandbox
    ///
    /// Defaults to "/usr/bin:/bin" if not specified.
    #[serde(default = "default_sandbox_path")]
    pub path: String,

    /// Resource limits for execution (overrides defaults)
    #[serde(default)]
    pub limits: Option<ResourceLimits>,
}

fn default_sandbox_path() -> String {
    DEFAULT_SANDBOX_PATH.to_owned()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn file_extension_new_valid() {
        let ext = FileExtension::new("cpp").unwrap();
        assert_eq!(ext.to_string(), "cpp");
    }

    #[test]
    fn file_extension_new_valid_with_numbers() {
        let ext = FileExtension::new("f90").unwrap();
        assert_eq!(ext.to_string(), "f90");
    }

    #[test]
    fn file_extension_new_empty() {
        let ext = FileExtension::new("").unwrap();
        assert!(ext.is_empty());
    }

    #[test]
    fn file_extension_new_rejects_slash() {
        let result = FileExtension::new("path/ext");
        assert!(result.is_err());
    }

    #[test]
    fn file_extension_new_rejects_dot() {
        let result = FileExtension::new(".cpp");
        assert!(result.is_err());
    }

    #[test]
    fn file_extension_new_rejects_multiple_dots() {
        let result = FileExtension::new(".tar.gz");
        assert!(result.is_err());
    }

    #[test]
    fn file_extension_is_empty() {
        let empty = FileExtension::new("").unwrap();
        let non_empty = FileExtension::new("rs").unwrap();
        assert!(empty.is_empty());
        assert!(!non_empty.is_empty());
    }

    #[test]
    fn file_extension_display() {
        let ext = FileExtension::new("py").unwrap();
        assert_eq!(format!("{ext}"), "py");
    }

    #[test]
    fn expand_command_source_placeholder() {
        let cmd = vec![
            "gcc".to_owned(),
            "-o".to_owned(),
            "out".to_owned(),
            "{source}".to_owned(),
        ];
        let result = Language::expand_command(&cmd, "main.c", "main");
        assert_eq!(result, vec!["gcc", "-o", "out", "main.c"]);
    }

    #[test]
    fn expand_command_output_placeholder() {
        let cmd = vec![
            "gcc".to_owned(),
            "-o".to_owned(),
            "{output}".to_owned(),
            "main.c".to_owned(),
        ];
        let result = Language::expand_command(&cmd, "main.c", "main");
        assert_eq!(result, vec!["gcc", "-o", "main", "main.c"]);
    }

    #[test]
    fn expand_command_binary_placeholder() {
        let cmd = vec!["./{binary}".to_owned()];
        let result = Language::expand_command(&cmd, "main.cpp", "main");
        assert_eq!(result, vec!["./main"]);
    }

    #[test]
    fn expand_command_multiple_placeholders() {
        let cmd = vec![
            "gcc".to_owned(),
            "{source}".to_owned(),
            "-o".to_owned(),
            "{output}".to_owned(),
        ];
        let result = Language::expand_command(&cmd, "test.c", "test");
        assert_eq!(result, vec!["gcc", "test.c", "-o", "test"]);
    }

    #[test]
    fn expand_command_no_placeholders() {
        let cmd = vec!["echo".to_owned(), "hello".to_owned()];
        let result = Language::expand_command(&cmd, "main.c", "main");
        assert_eq!(result, vec!["echo", "hello"]);
    }

    #[test]
    fn expand_command_empty() {
        let cmd: Vec<String> = vec![];
        let result = Language::expand_command(&cmd, "main.c", "main");
        assert!(result.is_empty());
    }

    #[test]
    fn expand_command_placeholder_in_middle() {
        let cmd = vec!["prefix-{source}-suffix".to_owned()];
        let result = Language::expand_command(&cmd, "main.c", "main");
        assert_eq!(result, vec!["prefix-main.c-suffix"]);
    }

    #[test]
    fn language_is_compiled_true() {
        let lang = Language {
            name: "C++".to_owned(),
            extension: FileExtension::new("cpp").unwrap(),
            compile: Some(CompileConfig {
                command: vec!["g++".to_owned()],
                source_name: "main.cpp".to_owned(),
                output_name: "main".to_owned(),
                env: std::collections::HashMap::new(),
                limits: None,
            }),
            run: RunConfig {
                command: vec!["./{binary}".to_owned()],
                env: std::collections::HashMap::new(),
                mounts: vec![],
                path: DEFAULT_SANDBOX_PATH.to_owned(),
                limits: None,
            },
        };
        assert!(lang.is_compiled());
    }

    #[test]
    fn language_is_compiled_false() {
        let lang = Language {
            name: "Python".to_owned(),
            extension: FileExtension::new("py").unwrap(),
            compile: None,
            run: RunConfig {
                command: vec!["python3".to_owned(), "{source}".to_owned()],
                env: std::collections::HashMap::new(),
                mounts: vec![],
                path: DEFAULT_SANDBOX_PATH.to_owned(),
                limits: None,
            },
        };
        assert!(!lang.is_compiled());
    }

    #[test]
    fn language_source_name_compiled() {
        let lang = Language {
            name: "C++".to_owned(),
            extension: FileExtension::new("cpp").unwrap(),
            compile: Some(CompileConfig {
                command: vec!["g++".to_owned()],
                source_name: "solution.cpp".to_owned(),
                output_name: "solution".to_owned(),
                env: std::collections::HashMap::new(),
                limits: None,
            }),
            run: RunConfig {
                command: vec!["./{binary}".to_owned()],
                env: std::collections::HashMap::new(),
                mounts: vec![],
                path: DEFAULT_SANDBOX_PATH.to_owned(),
                limits: None,
            },
        };
        assert_eq!(lang.source_name(), "solution.cpp");
    }

    #[test]
    fn language_source_name_interpreted() {
        let lang = Language {
            name: "Python".to_owned(),
            extension: FileExtension::new("py").unwrap(),
            compile: None,
            run: RunConfig {
                command: vec!["python3".to_owned(), "{source}".to_owned()],
                env: std::collections::HashMap::new(),
                mounts: vec![],
                path: DEFAULT_SANDBOX_PATH.to_owned(),
                limits: None,
            },
        };
        assert_eq!(lang.source_name(), "main.py");
    }

    #[test]
    fn run_config_default_path() {
        assert_eq!(DEFAULT_SANDBOX_PATH, "/usr/bin:/bin");
    }
}

#[cfg(test)]
mod proptests {
    use proptest::prelude::*;

    use super::*;

    proptest! {
        #[test]
        fn file_extension_rejects_all_strings_with_slash(s in ".*/.*.") {
            // Any string containing a slash should be rejected
            let result = FileExtension::new(&s);
            prop_assert!(result.is_err());
        }

        #[test]
        fn file_extension_rejects_all_strings_with_dot(s in ".*\\..*.") {
            // Any string containing a dot should be rejected
            let result = FileExtension::new(&s);
            prop_assert!(result.is_err());
        }

        #[test]
        fn file_extension_accepts_alphanumeric(s in "[a-zA-Z0-9_-]+") {
            // Alphanumeric strings without dots or slashes should be accepted
            let result = FileExtension::new(&s);
            prop_assert!(result.is_ok());
        }

        #[test]
        fn expand_command_preserves_args_without_placeholders(
            arg1 in "[a-z]+",
            arg2 in "[a-z]+",
            arg3 in "[a-z]+"
        ) {
            let cmd = vec![arg1.clone(), arg2.clone(), arg3.clone()];
            let result = Language::expand_command(&cmd, "source.c", "binary");
            prop_assert_eq!(&result[0], &arg1);
            prop_assert_eq!(&result[1], &arg2);
            prop_assert_eq!(&result[2], &arg3);
        }

        #[test]
        fn expand_command_length_preserved(cmd_len in 1usize..10) {
            let cmd: Vec<String> = (0..cmd_len).map(|i| format!("arg{i}")).collect();
            let result = Language::expand_command(&cmd, "source", "binary");
            prop_assert_eq!(result.len(), cmd_len);
        }
    }
}