cosca 0.5.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
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
407
408
409
410
411
412
413
414
415
416
417
418
419
//! 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 beyond reading this process's cwd for a
//! relative `raw_executable()` with no absolute `current_dir()` (see
//! [`program_and_args`]) — 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, PathBuf};

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

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

/// 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 a
/// different `./tool`. 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: `cwd` is POSIX-absolute, and so is `program` unless `cwd` is set — enforced by
/// [`reject_structural_gui_config`] and [`program_and_args`].
pub(crate) fn build_shell_command(program: &OsStr, args: &[OsString], cwd: Option<&Path>) -> Result<Vec<u8>, Error> {
    debug_assert!(
        cwd.is_some() || matches!(is_posix_absolute(program), Ok(true)),
        "a relative program needs the directory it is read against"
    );
    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()
                ),
            });
        }
    }
    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)
}

/// The argv, refused unless it is one `do shell script` can exec. `exec`ing the program sets
/// argv[0] to its own path, so an argv[0] distinct from a set `executable()` cannot survive.
fn checked_argv(cmd: &Command) -> Result<&[OsString], Error> {
    super::elevation_argv(
        cmd,
        &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",
            argv0: "`do shell script` execs the program, which sets argv[0] to its own path; a separate \
                    argv[0] cannot survive elevation",
        },
    )
}

/// Program + args + the directory to run them in, honoring `executable()`. A `raw_executable()`
/// program comes back as [`crate::resolve::exact::enter_posix`] gives it: the absolute
/// directory the script `cd`s to, reading `process_cwd` at most once, and the name to exec there.
///
/// The directory itself is still a path, because root's shell starts wherever the trampoline
/// puts it: a rename of one of its ancestors between this call and the `cd` — a window that spans
/// authentication — moves both the file and the directory together. Nothing can change the file
/// between the `cd` and the exec.
pub(crate) fn program_and_args(
    cmd: &Command,
    process_cwd: impl FnOnce() -> std::io::Result<PathBuf>,
) -> Result<Launch, Error> {
    let argv = checked_argv(cmd)?;
    let as_given = || cmd.cwd().map(Path::to_path_buf);
    let (program, cwd) = match cmd.executable_spec() {
        Some(crate::command::ExecutableSpec::Exact(p)) => {
            let entered = crate::resolve::exact::enter_posix(
                p.as_os_str(),
                cmd.cwd(),
                crate::command::explain_cwd_read(process_cwd),
            )?;
            (entered.program, entered.dir)
        }
        Some(crate::command::ExecutableSpec::Search(p)) => (p.clone(), as_given()),
        None => (PathBuf::from(&argv[0]), as_given()),
    };
    Ok(Launch {
        program: program.into_os_string(),
        args: argv[1..].to_vec(),
        cwd,
    })
}

/// 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> {
    // 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"
                ),
            ));
        }
    }
    let argv = checked_argv(cmd)?;
    // A `raw_executable()` program is completed to an absolute path at build time, so only its
    // shape can be judged here.
    let program = match cmd.executable_spec() {
        Some(crate::command::ExecutableSpec::Exact(p)) => {
            crate::resolve::exact::refuse_unnameable(p.as_os_str())?;
            None
        }
        _ => Some(cmd.executable_path().map_or(argv[0].as_os_str(), |p| p.as_os_str())),
    };
    // 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.
    if let Some(program) = program {
        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(())
}

/// Build the DERIVED command (`osascript -e <script>`) plus the report to attach to
/// the resulting child. The caller's `Command` keeps its `input`/`env_ops`; its fd
/// 0-2 stdio is MOVED (`ResolvedStdio::File` is not `Clone`), matching the POSIX
/// rewrite's contract.
///
/// 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 it is transferred like the POSIX rewrite
/// transfers it.
pub(crate) fn build_rewrite(
    cmd: &mut Command,
    launch: Launch,
    osascript: &Path,
    arg_max: Option<usize>,
) -> Result<(Command, ElevationReport), 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)?;

    let mut derived = Command::new();
    derived.set_input_argv(vec![
        osascript.as_os_str().to_os_string(),
        OsString::from("-e"),
        OsString::from(script),
    ]);
    // The cwd is set on osascript AS WELL AS stated in the script: setting it here
    // turns a bogus directory into 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.
    if let Some(d) = cwd {
        derived.current_dir(d);
    }
    // kill_on_drop MUST be carried, exactly as `posix::transfer_process_attrs`
    // carries it: the async spawn recurses with `spawn(&mut derived)` and re-reads
    // the flag off the DERIVED command, so leaving it at `Command::default()`'s
    // `true` would silently discard a caller's `.kill_on_drop(false)` on the tokio
    // path while honoring it on the sync one.
    derived.kill_on_drop(cmd.kill_on_drop_flag());
    // …and when it 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."
        );
    }
    // Containment IS rejected by the structural gate, so there is nothing else to carry.
    for (slot, resolved) in std::mem::take(cmd.fds_mut()) {
        derived.fds_mut().insert(slot, resolved);
    }

    let report = 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,
    };
    Ok((derived, report))
}

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