Web Security: Injection, XSS, CSRF and SSRF
Backend security interviews revolve around the OWASP Top 10. You should explain SQL injection, cross-site scripting, cross-site request forgery and server-side request forgery, and show the fix for each in code.
What you will learn
- Prevent SQL and NoSQL injection
- Explain stored and reflected XSS and output encoding
- Prevent CSRF with SameSite cookies and tokens
- Prevent SSRF when fetching user-supplied URLs
Key concepts
SQL injection
User input is concatenated into SQL, so input like ' OR 1=1 -- changes the query. Fix: parameterised queries everywhere; the driver sends values separately from the SQL.
XSS
Attacker-controlled script runs in another user's browser, through stored data (a comment) or a reflected parameter. Fix: escape output by context, sanitise HTML you must allow (DOMPurify), and set a Content-Security-Policy.
CSRF
A malicious site makes the victim's browser send a request to your site with their cookies. Fix: SameSite=Lax/Strict cookies, CSRF tokens for cookie-authenticated forms, and checking Origin.
SSRF
Your server fetches a URL the user provides, and the attacker points it at internal addresses (169.254.169.254 cloud metadata, localhost admin panels). Fix: allow-list hosts, resolve and block private IP ranges, disable redirects.
Step by step
Injection, before and after
// Vulnerable
await db.query("SELECT * FROM users WHERE email = '" + req.body.email + "'")
// Safe: parameterised
await db.query('SELECT * FROM users WHERE email = $1', [req.body.email])
// MongoDB operator injection: { "password": { "$ne": null } }
// Fix: validate types with a schema so password must be a stringSecurity checklist for an API
- helmet() security headers and a CSP
- Validate and type-check all input
- Parameterised queries
- Least-privilege database user
- Dependency scanning (npm audit, Dependabot)
- Secrets in a secrets manager, never in Git
- HTTPS everywhere with HSTS
Examples
Blocking SSRF to private addresses
import { lookup } from 'node:dns/promises'
import ipaddr from 'ipaddr.js'
async function assertPublicUrl(raw) {
const url = new URL(raw)
if (!['http:', 'https:'].includes(url.protocol)) throw new AppError('Bad URL', 400)
const { address } = await lookup(url.hostname)
if (ipaddr.parse(address).range() !== 'unicast') throw new AppError('URL not allowed', 400)
return url
}
// also pass redirect: 'manual' to fetch so a redirect cannot jump to an internal addressPractise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Find the injection bug in a search endpoint that builds ORDER BY from a query parameter, and fix it with an allow-list.
- Write a CSP header for an app that loads scripts only from itself and one CDN.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
Do prepared statements protect ORDER BY and table names?
No. Placeholders work for values, not identifiers. Map user input to an allow-list of known column names instead.
Does a JWT in an Authorization header need CSRF protection?
No, because the browser does not attach that header automatically; a malicious site cannot add it. CSRF is a problem when authentication rides on cookies.
Summary
Parameterise queries, validate types, encode output and use CSP against XSS, use SameSite cookies and tokens against CSRF, block private addresses against SSRF, and keep headers, dependencies and secrets in order.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus