cosca 0.9.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! macOS graphical elevation: the `osascript … with administrator privileges`
//! effect path for [`Auth::Gui`](super::Auth::Gui).
//!
//! Everything here is PURE — no `cfg!`, and no syscalls — so the whole module is compiled and
//! unit-tested on every platform, exactly like [`super::plan`]. The one exception is
//! [`build_shim_script`], which is `cfg(unix)` because the shim's `ShimArgs` is.
//!
//! # The two quoting layers
//!
//! `do shell script` hands its argument to `/bin/sh -c`, and the argument is itself
//! an AppleScript string literal. So a command crosses two grammars:
//!
//! 1. argv -> `/bin/sh` word-quoting ([`crate::quote::posix::join`]);
//! 2. that text -> AppleScript literal body ([`crate::quote::applescript::escape_literal`]).
//!
//! There is deliberately no third layer: the finished script is handed to
//! `osascript` as ONE `-e` argv element through `std::process::Command`, which never
//! involves a shell. Layer 2 is applied in exactly one place,
//! [`wrap_do_shell_script`].

use std::ffi::{OsStr, OsString};
use std::path::Path;

use super::{ElevatedStdio, ElevatedVia, ElevationReport, Launch};
use crate::command::{Command, CommandInput};
use crate::error::Error;
use crate::stdio::{Fd, ResolvedStdio};

fn unsupported(op: &str, detail: String) -> Error {
    Error::Unsupported {
        op: op.into(),
        platform: "macos",
        detail,
        source: None,
    }
}

/// The raw bytes of an `OsStr`. Exact on unix. Off-unix an `OsStr` is WTF-16 and
/// may hold unpaired surrogates with no byte form — a TYPED error, never a panic
/// and never a lossy substitution, per the crate's rule. (Production only ever
/// reaches this on unix, because `posix.rs` is the sole caller and is `cfg(unix)`,
/// but that is a structural coincidence, not a guarantee, and this module is
/// compiled and tested on Windows.)
fn os_bytes(s: &OsStr) -> Result<&[u8], Error> {
    #[cfg(unix)]
    {
        use std::os::unix::ffi::OsStrExt;
        Ok(s.as_bytes())
    }
    #[cfg(not(unix))]
    {
        if let Some(valid) = s.to_str() {
            return Ok(valid.as_bytes());
        }
        // A REAL offset, not a fabricated zero: `QuoteError.pos` is documented as a
        // byte offset, and the crate's other constructors all measure one. Decode
        // the WTF-16 and accumulate the UTF-8 length of everything that decoded
        // cleanly, so `pos` points at the first code unit with no byte form.
        use std::os::windows::ffi::OsStrExt;
        let pos = char::decode_utf16(s.encode_wide())
            .take_while(|c| c.is_ok())
            .map(|c| c.expect("take_while kept only Ok").len_utf8())
            .sum();
        Err(Error::Quote(crate::error::QuoteError::new(
            pos,
            crate::error::QuoteErrorKind::NonUtf8,
        )))
    }
}

/// Does `s` name a POSIX-absolute path? A leading-`/` BYTE test, deliberately not
/// `Path::is_absolute`: the target grammar is `/bin/sh`'s, not the build host's, and
/// on Windows `Path::is_absolute("/usr/bin/id")` is `false` (no drive prefix) while
/// `Path::is_absolute(r"C:\tool.exe")` is `true` — both backwards here.
fn is_posix_absolute(s: &OsStr) -> Result<bool, Error> {
    Ok(os_bytes(s)?.first() == Some(&b'/'))
}

