Summary
jsPDF's createAnnotation method previously allowed untrusted input in the color parameter to be embedded directly into the generated PDF structure without proper neutralization.
In scenarios where user-controlled data is passed to this API, an attacker could inject arbitrary PDF objects, including JavaScript actions that may execute when the PDF is opened or interacted with by the viewer.
Affected context
This vulnerability affects applications that use jsPDF (prior to 4.2.1) to generate PDFs in the browser or server-side and that allow user-controlled data to flow into the createAnnotation method—particularly into the color parameter or other fields that are written into the PDF structure.
It becomes risky when applications treat user input as harmless display data and pass it directly to jsPDF without sanitization or structural validation, allowing PDF syntax to be injected into the generated document.
Exploit preconditions
- Attack surface: Any web or backend service that generates PDFs using jsPDF's
createAnnotationAPI. - Required access: Ability to supply or influence the arguments passed to
createAnnotation(for example, form fields, query parameters, or stored data used in PDF generation). - Trigger point: Opening or interacting with the generated PDF in a viewer that interprets embedded PDF objects or JavaScript actions.
- Why exploitation is possible: The
colorparameter and related annotation fields are written into the PDF document without sufficient neutralization of special characters and PDF syntax, allowing attackers to break out of expected values and inject arbitrary PDF object definitions.
Why this happens
The core issue is improper output neutralization (CWE-116) when composing PDF content from user-supplied values.
The createAnnotation method treats the color parameter (and similar arguments) as benign data but writes it into the PDF structure without robust escaping or validation.
Because PDF is a structured format where special characters and tokens denote objects and actions, malformed or malicious input can escape the intended value context and introduce new objects, including JavaScript actions, into the final document.
This is an output encoding and data validation failure on the boundary between application data and the PDF document syntax.
Potential impact
- Injection of arbitrary PDF objects into generated documents.
- Embedding of JavaScript actions that may execute when the PDF is opened or when specific annotations are interacted with, depending on the viewer's capabilities and settings.
- User interface redressing or deceptive document content that can mislead users.
- Potential for phishing or social engineering via crafted PDFs that appear legitimate but contain hidden behaviors.
- In specific environments that trust PDF-embedded scripts, this can contribute to broader compromise chains when combined with viewer or ecosystem vulnerabilities.
General mitigation guidance
Effective mitigation centers on treating all user-derived annotation values as untrusted data that must not control PDF structure:
- Normalize and strictly validate annotation parameters such as
coloragainst a whitelist of allowed formats (e.g., numeric arrays, bounded ranges, known color names). - Apply robust output encoding/escaping for any values that are written into the PDF syntax, ensuring special characters and PDF control tokens cannot be injected.
- Implement a clear separation between content data (text, colors, labels) and internal PDF structural tokens, avoiding direct string concatenation whenever possible.
- Reduce trust boundaries by minimizing how much user input influences PDF features like annotations, actions, or interactive elements; avoid allowing arbitrary user input to configure advanced annotation properties.
- Apply the principle of least privilege in the PDF features you enable—disable or avoid generating JavaScript actions and other active content unless there is a compelling, well-understood need.
How teams detect issues like this
Organizations can detect this class of vulnerability through a mix of code and artifact analysis:
- Static analysis and code review:
- Search for jsPDF
createAnnotationcalls where arguments are directly derived from request parameters, form fields, or other untrusted sources. - Flag string concatenations or template literals that build PDF-related parameters from user input without validation or encoding.
- Review validation paths to ensure annotation properties are constrained to well-defined formats and ranges.
- Security-focused linters or SAST tools:
- Configure rules to detect unsafe use of PDF-generation APIs with unvalidated input.
- Enforce policies that require sanitization helpers for any data reaching PDF output routines.
- Dynamic and behavioral testing:
- Fuzz annotation-related parameters with payloads that include PDF control characters to see if generated files contain unexpected objects or JavaScript actions.
- Analyze generated PDFs using PDF inspection tools to detect anomalous embedded objects and scripts that cannot be traced to legitimate application logic.

