Repository navigation
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.