/// The `/bin/sh` command text: an optional `cd`, then `exec` of the quoted argv.
///
/// `exec` replaces the shell so the payload is the trampoline's direct child rather
/// than sitting under an idle `sh`. The cwd is stated rather than inherited: `do
/// shell script` does not carry the caller's working directory across the
/// authorization trampoline.
///
/// Requiring the cwd to be absolute makes osascript's directory and the script's
/// `cd -P --` resolve the same PATH. `-P` makes the `cd` read it as the kernel's `chdir` does —
/// osascript's own cwd, and the unelevated spawn's: a shell's default logical `cd` takes
/// `link/..` to the directory holding `link`, not to the parent of its target, and would run
/// the payload in a different directory. It does not make them resolve the same OUTCOME:
/// the `cd` runs as root on the far side of the trampoline, so a directory the
/// caller can traverse but root cannot (NFS `root_squash`) fails there, `&&`
/// short-circuits, and the payload never runs. That surfaces as a bare non-zero
/// exit, since [`ElevatedStdio::OsascriptRelay`] never relays the payload's stderr.
///
/// Precondition: `program` and `cwd` are POSIX-absolute — enforced by
/// [`reject_structural_gui_config`].
pub(crate) fn build_shell_command(program: &OsStr, args: &[OsString], cwd: Option<&Path>) -> Result<Vec<u8>, Error> {
    debug_assert!(
        matches!(is_posix_absolute(program), Ok(true)),
        "the structural gate must reject a non-absolute program before composition"
    );
    debug_assert!(
        cwd.is_none_or(|d| matches!(is_posix_absolute(d.as_os_str()), Ok(true))),
        "the structural gate must reject a non-absolute cwd before composition"
    );
    let mut words: Vec<&[u8]> = Vec::with_capacity(args.len() + 2);
    words.push(b"exec".as_slice());
    words.push(os_bytes(program)?);
    let arg_bytes: Vec<&[u8]> = args.iter().map(|a| os_bytes(a)).collect::<Result<_, _>>()?;
    words.extend(arg_bytes.iter().copied());

    let mut out = Vec::new();
    if let Some(dir) = cwd {
        out.extend_from_slice(b"cd -P -- ");
        out.extend_from_slice(&crate::quote::posix::quote(os_bytes(dir.as_os_str())?));
        out.extend_from_slice(b" && ");
    }
    out.extend_from_slice(&crate::quote::posix::join(&words));
    Ok(out)
}

/// Wrap a `/bin/sh` command as the complete AppleScript statement.
///
/// `without altering line endings` is mandatory: without it `do shell script`
/// rewrites the payload's newlines to carriage returns before relaying them.
///
/// No `with timeout of …` clause is emitted. Per the AppleScript Language Guide,
/// `with timeout` governs commands sent to *application objects*; `do shell script`
/// under `osascript` is executed by the running script's own application, so neither
/// the default event timeout nor an explicit clause applies to it. A clause here
/// would be a silent no-op.
///
/// Non-ASCII passes through. `osascript(1)` documents no encoding for `-e`, but that
/// is a documentation gap, not a behavior gap, and it is closed by measurement:
/// `both_quoting_layers_survive_a_real_osascript_round_trip` drives CJK, combining
/// marks, astral-plane codepoints and mixed scripts through the REAL osascript on
/// macOS CI and compares byte-exact hex. Refusing the class instead would reject a
/// CJK-named file argument on a UTF-8 filesystem.
///
/// `arg_max` is the detected `kern.argmax`. The check is a LOWER bound: the script is
/// the dominant argv element of the `osascript` exec, but argv and the environment
/// share the same budget, so a script under the cap can still overflow. `None` (the
/// sysctl failed) disables the check rather than substituting a guess. Overflow is
/// [`crate::error::ElevationErrorKind::CommandTooLong`], NOT `Unsupported`: the
/// crate reserves `Unsupported` for "can never work on this platform", and this
/// verdict is derived from a runtime sysctl reading.
pub(crate) fn wrap_do_shell_script(shell_command: &[u8], arg_max: Option<usize>) -> Result<String, Error> {
    let body = crate::quote::applescript::escape_literal(shell_command).map_err(Error::Quote)?;
    let script = format!("do shell script \"{body}\" with administrator privileges without altering line endings");
    if let Some(max) = arg_max {
        if script.len() > max {
            return Err(Error::Elevation {
                kind: crate::error::ElevationErrorKind::CommandTooLong,
                detail: format!(
                    "the composed AppleScript is {} bytes; this system's kern.argmax is {max}. \
                     Shorten the command, or write the arguments to a file the elevated program reads",
                    script.len()
                ),
                source: None,
            });
        }
    }
    Ok(script)
}

/// The AppleScript that runs the elevation shim (`shim_exe`, told `shim`) in place of the program.
/// The shim's arguments are always hex ([`ShimArgs::to_argv`]), so the script is ASCII whenever
/// `shim_exe` and `cwd` are.
///
/// Precondition: `shim_exe` is POSIX-absolute and `cwd` is as [`build_shell_command`] requires.
#[cfg(unix)]
#[allow(dead_code, reason = "no caller yet")]
pub(crate) fn build_shim_script(
    shim_exe: &OsStr,
    shim: &super::shim::protocol::ShimArgs,
    cwd: Option<&Path>,
    arg_max: Option<usize>,
) -> Result<String, Error> {
    debug_assert!(
        matches!(is_posix_absolute(shim_exe), Ok(true)),
        "shim_exe must be POSIX-absolute"
    );
    let argv = shim.to_argv(shim_exe, true);
    let shell_command = build_shell_command(&argv[0], &argv[1..], cwd)?;
    wrap_do_shell_script(&shell_command, arg_max)
}

