What is cross-site scripting (XSS), and what are the different types of XSS attacks?
- User
What is cross-site scripting (XSS), and what are the different types of XSS attacks?
- Assistant
We need to answer: "What is cross-site scripting (XSS), and what are the different types of XSS attacks?" Provide a clear explanation.
Cross-site scripting (XSS) is a type of security vulnerability that allows attackers to inject malicious scripts into web pages viewed by other users. The "cross-site" refers to the fact that the malicious script runs in the context of the trusted site, bypassing same-origin policies. There are three main types: stored (persistent), reflected (non-persistent), and DOM-based XSS. Some also mention XSS via flash or other vectors, but typically the three.
We should explain each:
- Stored XSS: The malicious script is stored on the target server (e.g., in a database, comment field, etc.) and then served to users when they request the page. It's persistent and can affect many users.
- Reflected XSS: The malicious script is reflected off the web server, typically via a link or request, and executes immediately. Often used in phishing emails. The payload is in the request and the server returns it as part of the response.
- DOM-based XSS: The vulnerability exists in client-side JavaScript code, not in the server. The script manipulates the DOM based on untrusted data (e.g., URL parameters) and causes execution of malicious code. The payload is not sent to the server; the attack occurs entirely in the browser.
Additionally, there are some subcategories:
- Self-XSS: The victim executes the script themselves, but it's often combined with social engineering. Not considered a true XSS.
- XSS in HTTP headers: like in the Referer header, etc.
- Mutation-based XSS: using the way browsers parse HTML or execute scripts.
- Some talk about Non-persistent vs Persistent, but those are basically reflected vs stored.
We should also note the impact: cookie theft, session hijacking, phishing, defacement, malware distribution, etc.
The question: "What is cross-site scripting (XSS), and what are the different types of XSS attacks?" So we should define XSS, then list and explain the types.
We should write a concise answer, but thorough. Since it's a typical interview question, we can provide a structured response.
We'll start: "Cross-site scripting (XSS) is a client-side code injection attack where an attacker injects malicious scripts into a trusted website. These scripts execute in the victim's browser, allowing the attacker to bypass the same-origin policy and access sensitive data, hijack user sessions, or perform actions on behalf of the victim."
Then list types:
- Stored (or Persistent) XSS
- Reflected (or Non-persistent) XSS
- DOM-based XSS
We can also mention that some classifications include "Blind XSS" (where the attacker cannot see the result immediately) and "XSS via Third-Party Applications". But main ones are those three.
We should also note that there is also "Mutated XSS" and "DOM XSS" has subtypes like location.hash, location.search, document.referrer, etc.
But the question likely expects the three main types.
Let's write a good answer.
Also note: modern contexts might include "XSS in WebSockets", "XSS in JSONP", but we'll stick to basics.
We need to produce final answer with explanation.
We should also mention that XSS can be mitigated by input validation, output encoding, content security policy, etc. But the question only asks for definition and types. So we can keep it focused.
But maybe we could briefly mention prevention in passing? The question doesn't ask, but we can end with "Understanding these types helps in proper mitigation." Not necessary.
We'll provide:
- Definition of XSS.
- Explanation of how it works (injection, execution as same origin).
- Types: Stored, Reflected, DOM-based. For each, definition and typical example.
We'll also note that there is "Self-XSS" not a true XSS.
We'll also mention "Blind XSS" as a variant of stored where attacker cannot see the payload being executed.
But main answer: three types.
Let's craft a thorough response.
We'll include:
-
Detailed definition: "Cross-site scripting (XSS) is a security vulnerability that allows attackers to inject client-side scripts (usually JavaScript) into web pages viewed by other users. The injected code runs in the browser under the security context of the vulnerable site, allowing the attacker to bypass the same-origin policy and access sensitive data (like cookies, session tokens), perform actions on behalf of the victim, or deface the site."
-
Types:
-
Stored XSS (Persistent): The malicious script is permanently stored on the target server (e.g., database, comment field, forum post, user profile). When a victim visits the affected page, the script is served and executed. This is the most damaging because it can affect many users automatically. Example: A comment that includes alert('XSS') that gets saved and displayed to all viewers.
-
Reflected XSS (Non-persistent): The malicious script is not stored. Instead, it is part of a request (e.g., URL query parameter, form input) that the server reflects back to the user in the response. Typically delivered via a crafted link. When the user clicks the link, the injected payload is sent to the server, which returns it in the page, causing immediate execution. Example: Search result page that echoes the search term without sanitization: http://example.com/search?q=malicious().
-
DOM-based XSS: The vulnerability exists entirely on the client side. The server response may be safe, but client-side JavaScript processes untrusted data (usually from the URL, DOM, or user input) in an unsafe way, leading to code injection. The malicious payload does not need to reach the server. Example: A page that reads location.hash and writes it to the DOM via innerHTML without sanitization: and script: document.getElementById('content').innerHTML = location.hash.substring(1); Attacker can craft URL with #stealCookies(). When the victim visits that URL, the script runs.
We can also mention:
- Self-XSS: where the victim is tricked into pasting malicious code into their browser console. Not a true XSS because it requires user action and infects only that user.
- Blind XSS: a variant of stored XSS where the attacker cannot directly see the effect (e.g., admin panels), but the payload is stored and executed later, possibly sending data to an attacker-controlled server. It's often discovered using automated tools.
- DOM XSS over other sources: e.g., using document.referrer, window.name, etc., as sources.
But the main three are enough.
We should also explain that XSS differs from CSRF which is about forging requests.
We'll produce final answer.
Let's structure:
- Short intro.
- Definition.
- Types with bullet points and explanations.
- Optionally mention impact.
Given the instruction "What is cross-site scripting (XSS), and what are the different types of XSS attacks?" Probably expects a straightforward answer.
We'll write:
Cross-site scripting (XSS) is a web security vulnerability that enables attackers to inject malicious scripts into web pages viewed by other users. These scripts execute in the victim's browser in the security context of the trusted site, allowing the attacker to bypass the same-origin policy and potentially steal sensitive data, hijack user sessions, or perform unauthorized actions.
The three main types of XSS attacks are:
-
Stored (Persistent) XSS: The malicious script is permanently stored on the target server (e.g., database, comment fields). When a user accesses the affected page, the stored script is served and executed. This type can affect many users and is often considered the most severe because it can be triggered without any further interaction from the attacker.
Example: An attacker posts a comment containing stealCookies(). The comment is saved and displayed to all visitors, causing the script to run in their browsers.
-
Reflected (Non-persistent) XSS: The malicious script is part of a request (typically a URL parameter) and is reflected back by the server in its response. The attack usually involves tricking a user into clicking a crafted link that includes the payload. When the page loads, the script executes.
Example: A search page that displays the search term without sanitizing it: http://vulnerable-site.com/search?q=alert('XSS'). The server returns a page containing the script, which runs in the user's browser.
-
DOM-based XSS: The vulnerability lies in client-side JavaScript that processes untrusted data (e.g., URL fragments, form inputs) in an unsafe manner. The malicious payload never reaches the server; instead, the script manipulates the Document Object Model (DOM) to introduce executable code. Because the attack happens entirely in the browser, server-side defenses may not detect it.
Example: A page that reads the URL hash and inserts it into the page using innerHTML: document.body.innerHTML = location.hash; An attacker can create a link like http://site/page#malicious(). When the user visits, the script runs without any server-side interaction.
Other variants include Blind XSS (a form of stored XSS where the attacker cannot directly observe the payload execution) and Self-XSS (which relies on social engineering rather than a site vulnerability), but the three types above are the fundamental categories.
That should be a comprehensive answer. Cross-site scripting (XSS) is a web security vulnerability that allows an attacker to inject malicious client‑side scripts (usually JavaScript) into a trusted website. The injected script executes in the victim’s browser under the same origin as the vulnerable site, thereby bypassing the same‑origin policy. This can lead to session hijacking, credential theft, phishing, defacement, malware distribution, and other attacks.
Main Types of XSS Attacks
-
Stored (Persistent) XSS
The malicious script is permanently stored on the target server (e.g., in a database, comment field, forum post, or user profile). When a user accesses the affected page, the stored script is served and executed automatically. Because the payload is retained, it can affect many users over time and is often considered the most severe form.Example: An attacker posts a comment containing
<script>stealCookies()</script>. The comment is saved and displayed to all visitors, causing the script to run in their browsers without any additional action. -
Reflected (Non‑persistent) XSS
The malicious script is part of a request (typically a URL query parameter or form input) and is reflected back by the server in its response. The attack relies on social engineering to trick a user into clicking a crafted link. The payload does not get stored; it executes immediately when the page loads.Example: A search page echoes the search term without sanitization:
http://vulnerable-site.com/search?q=<script>alert('XSS')</script>
The server returns a page that includes the script, which runs in the user’s browser. -
DOM‑based XSS
The vulnerability exists entirely on the client side. The server’s response may be safe, but client‑side JavaScript processes untrusted data (e.g.,location.hash,document.referrer, form inputs) in an unsafe manner, resulting in code injection. The malicious payload never reaches the server; manipulation of the Document Object Model (DOM) introduces the executable script.Example: A page reads the URL fragment and writes it to the DOM using
innerHTML:
document.getElementById('content').innerHTML = location.hash;
An attacker can craft a link likehttp://site/page#<script>malicious()</script>. When the user visits, the script executes without any server‑side interaction.
Other Variants (Less Common)
- Blind XSS: A subtype of stored XSS where the attacker cannot directly observe the payload’s execution (e.g., in an admin panel). The attacker plants the script and later captures data (e.g., via network requests to a remote server) when the victim triggers it.
- Self‑XSS: Requires the victim themselves to paste malicious code into their browser console. It relies on social engineering and is not a true XSS vulnerability in the website.
- XSS via Third‑Party Components: Such as flash, Java applets, or JSONP callbacks that execute attacker‑controlled code.
Understanding these core types is essential for identifying, preventing, and mitigating XSS risks effectively.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.