orchestrator-runner 0.2.5

Command runner, sandbox, output capture, and network allowlist
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
mod policy;
mod profile;
mod redact;
mod resource_limits;
mod sandbox;
#[cfg(target_os = "linux")]
mod sandbox_linux;
#[cfg(target_os = "macos")]
mod sandbox_macos;
mod spawn;

pub use policy::{DaemonPidGuardBlocked, enforce_runner_policy};
pub use profile::ResolvedExecutionProfile;
pub use redact::redact_text;
pub use sandbox::{
    SandboxBackendError, SandboxResourceKind, sandbox_backend_label,
    sandbox_backend_preflight_issues, validate_execution_profile_support,
};
pub use spawn::{
    CapturedChild, RunnerExecutor, RunnerStdioMode, ShellRunnerExecutor, SpawnParams,
    kill_child_process_group, spawn_with_runner, spawn_with_runner_and_capture,
};

#[cfg(test)]
mod tests {
    use super::*;
    use orchestrator_config::config::{
        ExecutionNetworkMode, ExecutionProfileMode, RunnerConfig, RunnerExecutorKind, RunnerPolicy,
    };
    use std::fs::File;
    use std::io;
    use tempfile::tempdir;

    use sandbox::classify_sandbox_spawn_error;

    fn make_runner_config() -> RunnerConfig {
        RunnerConfig {
            shell: "/bin/bash".to_string(),
            shell_arg: "-lc".to_string(),
            policy: RunnerPolicy::Unsafe,
            executor: RunnerExecutorKind::Shell,
            allowed_shells: vec!["/bin/bash".to_string()],
            allowed_shell_args: vec!["-lc".to_string()],
            env_allowlist: vec!["PATH".to_string()],
            redaction_patterns: vec!["password".to_string()],
        }
    }

    #[test]
    fn test_enforce_runner_policy_allows_valid_command() {
        let runner = make_runner_config();
        let result = enforce_runner_policy(&runner, "echo hello");
        assert!(result.is_ok());
    }

    #[test]
    fn test_enforce_runner_policy_rejects_empty_command() {
        let runner = make_runner_config();
        let result = enforce_runner_policy(&runner, "");
        assert!(result.is_err());
        assert!(
            result
                .expect_err("operation should fail")
                .to_string()
                .contains("cannot be empty")
        );
    }

    #[test]
    fn test_enforce_runner_policy_allows_newline_in_command() {
        let runner = make_runner_config();
        let result = enforce_runner_policy(&runner, "echo hello\nwhoami");
        assert!(result.is_ok(), "newlines are valid in bash -c commands");
    }

    #[test]
    fn test_enforce_runner_policy_rejects_cr_in_command() {
        let runner = make_runner_config();
        let result = enforce_runner_policy(&runner, "echo hello\rwhoami");
        assert!(result.is_err());
        assert!(
            result
                .expect_err("operation should fail")
                .to_string()
                .contains("control characters")
        );
    }

    #[test]
    fn test_enforce_runner_policy_rejects_too_long_command() {
        let runner = make_runner_config();
        let long_command = "x".repeat(131_073);
        let result = enforce_runner_policy(&runner, &long_command);
        assert!(result.is_err());
        assert!(
            result
                .expect_err("operation should fail")
                .to_string()
                .contains("too long")
        );
    }

    #[test]
    fn test_enforce_runner_policy_rejects_disallowed_shell() {
        let mut runner = make_runner_config();
        runner.policy = RunnerPolicy::Allowlist;
        runner.shell = "/bin/sh".to_string();

        let result = enforce_runner_policy(&runner, "echo hello");
        assert!(result.is_err());
        assert!(
            result
                .expect_err("operation should fail")
                .to_string()
                .contains("runner.shell")
        );
    }

    #[test]
    fn test_enforce_runner_policy_rejects_disallowed_shell_arg() {
        let mut runner = make_runner_config();
        runner.policy = RunnerPolicy::Allowlist;
        runner.shell_arg = "-c".to_string();

        let result = enforce_runner_policy(&runner, "echo hello");
        assert!(result.is_err());
        assert!(
            result
                .expect_err("operation should fail")
                .to_string()
                .contains("runner.shell_arg")
        );
    }

    #[test]
    fn test_redact_text_removes_matching_patterns() {
        let patterns = vec!["password".to_string(), "token".to_string()];
        let input = "my password is [REDACTED] and token is [REDACTED]";
        let result = redact_text(input, &patterns);
        assert!(result.contains("[REDACTED]"));
        assert!(!result.contains("password"));
        assert!(!result.contains("token"));
    }

