Skip to main content

aube_scripts/
lib.rs

1//! Lifecycle script runner for aube.
2//!
3//! **Security model**:
4//! - Scripts from the **root package** (the project's own `package.json`)
5//!   run by default. They're written by the user, so they're trusted the
6//!   same way a user trusts `aube run <script>`.
7//! - Scripts from **installed dependencies** (e.g. `node-gyp` postinstall
8//!   from a native module) are SKIPPED by default. A package runs its
9//!   lifecycle scripts only if the active [`BuildPolicy`] allows it —
10//!   configured via `pnpm.allowBuilds` in `package.json`, `allowBuilds`
11//!   in `pnpm-workspace.yaml`, or the escape-hatch
12//!   `--dangerously-allow-all-builds` flag.
13//! - `--ignore-scripts` forces everything off, matching pnpm/npm.
14
15pub mod policy;
16
17pub use policy::{AllowDecision, BuildPolicy, BuildPolicyError};
18
19use aube_manifest::PackageJson;
20use std::path::{Path, PathBuf};
21
22/// Settings that affect every package-script shell aube spawns.
23#[derive(Debug, Clone, Default)]
24pub struct ScriptSettings {
25    pub node_options: Option<String>,
26    pub script_shell: Option<PathBuf>,
27    pub unsafe_perm: Option<bool>,
28    pub shell_emulator: bool,
29}
30
31static SCRIPT_SETTINGS: std::sync::OnceLock<std::sync::RwLock<ScriptSettings>> =
32    std::sync::OnceLock::new();
33
34fn script_settings_lock() -> &'static std::sync::RwLock<ScriptSettings> {
35    SCRIPT_SETTINGS.get_or_init(|| std::sync::RwLock::new(ScriptSettings::default()))
36}
37
38/// Replace the process-wide script settings snapshot. CLI commands call
39/// this after resolving `.npmrc` / workspace settings for the active
40/// project.
41pub fn set_script_settings(settings: ScriptSettings) {
42    *script_settings_lock()
43        .write()
44        .expect("script settings lock poisoned") = settings;
45}
46
47fn script_settings() -> ScriptSettings {
48    script_settings_lock()
49        .read()
50        .expect("script settings lock poisoned")
51        .clone()
52}
53
54/// Prepend `bin_dir` to the current `PATH` using the platform's path
55/// separator (`:` on Unix, `;` on Windows).
56pub fn prepend_path(bin_dir: &Path) -> std::ffi::OsString {
57    let path = std::env::var_os("PATH").unwrap_or_default();
58    let mut entries = vec![bin_dir.to_path_buf()];
59    entries.extend(std::env::split_paths(&path));
60    std::env::join_paths(entries).unwrap_or(path)
61}
62
63/// Spawn a shell command line. On Unix we go through `sh -c`, on
64/// Windows through `cmd.exe /d /s /c` — matching what npm passes in
65/// `@npmcli/run-script`.
66///
67/// On Windows, the script command line is appended with
68/// [`std::os::windows::process::CommandExt::raw_arg`] instead of
69/// the normal `.arg()` path. `.arg()` would run the string through
70/// Rust's `CommandLineToArgvW`-oriented encoder, which wraps it in
71/// `"..."` and escapes interior `"` as `\"` — but `cmd.exe` parses
72/// command lines with a different set of rules and does not
73/// understand `\"`, so a script like
74/// `node -e "require('is-odd')(3)"` arrives mangled. `raw_arg`
75/// hands the command line to `CreateProcessW` verbatim, so we
76/// control the exact bytes cmd.exe sees. We wrap the whole script
77/// in an outer pair of double quotes, which `/s` tells cmd.exe to
78/// strip (just those outer quotes — the rest of the string is
79/// preserved literally). This is the same trick
80/// `@npmcli/run-script` and `node-cross-spawn` use.
81pub fn spawn_shell(script_cmd: &str) -> tokio::process::Command {
82    let settings = script_settings();
83    #[cfg(unix)]
84    {
85        let mut cmd = tokio::process::Command::new(
86            settings
87                .script_shell
88                .as_deref()
89                .unwrap_or_else(|| Path::new("sh")),
90        );
91        cmd.arg("-c").arg(script_cmd);
92        apply_script_settings_env(&mut cmd, &settings);
93        cmd
94    }
95    #[cfg(windows)]
96    {
97        let mut cmd = tokio::process::Command::new(
98            settings
99                .script_shell
100                .as_deref()
101                .unwrap_or_else(|| Path::new("cmd.exe")),
102        );
103        if settings.script_shell.is_some() {
104            cmd.arg("-c").arg(script_cmd);
105        } else {
106            // `/d` skips AutoRun, `/s` flips the quote-stripping rule
107            // so only the *outer* `"..."` pair is removed, `/c` runs
108            // the command and exits. Build the raw argv tail manually
109            // so cmd.exe sees the original script bytes.
110            cmd.raw_arg("/d /s /c \"").raw_arg(script_cmd).raw_arg("\"");
111        }
112        apply_script_settings_env(&mut cmd, &settings);
113        cmd
114    }
115}
116
117/// Shell-quote one arg for safe splicing into a shell command line.
118///
119/// Used by `aube run <script> -- args`. Args get joined into the
120/// script string, then sh -c or cmd /c reparses the whole thing. If
121/// user arg contains $, backticks, ;, |, &, (, ), etc, the shell
122/// interprets those as metacharacters. That is shell injection.
123/// `aube run echo 'hello; rm -rf ~'` would run two commands. Same
124/// issue npm had pre-2016. Quote each arg so shell treats it as one
125/// literal token.
126///
127/// Unix: wrap in single quotes. sh treats interior of '...' as pure
128/// literal with one exception, embedded single quote. Handle that
129/// with the standard '\'' escape trick: close the single-quoted
130/// string, emit an escaped quote, reopen. Works in every POSIX sh.
131///
132/// Windows cmd.exe: wrap in double quotes. cmd interprets many
133/// metachars even inside double quotes, but CreateProcessW hands the
134/// string to our spawn_shell that uses `/d /s /c "..."`, the outer
135/// quotes get stripped per /s rule and the content runs. Escape
136/// interior " and backslash per CommandLineToArgvW. Full cmd.exe
137/// metachar caret-escaping is a rabbit hole, so this is best-effort,
138/// works for the common cases, matches what node's shell-quote does.
139pub fn shell_quote_arg(arg: &str) -> String {
140    #[cfg(unix)]
141    {
142        let mut out = String::with_capacity(arg.len() + 2);
143        out.push('\'');
144        for ch in arg.chars() {
145            if ch == '\'' {
146                out.push_str("'\\''");
147            } else {
148                out.push(ch);
149            }
150        }
151        out.push('\'');
152        out
153    }
154    #[cfg(windows)]
155    {
156        let mut out = String::with_capacity(arg.len() + 2);
157        out.push('"');
158        for ch in arg.chars() {
159            match ch {
160                '"' => out.push_str("\\\""),
161                '\\' => out.push_str("\\\\"),
162                // cmd.exe expands %VAR% even inside double quotes.
163                // Outer `/s /c "..."` only strips the outermost
164                // quote pair, the shell still runs env expansion
165                // on the body. Argument like `%COMSPEC%` would
166                // otherwise get replaced with the shell path
167                // before the child saw it. Double the percent so
168                // cmd passes a literal `%` through. Full
169                // caret-escaping of `^ & | < > ( )` is a deeper
170                // rabbit hole, this handles the common injection
171                // vector.
172                '%' => out.push_str("%%"),
173                _ => out.push(ch),
174            }
175        }
176        out.push('"');
177        out
178    }
179}
180
181/// Translate child ExitStatus to a parent exit code.
182///
183/// On Unix a signal-killed child has None from .code(). Old code
184/// collapsed that to 1. That loses signal identity: SIGKILL (OOM
185/// killer, exit 137), SIGSEGV (139), Ctrl-C (130) all look like
186/// plain exit 1. CI pipelines watching for 137 to detect OOM cannot
187/// distinguish it from a normal script error anymore. Bash convention
188/// is 128 + signum, match that.
189///
190/// Windows has no signal concept so .code() is always Some, the
191/// fallback 1 is dead code there but keeps the function total.
192pub fn exit_code_from_status(status: std::process::ExitStatus) -> i32 {
193    if let Some(code) = status.code() {
194        return code;
195    }
196    #[cfg(unix)]
197    {
198        use std::os::unix::process::ExitStatusExt;
199        if let Some(sig) = status.signal() {
200            return 128 + sig;
201        }
202    }
203    1
204}
205
206fn apply_script_settings_env(cmd: &mut tokio::process::Command, settings: &ScriptSettings) {
207    // Strip credentials that aube itself owns before we spawn any
208    // lifecycle script. AUBE_AUTH_TOKEN is aube's own registry login
209    // token. No transitive postinstall has any business reading it.
210    // NPM_TOKEN and NODE_AUTH_TOKEN stay untouched because release
211    // flows ("npm publish" in a postpublish script) genuinely need
212    // them. Matches what pnpm does today.
213    cmd.env_remove("AUBE_AUTH_TOKEN");
214    if let Some(node_options) = settings.node_options.as_deref() {
215        cmd.env("NODE_OPTIONS", node_options);
216    }
217    if let Some(unsafe_perm) = settings.unsafe_perm {
218        cmd.env(
219            "npm_config_unsafe_perm",
220            if unsafe_perm { "true" } else { "false" },
221        );
222    }
223    if settings.shell_emulator {
224        cmd.env("npm_config_shell_emulator", "true");
225    }
226}
227
228/// Lifecycle hooks that `aube install` runs against the root package's
229/// `scripts` field, in this order: `preinstall` → (dependencies link) →
230/// `install` → `postinstall` → `prepare`. Matches pnpm / npm.
231#[derive(Debug, Clone, Copy, PartialEq, Eq)]
232pub enum LifecycleHook {
233    PreInstall,
234    Install,
235    PostInstall,
236    Prepare,
237}
238
239impl LifecycleHook {
240    pub fn script_name(self) -> &'static str {
241        match self {
242            Self::PreInstall => "preinstall",
243            Self::Install => "install",
244            Self::PostInstall => "postinstall",
245            Self::Prepare => "prepare",
246        }
247    }
248}
249
250/// Dependency lifecycle hooks, in the order aube runs them for each
251/// allowlisted package. `prepare` is intentionally omitted — it's meant
252/// for the root package and git-dep preparation, not installed tarballs.
253pub const DEP_LIFECYCLE_HOOKS: [LifecycleHook; 3] = [
254    LifecycleHook::PreInstall,
255    LifecycleHook::Install,
256    LifecycleHook::PostInstall,
257];
258
259/// Holds the real stderr fd saved before `aube` redirects fd 2 to
260/// `/dev/null` under `--silent`. Child processes spawned through
261/// `child_stderr()` get a fresh dup of this fd so their stderr still
262/// reaches the user's terminal — `--silent` only silences aube's own
263/// output, not the scripts / binaries it invokes (matches `pnpm
264/// --loglevel silent`). A value of `-1` means silent mode is off and
265/// children should inherit stderr normally.
266#[cfg(unix)]
267static SAVED_STDERR_FD: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(-1);
268
269/// Called once by `aube` after it saves + redirects fd 2. Passing
270/// the caller-owned saved fd here means child processes spawned via
271/// `child_stderr()` will write to the real terminal stderr instead of
272/// `/dev/null`.
273#[cfg(unix)]
274pub fn set_saved_stderr_fd(fd: std::os::fd::RawFd) {
275    SAVED_STDERR_FD.store(fd, std::sync::atomic::Ordering::SeqCst);
276}
277
278/// Windows has no equivalent fd-based silencing plumbing: aube's
279/// `SilentStderrGuard` is `libc::dup`/`libc::dup2` on fd 2, and those
280/// calls are gated to unix in `aube`. The stub keeps the public
281/// API shape identical so call sites compile unchanged.
282#[cfg(not(unix))]
283pub fn set_saved_stderr_fd(_fd: i32) {}
284
285/// Returns a `Stdio` suitable for a child process's stderr. When silent
286/// mode is active, this dups the saved real-stderr fd so the child
287/// bypasses the `/dev/null` redirect on fd 2. Otherwise returns
288/// `Stdio::inherit()`.
289#[cfg(unix)]
290pub fn child_stderr() -> std::process::Stdio {
291    let fd = SAVED_STDERR_FD.load(std::sync::atomic::Ordering::SeqCst);
292    if fd < 0 {
293        return std::process::Stdio::inherit();
294    }
295    // SAFETY: `fd` was registered by `set_saved_stderr_fd` from a live
296    // `dup` that `aube`'s `SilentStderrGuard` keeps open for the
297    // duration of main. `BorrowedFd` only borrows, so this does not
298    // transfer ownership.
299    let borrowed = unsafe { std::os::fd::BorrowedFd::borrow_raw(fd) };
300    match borrowed.try_clone_to_owned() {
301        Ok(owned) => std::process::Stdio::from(owned),
302        Err(_) => std::process::Stdio::inherit(),
303    }
304}
305
306#[cfg(not(unix))]
307pub fn child_stderr() -> std::process::Stdio {
308    std::process::Stdio::inherit()
309}
310
311/// Run a single npm-style script line through `sh -c` with the usual
312/// environment (`$PATH` extended with `node_modules/.bin`, `INIT_CWD`,
313/// `npm_lifecycle_event`, `npm_package_name`, `npm_package_version`).
314///
315/// `extra_bin_dirs` are prepended to `PATH` in order, *before* the
316/// project-level `.bin`. Dep lifecycle scripts pass the dep's own
317/// sibling `node_modules/.bin/` so transitive binaries (e.g.
318/// `prebuild-install`, `node-gyp`) declared in the dep's
319/// `dependencies` are reachable, optionally followed by aube-owned
320/// tool dirs (e.g. the bootstrapped node-gyp). Root scripts pass
321/// `&[]` — their transitive bins are already hoisted into the
322/// project-level `.bin`.
323///
324/// Inherits stdio from the parent so the user sees script output live.
325/// Returns Err on non-zero exit so install fails fast if a lifecycle
326/// script breaks, matching pnpm.
327pub async fn run_script(
328    script_dir: &Path,
329    project_root: &Path,
330    modules_dir_name: &str,
331    manifest: &PackageJson,
332    script_name: &str,
333    script_cmd: &str,
334    extra_bin_dirs: &[&Path],
335) -> Result<(), Error> {
336    // PATH prepends (most-local-first): `extra_bin_dirs` in caller
337    // order, then the project root's `<modules_dir>/.bin`. For root
338    // scripts `script_dir == project_root` and `extra_bin_dirs` is
339    // empty, which matches the old behavior. `modules_dir_name`
340    // honors pnpm's `modulesDir` setting — defaults to
341    // `"node_modules"` at the call site, but a workspace may have
342    // configured something else.
343    let project_bin = project_root.join(modules_dir_name).join(".bin");
344    let path = std::env::var_os("PATH").unwrap_or_default();
345    let mut entries: Vec<PathBuf> = Vec::with_capacity(extra_bin_dirs.len() + 1);
346    for dir in extra_bin_dirs {
347        entries.push(dir.to_path_buf());
348    }
349    entries.push(project_bin);
350    entries.extend(std::env::split_paths(&path));
351    let new_path = std::env::join_paths(entries).unwrap_or(path);
352
353    let mut cmd = spawn_shell(script_cmd);
354    cmd.current_dir(script_dir)
355        .stderr(child_stderr())
356        .env("PATH", &new_path)
357        .env("npm_lifecycle_event", script_name);
358
359    // Pass INIT_CWD the way npm/pnpm do — the directory the user
360    // invoked the package manager from, *not* the script's own cwd.
361    // Native-module build tooling (node-gyp, prebuild-install, etc.)
362    // reads INIT_CWD to locate the project root when caching binaries.
363    // Preserve if already set by a parent aube invocation so nested
364    // scripts see the outermost cwd.
365    if std::env::var_os("INIT_CWD").is_none() {
366        cmd.env("INIT_CWD", project_root);
367    }
368
369    if let Some(ref name) = manifest.name {
370        cmd.env("npm_package_name", name);
371    }
372    if let Some(ref version) = manifest.version {
373        cmd.env("npm_package_version", version);
374    }
375
376    tracing::debug!("lifecycle: {script_name} → {script_cmd}");
377    let status = cmd
378        .status()
379        .await
380        .map_err(|e| Error::Spawn(script_name.to_string(), e.to_string()))?;
381
382    if !status.success() {
383        return Err(Error::NonZeroExit {
384            script: script_name.to_string(),
385            code: status.code(),
386        });
387    }
388
389    Ok(())
390}
391
392/// Run a lifecycle hook against the root package, if a script for it is
393/// defined. Returns `Ok(false)` if the hook wasn't defined (no-op),
394/// `Ok(true)` if it ran successfully.
395///
396/// The caller is responsible for gating on `--ignore-scripts`.
397pub async fn run_root_hook(
398    project_dir: &Path,
399    modules_dir_name: &str,
400    manifest: &PackageJson,
401    hook: LifecycleHook,
402) -> Result<bool, Error> {
403    let name = hook.script_name();
404    let Some(script_cmd) = manifest.scripts.get(name) else {
405        return Ok(false);
406    };
407    run_script(
408        project_dir,
409        project_dir,
410        modules_dir_name,
411        manifest,
412        name,
413        script_cmd,
414        &[],
415    )
416    .await?;
417    Ok(true)
418}
419
420/// Single source of truth for the implicit `node-gyp rebuild`
421/// fallback: returns `Some("node-gyp rebuild")` when the package ships
422/// a `binding.gyp` at its root AND the manifest leaves both `install`
423/// and `preinstall` empty (either one is the author's explicit
424/// opt-out from the default).
425///
426/// `has_binding_gyp` is passed by the caller so this helper is
427/// agnostic to *how* presence was detected — the install pipeline
428/// stats the materialized package dir, while `aube ignored-builds`
429/// reads the store `PackageIndex` since the package may not be
430/// linked into `node_modules` yet. Both paths must agree on the gate
431/// condition, so they both go through this.
432pub fn implicit_install_script(
433    manifest: &PackageJson,
434    has_binding_gyp: bool,
435) -> Option<&'static str> {
436    if !has_binding_gyp {
437        return None;
438    }
439    if manifest
440        .scripts
441        .contains_key(LifecycleHook::Install.script_name())
442        || manifest
443            .scripts
444            .contains_key(LifecycleHook::PreInstall.script_name())
445    {
446        return None;
447    }
448    Some("node-gyp rebuild")
449}
450
451/// Default `install` command for a materialized dependency directory.
452/// Thin wrapper around [`implicit_install_script`] that supplies
453/// `has_binding_gyp` by stat'ing `<package_dir>/binding.gyp`.
454pub fn default_install_script(package_dir: &Path, manifest: &PackageJson) -> Option<&'static str> {
455    implicit_install_script(manifest, package_dir.join("binding.gyp").is_file())
456}
457
458/// True if [`run_dep_hook`] would actually execute something for this
459/// package across any of the dependency lifecycle hooks. Callers use
460/// this to skip fan-out work for packages that have nothing to run —
461/// including the implicit `node-gyp rebuild` default.
462pub fn has_dep_lifecycle_work(package_dir: &Path, manifest: &PackageJson) -> bool {
463    if DEP_LIFECYCLE_HOOKS
464        .iter()
465        .any(|h| manifest.scripts.contains_key(h.script_name()))
466    {
467        return true;
468    }
469    default_install_script(package_dir, manifest).is_some()
470}
471
472/// Run a lifecycle hook against an installed dependency's package
473/// directory. Mirrors [`run_root_hook`] but spawns inside `package_dir`
474/// (the actual linked package directory, e.g.
475/// `node_modules/.aube/<dep_path>/node_modules/<name>`). The manifest
476/// is the dependency's own `package.json`, *not* the project root's.
477///
478/// `dep_modules_dir` is the dep's sibling `node_modules/` — i.e.
479/// `package_dir`'s parent for unscoped packages, or `package_dir`'s
480/// grandparent for scoped (`@scope/name`). `<dep_modules_dir>/.bin`
481/// is prepended to `PATH` so the dep's postinstall can spawn tools
482/// declared in its own `dependencies` (the transitive-bin case —
483/// `prebuild-install`, `node-gyp`, `napi-postinstall`). The install
484/// driver writes shims there via `link_dep_bins`; `rebuild` mirrors
485/// the same pass.
486///
487/// For the `install` hook specifically, if the manifest leaves both
488/// `install` and `preinstall` empty but the package has a top-level
489/// `binding.gyp`, this falls back to running `node-gyp rebuild` — the
490/// node-gyp default that npm and pnpm both honor so native modules
491/// without a prebuilt binary still compile on install.
492///
493/// `tool_bin_dirs` are prepended to `PATH` *after* the dep's own
494/// `.bin` so that aube-bootstrapped tools (e.g. node-gyp) fill the
495/// gap for deps that shell out to them without declaring them as
496/// their own `dependencies`. The dep's local bin still wins if it
497/// shipped its own copy.
498///
499/// The caller is responsible for gating on `BuildPolicy` and
500/// `--ignore-scripts`. Returns `Ok(false)` if the hook wasn't defined.
501pub async fn run_dep_hook(
502    package_dir: &Path,
503    dep_modules_dir: &Path,
504    project_root: &Path,
505    modules_dir_name: &str,
506    manifest: &PackageJson,
507    hook: LifecycleHook,
508    tool_bin_dirs: &[&Path],
509) -> Result<bool, Error> {
510    let name = hook.script_name();
511    let script_cmd: &str = match manifest.scripts.get(name) {
512        Some(s) => s.as_str(),
513        None => match hook {
514            LifecycleHook::Install => match default_install_script(package_dir, manifest) {
515                Some(s) => s,
516                None => return Ok(false),
517            },
518            _ => return Ok(false),
519        },
520    };
521    let dep_bin_dir = dep_modules_dir.join(".bin");
522    let mut bin_dirs: Vec<&Path> = Vec::with_capacity(tool_bin_dirs.len() + 1);
523    bin_dirs.push(&dep_bin_dir);
524    bin_dirs.extend(tool_bin_dirs.iter().copied());
525    run_script(
526        package_dir,
527        project_root,
528        modules_dir_name,
529        manifest,
530        name,
531        script_cmd,
532        &bin_dirs,
533    )
534    .await?;
535    Ok(true)
536}
537
538#[derive(Debug, thiserror::Error)]
539pub enum Error {
540    #[error("failed to spawn script {0}: {1}")]
541    Spawn(String, String),
542    #[error("script `{script}` exited with code {code:?}")]
543    NonZeroExit { script: String, code: Option<i32> },
544}