Cross-site Scripting (XSS) Tutorial: Learn About XSS Vulnerabilities, Injections, and Cross-Site Scripting Prevention

What Is Cross-Site Scripting?

Cross-Site Scripting attacks (also known as XSS attacks) target scripts embedded in a page that are executed on the client-side (in the user’s web browser) rather than on the server-side. Cross-site scripting is one of the most common application-layer web attacks. XSS in itself is a threat that is brought about by the internet security weaknesses of client-side scripting languages, such as HTML and JavaScript. The concept of XSS is to manipulate client-side scripts of a web application to execute in the manner desired by the malicious user. Such a manipulation can embed a script in a page that can be executed every time the page is loaded, or whenever an associated event is performed.

XSS is the most common security vulnerability in software today. This should not be the case as XSS is easy to find and easy to fix. XSS vulnerabilities can have consequences such as tampering and sensitive data theft.

Click here for Remediation Guidance for XSS in Java, or here for Remediation Guidance in ASP.NET

State of Software Security v11

Read the Report

Key Concepts of XSS

  • XSS is a web-based attack performed on vulnerable web applications.
  • In XSS attacks, the victim is the user and not the application.
  • In XSS attacks, malicious content is delivered to users using JavaScript.

Explaining Cross-Site Scripting

An XSS vulnerability arises when web applications take data from users and dynamically include it in web pages without first properly validating the data. XSS vulnerabilities allow an attacker to execute arbitrary commands and display arbitrary content in a victim user's browser. A successful XSS attack leads to an attacker controlling the victim’s browser or account on the vulnerable web application. Although XSS is enabled by vulnerable pages in a web application, the victims of an XSS attack are the application's users, not the application itself. The potency of an XSS vulnerability lies in the fact that the malicious code executes in the context of the victim's session, allowing the attacker to bypass normal security restrictions.

Cross-Site Scripting Video


XSS Attack Examples

Reflective XSS

There are many ways in which an attacker can entice a victim into initiating a reflective XSS request. For example, the attacker could send the victim a misleading email with a link containing malicious JavaScript. If the victim clicks on the link, the HTTP request is initiated from the victim's browser and sent to the vulnerable web application. The malicious JavaScript is then reflected back to the victim's browser, where it is executed in the context of the victim user's session. 

xss cross site scripting attack example


Persistent XSS

Consider a web application that allows users to enter a username that is displayed on each user’s profile page. The application stores each username in a local database. A malicious user notices that the web application fails to sanitize the username field and inputs malicious JavaScript code as part of their username. When other users view the attacker’s profile page, the malicious code automatically executes in the context of their session.

persistent xss cross site scripting example

Impact of Cross-Site Scripting

When attackers succeed in exploiting XSS vulnerabilities, they can gain access to account credentials. They can also spread web worms or access the user’s computer and view the user’s browser history or control the browser remotely. After gaining control to the victim’s system, attackers can also analyze and use other intranet applications.
By exploiting XSS vulnerabilities, an attacker can perform malicious actions, such as:

  • Hijack an account.
  • Spread web worms.
  • Access browser history and clipboard contents.
  • Control the browser remotely.
  • Scan and exploit intranet appliances and applications.

Identifying Cross-Site Scripting Vulnerabilities

XSS vulnerabilities may occur if:

  • Input coming into web applications is not validated
  • Output to the browser is not HTML encoded

XSS Examples

Example 1.
For example, the HTML snippet:

<title>Example document: %(title)</title>

is intended to illustrate a template snippet that, if the variable title has value Cross-Site Scripting, results in the following HTML to be emitted to the browser:

<title>Example document: XSS Doc</title>

A site containing a search field does not have the proper input sanitizing. By crafting a search query looking something like this:


sitting on the other end, at the web server, you will be receiving hits where after a double space is the user's cookie. If an administrator clicks the link, an attacker could steal the session ID and hijack the session.


Example 2.
Suppose there's a URL on Google's site,, which returns HTML documents containing the fragment

<p>Your search for 'flowers' returned the following results:</p>

i.e., the value of the query parameter q is inserted into the page returned by Google. Suppose further that the data is not validated, filtered or escaped. could put up a page that causes the following URL to be loaded in the browser (e.g., in an invisible<iframe>):


When a victim loads this page from, the browser will load the iframe from the URL above. The document loaded into the iframe will now contain the fragment

<p>Your search for 'flowers <script>evil_script()</script>'


returned the following results:</p>

Loading this page will cause the browser to execute evil_script(). Furthermore, this script will execute in the context of a page loaded from


Resources for Cross-Site Scripting Prevention

Cross-site scripting prevention should be addressed in the early stages of development; however, if you’re already well into production there are still several cross-site prevention steps you can take to prevent an attack.

This blog post provides a summary of what you need to know about Cross-Site Scripting

XSS Cheat Sheet: Prevent a Cross-Site Scripting Attack


Secure Coding Handbook

Get the Handbook

More FREE Security Threat Tutorials from Veracode

Avoiding cross-site scripting vulnerabilities with Veracode.

Veracode provides leading application security solutions that help to protect the software that is critical to business operations. Built on a cloud-based platform, Veracode’s comprehensive testing methodologies allow developers and administrators to test for vulnerabilities throughout the development process, from inception through production. From tools for the developers IDE to static analyses and web vulnerability scanners, Veracode provides on-demand, SaaS-based services that enable organizations to embed security testing into development without sacrificing agility or speed.

Fixing cross-site scripting errors in applications involves three steps:

  • Applications must validate data input to the web application from user browsers.
  • All output from the web application to user browsers must be encoded.
  • Users must have the option to disable client-site scripts.

Veracode’s testing services can quickly scan control and data flow for an application and accurately identify cross-site scripting and other flaws in code that is built in-house or acquired from third-parties. Scan results are returned quickly – usually within four hours – and include a step-by-step remediation plan that helps to accelerate fixes and prioritize efforts.

Veracode testing methodologies for cross-site scripting.

Veracode provides multiple testing and security analysis services to help mitigate cross-site scripting flaws:

  • Veracode Static Analysis scans binaries to identify errors in code that is built, bought or assembled.
  • Veracode Static Analysis IDE Scan provides immediate feedback within an IDE, alerting developers to potential flaws as code is being written.
  • Veracode WAS is a web application scanner that discovers all public-facing web applications and performs lightweight and authenticated scans to identify cross-site scripting vulnerabilities.
  • Veracode Vendor Application Security Testing helps to identify vulnerabilities in third-party code.
  • Veracode Software Composition Analysis helps to prevent cross-site scripting errors in open source components and commercial code.

Learn more about cross-site scripting and Veracode, and about Veracode solutions for software containers.

Questions About Application Security?

Contact Us