Notes
Injection in JPA
First, injection. People assume an ORM like JPA makes injection impossible; it does not. Concatenating user input into a JPQL or native query is just as injectable as raw SQL. The fix is the same as last episode: bound parameters, with named parameters in Spring Data or positional ones in native queries. Two extra Java traps: dynamic ORDER BY or column names from user input cannot be bound as parameters, so validate them against an allowlist, and the Criteria API or QueryDSL is safer for dynamic queries.
// WRONG: concatenated JPQL
"from User where name = '" + n + "'"
// RIGHT: bound parameter
@Query("from User where name = :n")
User find(@Param("n") String n);
// native queries: still use ?1,
// never string-build. Avoid
// dynamic sort/column from input Unsafe deserialization
Unsafe deserialization is the most Java-specific entry on this list. Deserializing attacker-controlled bytes with native Java serialization can lead to remote code execution through gadget chains in libraries on the classpath. Never call readObject on untrusted data. For JSON, keep Jackson's polymorphic default typing off, because it lets an attacker choose which class to instantiate. For XML, disable external entity resolution on every parser to prevent XXE, which reads server files and reaches internal services.
- native Java serialization of untrusted data = remote code execution (gadget chains)
- never readObject() on untrusted bytes
- JSON: avoid polymorphic type handling from input (Jackson default typing off)
- XML: disable external entities (XXE) on every parser
SSRF
Server-side request forgery, or SSRF, is increasingly the highest-impact web bug, especially in the cloud. Your server fetches a URL the user supplied, for a webhook, an image, a PDF or an import, and an attacker points it at internal services or the cloud metadata endpoint to steal credentials. The fix is to allowlist the hosts and schemes you will fetch, resolve the DNS and check the resulting address is not private or link-local, and disable redirects. Never fetch an arbitrary user-supplied URL.
- user supplies a URL; the server fetches it
- attacker targets internal services, cloud metadata (169.254.169.254), localhost
- fix: allowlist hosts and schemes, resolve DNS and check the IP, block private ranges, no redirects
- applies to webhooks, image fetch, PDF render, imports
Mass assignment
Mass assignment, also called over-posting, is common in Spring. You bind the request body directly onto a JPA entity for convenience. An attacker adds a field you never intended to accept, such as role equals admin or a balance, and because the entity has that field, Spring binds it. The fix is to bind to a data transfer object that contains only the fields a client may set, then map it to the entity explicitly in your code. Never bind requests straight onto your persistence entities.
- binding the request body straight onto a JPA entity
- attacker adds "role":"ADMIN" or "balance":99999 and it is bound
- fix: bind to a DTO with only the allowed fields
- map DTO -> entity explicitly
- never expose entities directly in controllers
Config footguns
A cluster of configuration footguns. Actuator endpoints are powerful; never expose env, heapdump or mappings to the internet, and secure the rest. For CORS, do not combine a wildcard origin with credentials, and list only trusted origins. For CSRF, keep Spring's protection for cookie-session apps; disabling it for a purely stateless token API is reasonable if you know why. And turn off detailed error stack traces in production so you do not leak internals.
- Actuator: never expose env, heapdump or mappings; restrict and secure it
- CORS: no "*" with credentials; list origins
- CSRF: keep it for cookie- session apps; off is fine for stateless token APIs
- no error stack traces in production
Secrets and dependencies
Two problems live around your code, not in it. Secrets: never put database passwords or API keys in application dot properties or in git. Inject them at runtime from environment variables or a secrets manager, which Part twelve covers. And dependencies: two of the worst recent Java vulnerabilities, Log4Shell and Spring4Shell, were in dependencies, not application code. Run software composition analysis in the pipeline and keep Spring Boot and libraries current.
- secrets: never in application.properties or git; use env vars or a vault, inject at runtime
- dependencies: Log4Shell and Spring4Shell were dependency issues; run SCA (OWASP Dependency-Check, Snyk) and patch
- keep Spring Boot current
Validation in Spring
Tie it back to validation. Spring supports Bean Validation with annotations on DTOs: not blank, size limits, email format, numeric ranges. Put at valid on the controller parameter and Spring enforces them, returning a clean error when input is malformed. This gives server-side allowlist validation declaratively, where input enters the app. Combine it with last episode's output defenses, parameterised queries and auto-escaping, for both halves of injection defense.
public record CreateUser(
@NotBlank @Size(max=50)
String name,
@Email String email,
@Min(0) @Max(150) int age) {}
// @Valid on the controller param
public X create(@Valid @RequestBody
CreateUser body) { ... } Common mistakes
The recurring findings, in one place. Concatenated JPQL or native SQL. Binding requests straight to entities, allowing mass assignment. Role checks with no ownership check, allowing one user to read another's data. Deserializing untrusted data, or leaving Jackson default typing on. SSRF from fetching user-supplied URLs. Actuator endpoints or error stack traces exposed to the internet. And secrets in application dot properties or committed to git. If you review for just these, you catch most Spring security bugs.
- concatenated JPQL or native SQL
- binding requests to entities (mass assignment)
- role checks without ownership checks (IDOR)
- deserializing untrusted data; Jackson default typing
- SSRF on user-supplied URLs
- Actuator or stack traces exposed; secrets in config
Say it like this
In the interview: Spring Security that denies by default, with method-level PreAuthorize including ownership checks, not just roles, to prevent insecure direct object references. I bind to DTOs not entities to stop mass assignment, use Bean Validation and parameterised JPA. I avoid unsafe deserialization, keep Jackson default typing off, allowlist server-side URL fetches against SSRF, keep secrets in a vault, scan dependencies, and lock down Actuator.
- "Deny-by-default Spring
- Security, method-level
- @PreAuthorize with ownership
- checks, DTOs not entities, Bean Validation, parameterised
- JPA, no unsafe
- deserialization, SSRF
- allowlists, secrets in a vault, SCA on dependencies, and Actuator locked down."