Launching a subprocess turns application input into operating-system work. The safest API accepts a known executable and a list of validated arguments; it does not accept a shell command assembled from user text. Shell metacharacters, quoting rules, and environment expansion make string concatenation an unsafe boundary.
Make execution constrained
Use the runtime’s spawn API with an argument array and shell execution disabled. Resolve the executable from a server-controlled allowlist, and validate each argument according to the specific command. Set a minimal environment rather than inheriting secrets, and choose a working directory that cannot escape the intended workspace.
Every process needs a wall-clock timeout, output byte limit, and cleanup path. On timeout, terminate the process group where supported and wait for it to exit; killing only the parent can leave child processes running. Treat output as data, never as markup. Record exit code and duration, but redact tokens and sensitive arguments from logs.
Design for failure
A process may fail to spawn, exit nonzero, emit invalid encoding, exceed output limits, or be killed during shutdown. Return a structured result that distinguishes these outcomes from an empty successful result. Do not report a successful operation merely because the process started.
Test with spaces and Unicode in allowed arguments, malformed input, timeout, noisy output, missing executables, and child processes. For untrusted code, a child process alone is not a security sandbox: use a separately hardened container or isolation boundary with resource and network limits. The runner should be small, explicit, and difficult to misuse.
Choose a launch API that matches the job
Node.js exec accepts a command string and uses a shell. execFile starts the executable directly by default and takes an argument array. That makes execFile a practical choice for a small, known command that returns a bounded response. The timeout and maxBuffer options let callers cap runtime and buffered output. Use spawn with streams when data should be consumed incrementally. Node.js 24 child process reference
Fix the executable in application configuration and validate all argument values before launch. Own the environment and working directory. For a health probe, distinguish a program that returns a nonzero exit status from a program that could not start at all. When a process can start descendants, test whether cancellation also stops those descendants. Give jobs that process hostile source a separate isolation design and a runner that can terminate the whole execution boundary.
Worked example
Run as an ES module with Node.js 24. It prints 5 and avoids a shell. A timeout and output cap limit this known command; they do not make arbitrary untrusted code safe.
jsimport { execFile } from "node:child_process"; execFile(process.execPath, ["-e", "console.log(2 + 3)"], { timeout: 1000, maxBuffer: 1024 }, (error, stdout) => { if (error) { console.error(error.code); return; } console.log(stdout.trim()); });




Community
Discussion
Loading comments…