|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Login-shell resolution for sandbox images. |
| 5 | +//! |
| 6 | +//! The default sandbox command and the interactive SSH session need a shell, |
| 7 | +//! but not every base image ships the same one. Debian-based images provide |
| 8 | +//! `bash`; minimal images such as Alpine only provide `/bin/sh` (`BusyBox` |
| 9 | +//! `ash`). Hard-coding `/bin/bash` makes sandbox startup fail on those images |
| 10 | +//! with an opaque `No such file or directory`. |
| 11 | +//! |
| 12 | +//! These helpers resolve a shell that actually exists in the current root |
| 13 | +//! filesystem. They must run inside the sandbox (i.e. in the supervisor), not |
| 14 | +//! on the gateway, because the answer depends on the sandbox image's contents. |
| 15 | +
|
| 16 | +/// Preferred interactive shell when the image provides it. |
| 17 | +pub const BASH: &str = "/bin/bash"; |
| 18 | + |
| 19 | +/// Preferred interactive shell on `usr`-merged images where `/bin` is not a |
| 20 | +/// top-level directory. |
| 21 | +pub const USR_BASH: &str = "/usr/bin/bash"; |
| 22 | + |
| 23 | +/// POSIX shell. Guaranteed on virtually every image, including Alpine/`BusyBox`. |
| 24 | +/// Used as the ultimate fallback. |
| 25 | +pub const POSIX_SH: &str = "/bin/sh"; |
| 26 | + |
| 27 | +/// Shell paths tried, in preference order, by [`detect_login_shell`]. |
| 28 | +pub const SHELL_CANDIDATES: &[&str] = &[BASH, USR_BASH, POSIX_SH]; |
| 29 | + |
| 30 | +/// Return `true` if `path` is a regular, executable file in the current root |
| 31 | +/// filesystem. |
| 32 | +#[must_use] |
| 33 | +pub fn is_executable(path: &str) -> bool { |
| 34 | + let Ok(meta) = std::fs::metadata(path) else { |
| 35 | + return false; |
| 36 | + }; |
| 37 | + if !meta.is_file() { |
| 38 | + return false; |
| 39 | + } |
| 40 | + #[cfg(unix)] |
| 41 | + { |
| 42 | + use std::os::unix::fs::PermissionsExt; |
| 43 | + meta.permissions().mode() & 0o111 != 0 |
| 44 | + } |
| 45 | + #[cfg(not(unix))] |
| 46 | + { |
| 47 | + true |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/// Resolve a login shell that exists in the current root filesystem. |
| 52 | +/// |
| 53 | +/// Tries [`SHELL_CANDIDATES`] in order and falls back to [`POSIX_SH`]. Because |
| 54 | +/// this inspects the filesystem, call it from the supervisor (inside the |
| 55 | +/// sandbox), never on the gateway. |
| 56 | +/// |
| 57 | +/// `$SHELL` is intentionally not consulted: it is image/user-controlled, the |
| 58 | +/// result is later invoked with `-lc`, and an executable that is not a |
| 59 | +/// compatible shell (e.g. `SHELL=/bin/false`) would pass the executable check |
| 60 | +/// yet break command execution. Resolving only from known shell paths avoids |
| 61 | +/// that footgun. |
| 62 | +#[must_use] |
| 63 | +pub fn detect_login_shell() -> String { |
| 64 | + SHELL_CANDIDATES |
| 65 | + .iter() |
| 66 | + .find(|candidate| is_executable(candidate)) |
| 67 | + .map_or_else( |
| 68 | + || POSIX_SH.to_string(), |
| 69 | + |candidate| (*candidate).to_string(), |
| 70 | + ) |
| 71 | +} |
| 72 | + |
| 73 | +#[cfg(test)] |
| 74 | +mod tests { |
| 75 | + use super::*; |
| 76 | + |
| 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)] |
| 80 | + #[test] |
| 81 | + fn posix_sh_exists_on_test_host() { |
| 82 | + // /bin/sh is present on all supported Unix CI hosts (Linux and macOS). |
| 83 | + assert!(is_executable(POSIX_SH)); |
| 84 | + } |
| 85 | + |
| 86 | + #[test] |
| 87 | + fn missing_path_is_not_executable() { |
| 88 | + assert!(!is_executable("/nonexistent/definitely/not/here")); |
| 89 | + } |
| 90 | + |
| 91 | + #[cfg(unix)] |
| 92 | + #[test] |
| 93 | + fn non_file_is_not_executable() { |
| 94 | + // A directory is not an executable shell. |
| 95 | + assert!(!is_executable("/")); |
| 96 | + } |
| 97 | + |
| 98 | + #[cfg(unix)] |
| 99 | + #[test] |
| 100 | + fn detect_returns_an_executable_shell() { |
| 101 | + let shell = detect_login_shell(); |
| 102 | + assert!( |
| 103 | + is_executable(&shell), |
| 104 | + "detected shell {shell} is not executable" |
| 105 | + ); |
| 106 | + } |
| 107 | +} |
0 commit comments