/// macOS graphical elevation's words for [`super::elevation_argv`]'s refusals.
pub(crate) const ARGV_REFUSALS: super::ArgvRefusals = super::ArgvRefusals {
    platform: "macos",
    op_prefix: "macOS graphical elevation",
    commandline: "the command must be an argv (set .args([...])); a raw command line cannot be \
                  re-quoted for /bin/sh without guessing its word boundaries",
    setter: super::SETTER_REFUSAL,
};

/// The argv, refused unless it is one `do shell script` can exec.
fn checked_argv(cmd: &Command) -> Result<&[OsString], Error> {
    super::elevation_argv(cmd, &ARGV_REFUSALS)
}

/// Program + args + the directory to run them in: argv[0], the rest, and `current_dir()` as given.
/// Reads nothing.
pub(crate) fn program_and_args(cmd: &Command) -> Result<Launch, Error> {
    let argv = checked_argv(cmd)?;
    Ok(Launch {
        program: argv[0].clone(),
        args: argv[1..].to_vec(),
        cwd: cmd.cwd().map(Path::to_path_buf),
    })
}

/// The honest capability matrix for macOS graphical elevation. Every rejection below
/// is a thing Authorization Services genuinely cannot do — reported loudly rather
/// than half-honored.
///
/// Returns `Error::Unsupported { platform: "macos", .. }` for a capability mismatch,
/// and `Error::Quote(NonUtf8)` for a program or directory with no byte form
/// (reachable only off-unix, where an `OsStr` is WTF-16). Both are typed; neither is
/// a panic.
/// Reads nothing: an already-root caller runs no osascript, and needs no path to its cwd.
pub(crate) fn reject_structural_gui_config(cmd: &Command) -> Result<(), Error> {
    // First, so a program set by `executable()` or `raw_executable()` is refused whatever else is
    // wrong, as on every backend.
    let argv = checked_argv(cmd)?;
    // The caller's cwd is applied twice — to osascript, and as `cd -P --` inside the
    // script — and the trampoline does not carry a cwd across, so a RELATIVE path
    // resolves against two different bases and the two silently disagree. Absolute
    // makes them name the same directory.
    if let Some(dir) = cmd.cwd() {
        if !is_posix_absolute(dir.as_os_str())? {
            return Err(unsupported(
                "macOS graphical elevation with a relative current_dir()",
                format!(
                    "{dir:?} would resolve against this process's directory for osascript but \
                     against whatever the authorization trampoline hands the elevated shell for \
                     the command itself; pass an absolute path"
                ),
            ));
        }
    }
    // root's /bin/sh resolves a bare name against ITS OWN PATH, so a relative
    // program would let the environment choose which binary runs as root. The crate
    // closes the same hole for its POSIX backends by carrying absolute paths.
    let program = &argv[0];
    if !is_posix_absolute(program)? {
        return Err(unsupported(
            "macOS graphical elevation of a non-absolute program",
            format!(
                "{program:?} would be resolved by the elevated shell's own PATH, not the caller's, \
                 so the binary that runs as root is not the one you selected; pass an absolute path"
            ),
        ));
    }
    for (&slot, resolved) in cmd.fds() {
        if slot.raw() >= 3 {
            return Err(unsupported(
                "fd >= 3 on a macOS graphically-elevated child",
                "the authorization trampoline passes no descriptors, so fd >= 3 cannot reach the \
                 elevated program"
                    .into(),
            ));
        }
        // fd1/fd2 are osascript's own streams and carry the relay, so any resolution
        // is honest there (see ElevatedStdio::OsascriptRelay). fd0 is not: osascript
        // never reads it and the elevated program never sees it, so anything with
        // content behind it silently goes nowhere. Written as an ALLOW-list so a
        // future `ResolvedStdio` variant (or `Stdio::merge`, which resolves to
        // `Merge` and would otherwise slip through) defaults to a loud rejection.
        if slot == Fd::STDIN && !matches!(resolved, ResolvedStdio::Inherit | ResolvedStdio::Null) {
            return Err(unsupported(
                "a pipe or file on stdin for a macOS graphically-elevated child",
                "the elevated program's stdin comes from Authorization Services; osascript never \
                 reads fd0, so anything written there would be silently discarded. Use null() or \
                 inherit()"
                    .into(),
            ));
        }
    }
    if !cmd.env_ops().is_empty() {
        return Err(unsupported(
            "env forwarding to a macOS graphically-elevated child",
            "the authorization trampoline builds the root environment; the only way to pass a \
             variable would be an assignment in the command text, which is visible to anyone \
             inspecting the running processes"
                .into(),
        ));
    }
    if cmd.contain_request().mode.is_some() {
        return Err(unsupported(
            ".contain() + macOS graphical elevation",
            "the elevated program runs under the authorization trampoline, parented to launchd \
             and outside this process's tree; containment cannot span it"
                .into(),
        ));
    }
    // kill_on_drop is deliberately NOT rejected. `Command::default()` sets it to
    // `true` and offers no way to distinguish that default from an explicit request,
    // so rejecting it would reject every default-constructed command and make this
    // path unreachable. What a kill and a drop do to it (nothing: osascript is a
    // front) is documented on `ElevatedVia::MacosOsascript` instead.
    Ok(())
}

