merry-process 0.1.3

Sandboxed host-process execution (Landlock/seccomp, Seatbelt, Windows Job Objects) for Merry.
Documentation
//! Unsupported-platform stub for the workspace sandbox boundary.
//!
//! Linux (Landlock + seccomp) and macOS (Seatbelt) use `confinement.rs`.
//! Targets without a native kernel workspace sandbox (such as Windows) still
//! expose the helper and probe entrypoints and fail closed if workspace
//! isolation is requested.

use std::env;

const HELPER_ENV: &str = "MERRY_SANDBOX_HELPER";
const POLICY_ENV: &str = "MERRY_SANDBOX_POLICY";
const SETUP_EXIT_CODE: i32 = 126;

/// Reports whether this process was spawned as the sandbox helper.
#[must_use]
pub fn sandbox_helper_requested() -> bool {
    env::var_os(HELPER_ENV).is_some() && env::var_os(POLICY_ENV).is_some()
}

/// Reports that the workspace sandbox helper is unavailable in this build or
/// on this target and exits with the sandbox setup failure code.
pub fn sandbox_helper_main() -> ! {
    eprintln!("merry-sandbox: {}", unavailable_reason());
    std::process::exit(SETUP_EXIT_CODE);
}

/// Reports that kernel workspace confinement is unavailable in this build or on
/// this target.
pub fn probe_workspace_sandbox() -> Result<(), String> {
    Err(unavailable_reason())
}

fn unavailable_reason() -> String {
    if !cfg!(feature = "sandbox") && cfg!(any(target_os = "linux", target_os = "macos")) {
        "workspace sandbox is disabled in this build (compiled without the `sandbox` feature)"
            .to_owned()
    } else {
        format!(
            "workspace sandbox is unavailable on {}",
            std::env::consts::OS
        )
    }
}

pub(crate) fn setup_failure(stderr: &[u8]) -> Option<String> {
    let text = String::from_utf8_lossy(stderr);
    text.lines()
        .find(|line| line.starts_with("merry-sandbox:"))
        .map(|line| line.trim().to_owned())
}

pub(crate) async fn spawn_sandboxed_browser(
    _executable: &std::path::Path,
    _args: &[String],
    _profile_dir: &std::path::Path,
    _denied_paths: &[std::path::PathBuf],
    _env_isolation: &crate::ProcessEnvIsolation,
) -> Result<tokio::process::Child, merry_runtime::ProcessRunnerError> {
    Err(merry_runtime::ProcessRunnerError::infrastructure(
        unavailable_reason(),
    ))
}