Skip to content

Fix OS command injection: run gs/qpdf via execFile instead of a shell - #13

Open
tsryo wants to merge 1 commit into
divizend:mainfrom
tsryo:fix/command-injection-execfile
Open

tsryo wants to merge 1 commit into
divizend:mainfrom
tsryo:fix/command-injection-execfile

Conversation

@tsryo

@tsryo tsryo commented Nov 18, 2026

Copy link
Copy Markdown

What this fixes
Several exported functions build a command string and run it through util.promisify(child_process.exec), which executes it in a shell. Caller-supplied values are interpolated into that string without escaping, so untrusted input reaching these parameters allows arbitrary OS command execution (RCE):

extractPDFPages(buf, firstPage, lastPage) — firstPage / lastPage
renderPDFPagesToPNG(buf, firstPage, lastPage, resolution) — resolution (and string page values)
convertToPDFA(buf, { version }) — version
A consumer that renders or extracts user-selected pages — e.g. renderPDFPagesToPNG(pdf, req.query.from, req.query.to, req.query.dpi) — passes attacker input straight into the shell command.

The change
Replace child_process.exec (shell) with execFile and an argument array at every call site. execFile passes each argument directly to the process, so shell metacharacters (;, &, |, `, $()) can never be interpreted. This removes the injection class entirely, not only the three parameters above — combinePDFs, countPDFPages, rotatePDF, and compressPDF are converted too, for defense in depth.
Add assertSafeInteger() validation for firstPage, lastPage, resolution, and the PDF/A version, so malformed values are rejected early with a clear error.
No behavioural change for valid inputs: the same gs / qpdf arguments are sent, just directly to the process rather than via a shell. The PostScript-level backslash escaping in countPDFPages is preserved.

Compatibility note
execFile does not use a shell, so on Windows gs / qpdf must resolve to an executable on PATH (e.g. gs.exe / a Ghostscript console binary), not a .cmd/.bat wrapper. On Linux/macOS this is the usual case and nothing changes.

Testing
npx tsc --noEmit passes.
The Jest suite was not run in this change (it requires the Ghostscript and qpdf binaries); please let CI exercise it.
Disclosure
This addresses a vulnerability being handled as a coordinated disclosure (CWE-78, CVSS 3.1 ~9.8); a CVE is pending. A patched npm release after merge would let downstream users pick up the fix. Happy to adjust anything to your preferences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants