--- url: "https://portswigger.net/web-security/cross-site-scripting" titulek: "What is cross-site scripting (XSS) and how to prevent it? | Web Security Academy" h1: "Cross-site scripting" popis: "In this section, we'll explain what cross-site scripting is, describe the different varieties of cross-site scripting vulnerabilities, and spell out how to ..." jazyk: "" kanonicka: "https://portswigger.net/web-security/cross-site-scripting" drobecky: ["Web Security Academy", "Cross-site scripting"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:24:49" znaku: 12905 slov: 1983 kodovani: "utf-8" sha256: "751a46fad41e1e65a26ec819923755e6cccfc2e14f6904a80eb1f01bde7a049e" --- # Cross-site scripting In this section, we'll explain what cross-site scripting is, describe the different varieties of cross-site scripting vulnerabilities, and spell out how to find and prevent cross-site scripting. ## 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. ## How does XSS work? Cross-site scripting works by manipulating a vulnerable web site so that it returns malicious JavaScript to users. When the malicious code executes inside a victim's browser, the attacker can fully compromise their interaction with the application. #### Labs If you're already familiar with the basic concepts behind XSS vulnerabilities and just want to practice exploiting them on some realistic, deliberately vulnerable targets, you can access all of the labs in this topic from the link below. - [View all XSS labs](/web-security/all-labs#cross-site-scripting) ## XSS proof of concept You can confirm most kinds of XSS vulnerability by injecting a payload that causes your own browser to execute some arbitrary JavaScript. It's long been common practice to use the `alert()` function for this purpose because it's short, harmless, and pretty hard to miss when it's successfully called. In fact, you solve the majority of our XSS labs by invoking `alert()` in a simulated victim's browser. Unfortunately, there's a slight hitch if you use Chrome. From version 92 onward (July 20th, 2021), cross-origin iframes are prevented from calling `alert()`. As these are used to construct some of the more advanced XSS attacks, you'll sometimes need to use an alternative PoC payload. In this scenario, we recommend the `print()` function. If you're interested in learning more about this change and why we like `print()`, [check out our blog post](https://portswigger.net/research/alert-is-dead-long-live-print) on the subject. As the simulated victim in our labs uses Chrome, we've amended the affected labs so that they can also be solved using `print()`. We've indicated this in the instructions wherever relevant. ## What are the types of XSS attacks? There are three main types of XSS attacks. These are: - [Reflected XSS](/web-security/cross-site-scripting#reflected-cross-site-scripting) , where the malicious script comes from the current HTTP request. - [Stored XSS](/web-security/cross-site-scripting#stored-cross-site-scripting) , where the malicious script comes from the website's database. - [DOM-based XSS](/web-security/cross-site-scripting#dom-based-cross-site-scripting) , where the vulnerability exists in client-side code rather than server-side code. ## Reflected cross-site scripting Reflected XSS is the simplest variety of cross-site scripting. It arises when an application receives data in an HTTP request and includes that data within the immediate response in an unsafe way. Here is a simple example of a reflected XSS vulnerability: `https://insecure-website.com/status?message=All+is+well.
Status: All is well.
` 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=Status:
` 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. #### Read more - [Reflected cross-site scripting](/web-security/cross-site-scripting/reflected) - [Cross-site scripting cheat sheet](/web-security/cross-site-scripting/cheat-sheet) ## Stored cross-site scripting Stored XSS (also known as persistent or second-order XSS) arises when an application receives data from an untrusted source and includes that data within its later HTTP responses in an unsafe way. The data in question might be submitted to the application via HTTP requests; for example, comments on a blog post, user nicknames in a chat room, or contact details on a customer order. In other cases, the data might arrive from other untrusted sources; for example, a webmail application displaying messages received over SMTP, a marketing application displaying social media posts, or a network monitoring application displaying packet data from network traffic. Here is a simple example of a stored XSS vulnerability. A message board application lets users submit messages, which are displayed to other users: `Hello, this is my message!
` The application doesn't perform any other processing of the data, so an attacker can easily send a message that attacks other users: `` #### Read more - [Stored cross-site scripting](/web-security/cross-site-scripting/stored) - [Cross-site scripting cheat sheet](/web-security/cross-site-scripting/cheat-sheet) ## DOM-based cross-site scripting DOM-based XSS (also known as DOM XSS) arises when an application contains some client-side JavaScript that processes data from an untrusted source in an unsafe way, usually by writing the data back to the DOM. In the following example, an application uses some JavaScript to read the value from an input field and write that value to an element within the HTML: `var search = document.getElementById('search').value; var results = document.getElementById('results'); results.innerHTML = 'You searched for: ' + search;` If the attacker can control the value of the input field, they can easily construct a malicious value that causes their own script to execute: `You searched for: