Contact Us

How to mitigate
cross-site scripting

Author: Mark Stone

While cross-site scripting (XSS) is a website vulnerability that’s existed since the 1990s, XSS is still prominent today. Cross-site scripting is one of the most commonly detected vulnerabilities in Verizon's 2020 Data Breach Investigations Report and has been listed as one of the Open Web Application Security Project's top 10 vulnerabilities since its first publication.

Here's a closer look at the challenge of how to mitigate cross-site scripting.

What is cross-site scripting?

With cross-site scripting, an attacker injects their own code onto a legitimate website; the code then gets executed when the site is loaded onto the victim's browser.

XSS primarily exploits vulnerabilities existing in programming languages such as Flash, ActiveX, VBScript and JavaScript. JavaScript is most common due to its close integration with most browsers. Because XSS can exploit these popular platforms, XSS attacks are both dangerous and common.

How does cross-site scripting work?

XSS works because web browsers inherently trust that the code behind the websites they load will be "normal" and secure. In popular XSS attacks, malicious code is either added to the end of a URL or posted directly onto a page that displays user-generated content.

These attacks succeed because vulnerabilities are widespread and can happen whenever a web application fails to validate or encode user input. In many cases, the unsuspecting user's browser will trust—and therefore execute—the malicious script.

What is cross-site scripting's aim?

An XSS attack's primary goal is to take over access to the user's resources or data. With the right access, the attacker can read data, impersonate the user, intercept confidential data or even make website changes.

What are the risks of cross-site scripting?

The risks of an XSS are dangerous yet straightforward. A successful attack allows the attacker to perform all the available actions of the target user in a web application—including sending messages, capturing keystrokes or conducting financial transactions. XSS scripts may access cookies or session tokens or other sensitive browser data.

How to mitigate cross-site scripting risks

Preventing cross-site scripting isn't a simple fix that you can turn on or off. Depending on the web application, protection strategies will differ. That said, it's important to consider the following strategies for how to mitigate cross-site scripting.

  • Whenever possible, prohibit HTML code in inputs. Preventing users from posting HTML code into form inputs is a straightforward and effective measure.
  • Validate inputs. If you're going to accept form inputs, validating the data to ensure it meets specific criteria will be helpful
  • Secure your cookies. Setting rules for your web applications defining how cookies are handled can prevent XSS and even block JavaScript from accessing cookies.
  • Sanitize data. Similar to validation, sanitizing occurs after data has been posted but before it is executed. Look for online tools like HTMLSanitizer to sanitize HTML code online for XSS vulnerabilities.
  • Use a web application firewall (WAF). Rules can be created on a WAF to specifically address XSS by blocking abnormal server requests. A robust WAF should be a key component of your organization's security strategy, as it can also prevent SQL injection attacks, distributed denial-of-service attacks and other common threats.

When it comes to how to mitigate cross-site scripting, a vulnerability assessment or penetration test (or preferably both) can be incredibly helpful to identify not only XSS but also any other vulnerabilities within your network.

Learn how to mitigate cross-site scripting with Verizon's Web Application Firewall solution.