CVE-2026-31938: jsPDF HTML Injection Vulnerability in New Window Paths
Summary
CVE-2026-31938: jsPDF has HTML Injection in New Window paths. Affects jspdf versions < 4.2.1. Fixed in 4.2.1. How to fix ↓
Check if this vulnerability affects your application
Upload a manifest or SBOM and we'll show whether you're affected, plus every other risky package in the same scan.
No signup required · Results in about 60 seconds
Background
jsPDF is a popular open-source library used for generating PDF documents in web applications. It allows developers to create PDFs directly in the browser using JavaScript. The library offers various functionalities, including text, images, and shapes rendering, and supports multiple output formats. However, a critical vulnerability has been identified in certain output methods that can lead to HTML injection, posing significant security risks to users.
Vulnerability Detail
The vulnerability arises from user control over the 'options' argument of the 'output' function in jsPDF. Attackers can inject arbitrary HTML, including scripts, into the browser context where the generated PDF is opened. This is particularly concerning for the overloads 'pdfobjectnewwindow', 'pdfjsnewwindow', and 'dataurlnewwindow', where unsanitized user input can be exploited. By providing malicious values through a web interface, attackers can craft a scenario where victims unknowingly execute harmful scripts in their browsers, potentially leading to data theft or unauthorized actions.
How To Fix
Latest published version: 4.2.1 as of 23 Oct 2026
npm install jspdf@4.2.1
The newest release already contains this fix.
Tip — Not sure which version your app runs? Vulert can match this fix to it. Scan free →
Workaround
As a temporary measure, sanitize all user inputs before passing them to the output method. For example, you can use a function to escape potentially harmful characters in the input:
function sanitizeInput(input) {
return input.replace(/</g, '<').replace(/>/g, '>');
}
const safeFilename = sanitizeInput(userProvidedFilename);
doc.output('pdfjsnewwindow', {
filename: safeFilename,
pdfJsUrl: 'viewer.html'
});
Conclusion
The jsPDF library has a critical vulnerability that allows for HTML injection through certain output methods. It is essential to update to the patched version 4.2.1 to mitigate the risks associated with this vulnerability. Additionally, implementing input sanitization can serve as a temporary measure until the update is applied.
See every vulnerability recorded for jspdf →
FAQ
The vulnerability has a CVSS score of 9.6, indicating it is critical and poses a significant risk to users.
You can use Vulert, which monitors and alerts for open-source vulnerabilities in real-time. Check your application using the Vulert playground at Vulert Scanner without any signup.
If exploited, attackers can inject scripts that run in the victim's browser context, leading to data theft, unauthorized actions, or other malicious activities.
Yes, the vulnerability has been fixed in jsPDF version 4.2.1. It is recommended to update to this version or later.
If immediate updating is not possible, ensure to sanitize all user inputs before passing them to the output method as a temporary workaround.
References
Related vulnerabilities
Recent vulnerabilities recorded for jspdf.