Summary
The pdf-image npm package up to version 2.0.0 constructs shell commands using user-controlled file paths and executes them with child_process.exec(), enabling OS command injection.
If an attacker can influence the pdfFilePath parameter, they may execute arbitrary system commands with the privileges of the Node.js process.
Affected context
This vulnerability affects Node.js applications that integrate the pdf-image package (through 2.0.0) and pass file paths that may be influenced, directly or indirectly, by users or external systems.
The issue occurs in the constructGetInfoCommand and constructConvertCommandForPage functions, where pdfFilePath is interpolated into shell command strings before being executed with child_process.exec().
It is most risky in services that process user-uploaded PDFs, accept path-like parameters from clients, or derive file locations from less-trusted upstream inputs.
Exploit preconditions
- Attack surface: Application functionality that calls pdf-image with a pdfFilePath derived from HTTP requests, RPC calls, message queues, or other external inputs.
- Required access: Ability to control or significantly influence the value of pdfFilePath (e.g., supplying a crafted file name, path, or metadata).
- Trigger point: Invocation of pdf-image APIs that internally call constructGetInfoCommand or constructConvertCommandForPage, leading to child_process.exec() execution.
- Why exploitation is possible: The library concatenates unsanitized user-controlled strings directly into shell commands using util.format(), with no proper escaping or parameterization, allowing attackers to inject additional shell metacharacters and commands.
Why this happens
The core issue is the dynamic construction of shell command strings using unvalidated, user-influenced input that is passed to a shell interpreter.
In pdf-image, util.format() is used to interpolate pdfFilePath into command templates, which are then executed via child_process.exec(). Because exec() spawns a shell, special characters in pdfFilePath (such as ;, &&, |, or backticks) are interpreted as part of the command, not just as data.
This pattern corresponds to OS command injection within the broader class of code injection issues (CWE-94), where external input is allowed to alter the intended command structure rather than being treated as opaque data.
Potential impact
- Execution of arbitrary OS commands with the privileges of the Node.js service account
- Full compromise of the application host, including reading or modifying local files
- Lateral movement within the environment by using the compromised host as a pivot
- Exfiltration of sensitive configuration data, secrets, or credentials
- Disruption of service availability via destructive commands or resource exhaustion
- Establishment of persistent access (e.g., installing backdoors or malicious agents)
General mitigation guidance
Effective mitigation focuses on eliminating direct shell interpretation of user-controlled data and reducing trust in file path inputs:
- Prefer parameterized process execution APIs (e.g., execFile/spawn with argument arrays) that avoid passing through a shell.
- Treat pdfFilePath and similar inputs as untrusted, validating against strict whitelists (e.g., allowed characters and directories) and rejecting unexpected patterns.
- Normalize and constrain file system operations to controlled directories, avoiding user-driven absolute paths or path traversal patterns.
- Apply the principle of least privilege to the Node.js process, limiting its filesystem and OS capabilities to reduce impact if command injection occurs.
- Centralize command-building logic with consistent escaping/quoting utilities, and design APIs so that consumers cannot inject raw shell fragments.
Using safer execution patterns and tight input constraints substantially reduces the risk of command injection, even when processing user-supplied files.
How teams detect issues like this
Detection can combine code-centric and runtime approaches:
- Static analysis: Configure SAST tools or custom lint rules to flag uses of child_process.exec() (and similar APIs) where arguments are built via string concatenation or util.format() with external input.
- Code review: During review, look for command construction patterns that embed variables into shell strings, especially around file processing, conversion, and tooling integration.
- Policy enforcement: Enforce repository or CI policies that prohibit new uses of exec() without documented justification and require safer alternatives such as execFile or spawn with arrays.
- Runtime monitoring: Use logging and security monitoring to detect anomalous child process invocations, unexpected command arguments, or shell utilities being launched from web-facing services.
- Dependency review: Periodically scan third-party packages for known command injection patterns, and establish intake checks for libraries that wrap command-line tools.

