Quick revision
The short answer
We replace the concatenated user input with bind parameters, :name or ?1 in @Query or setParameter() with EntityManager, so the database treats the input as a value and never as SQL. Sort columns and other identifiers cannot be bound, so we map them through an allow-list, and for truly dynamic filters we use the Criteria API or Spring Data Specifications. Then we add a test with malicious input and share the finding with a review checklist item so it does not come back.
What to remember
In this answer, we saw that SQL injection happens when user input is concatenated into a query, so the database parses it as code. We fixed it with bind parameters in @Query or setParameter(), mapped sort columns and other identifiers through an allow-list because they cannot be bound, used Specifications for dynamic filters, added tests with malicious input, and shared the finding with a checklist item and a failing static analysis rule to prevent repeats.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We replace the concatenated user input with bind parameters, :name or ?1 in @Query or setParameter() with EntityManager, so the database treats the input as a value and never as SQL. Sort columns and other identifiers cannot be bound, so we map them through an allow-list, and for truly dynamic filters we use the Criteria API or Spring Data Specifications. Then we add a test with malicious input and share the finding with a review checklist item so it does not come back.
Why this question is asked
SQL injection is one of the oldest web vulnerabilities and still one of the most damaging, because a single vulnerable query can expose or destroy an entire database. Spring Data protects us by default, which is why it surprises people when a static analysis tool finds it: usually someone has dropped down to a native query and built it by hand. Interviewers ask this to see whether we know the right fix and the cases where the usual fix does not apply. In this answer, we will reproduce the vulnerability, fix it with bind parameters, handle dynamic sorting safely, look at Specifications for dynamic queries, and prevent repeats with a test and a team habit.
1Reproducing the Vulnerability
A typical case is a custom repository method that searches customers by name with a native query built by concatenation.
Notice what the attacker did. The input contains a quote that ends the string literal early, and the rest of the input becomes part of the SQL. The database cannot tell which parts the developer wrote and which the user typed, because by the time the query arrives it is one string. That is the whole vulnerability: data was mixed into code.
2Fix 1: Bind Parameters
With a bind parameter, the SQL text and the value travel separately. The query is sent with a placeholder, and the driver sends the value on its own, so the database always treats it as a value, whatever characters it contains. In Spring Data JPA, we can use a named parameter in @Query, or positional ?1, or setParameter() on a hand-built query.
6 messages in order. Play them, or step through them one at a time.
With binding, the SQL is fixed before the user's value ever arrives.
Now the malicious input simply finds no customer with that strange name. Notice that a derived method such as findByName(String name), without any @Query, is also safe, because Spring Data generates a parameterised query. The danger only appears when we build SQL or JPQL strings ourselves.
3Fix 2: Allow-Listing What Cannot Be Bound
There is a catch. Bind parameters only work for values. Column names, table names and sort directions are identifiers, part of the SQL structure, and a database will not accept a placeholder there. So ORDER BY ?1 does not sort by the column named in the parameter. For these, we never pass user input through; we map it through an allow-list of known columns.
The string we concatenate now only ever comes from our own constant map, never from the request. Mapping public names to column names has a bonus: we can rename a column without changing the API.
4Fix 3: Criteria API or Specifications for Dynamic Queries
Many hand-built queries exist because the filters are optional: search by name, by city, by date range, in any combination. Building such SQL by string is where injection hides. The JPA Criteria API builds the query as objects, and Spring Data's Specification interface wraps it in small reusable pieces. Values become parameters automatically.
Here the input never touches a query string at all. Sort.by() with a property name is checked against the entity's properties, but if that name comes from the request, we still validate it against an allow-list, to give a clean 400 error instead of an exception deep in the persistence layer. In older Spring Data versions, combining specifications used where(...).and(...), so check your version.
5Preventing Repeats: a Test and a Team Habit
We add a test that proves the fix with hostile input, so a future refactoring cannot quietly bring the problem back. A Spring Boot test with Testcontainers runs it against the real database.
Finally, we share the finding. A short note in the team channel explains what was found, why concatenation was dangerous and what the safe patterns are. We add one item to the review checklist, such as 'no SQL or JPQL built by concatenating input; identifiers through an allow-list', and we keep the static analysis rule failing the build for this category. One finding then improves the whole team's code instead of one method.
6Follow-up questions
Is JPQL immune to injection?
No. JPQL built by concatenation is injectable in the same way, an attacker can change the query's conditions. The safe patterns are the same: named or positional parameters, derived query methods, or the Criteria API.
Does escaping quotes fix the problem?
Escaping is fragile: it depends on the database, the character set and every place remembering to do it. Bind parameters remove the problem structurally, because the value is never parsed as SQL. Escaping should not be the fix.
What about LIKE searches?
Bind the whole pattern as a parameter, for example name LIKE :pattern with the value built as "%" + input + "%". If users must not use wildcards themselves, escape the % and _ characters in the input and declare the escape character in the query.
7Conclusion
In this answer, we saw that SQL injection happens when user input is concatenated into a query, so the database parses it as code. We fixed it with bind parameters in @Query or setParameter(), mapped sort columns and other identifiers through an allow-list because they cannot be bound, used Specifications for dynamic filters, added tests with malicious input, and shared the finding with a checklist item and a failing static analysis rule to prevent repeats.
Open with
The root cause is that user input is concatenated into the SQL string, so a quote in the input ends the literal and the rest becomes SQL; something like x' OR '1'='1 returns every row.
Then, if they want more
The fix is bind parameters: in Spring Data I'd use @Query with :name and @Param, or ?1, or setParameter on the EntityManager query. The driver sends the value separately, so it's always treated as data. Derived query methods are already safe.
The catch is that identifiers, like the ORDER BY column or the sort direction, can't be bound, so I'd map the user's choice through an allow-list of known columns and reject anything else with a 400.
If the query is dynamic because filters are optional, I'd switch to the Criteria API or Spring Data Specifications, which build the query as objects and parameterise values automatically. Then I'd add tests with malicious input, for example that the injection string returns no rows and that an unknown sort column is rejected, ideally against the real database with Testcontainers.
And I'd share the finding with the team, add a review checklist item about never concatenating input into SQL or JPQL, and keep the static analysis rule failing the build.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A static analysis tool in your CI pipeline reports a SQL injection risk in a Spring Data...": https://scrivio-interview-library.netlify.app/architecture/platform-and-governance/ef920aadbe/