Skip to content
XSS Write-up Intermediate

PDF.js Arbitrary JavaScript Code Execution (CVE-2024-4367)

PDF.js failed to validate that every element of a PDF font's /FontMatrix array was a number, allowing attacker-controlled strings to be injected into dynamically generated JavaScript. An attacker could craft a malicious PDF that, when opened in a vulnerable PDF.js viewer, executes arbitrary JavaScript in the viewer's origin, potentially leading to data theft or account takeover.

medium.com

Published on medium.com

In this article, i’ll break down how the vulnerability works, demonstrate its impact and explore how to identify and mitigate it in vulnerable applications.

𝙇𝙤𝙨𝙩𝙨𝙚𝙘, on medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the lack of type checking on /FontMatrix array elements allowed attacker-controlled strings to be interpolated into dynamically generated JavaScript code.

Impact

An attacker could execute arbitrary JavaScript in the origin of the vulnerable application, leading to cross-site scripting, data theft, account takeover, or, in Electron apps with nodeIntegration enabled, full remote code execution on the victim's machine.

What the attacker needed

Victim opens a malicious PDF; PDF.js version below 4.2.67; PDF.js used to render untrusted PDFs

The fix

Upgrade PDF.js to version 4.2.67 or later, which removes the unsafe code-generation path; interim mitigations include setting isEvalSupported to false, enforcing a strict CSP that disallows unsafe-eval, and properly sandboxing Electron renderers.

How it was found

  1. Find a website with a file upload feature that accepts and previews PDF files.
  2. Check if the site uses PDF.js and whether its version is below 4.2.67.
  3. Create a PDF with a modified /FontMatrix array containing a harmless JavaScript payload.
  4. Upload the crafted PDF and open it in the website's PDF viewer.
  5. Observe whether the JavaScript executes, indicating the target may be vulnerable.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

PDF.js is a tool that lets web browsers display PDF files. It builds small pieces of JavaScript code to draw certain fonts, and it forgot to check that the numbers it used for font positioning were really numbers. A malicious PDF could sneak in extra JavaScript code, which then runs as if it were part of the website, letting an attacker steal data or take over an account.

What is XSS?

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.