What it is: Improper Neutralization of Script in an Error Message Web Page (CWE-81) is a type of cross-site scripting vulnerability where an error page reflects the input that caused the error without escaping it.
Why it matters: Error pages are often built and tested less carefully than normal pages, so developers forget to apply the same output-encoding discipline, leaving an easy injection point.
How to fix it: Apply the same automatic contextual output encoding to error pages as any other page, and avoid echoing raw user input into error messages.
TL;DR: CWE-81 lets an attacker inject script through an error page that reflects the offending input unescaped; the fix is applying the same output-encoding discipline to error pages, or avoiding echoing raw input at all.
| Field | Value |
|---|---|
| CWE ID | CWE-81 |
| OWASP Category | Not directly mapped |
| CAPEC | CAPEC-198 |
| Typical Severity | High |
| Affected Technologies | Web applications, browsers |
| Detection Difficulty | Easy |
| Last Updated | 2026-07-27 |
What is XSS in Error Messages?
Improper Neutralization of Script in an Error Message Web Page (CWE-81) is a type of cross-site scripting vulnerability that occurs when a product receives input from an upstream component but does not neutralize special characters that could be interpreted as web-scripting elements when sent to an error page. As defined by the MITRE Corporation under CWE-81, this weakness has no official OWASP Top 10:2025 category mapping but is a direct child of the broader Cross-site Scripting weakness (CWE-79), and can also manifest alongside sensitive-information disclosure (CWE-209).
Quick Summary
Error pages are an easy blind spot: developers spend most of their escaping-discipline attention on the “happy path” pages users see normally, and often build error handling later, faster, and with less scrutiny. That gap is exactly where this weakness lives — the same input validation that triggers the error is frequently echoed straight back onto the error page unescaped.
Jump to: Quick Summary · XSS in Error Messages Overview · How XSS in Error Messages Works · Business Impact of XSS in Error Messages · XSS in Error Messages Attack Scenario · How to Detect XSS in Error Messages · How to Fix XSS in Error Messages · Framework-Specific Fixes for XSS in Error Messages · How to Ask AI to Check Your Code for XSS in Error Messages · XSS in Error Messages Best Practices Checklist · XSS in Error Messages FAQ · Vulnerabilities Related to XSS in Error Messages · References · Scan Your Own Site
XSS in Error Messages Overview
What: An error page reflects the user input that triggered the error without escaping script-enabling characters.
Why it matters: Error-handling code paths are frequently built with less scrutiny than normal pages, making this an easy weakness to miss in review.
Where it occurs: Validation error pages, exception handlers, and any “Invalid input: X” style message that echoes the offending value.
Who is affected: Applications that build error messages by concatenating the offending input into the response without escaping.
Who is NOT affected: Applications that apply the same automatic output encoding to error pages as normal pages, or that use generic error text without echoing raw input at all.
How XSS in Error Messages Works
Root Cause
The application writes the input that caused a validation or processing error directly into the error page’s HTML without escaping it.
Attack Flow
- The attacker identifies a form or endpoint whose validation errors echo the offending input back to the page.
- The attacker submits a value containing a script payload, deliberately triggering the validation error.
- The error-handling code writes the payload into the error page without escaping it.
- The victim’s browser (if directed to the same error page, e.g. via a crafted link) executes the injected script.
Prerequisites to Exploit
- An error page reflects the input that caused the error.
- The error-handling code path does not apply the same output escaping as normal pages.
- The attacker can direct a victim to trigger the same error condition (e.g. via a crafted URL).
Vulnerable Code
@app.route("/submit")
def submit():
value = request.args.get("amount", "")
if not value.isdigit():
return f"<p>Error: '{value}' is not a valid amount.</p>", 400
...
The invalid value is written directly into the error message HTML with no escaping, so a script payload submitted as amount executes when the error page renders.
Secure Code
from markupsafe import escape
@app.route("/submit")
def submit():
value = request.args.get("amount", "")
if not value.isdigit():
return f"<p>Error: '{escape(value)}' is not a valid amount.</p>", 400
...
Escaping the offending value before it’s written into the error page neutralizes any script-enabling characters, so the injected payload renders as inert text instead of executing.
Business Impact of XSS in Error Messages
Confidentiality: Attackers may read application data reachable from the victim’s session via injected script running on the error page.
Integrity: Attackers may perform actions as the victim through script injected via a crafted error-triggering link.
Availability: Attackers may disrupt the error page’s functionality for the victim through injected script.
- Session hijacking via cookie theft if the session cookie is not HttpOnly-protected
- The error page’s typically lower scrutiny in QA/review makes this an easy weakness to miss until exploited
XSS in Error Messages Attack Scenario
- An attacker finds a form field whose validation error echoes the submitted value back onto the error page unescaped.
- They craft a link that pre-fills the field with a script payload, deliberately triggering the error.
- They send this link to a victim via a phishing message.
- When the victim clicks it, the error page renders, and the injected script executes in their browser, stealing their session cookie.
How to Detect XSS in Error Messages
Manual Testing
- Identify every form field or endpoint whose validation errors echo the offending input.
- Submit a harmless script payload as the deliberately-invalid value to trigger the error.
- Confirm whether the resulting error page executes the payload rather than displaying it as literal text.
Automated Scanners (SAST / DAST)
Static analysis can flag error-handling code that concatenates request data into an HTML response without an escaping call, while dynamic testing with a real script payload confirms whether the live error page actually executes injected markup.
PenScan Detection
PenScan’s scanner engines deliberately trigger error conditions with script payloads as the input and check whether the resulting error page reflects them unescaped.
False Positive Guidance
If the error page uses a generic message that never echoes the offending input value at all, there is no injection point regardless of how the error is triggered — confirm the actual error page content before treating it as a real finding.
How to Fix XSS in Error Messages
- Apply the same automatic contextual output encoding to error pages as any other page.
- Prefer generic error messages that don’t echo the raw offending input at all.
- Route error-handling code through the same templating layer used for normal pages, rather than building error HTML manually.
Framework-Specific Fixes for XSS in Error Messages
- Java: route error pages through the same JSP/Thymeleaf auto-escaping templates used elsewhere, via a configured
<error-page>inweb.xml. - Node.js: use the same auto-escaping templating engine for error-handling middleware responses as for normal routes.
- Python/Flask/Django: use
escape()/Jinja2 auto-escaping for any offending value written into a custom error handler, as shown above. - PHP: use
htmlspecialchars()on any value echoed into a custom error page, or route errors through the same auto-escaping template engine as normal pages.
How to Ask AI to Check Your Code for XSS in Error Messages
Review the following [language] code block for potential CWE-81 XSS-in-error-message vulnerabilities and rewrite it to escape any user input echoed into the error page: [paste code here]
XSS in Error Messages Best Practices Checklist
✅ Apply the same output encoding to error pages as normal pages ✅ Prefer generic error messages over echoing raw offending input ✅ Route error handling through the same auto-escaping templates as normal pages ✅ Test error-triggering inputs with script payloads during review
XSS in Error Messages FAQ
How does an error page become an XSS injection point?
If an application reflects the input that caused an error (e.g. “Invalid value:
How is this different from Basic XSS (CWE-80)?
CWE-80 covers the general failure to escape script-enabling characters anywhere in a page; CWE-81 is the specific case where that unescaped reflection happens on an error page, which developers often forget to treat with the same care as normal output.
How could this weakness also relate to information disclosure?
MITRE notes this weakness can also be viewed as Generation of Error Message Containing Sensitive Information (CWE-209) — an error page reflecting raw input can both enable script injection and leak details about internal processing.
How do I detect this in my own application?
Deliberately trigger validation or processing errors using a script payload as the offending input, and check whether the resulting error page reflects it unescaped.
How do I fix this correctly?
Apply the same automatic contextual output encoding to error pages as to any other page, and avoid writing raw user input into error messages wherever possible.
How does avoiding user input in error text help beyond escaping alone?
A generic error message (“Invalid input provided”) that never echoes the offending value back to the page removes the injection point entirely, rather than relying on escaping being applied correctly every time.
How can PenScan help find this weakness?
PenScan deliberately triggers error conditions with script payloads as the input and checks whether the resulting error page reflects them unescaped.
Vulnerabilities Related to XSS in Error Messages
| CWE | Name | Relationship |
|---|---|---|
| CWE-79 | Improper Neutralization of Input During Web Page Generation (‘Cross-site Scripting’) | ChildOf |
| CWE-209 | Generation of Error Message Containing Sensitive Information | CanAlsoBe |
| CWE-390 | Detection of Error Condition Without Action | CanAlsoBe |
References
Scan Your Own Site
Manual code review catches what you know to look for. An automated scan catches what you didn’t. Scan your own website using PenScan to find XSS in error messages and other risks before an attacker does.