Learning center

Is a reflected input alert from a dynamic scanner an XSS?

It is when the reflected value can change the syntax of the place it lands and the browser renders the response as HTML. Read the raw response: escaping that protects text between tags does not protect an unquoted attribute or an inline script, and a JSON response declared as application/json with nosniff is not an HTML page.

Read the raw response, not the alert#

A dynamic scanner that reports "reflected input" or "possible cross-site scripting" has sent a value in a request parameter and found some form of it in the response. The alert says the value came back. It does not say what the browser does with it. Two facts decide that: the output context (element text, attribute value, script, or a non-HTML body) and the response's Content-Type. Both are in the raw response, so fetch it yourself with the scanner's payload.

The OWASP cheat sheet frames the work the same way: "There are many different output encoding methods because browsers parse HTML, JS, URLs, and CSS differently. Using the wrong encoding method may introduce weaknesses or harm the functionality of your application." (Cross Site Scripting Prevention Cheat Sheet, OWASP CheatSheetSeries 05861f2)

The fixture is a constructed Python 3.11 http.server app with four endpoints that reflect the parameter q. Every response carries X-Content-Type-Options: nosniff. The lines that build the bodies:

if u.path == "/search":        # element body, HTML-escaped
    self.send(f"<p>Results for {html.escape(q)}</p>\n", "text/html; charset=utf-8")
elif u.path == "/filter":      # unquoted attribute, same escaping
    self.send(f"<input name=q value={html.escape(q)}>\n", "text/html; charset=utf-8")
elif u.path == "/track":       # inline script string, element-body escaping
    self.send(f"<script>var ref = '{html.escape(q, quote=False)}';</script>\n", "text/html; charset=utf-8")
elif u.path == "/api/echo":    # JSON, nosniff
    self.send(json.dumps({"q": q}) + "\n", "application/json")

Requests were sent with curl 8.5.0 as curl -s -i -G --data-urlencode "q=PAYLOAD" http://127.0.0.1:58431/ENDPOINT. Server, Date and Content-Length headers are trimmed below.

Escaped text between tags#

--- /search  q="><svg onload=alert(1)>
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
X-Content-Type-Options: nosniff

<p>Results for &quot;&gt;&lt;svg onload=alert(1)&gt;</p>

Every character that could start a tag or close an attribute arrives as an entity, so the browser shows the payload as text inside the paragraph. A scanner that reports reflection of a harmless marker, or of the letters alert(1), still raises an alert here. For this context the cheat sheet's rule is met: "use HTML entity encoding for that variable." Dismiss, and record the raw response and the context.

The disconfirmation: the same escaping in an unquoted attribute#

/filter applies the same html.escape call, and its response looks just as encoded:

--- /filter  q="><svg onload=alert(1)>
<input name=q value=&quot;&gt;&lt;svg onload=alert(1)&gt;>

A scanner that looks for its payload verbatim does not find it here and may report nothing. The attribute value has no quotes around it, though, and an unquoted value ends at whitespace. A payload without any characters to escape shows the effect:

--- /filter q=x onfocus=alert(1) autofocus
<input name=q value=x onfocus=alert(1) autofocus>

Parsed with html5lib 1.1 (its package summary: "HTML parser based on the WHATWG HTML specification"), the two responses give the <input> element these attributes:

input {'name': 'q', 'value': '"><svg', 'onload': 'alert(1)>'}
input {'name': 'q', 'value': 'x', 'onfocus': 'alert(1)', 'autofocus': ''}

The request chose the element's attributes, including an event handler. The cheat sheet's rule for this context is explicit: "Always surround attribute values with double (") or single (') quotation marks." It explains why: the HTML parsing rules "distinguish quoted values from unquoted ones, where whitespace ends the value." The escaping is correct for the context it was written for and wrong for this one. Fix the template by quoting the attribute; the escaping call can stay.

A string inside an inline script#

/track uses html.escape(q, quote=False), the mode meant for text between tags, inside a single-quoted JavaScript string:

--- /track  q=';alert(1)//
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
X-Content-Type-Options: nosniff

<script>var ref = '';alert(1)//';</script>

The quote closes the string, alert(1) becomes a statement, and // comments out the rest. Running the script's text in Node.js 22.23.5 with a stub alert that records its calls confirms it:

$ curl -s -G --data-urlencode "q=';alert(1)//" http://127.0.0.1:58431/track \
    | sed -e 's/^<script>//' -e 's#</script>$##' | node run-script.js
alert calls: [1]
$ curl -s -G --data-urlencode 'q="><svg onload=alert(1)>' http://127.0.0.1:58431/track \
    | sed -e 's/^<script>//' -e 's#</script>$##' | node run-script.js
alert calls: []

The tag-breaking payload is inert in this context, and the quote-breaking one runs. A scanner that sent only the first payload would underreport this endpoint. The cheat sheet asks for a different encoder here: "When a server template inserts string data into an inline script, use an encoder documented for both the JavaScript string and the enclosing HTML script." Use an encoder documented for that context, or move the value out of the script; more HTML escaping leaves the context unchanged.

JSON served as JSON#

--- /api/echo  q="><svg onload=alert(1)>
HTTP/1.0 200 OK
Content-Type: application/json
X-Content-Type-Options: nosniff

{"q": "\"><svg onload=alert(1)>"}

The payload is in the body nearly verbatim, which is what triggers the alert. The body is declared as JSON, and nosniff tells the browser to follow that declaration: the OWASP headers cheat sheet says the header indicates that "the MIME types advertised in the Content-Type headers should be followed and not guessed." (HTTP Headers Cheat Sheet, OWASP CheatSheetSeries 05861f2) The XSS cheat sheet gives the check to record: "For JSON, verify that the Content-Type header is application/json and not text/html to prevent XSS." Dismiss for this response, after checking that error responses from the same route keep that content type. A page script that later inserts the value into the DOM as HTML is a separate, DOM-based finding.

Decide by context and content type#

What the finding saysEvidence to collectDecision
Value reflected as entities between tagsRaw response; the value's position in the markupDismiss; record the response
Value reflected encoded inside an unquoted attributeRaw response with a payload of spaces and name=value pairs; the parsed attributesFix: quote the attribute; encoding alone is not enough
Value reflected inside a <script> blockRaw response with a quote-breaking payload; whether the script's text runs itFix with a script-context encoder or by passing data outside the script
Value reflected in a JSON or text/plain bodyContent-Type, X-Content-Type-Options, and the same headers on error responsesDismiss if every response is non-HTML with nosniff; otherwise fix the header
No verbatim reflection foundThe response to payloads for each context, not only tag-breaking onesTest the remaining contexts before treating the endpoint as clean

When the same concern arrives as a review comment on a template change, the guide to XSS review comments covers tracing the data to its source. The guide to reading the evidence behind an automated finding covers the decision for other kinds of tool output.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading