Skip to content
XSS Write-up Intermediate

DOM-Based XSS via eval() in ajax-cart.js

The ajax-cart.js script used eval() on the raw response text of a failed AJAX request, so any JavaScript embedded in that response would run in the page. An attacker who could control or spoof that error response could execute arbitrary script in the storefront, though the report only demonstrated this by calling the handler manually from the console.

medium.com

Published on medium.com

While poking around a storefront JavaScript file (because what else do security folks do on weekends?), I stumbled upon a subtle but dangerous bug. It involved the infamous eval() function being used directly on untrusted data — a recipe for client-side disaster.

Mostafa Ghanem, 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

The error handler passed untrusted server response text straight into eval() instead of parsing it as JSON.

Impact

If an attacker could control or spoof the response of a failing AJAX request, they could run arbitrary JavaScript and then steal session cookies, modify the page or redirect users; the report notes the demo needed developer console interaction and was marked Informative.

What the attacker needed

Attacker controls or spoofs an AJAX error response; script ajax-cart.js loaded on the storefront

The fix

The eval() call should be replaced with JSON.parse inside a try/catch that falls back to a generic error message.

How it was found

  1. Craft a fake AJAX response whose text contains a message field plus a script field holding JavaScript.
  2. Build a fake XMLHttpRequest object whose responseText is that crafted string.
  3. Call CustomAPI.onError with the fake object and an error status.
  4. Observe the injected JavaScript run, shown by an alert.

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 website's JavaScript sometimes receives text from the server and needs to turn it into data. This code instead handed that text to eval(), a JavaScript function that runs whatever code it is given. If an attacker can make the server's error response contain their own code, the browser will run it as if the site itself wrote it.

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.