1

Concept

What Is an XSS Context?

In XSS, context means where exactly the user's input is placed in the webpage.

This matters because the same input can be safe in one location but dangerous in another.

Common XSS contexts
User input can appear inside:

HTML text: <p>USER_INPUT</p>
HTML attributes: <input value="USER_INPUT">
JavaScript: <script>
let name = 'USER_INPUT';
</script>
URLs: <a href="USER_INPUT">

Each context has different rules for safely handling untrusted data. XSS protection depends on the context where the data is inserted.

You need to know where the data will be used and apply the appropriate context-specific output encoding.

2

Concept

HTML Context

The input appears as normal text between HTML tags: <div>USER_INPUT</div>
If an attacker enters: <script>alert(1)</script> and if application doesn't encode it, the browser may interpret it as HTML.

Protection: HTML-encode special characters such as:

<&lt;
>&gt;
&&amp;
"&quot;
'&#x27;

The browser then displays the input as text instead of HTML.

Note: HTML context → HTML encoding

3

Concept

HTML Attribute Context

This happens when user input is placed inside an HTML attribute: <input value="Bob">
The browser understands Bob as the value of the value attribute.

If attacker-controlled data contains a quotation mark and isn't properly encoded, it may change the structure of the HTML.
The attacker could potentially turn one attribute into multiple attributes or introduce an event-handler attribute.
<input value="..." onmouseover="...">

The important point is that a <script> tag is not required for XSS. HTML event-handler attributes can also cause JavaScript to execute.

Quoted Attribute: <input value="USER_INPUT">
UnQuoted Attribute: <input value=USER_INPUT>

Unquoted attributes have more characters that can terminate or alter the value, making them harder to handle safely.

HTML text: encode HTML special characters.
HTML attribute: use quoted attributes and attribute-safe HTML encoding.

4

Concept

JavaScript Context

This happens when user input is placed directly inside JavaScript: var name = "USER_INPUT";

HTML encoding doesn't protect it because the browser is parsing the content as JavaScript, not HTML.

The input must be safely encoded for a JavaScript string context, including quotes and special characters. Also, avoid allowing </script> to appear literally inside a <script> block, as it can terminate the block.

5

Concept

URL Context

This happens when user input is placed inside a URL: <a href="USER_INPUT">

There are two main risks:
Improper URL encoding can change how the URL is interpreted.
A user-controlled URL could use a dangerous scheme such as javascript:.

Protection:
Properly URL-encode user-controlled data.
Allowlist safe schemes such as https:// and http://.
Avoid allowing arbitrary URLs when possible.

URL context → URL encoding + validate the URL scheme.

6

Detection

Identifying User-Controlled Input

🔎

Detection

Before testing for XSS, first identify where the page gets its data from.

Look at: URL parameters, Form fields, HTTP headers, Cookies, Stored database values, API responses, Other user-controlled data etc

For every piece of content displayed on the page, ask: "Where did this exact value come from?"

7

Detection

Identifying Reflection and Storage

🔎

Detection

Once you find a possible input point, determine how the data reaches the page.

Reflected XSS
Submit a unique marker.
Check whether it appears in the same response.
Nothing is stored.

Stored XSS
Submit a unique marker.
Look for it later, such as on a profile, comment, or dashboard.
The application has stored the value.

8

Detection

Detecting XSS

🔎

Detection

Send a unique, harmless marker into each input point, such as:

XSS_TEST

Then inspect the raw HTML response to see whether and where the marker appears.

Check:

Is it reflected?
Is it HTML-encoded?
Is it inside HTML text?
Is it inside an attribute?
Is it inside JavaScript?
Is it inside a URL?

If the value appears unescaped in a context where it could be interpreted as code, then test further with a context-appropriate, harmless proof of concept.

9

Explanation

False Positives

Seeing your test string appear in the response does not automatically mean XSS exists.

The value might be:

HTML-encoded, making it safe.
Inside a location that cannot execute, such as an HTML comment.
Modified or blocked by a WAF, so the response doesn't represent the application's original behavior.

Check the raw HTML source to see how your input was actually processed.

Then determine whether the specific context can interpret the input as code and, in an authorized test environment, confirm with a safe proof of concept.

10

Summary

Key Takeaways

Summary

XSS depends heavily on where the input is placed.

The same unescaped input might be dangerous in one context but harmless in another.

Map user-controlled inputs — parameters, forms, headers, stored values, etc.
Send a unique marker to each input.
Inspect the raw response to see where the marker appears.
Identify the context — HTML, attribute, JavaScript, URL, etc.
Determine whether the input is reflected or stored.
Check whether the input is properly encoded or neutralized.
In an authorized environment, confirm the issue with a safe proof of concept.