Arezgitfield notes / engineering
SecurityUPDATED OCT 28, 2026

Constrain Subprocesses Against Command Injection

Choose fixed executables and validated operands, distinguish shell injection from argument injection, and bound subprocess authority, output, and failure behavior.

Arezgit Editorial7 min read
AREZGIT / FIELD NOTESECURITY
A subprocess can interpret a user supplied value as an option even when no shell is involved. Removing shell interpolation closes one path, but the selected executable still has its own comm
READ / VERIFY / APPLYTECHNICALLY REVIEWED

A subprocess can interpret a user-supplied value as an option even when no shell is involved. Removing shell interpolation closes one path, but the selected executable still has its own command language and filesystem authority. Define both boundaries before passing external input to an operating-system process.

A media-inspection service should accept an owned upload and return bounded metadata. It should not let a request choose an executable, add utility options, or write to arbitrary paths. Trace those capabilities from the request schema to the child process, including the file parser and cleanup after failure.

Prefer an API with a narrower capability

First determine whether a maintained library can perform the operation directly. Creating a directory, parsing an archive, or inspecting a file may not require a command interpreter. Choose an API whose inputs express the intended operation rather than a general command string.

The OWASP command injection guidance recommends avoiding direct operating-system commands where a suitable alternative exists. It also distinguishes argument injection from the ability to add a separate shell command.

Evaluate the library's own boundaries. An archive API still needs path and resource validation; a parser may still process hostile input. The benefit is a more specific interface to review, not an automatic claim that all risk disappears when a subprocess is removed.

If a subprocess remains necessary, document why and name the smallest required capability. In a synthetic media-inspection service, the task may be reading metadata from a quarantined file. It should not silently include converting arbitrary URLs, writing arbitrary destinations, or launching user-selected plugins.

Fix the executable and avoid shell interpretation

Select the executable through controlled application configuration, with a reviewed deployment path and version. Do not let the request choose an executable name or influence its search directory. A fixed argument list is insufficient if the operating system resolves an unintended program first.

Use a direct process API with a separate argument array when the platform supports it. The Node.js child-process reference explains that exec invokes a shell and that execFile does not do so by default. Review the selected API's actual options rather than assuming every child-process function has identical behavior.

Account for operating-system differences. Script wrappers and Windows command files may require interpretation that a native executable does not. Verify the exact launch path on supported platforms and keep that requirement visible in the design.

Avoid building a shell string and then applying a generic escaping helper. Correct quoting depends on the interpreter and context. A change in shell, wrapper, or argument construction can invalidate an earlier assumption while leaving the code superficially similar.

Validate operands against the utility's own grammar

There are two parsers to account for. With a shell command string, shell syntax may interpret separators or substitutions before the utility runs. With a direct argument array, that shell interpretation is absent, but the utility still parses its arguments. The following conceptual inputs illustrate separate cases; they are not commands to execute:

report draft.txt
report.txt; unexpected-action
--help

The first should remain one file operand despite its space. Under a direct process API, the second can also remain one argument rather than becoming a second shell command. That does not make it a permitted application filename. The third contains no shell metacharacters at all, yet a utility can interpret it as an option. A test that only rejects semicolons misses that boundary.

For the media-inspection service, expose an uploaded-object ID and a small format enum in the request. Look up that object under the authenticated owner's scope, confirm that its quarantine state permits inspection, then map the enum to application-owned arguments. Resolve the input file through the storage layer. Reject requests containing an executable, output path, URL, or arbitrary option list; these fields are outside the service's task.

The resulting launch record should be explainable without raw user input: approved native executable, fixed inspection mode, controlled input object, restricted working directory, and bounded output. A valid object ID is not enough if it belongs to another tenant, and an authorized upload is not enough if the utility's format parser can use unlimited memory. Those are distinct checks before and during execution.

Define a typed application request that exposes only the intended options. Map an approved enum to fixed arguments rather than accepting arbitrary flags. Validate lengths, ranges, allowed file types, and the ownership of referenced objects at the authoritative server or native boundary.

Consider a user value beginning with a hyphen. The utility may interpret it as an option even when it arrives as one array element. Use an end-of-options marker only when that utility documents support for it, and still validate the operand's meaning.

For file inputs, resolve an application-owned object identity to a controlled file rather than accepting an arbitrary client filesystem path. Account for links, replacement races, and platform-specific path behavior using mechanisms suitable to the storage model. A string prefix check alone is not a complete filesystem containment design.

Review environment variables, working directory, standard input, and configuration files consulted by the utility. Each can introduce behavior outside the visible argument list. Pass only the environment and permissions the task needs, with no unrelated service authority inherited for convenience.

Bound the process and sanitize its failure

Set limits for elapsed time, concurrent children, input size, and captured output. A permitted command can still exhaust resources through a very large file or a utility that waits for interaction. Choose a noninteractive invocation supported by the tool and test how it behaves with malformed input.

Define cancellation and cleanup through the platform's supported process-lifecycle mechanisms. Terminating one parent process does not necessarily terminate every descendant or remove partially written files. Use a process-group, job, or sandbox strategy appropriate to the operating system and verify its behavior.

Treat standard output and standard error as untrusted data. Parse only an expected format, bound retained bytes, and encode values before displaying them in HTML. Do not return raw diagnostics that reveal internal paths or application configuration.

Keep errors actionable. A client may need to know that inspection failed or exceeded its allowed budget, while operators need a sanitized tool exit classification. Preserve those separate purposes without exposing the utility's entire diagnostic stream.

Test denied capabilities and cleanup

Make rejection measurable at the process boundary. A request with an unknown format, another tenant's object, or an extra flag field should create zero child processes. A valid request for a malformed owned file may create one child, but it should finish with a controlled inspection failure and no output outside its assigned area. A timeout should stop retaining output and invoke the supported process cleanup mechanism.

Also test output limits independently of exit code. A utility can exceed the application's capture budget and still be capable of exiting successfully later. The application must not keep accumulating bytes until then. If using Node's buffered APIs, distinguish their capture limits from an overall memory or descendant-process limit. A streaming API needs its own byte accounting; choosing spawn alone does not impose a cap.

Create synthetic test cases for leading options, unexpected encodings, oversized operands, malformed files, missing executables, excessive output, and cancellation. Verify that rejected inputs never reach process creation and that accepted inputs cannot expand the intended capability.

Run the utility with the restricted permissions used by deployment. Test an attempted write outside its assigned output area and inspect whether any partial state remains after failure. The expected result should be a controlled rejection with cleanup owned by the application.

Review the resource budget guide for admission limits when clients can request repeated executions. Per-process timeouts do not limit the aggregate number of processes a caller can trigger.

If a denied-input test creates a child process, move the authoritative check before launch. If an accepted malformed upload leaves a runaway child or partial output, repair the lifecycle rather than widening the input filter. Unexpected execution requires containing the launch path and process authority before restoring the feature.

READER SIGNAL

Was this guide useful?

Your rating helps us prioritize clearer, more practical technical content.

AREZGIT / DESKTOP WORKSPACEFROM FIELD NOTE TO RELEASE

Review diffs, run checks, and prepare the release in Arezgit.

Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.

Explore Arezgit