What Is Cross-Site Scripting (XSS) ?
What Is Cross-Site Scripting (XSS) ?
Cross-site scripting (also known as XSS) is a web security vulnerability that allows an attacker to
compromise the interactions that users have with a vulnerable application. It allows an attacker
to circumvent the same origin policy, which is designed to segregate different websites from
each other. Cross-site scripting vulnerabilities normally allow an attacker to masquerade as a
victim user, to carry out any actions that the user is able to perform, and to access any of the
user's data. If the victim user has privileged access within the application, then the attacker
might be able to gain full control over all of the application's functionality and data.
Reflected XSS, where the malicious script comes from the current HTTP request.
Stored XSS, where the malicious script comes from the website's database.
DOM-based XSS, where the vulnerability exists in client-side code rather than server-
side code.
The application doesn't perform any other processing of the data, so an attacker can easily
construct an attack like this:
https://insecure-website.com/status?message=<script>/*+Bad+stuff+here...+*/</script>
If the user visits the URL constructed by the attacker, then the attacker's script executes in the
user's browser, in the context of that user's session with the application. At that point, the
script can carry out any action, and retrieve any data, to which the user has access.
The application doesn't perform any other processing of the data, so an attacker can easily send
a message that attacks other users:
<p><script>/* Bad stuff here... */</script></p>
In a typical case, the input field would be populated from part of the HTTP request, such as a
URL query string parameter, allowing the attacker to deliver an attack using a malicious URL, in
the same manner as reflected XSS.
In a brochureware application, where all users are anonymous and all information is
public, the impact will often be minimal.
In an application holding sensitive data, such as banking transactions, emails, or
healthcare records, the impact will usually be serious.
If the compromised user has elevated privileges within the application, then the impact
will generally be critical, allowing the attacker to take full control of the vulnerable
application and compromise all users and their data.
Filter input on arrival. At the point where user input is received, filter as strictly as
possible based on what is expected or valid input.
Encode data on output. At the point where user-controllable data is output in HTTP
responses, encode the output to prevent it from being interpreted as active content.
Depending on the output context, this might require applying combinations of HTML,
URL, JavaScript, and CSS encoding.
Use appropriate response headers. To prevent XSS in HTTP responses that aren't
intended to contain any HTML or JavaScript, you can use the Content-Type and X-
Content-Type-Options headers to ensure that browsers interpret the responses in the
way you intend.
Content Security Policy. As a last line of defense, you can use Content Security Policy
(CSP) to reduce the severity of any XSS vulnerabilities that still occur.