Skip to content
XSS Write-up Advanced

Exploiting XSS using Polyglot JPEGs+Javascript to bypass CSP

A JPEG polyglot can hide JavaScript that executes when loaded as a script, bypassing CSP. An attacker could run arbitrary JavaScript in a victim's browser if they can upload and reference such an image.

Emin Skrijelj Follow

Published on systemweakness.com

This vulnerability allows an attacker to use a JPEG polyglot with JavaScript to hide the malicious JavaScript payload in the image successfully without corrupting the image to bypass the site's CSP. For this exploit you will need two parameters, one to call the malicious image and the other one to upload it.

Emin Skrijelj, on systemweakness.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 application allows user-uploaded images to be served with a script-compatible content type or without proper CSP restrictions, enabling a polyglot file to execute as JavaScript.

What the attacker needed

Ability to upload an image; ability to reference the uploaded image; victim loads the crafted script tag

How it was found

  1. Choose a JavaScript payload to embed.
  2. Ensure the image does not contain the byte sequence 2F 2A (/*) that would break the JPEG.
  3. Modify the JPEG header length field to 2F 2A and insert the payload between the start-of-image and quantization-table markers.
  4. Add null bytes as padding to keep the image valid.
  5. Insert the payload in hex after the null bytes.
  6. Close the JavaScript comment before the end-of-image marker and load the image via a script tag with ISO-8859-1 charset.

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.

A polyglot is a file that can be read as two different formats. Here, a JPEG image is crafted so that when a browser loads it as a script, the hidden JavaScript runs. This can bypass a security rule called CSP that normally blocks inline scripts.

What is XSS?

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

More by Emin Skrijelj

View all

1 more disclosed report, best first.

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.