    #[test]
    fn test_redact_text_handles_uppercase_patterns() {
        let patterns = vec!["password".to_string()];
        let input = "PASSWORD is secret";
        let result = redact_text(input, &patterns);
        assert!(result.contains("[REDACTED]"));
        assert!(!result.contains("PASSWORD"));
    }

    #[test]
    fn test_redact_text_case_insensitive() {
        let patterns = vec!["secret".to_string()];
        let input = "My SeCrEt value";
        let result = redact_text(input, &patterns);
        assert!(result.contains("[REDACTED]"));
        assert!(!result.contains("SeCrEt"));
    }

    #[test]
    fn test_redact_text_multiple_case_variants() {
        let patterns = vec!["token".to_string()];
        let input = "token TOKEN Token all here";
        let result = redact_text(input, &patterns);
        assert!(!result.contains("token"));
        assert!(!result.contains("TOKEN"));
        assert!(!result.contains("Token"));
        assert_eq!(result, "[REDACTED] [REDACTED] [REDACTED] all here");
    }

    #[test]
    fn test_redact_text_secret_value_redaction() {
        let patterns = vec!["sk-abc123".to_string()];
        let input = "api key is sk-abc123 in output";
        let result = redact_text(input, &patterns);
        assert!(result.contains("[REDACTED]"));
        assert!(!result.contains("sk-abc123"));
    }

    #[test]
    fn test_redact_text_ignores_empty_patterns() {
        let patterns = vec!["".to_string()];
        let input = "hello world";
        let result = redact_text(input, &patterns);
        assert_eq!(result, "hello world");
    }

    #[test]
    fn test_classify_sandbox_spawn_error_for_memory_limit() {
        let mut profile = ResolvedExecutionProfile::host();
        profile.name = "sandbox_memory_limit".to_string();
        profile.mode = ExecutionProfileMode::Sandbox;
        profile.max_memory_mb = Some(256);

        let err = io::Error::other("Cannot allocate memory");
        let classified =
            classify_sandbox_spawn_error(&profile, &err).expect("memory spawn error classified");

        assert_eq!(classified.event_type, "sandbox_resource_exceeded");
        assert_eq!(classified.reason_code, "memory_limit_exceeded");
        assert_eq!(
            classified
                .resource_kind
                .as_ref()
                .map(|value| value.as_str()),
            Some("memory")
        );
    }

    #[test]
    fn test_classify_sandbox_spawn_error_uses_single_configured_limit_fallback() {
        let mut profile = ResolvedExecutionProfile::host();
        profile.name = "sandbox_memory_limit".to_string();
        profile.mode = ExecutionProfileMode::Sandbox;
        profile.max_memory_mb = Some(256);

        let err = io::Error::other("spawn failed");
        let classified =
            classify_sandbox_spawn_error(&profile, &err).expect("single-limit fallback");

        assert_eq!(classified.reason_code, "memory_limit_exceeded");
        assert_eq!(
            classified
                .resource_kind
                .as_ref()
                .map(|value| value.as_str()),
            Some("memory")
        );
    }

    #[test]
    fn test_sandbox_backend_preflight_issues_reports_macos_allowlist_gap() {
        let mut profile = ResolvedExecutionProfile::host();
        profile.mode = ExecutionProfileMode::Sandbox;
        profile.network_mode = ExecutionNetworkMode::Allowlist;
        profile.network_allowlist = vec!["example.com:443".to_string()];

        let issues = sandbox_backend_preflight_issues(&profile);
        #[cfg(target_os = "macos")]
        assert!(
            issues
                .iter()
                .any(|issue| issue.contains("does not support network_mode=allowlist"))
        );
        #[cfg(not(target_os = "macos"))]
        assert!(!issues.is_empty());
    }

    #[tokio::test]
    async fn test_spawn_with_runner_allowlist_filters_environment() {
        let temp = tempdir().expect("create tempdir");
        let stdout_path = temp.path().join("stdout.log");
        let stderr_path = temp.path().join("stderr.log");
        let stdout = File::create(&stdout_path).expect("create stdout file");
        let stderr = File::create(&stderr_path).expect("create stderr file");

        let mut runner = make_runner_config();
        runner.policy = RunnerPolicy::Allowlist;
        runner.env_allowlist = vec!["RUNNER_ALLOWED_TEST".to_string()];

        // SAFETY: test runs single-threaded; no concurrent env reads.
        unsafe {
            std::env::set_var("RUNNER_ALLOWED_TEST", "visible");
            std::env::set_var("RUNNER_BLOCKED_TEST", "hidden");
            std::env::set_var("CLAUDECODE", "nested-session");
        }

        let mut child = spawn_with_runner(
            &runner,
            "printf '%s|%s|%s' \"${RUNNER_ALLOWED_TEST:-missing}\" \"${RUNNER_BLOCKED_TEST:-missing}\" \"${CLAUDECODE:-missing}\"",
            temp.path(),
            stdout,
            stderr,
            &std::collections::HashMap::new(),
            false,
            &ResolvedExecutionProfile::host(),
        )
        .expect("spawn with allowlist");

        let status = child.wait().await.expect("wait for child");
        // SAFETY: test runs single-threaded; no concurrent env reads.
        unsafe {
            std::env::remove_var("RUNNER_ALLOWED_TEST");
            std::env::remove_var("RUNNER_BLOCKED_TEST");
            std::env::remove_var("CLAUDECODE");
        }

        assert!(status.success());
        assert_eq!(
            std::fs::read_to_string(&stdout_path).expect("read stdout"),
            "visible|missing|missing"
        );
        let stderr_output = std::fs::read_to_string(&stderr_path).expect("read stderr");
        assert!(!stderr_output.contains("RUNNER_ALLOWED_TEST"));
        assert!(!stderr_output.contains("RUNNER_BLOCKED_TEST"));
    }

