June 22, 2026
XSS, CSRF, SQL Injection, and SSRF Explained with Real Stories
Beginner-friendly attack explanations and how developers prevent them
Wanuja Ranasinghe
2 min read
Web security sounds complex, but most real-world attacks come from a few common mistakes. In this article, we'll break down four major web vulnerabilities:
- XSS
- CSRF
- SQL Injection
- SSRF
We'll also look at how real systems get attacked and how developers prevent them.
1. XSS (Cross-Site Scripting)
๐ง What it is
XSS happens when an attacker injects malicious JavaScript into a website, and other users unknowingly run it in their browser.
๐ Real story
Imagine a comment section on a blog.
A user posts:
Great post! <script>alert('Hacked!')</script>Great post! <script>alert('Hacked!')</script>If the website does not clean input properly, every visitor will see that script run in their browser.
Attackers can:
- Steal cookies
- Hijack sessions
- Deface pages
๐ก๏ธ How to prevent it
XSS happens when user input is shown as code instead of plain text.
- Escape output before rendering Convert special characters like < and > into safe text so browsers don't execute them.
- Use Content Security Policy (CSP) Restrict which scripts the browser is allowed to run, blocking most injected scripts.
- Never render raw HTML from users
Avoid
innerHTMLor equivalent unsafe rendering methods unless strictly sanitized. - Use modern frameworks (React, Angular, etc.) They automatically escape output by default, reducing risk.
2. CSRF (Cross-Site Request Forgery)
๐ง What it is
CSRF tricks a logged-in user into performing unwanted actions on a site without their knowledge.
๐ Real story
Imagine you are logged into your bank.
You visit a malicious website in another tab.
That website secretly sends a request like:
Transfer $1000 to attacker accountTransfer $1000 to attacker accountBecause you're already logged in, your browser automatically includes your session cookie. The bank thinks it's you.
๐ก๏ธ How to prevent it
CSRF works because the browser automatically sends cookies with requests.
- Use CSRF tokens Add a unique token to every request and validate it on the server.
- Set cookies with SameSite policy
Use
SameSite=StrictorLaxto prevent cross-site requests. - Require re-authentication for sensitive actions Ask the user to confirm password or OTP for critical operations.
- Check Origin / Referer headers Ensure requests are coming from your trusted domain.
3. SQL Injection
๐ง What it is
SQL Injection happens when attackers manipulate database queries by injecting malicious SQL through input fields.
๐ Real story
A login form runs this query:
SELECT * FROM users
WHERE username = 'admin' AND password = '123'SELECT * FROM users
WHERE username = 'admin' AND password = '123'Attacker enters:
username: admin' --
password: anythingusername: admin' --
password: anythingQuery becomes:
SELECT * FROM users
WHERE username = 'admin' --' AND password = 'anything'SELECT * FROM users
WHERE username = 'admin' --' AND password = 'anything'The password check is ignored.
๐ก๏ธ How to prevent it
SQL Injection happens when user input becomes part of a SQL query.
- Use parameterized queries (prepared statements) Keep SQL logic and user input separate.
- Never concatenate user input into SQL Avoid building queries using string joins.
- Use ORM safely ORMs reduce risk, but still validate inputs properly.
- Apply least-privilege DB access Database users should only have required permissions, not full admin access.
4. SSRF (Server-Side Request Forgery)
๐ง What it is
SSRF happens when an attacker tricks your server into making requests to internal or restricted systems.
๐ Real story
A feature lets users fetch images from a URL:
https://yourapp.com/fetch-image?url=http://example.com/image.pnghttps://yourapp.com/fetch-image?url=http://example.com/image.pngAttacker changes it to:
http://localhost:8080/adminhttp://localhost:8080/adminNow your server requests its own internal admin panel and returns sensitive data.
๐ก๏ธ How to prevent it
SSRF happens when attackers force your server to call unintended URLs.
- Allowlist trusted domains only Only permit requests to known safe domains.
- Block internal IP ranges
Prevent access to
localhost,127.0.0.1,169.254.x.x, and private networks. - Disable redirects in server requests Prevent attackers from bypassing restrictions.
- Validate and sanitize all URLs Ensure input is a valid, expected external URL format.
Wrapping Up
Most security vulnerabilities are not "advanced hacking tricks". They happen when user input is trusted too much.
A good rule of thumb:
Never trust input from the user โ always validate, sanitize, and restrict it.
If you build APIs or web apps, understanding these four attacks already puts you ahead of many developers.
Thanks for reading โค๏ธ Follow me for more software architecture content.