CVE-2026-31898: jsPDF Arbitrary PDF Object Injection via FreeText Color
Summary
CVE-2026-31898: jsPDF has a PDF Object Injection via FreeText color. 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 complex PDF files programmatically, offering features such as text, images, and annotations. However, a vulnerability exists in the library that permits users to inject arbitrary PDF objects, potentially leading to security risks when the generated PDF is opened or interacted with.
Vulnerability Detail
The vulnerability arises from the user-controlled arguments in the 'createAnnotation' method of jsPDF, specifically the 'color' parameter. This flaw allows an attacker to inject malicious PDF objects, including JavaScript actions, which could execute when the PDF is opened. For instance, an attacker could craft a payload that executes a command on the user's system, posing significant security threats.
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 'createAnnotation' method. For example, you can implement a function to validate and clean the 'color' parameter to prevent injection of malicious content. Here is a simple example of sanitization:
function sanitizeColor(input) {
// Allow only hexadecimal color codes
const regex = /^#[0-9A-F]{6}$/i;
return regex.test(input) ? input : '#000000'; // Default to black if invalid
}
const safeColor = sanitizeColor(userInput);
doc.createAnnotation({
type: 'freetext',
bounds: { x: 10, y: 10, w: 120, h: 20 },
contents: 'hello',
color: safeColor
});
Conclusion
In summary, the jsPDF library has a critical vulnerability that allows for arbitrary PDF object injection through unsanitized user input. It is essential to update to the patched version and implement input sanitization to protect against potential exploits.
See every vulnerability recorded for jspdf →
FAQ
The vulnerability allows for arbitrary PDF object injection through unsanitized user input in the 'createAnnotation' method.
Attackers could inject malicious JavaScript actions that execute when the PDF is opened, potentially compromising user systems.
You can use Vulert's playground at Vulert Scanner to check if your application is affected without needing to sign up.
Update jsPDF to version 4.2.1 or later to mitigate the vulnerability and sanitize user inputs before passing them to vulnerable methods.
The CVSS score for this vulnerability is 8.1, indicating a high severity level.
References
Related vulnerabilities
Recent vulnerabilities recorded for jspdf.