> 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/reflected-xss-in-a-javascript-string-with-html-encoded-angle-brackets.md).

# Reflected XSS in a JavaScript String with HTML-Encoded Angle Brackets

Reflected XSS in a JavaScript String with HTML-Encoded Angle Brackets notes for PortSwigger Web Security Academy labs, including exploitation workflow, payload testing, Burp Suite validation, and reme

### Reflected XSS Into a JavaScript String with Angle Brackets HTML Encoded

This lab contains a XSS-type vulnerability reflected in the search query tracking mechanism: the angle brackets (`<` and `>`) are encoded in HTML, but the reflected value is inserted inside a \*\* JavaScript string\*\*. The goal is to get out of this JS string and call `alert()`.

**Extract relevant to the code observed**

{% code overflow="wrap" %}

```javascript
var searchTerms = 'hello';
   document.write('<img src="/resources/images/tracker.gif?searchTerms='+encodeURIComponent(searchTerms)+'">');
```

{% endcode %}

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-d63c624dccd988c20d69f5c1a1c22cd950ab46ff%2F%7BF4D7F7B2-0557-41E0-A2B0-85CCD872B93F%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

The search value (`searchTerms`) is concatenated in a `document.write` instruction inside a JavaScript-built HTML string. By injecting an apostrophe (`'`) into the value, you can close the current JavaScript string and execute arbitrary code.

```javascript
hello'
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-a1d42efd972c9f1970689ef4445a2d0836d8d6de%2F%7B4C4E6FD2-56BF-4B1F-A489-67C396363992%7D.png?alt=media" alt=""><figcaption></figcaption></figure>

By sending the following string as a search value, you close the string, run `alert(0)` and then close:

```javascript
hello' ; alert(0);'
```

<figure><img src="https://131707722-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl4EVJpcOrsgjSGsLI7p2%2Fuploads%2Fgit-blob-8029beebb7a4c9902dd1bfee11320e7bf6e2a932%2F%7B0F4D322D-7C96-4F38-AC0A-BDA64E513A17%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/reflected-xss-in-a-javascript-string-with-html-encoded-angle-brackets.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.