/// What a graphically elevated child reports.
pub(crate) fn osascript_report() -> ElevationReport {
    ElevationReport {
        via: ElevatedVia::MacosOsascript,
        // Nothing is forwarded (env ops are rejected outright), so the sanitizer has
        // nothing to strip. Reporting an empty list is the truth, not a stub.
        stripped_env: Vec::new(),
        stdio: ElevatedStdio::OsascriptRelay,
    }
}

/// Build the DERIVED command (`osascript -e <script>`) carrying `report`. The caller's `Command`
/// keeps its `input`/`env_ops`; its fd 0-2 stdio is MOVED (`ResolvedStdio::File` is not `Clone`),
/// as [`Command::derive_for_elevation`] does for every arm.
///
/// Precondition: [`reject_structural_gui_config`] passed and `launch` is [`program_and_args`]'s,
/// so the program and cwd are absolute, and there are no env ops and no containment. `kill_on_drop`
/// is NOT gated (see that function), so the derived command carries it.
pub(crate) fn build_rewrite(
    cmd: &mut Command,
    launch: Launch,
    osascript: &Path,
    arg_max: Option<usize>,
    report: ElevationReport,
) -> Result<Command, Error> {
    // Contracts the upstream gate owns, mirroring `build_shell_command`'s own
    // precondition asserts. Zero-cost in release; a loud failure in debug if a
    // second call site ever reaches here without passing the gate.
    debug_assert!(
        cmd.env_ops().is_empty(),
        "reject_structural_gui_config must reject env ops before build_rewrite"
    );
    debug_assert!(
        cmd.contain_request().mode.is_none(),
        "reject_structural_gui_config must reject containment before build_rewrite"
    );
    debug_assert!(
        cmd.fds().keys().all(|s| s.raw() < 3),
        "reject_structural_gui_config must reject fd >= 3 before build_rewrite"
    );
    let Launch { program, args, cwd } = launch;
    let shell_command = build_shell_command(&program, &args, cwd.as_deref())?;
    let script = wrap_do_shell_script(&shell_command, arg_max)?;

    // The derived command carries the cwd AS WELL AS the script stating it: a bogus
    // directory becomes a precise spawn-time `Io` error instead of an opaque non-zero
    // exit, and the script's `cd -P --` makes the payload's cwd deterministic either
    // way. The two name the same directory by construction. It carries kill_on_drop too:
    // the async spawn recurses with `spawn(&mut derived)` and re-reads the flag off the
    // DERIVED command, so a caller's `.kill_on_drop(false)` must reach it.
    let derived = cmd.derive_for_elevation(
        CommandInput::Argv(vec![
            osascript.as_os_str().to_os_string(),
            OsString::from("-e"),
            OsString::from(script),
        ]),
        Vec::new(),
        report,
    );
    // When kill_on_drop is set, SAY SO — the default must not be silent (see
    // `ElevatedVia::MacosOsascript` for why nothing can kill the payload). The program is
    // named so a test can assert on its OWN record in the process-global capture buffer.
    if cmd.kill_on_drop_flag() {
        log::warn!(
            "kill_on_drop is set on a macOS graphically-elevated child ({program:?}), which cannot be \
             killed: dropping it early leaves the root program and its osascript front-end running, \
             with the program's exit status unobservable. Call .kill_on_drop(false) and wait() if that \
             matters."
        );
    }
    Ok(derived)
}

#[cfg(test)]
#[path = "macos_tests.rs"]
mod macos_tests;