> 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/dom/dom-based-cookie-manipulation.md).

# DOM-Based Cookie Manipulation

DOM-Based Cookie Manipulation notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### DOM-based Cookie Manipulation

This lab shows a client-side manipulation (DOM) of cookies allowing to inject a value into the `lastViewedProduct` cookie. Changing this value can cause a content execution (XSS) on another page and call the `print()` function. Exploitation is done by directing the victim via the exploit server to the necessary pages.

* There is a session cookie named `lastViewedProduct` which contains the link of the last product viewed.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-36d2d0540f1e3c9731099d4edd90921822f63955%2F%7B472575CE-A517-4CD9-8FBF-39B54F2C26C8%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* On the page there is a "Last viewed product" section with an anchor such as `&lt;a href=".../product?productId=1"&gt;Last viewed product&lt;/a&gt;`.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-1a494448d2178add2f3fd791944cbf8f2ad114f0%2F%7BAB74DE1F-9DE5-4422-8703-CF2DE098F4C0%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* By altering the value stored as "last page view", the displayed link can be interpreted as HTML/JS.
* By replacing the cookie value with an external URL (`https://jord4n.pro`) the link redirects to this site.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-bb2d1a3728f0e27028f867c7ca64d8befae12a0f%2F%7B4B4BBFEA-AF87-4629-B241-2623CF31F710%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

The insertion of `JavaScript:print()` in the value is interpreted by the browser in this context.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a18c40aa02357f1c4565c736da9b624e273d13ee%2F%7BBEE27B3D-6CE4-443D-A42D-9C8045F1087B%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

* By injecting a payload containing HTML, for example `productId=1&'><h1>TEST</h1>`, the content is rendered.

{% code overflow="wrap" %}

```html
<a href="https://0a73004204b35bb980b9218200c90043.web-security-academy.net/product?productId=1">Last viewed product</a>
```

{% endcode %}

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-0e3b88951a0b1f8f2b876bf32cad1ca341c967ff%2F%7B300C232B-B69B-413A-9A59-184840499D71%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

A scripting payload like `productId=1&'><script>print()</script>` is also interpreted and triggered `print()`.

```
productId=1&'><script>print()</script>
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-dfbb4ff2d46f45101875487647e5c3efe8ac0b8c%2F%7B9AFEBFB1-F361-4A70-80D1-1A4716ABB890%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

1. Store the victim in its `lastViewedProduct` cookie a value containing the injection (the product page is used to save this value).
2. Then direct the victim to the home page (or the page that displays "Last viewed product") so that the poorly formed value is returned and executes the script (`print()`).
3. Use the exploit server to orchestrate redirection to the product page (which writes the cookie) and then to the page where the cookie is displayed.

{% code overflow="wrap" %}

```javascript
<iframe src="https://0a73004204b35bb980b9218200c90043.web-security-academy.net/product?productId=1&'><script>print()</script>" onload="if(!window.x){this.src='https://0a73004204b35bb980b9218200c90043.web-security-academy.net/'; window.x=1;}"></iframe>
```

{% endcode %}

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-b109e12396eb114e843e97d8f606e98c19d2ed46%2F%7B9257C3B5-3544-4697-9B4B-C2DBA4B0441E%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/dom/dom-based-cookie-manipulation.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.
