> 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/xss/dom-xss-document-write-with-location-search.md).

# XSS DOM document.write with location.search

XSS DOM document.write with location.search notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and remediation context.

### DOM XSS in document.write Sink Using Source location.search

This lab contains an XSS-side DOM vulnerability in the search query tracking feature. The page uses `document.write` to write data from `location.search` into the DOM, which can be controlled via URL.

**Description of vulnerability**/ `document.write` is called with the `location.search` string without sanitation. As the input comes directly from the URL, an attacker can inject HTML/JS into the page by manipulating `location.search`.

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-285c95883d25e18b3fd57703bb9cbfd4fadf1dfb%2F%7B71C2914B-1181-4283-97D9-BE369005ECA2%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

**Objective for resolution** Perform an XSS attack on the DOM side that calls the `alert()` function when the page is loaded.

**Comments/indices**/ The code generates something like a `<img>` tag containing a `searchTerms` parameter between quotes, for example:

```html
<img src="/resources/images/tracker.gif?searchTerms=Jordan">
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-7015f343e74f8f00a9f988ac5d97d58733c3fbff%2F%7BD88B1544-A14A-483D-843A-87C3D304FE97%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

If you close the `<img>` tag and quotes, you can insert arbitrary HTML/JS content.

**Examples of tested payloads (provided)**/ Close the tag and inject HTML:

```html
"><h1>hack</h1>
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-e990723f01026088e065e95c8ed8315f4f9d9273%2F%7B69F16E73-C94E-482E-B6AE-BAC849952B9E%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

Close the tag and inject script to trigger `alert`:

```javascript
"><script>alert("xss")</script>
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-c1cb6a560a846ee995ffcdfa9f704d0c2e4fc1c2%2F%7B6656AD8C-DF1B-4935-AE99-1203574875DC%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/xss/dom-xss-document-write-with-location-search.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.
