Skip to content

Commit 89f8783

Browse files
committed
fix(sandbox): resolve the default login shell in the supervisor (empty command = default)
Addresses review: interactive PTY SSH now uses the detected shell, the shell tests are portable across the Windows lane, and default-shell provenance is carried without a new spec field. An omitted command is left empty end to end and resolved in the supervisor, which is the only place that sees the sandbox image: - The CLI forwards the command as-is; the gateway persists an omitted command as empty (no baked /bin/bash -l) and requests a TTY. - MainProcessConfig carries the command empty (the transport now allows it); the supervisor resolves a login shell that exists in the sandbox image (bash when present, otherwise /bin/sh on minimal images like Alpine) and logs the resolved shell. - Interactive PTY SSH (spawn_pty_shell) uses the detected shell; a shared build_ssh_shell_command helper covers the PTY and non-PTY paths, with a deterministic sh-only regression test. - Unix-only shell tests are gated with cfg(unix). An explicit command is always run verbatim. Refs #3146 Signed-off-by: Akram <[email protected]>
1 parent c37f287 commit 89f8783

9 files changed

Lines changed: 182 additions & 107 deletions

File tree

‎crates/openshell-cli/src/run.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -593,11 +593,11 @@ pub async fn sandbox_create(
593593

594594
let main_terminal = tty_override
595595
.unwrap_or_else(|| std::io::stdin().is_terminal() && std::io::stdout().is_terminal());
596-
let main_command = if command.is_empty() {
597-
vec!["/bin/bash".to_string(), "-l".to_string()]
598-
} else {
599-
command.to_vec()
600-
};
596+
// Forward the command as-is. When empty, the gateway persists it empty and
597+
// the supervisor resolves the default login shell against the sandbox image
598+
// (bash when present, otherwise /bin/sh on minimal images like Alpine).
599+
// Baking a shell here would force a shell the image may not ship.
600+
let main_command = command.to_vec();
601601
let persist = sandbox_should_persist(keep, forward.as_ref());
602602
let create_detaches = detach
603603
|| (persist

‎crates/openshell-core/src/sandbox_env.rs‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ const MAIN_PROCESS_SPEC_BASE64URL_PREFIX: &str = "base64url:";
3838
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
3939
pub struct MainProcessConfig {
4040
pub version: u32,
41+
/// Canonical command. Empty means "no command supplied": the supervisor
42+
/// resolves the default login shell against the sandbox image. A non-empty
43+
/// command is the exact program+args and is run verbatim.
4144
pub command: Vec<String>,
4245
pub tty: bool,
4346
#[serde(default)]
@@ -47,11 +50,15 @@ pub struct MainProcessConfig {
4750
impl MainProcessConfig {
4851
pub const VERSION: u32 = 1;
4952

53+
/// Default config for a sandbox created without a command. The command is
54+
/// left empty on purpose: the supervisor picks a login shell that exists in
55+
/// the sandbox image (bash when present, otherwise `/bin/sh`). A TTY is
56+
/// requested because the default is an interactive login shell.
5057
#[must_use]
5158
pub fn scratch() -> Self {
5259
Self {
5360
version: Self::VERSION,
54-
command: vec!["/bin/bash".to_string(), "-l".to_string()],
61+
command: Vec::new(),
5562
tty: true,
5663
await_main_process_attachment: false,
5764
}
@@ -91,8 +98,13 @@ impl MainProcessConfig {
9198
config.version
9299
));
93100
}
94-
if config.command.is_empty() || config.command[0].is_empty() {
95-
return Err(format!("{MAIN_PROCESS_SPEC} command must not be empty"));
101+
// An empty command is valid: it means "no command supplied", and the
102+
// supervisor resolves the default login shell. Only a present-but-blank
103+
// program is rejected.
104+
if !config.command.is_empty() && config.command[0].is_empty() {
105+
return Err(format!(
106+
"{MAIN_PROCESS_SPEC} command program must not be empty"
107+
));
96108
}
97109
Ok(config)
98110
}
@@ -283,4 +295,40 @@ mod tests {
283295
let encoded = serde_json::to_string(&config).unwrap();
284296
assert_eq!(MainProcessConfig::decode(&encoded).unwrap(), config);
285297
}
298+
299+
#[test]
300+
fn omitted_command_stays_empty_for_supervisor_resolution() {
301+
// No command supplied → empty command; the supervisor resolves the
302+
// default login shell against the sandbox image.
303+
let empty = crate::proto::compute::v1::DriverSandboxSpec::default();
304+
assert!(
305+
MainProcessConfig::from_driver_spec(Some(&empty))
306+
.command
307+
.is_empty()
308+
);
309+
assert!(MainProcessConfig::from_driver_spec(None).command.is_empty());
310+
311+
// An explicit command is preserved verbatim and never rewritten.
312+
let explicit = crate::proto::compute::v1::DriverSandboxSpec {
313+
command: vec!["/bin/bash".into(), "-l".into()],
314+
tty: true,
315+
..Default::default()
316+
};
317+
assert_eq!(
318+
MainProcessConfig::from_driver_spec(Some(&explicit)).command,
319+
vec!["/bin/bash".to_string(), "-l".to_string()]
320+
);
321+
322+
// An empty command survives the transport round-trip.
323+
let encoded = serde_json::to_string(&MainProcessConfig::scratch()).unwrap();
324+
assert!(
325+
MainProcessConfig::decode(&encoded)
326+
.unwrap()
327+
.command
328+
.is_empty()
329+
);
330+
331+
// A present-but-blank program is still rejected.
332+
assert!(MainProcessConfig::decode(r#"{"version":1,"command":[""],"tty":false}"#).is_err());
333+
}
286334
}

‎crates/openshell-core/src/shell.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ pub fn detect_login_shell() -> String {
7474
mod tests {
7575
use super::*;
7676

77+
// These assume a Unix root filesystem (`/bin/sh`, POSIX permission bits) and
78+
// are skipped on the Windows workspace lane where openshell-core also builds.
79+
#[cfg(unix)]
7780
#[test]
7881
fn posix_sh_exists_on_test_host() {
79-
// /bin/sh is present on all supported CI hosts (Linux and macOS).
82+
// /bin/sh is present on all supported Unix CI hosts (Linux and macOS).
8083
assert!(is_executable(POSIX_SH));
8184
}
8285

@@ -85,12 +88,14 @@ mod tests {
8588
assert!(!is_executable("/nonexistent/definitely/not/here"));
8689
}
8790

91+
#[cfg(unix)]
8892
#[test]
8993
fn non_file_is_not_executable() {
9094
// A directory is not an executable shell.
9195
assert!(!is_executable("/"));
9296
}
9397

98+
#[cfg(unix)]
9499
#[test]
95100
fn detect_returns_an_executable_shell() {
96101
let shell = detect_login_shell();

‎crates/openshell-driver-docker/src/tests.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,9 @@ fn build_environment_sets_docker_tls_paths() {
10921092
})
10931093
.expect("main-process transport");
10941094
let main = openshell_core::sandbox_env::MainProcessConfig::decode(&encoded).unwrap();
1095-
assert_eq!(main.command, vec!["/bin/bash", "-l"]);
1095+
// An omitted command is forwarded empty; the supervisor resolves the default
1096+
// login shell against the sandbox image at startup.
1097+
assert!(main.command.is_empty());
10961098
assert!(main.tty);
10971099
}
10981100

‎crates/openshell-sandbox/src/main.rs‎

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -661,43 +661,31 @@ fn main() -> Result<()> {
661661
// drivers otherwise provide a versioned JSON transport so argument
662662
// boundaries are never reconstructed with shell parsing.
663663
let workdir = args.workdir.clone();
664-
let (command, interactive, await_main_process_attachment, is_default_command) =
665-
if !args.command.is_empty() {
666-
(args.command, args.interactive, false, false)
667-
} else if let Ok(json) = std::env::var(openshell_core::sandbox_env::MAIN_PROCESS_SPEC) {
668-
let config = openshell_core::sandbox_env::MainProcessConfig::decode(&json)
669-
.map_err(|error| miette::miette!("{error}"))?;
670-
// The driver bakes the built-in default (`scratch`) into the spec
671-
// when the user supplies no command. Detect that case so the
672-
// default shell can be resolved against the sandbox image below.
673-
let is_default =
674-
config == openshell_core::sandbox_env::MainProcessConfig::scratch();
675-
(
676-
config.command,
677-
config.tty,
678-
config.await_main_process_attachment,
679-
is_default,
680-
)
681-
} else {
682-
let config = openshell_core::sandbox_env::MainProcessConfig::scratch();
683-
(
684-
config.command,
685-
config.tty,
686-
config.await_main_process_attachment,
687-
true,
688-
)
689-
};
690-
691-
// The built-in default command is a login shell (`/bin/bash -l`), but
692-
// minimal images (e.g. Alpine) don't ship bash. Only the default is
693-
// remapped — an explicit user command is never rewritten. This runs in
694-
// the supervisor, so it resolves against the sandbox image's filesystem.
695-
let command = if is_default_command {
696-
resolve_default_shell_command(command)
664+
let (command, interactive, await_main_process_attachment) = if !args.command.is_empty() {
665+
(args.command, args.interactive, false)
666+
} else if let Ok(json) = std::env::var(openshell_core::sandbox_env::MAIN_PROCESS_SPEC) {
667+
let config = openshell_core::sandbox_env::MainProcessConfig::decode(&json)
668+
.map_err(|error| miette::miette!("{error}"))?;
669+
(
670+
config.command,
671+
config.tty,
672+
config.await_main_process_attachment,
673+
)
697674
} else {
698-
command
675+
let config = openshell_core::sandbox_env::MainProcessConfig::scratch();
676+
(
677+
config.command,
678+
config.tty,
679+
config.await_main_process_attachment,
680+
)
699681
};
700682

683+
// An omitted command (the gateway leaves the default empty rather than
684+
// baking a shell it cannot verify) is resolved to a login shell here, in
685+
// the supervisor, so it matches the sandbox image: bash when present,
686+
// otherwise /bin/sh (e.g. Alpine). An explicit command is used verbatim.
687+
let command = resolve_default_command(command);
688+
701689
info!(command = ?command, "Starting sandbox");
702690
// Note: "Starting sandbox" stays as plain info!() since the OCSF context
703691
// is not yet initialized at this point (run_sandbox hasn't been called).
@@ -738,26 +726,18 @@ fn main() -> Result<()> {
738726
std::process::exit(exit_code);
739727
}
740728

741-
/// Remap the built-in default shell command to a shell that exists in the
742-
/// sandbox image. The default program (`/bin/bash`) is absent on minimal images
743-
/// such as Alpine; falling back to a detected login shell lets the sandbox
744-
/// start instead of failing with an opaque `No such file or directory`. Only the
745-
/// built-in default is remapped — explicit user commands never reach this path.
746-
fn resolve_default_shell_command(mut command: Vec<String>) -> Vec<String> {
747-
let Some(program) = command.first().cloned() else {
748-
return command;
749-
};
750-
if openshell_core::shell::is_executable(&program) {
729+
/// Resolve an omitted canonical command to a login shell that exists in this
730+
/// sandbox image. Empty means "use the default": the gateway leaves an omitted
731+
/// command empty rather than persisting a shell it cannot verify, so the
732+
/// supervisor picks one here against the real sandbox filesystem (bash when
733+
/// present, otherwise `/bin/sh`). An explicit command is returned unchanged.
734+
fn resolve_default_command(command: Vec<String>) -> Vec<String> {
735+
if !command.is_empty() {
751736
return command;
752737
}
753738
let shell = openshell_core::shell::detect_login_shell();
754-
warn!(
755-
missing = %program,
756-
fallback = %shell,
757-
"default shell not found in sandbox image; falling back to a detected shell"
758-
);
759-
command[0] = shell;
760-
command
739+
info!(shell = %shell, "no command specified; resolved default login shell");
740+
vec![shell, "-l".to_string()]
761741
}
762742

763743
#[cfg(test)]

‎crates/openshell-server/src/grpc/sandbox.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,12 @@ async fn handle_create_sandbox_inner(
297297
(resolved, Some(provenance))
298298
};
299299

300-
// Every newly persisted sandbox has one explicit canonical process. This
301-
// portable default also preserves compatibility with callers compiled
302-
// before the main-process field was introduced.
300+
// Leave an omitted command empty rather than persisting a concrete shell:
301+
// the supervisor resolves the default login shell against the sandbox image
302+
// (bash when present, otherwise /bin/sh on minimal images like Alpine),
303+
// which the gateway cannot do since it does not see the sandbox filesystem.
304+
// The default is an interactive login shell, so request a TTY.
303305
if spec.command.is_empty() {
304-
spec.command = vec!["/bin/bash".to_string(), "-l".to_string()];
305306
spec.tty = true;
306307
}
307308

‎crates/openshell-supervisor-process/src/process.rs‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -910,16 +910,12 @@ impl ProcessHandle {
910910
#[cfg(target_os = "linux")]
911911
let mut child = spawn_command_with_supervisor_identity_namespace(cmd)
912912
.into_diagnostic()
913-
.wrap_err_with(|| {
914-
format!("failed to spawn sandbox entrypoint process '{program}'")
915-
})?;
913+
.wrap_err_with(|| format!("failed to spawn sandbox entrypoint process '{program}'"))?;
916914
#[cfg(not(target_os = "linux"))]
917915
let mut child = cmd
918916
.spawn()
919917
.into_diagnostic()
920-
.wrap_err_with(|| {
921-
format!("failed to spawn sandbox entrypoint process '{program}'")
922-
})?;
918+
.wrap_err_with(|| format!("failed to spawn sandbox entrypoint process '{program}'"))?;
923919
let pid = child.id().unwrap_or(0);
924920
managed_children::register(pid);
925921

@@ -2486,7 +2482,10 @@ mod tests {
24862482
Some(&current_user.dir.to_string_lossy().as_ref())
24872483
);
24882484
assert_eq!(variables.get("USER"), Some(&current_user.name.as_str()));
2489-
assert_eq!(variables.get("SHELL"), Some(&"/bin/bash"));
2485+
// SHELL is the shell detected in the current root filesystem, not a
2486+
// hardcoded path (bash-less images resolve to /bin/sh).
2487+
let expected_shell = openshell_core::shell::detect_login_shell();
2488+
assert_eq!(variables.get("SHELL"), Some(&expected_shell.as_str()));
24902489
assert_eq!(variables.get("TERM"), Some(&"xterm-256color"));
24912490
}
24922491

0 commit comments

Comments
 (0)