Summary
jsPDF versions prior to 4.2.0 contain a code injection vulnerability where user-controlled input passed to the addJS method is embedded into the PDF without proper escaping.
An attacker who can influence the JavaScript argument to addJS may inject arbitrary PDF objects or script, leading to malicious actions or document manipulation when the PDF is opened.
Affected context
This vulnerability affects applications that:
- Use jsPDF (prior to 4.2.0) to generate PDFs in the browser or server-side, and
- Call
addJSwith data that is directly or indirectly influenced by user input, and - Distribute or render the resulting PDFs to other users or systems.
The risk materializes when untrusted or insufficiently sanitized JavaScript code is embedded into a PDF and executed by a PDF viewer that supports JavaScript actions and object references.
Exploit preconditions
- Attack surface: PDF generation functionality that relies on jsPDF and exposes
addJSto user-controlled or externally sourced data. - Required access: Ability to supply or influence the string passed to
addJS(e.g., form fields, query parameters, API payloads, templating systems). - Trigger point: Opening the generated PDF in a viewer that executes embedded JavaScript or interprets the injected PDF objects.
- Why exploitation is possible: The
addJSmethod embeds its argument directly into the PDF structure without robust escaping (notably for parentheses and delimiters), allowing crafted input to break out of the intended JavaScript string and introduce arbitrary PDF objects or script.
Why this happens
The core issue is improper neutralization of special characters in user-supplied content before embedding it into a structured context (the PDF object and JavaScript string).
This maps to:
- CWE-94: Improper Control of Generation of Code, because user-controlled data is incorporated into executable JavaScript within the PDF without adequate constraints.
- CWE-116: Improper Encoding or Escaping of Output, as characters such as parentheses and delimiters are not safely escaped, enabling attackers to escape the intended string literal or object boundaries.
Conceptually, the bug arises from treating untrusted input as trusted code/structure within a document format that supports embedded scripting and complex object graphs.
Potential impact
- Execution of attacker-controlled JavaScript within the PDF viewer context (where supported).
- Manipulation of the PDF's internal structure, including injecting or altering objects, annotations, or actions.
- Content spoofing, such as modifying visible text, forms, or links to mislead users.
- Data exfiltration from PDF form fields or embedded data available to the script.
- Delivery of socially engineered documents that perform unexpected or unwanted actions when opened.
General mitigation guidance
Effective mitigation focuses on treating the addJS argument as untrusted and minimizing or eliminating dynamic code generation:
- Prefer avoiding the use of
addJSwith user-supplied data; embed only static, audited scripts where possible. - When dynamic behavior is unavoidable, constrain input to a safe subset (e.g., whitelisted tokens) rather than interpolating arbitrary JavaScript strings.
- Apply robust escaping and encoding for characters that have structural meaning in JavaScript strings and PDF syntax, such as parentheses, backslashes, quotes, and control characters.
- Introduce a sanitization layer that validates user input against expected patterns before including it in any executable or structural context.
- Architect PDF generation so that user input populates data fields, not code paths, reducing the need for runtime evaluation inside the PDF.
- Combine these patterns with the principle of least functionality, disabling or avoiding PDF JavaScript features when they are not strictly required by the application's use case.
How teams detect issues like this
Detection efforts can focus on both code-level and artifact-level analysis:
- Static analysis and code scanning rules that flag:
- Calls to
addJSwhere the argument is derived from request parameters, form data, or other untrusted sources. - String concatenation or template literals that mix user input with JavaScript code used in PDF generation.
- Code review checklists that include:
- Verifying that
addJSusage is limited, documented, and does not depend on arbitrary user data. - Ensuring any dynamic content inserted into PDF scripts passes through a dedicated encoding/sanitization routine.
- Security testing and fuzzing of PDF generation endpoints using crafted payloads that attempt to break out of string delimiters or introduce PDF object syntax.
- Applying policy-based controls (e.g., linters or CI rules) to prevent introduction of new code paths where user data is embedded into executable contexts.
- Organizations may leverage automated SAST/IAST tools to highlight unsafe output encoding patterns in JavaScript and TypeScript codebases that interact with jsPDF.