    #[test]
    fn test_spawn_with_runner_wraps_spawn_errors() {
        let temp = tempdir().expect("create tempdir");
        let stdout_path = temp.path().join("stdout.log");
        let stderr_path = temp.path().join("stderr.log");
        let stdout = File::create(&stdout_path).expect("create stdout file");
        let stderr = File::create(&stderr_path).expect("create stderr file");

        let mut runner = make_runner_config();
        runner.shell = "/definitely/missing-shell".to_string();

        let err = spawn_with_runner(
            &runner,
            "echo hello",
            temp.path(),
            stdout,
            stderr,
            &std::collections::HashMap::new(),
            false,
            &ResolvedExecutionProfile::host(),
        )
        .expect_err("missing shell should fail");
        assert!(err.to_string().contains("failed to spawn runner"));
    }

    #[tokio::test]
    async fn test_spawn_with_extra_env_injects_variables() {
        let temp = tempdir().expect("create tempdir");
        let stdout_path = temp.path().join("stdout.log");
        let stderr_path = temp.path().join("stderr.log");
        let stdout = File::create(&stdout_path).expect("create stdout file");
        let stderr = File::create(&stderr_path).expect("create stderr file");

        let runner = make_runner_config();
        let mut extra_env = std::collections::HashMap::new();
        extra_env.insert("EXTRA_TEST_VAR".to_string(), "injected_value".to_string());

        let mut child = spawn_with_runner(
            &runner,
            "printf '%s' \"${EXTRA_TEST_VAR:-missing}\"",
            temp.path(),
            stdout,
            stderr,
            &extra_env,
            false,
            &ResolvedExecutionProfile::host(),
        )
        .expect("spawn with extra env");

        let status = child.wait().await.expect("wait for child");
        assert!(status.success());
        assert_eq!(
            std::fs::read_to_string(&stdout_path).expect("read stdout"),
            "injected_value"
        );
    }

    #[tokio::test]
    async fn test_spawn_with_runner_and_capture_redacts_persisted_output() {
        let temp = tempdir().expect("create tempdir");
        let stdout_path = temp.path().join("stdout.log");
        let stderr_path = temp.path().join("stderr.log");
        let stdout = File::create(&stdout_path).expect("create stdout file");
        let stderr = File::create(&stderr_path).expect("create stderr file");

        let runner = make_runner_config();
        let captured = spawn_with_runner_and_capture(
            &runner,
            "printf 'api=sk-test-123'; printf ' secret=super-secret-value' >&2",
            temp.path(),
            stdout,
            stderr,
            vec!["sk-test-123".to_string(), "super-secret-value".to_string()],
            &std::collections::HashMap::new(),
            false,
            &ResolvedExecutionProfile::host(),
        )
        .expect("spawn with capture");
        let mut child = captured.child;
        let output_capture = captured.output_capture;

        let status = child.wait().await.expect("wait for child");
        assert!(status.success());
        output_capture
            .wait()
            .await
            .expect("wait for output capture");

        let stdout_output = std::fs::read_to_string(&stdout_path).expect("read stdout");
        let stderr_output = std::fs::read_to_string(&stderr_path).expect("read stderr");
        assert!(!stdout_output.contains("sk-test-123"));
        assert!(stdout_output.contains("[REDACTED]"));
        assert!(!stderr_output.contains("super-secret-value"));
        assert!(stderr_output.contains("[REDACTED]"));
    }

    #[test]
    fn test_sandbox_backend_label_for_current_platform() {
        let profile = ResolvedExecutionProfile::host();
        let label = sandbox_backend_label(&profile);
        assert_eq!(
            label, "host",
            "host profile should always return 'host' backend label"
        );
    }
}