Quick revision
The short answer
The method concatenates user input into the SQL, so a name such as x' or '1'='1 changes the query itself; that is SQL injection. We fix the value part with a bind parameter, where name = ?, and pass the name separately. A bind parameter cannot stand for a column name, so sortColumn must be checked against an allow-list and mapped to a fixed SQL fragment. While we are there, we list the columns instead of select *.
What to remember
In this answer, we saw that concatenating name and sortColumn into the SQL lets user input become code, both through a classic quote-breaking value and through a quieter ORDER BY expression. We fixed the value with a bind parameter, learned that bind parameters cannot replace identifiers, and fixed the sort with an allow-list map that turns the user's choice into SQL we wrote. We also replaced select * with explicit columns and added tests with malicious input.
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.
The method concatenates user input into the SQL, so a name such as x' or '1'='1 changes the query itself; that is SQL injection. We fix the value part with a bind parameter, where name = ?, and pass the name separately. A bind parameter cannot stand for a column name, so sortColumn must be checked against an allow-list and mapped to a fixed SQL fragment. While we are there, we list the columns instead of select *.
Why this question is asked
SQL injection has been at or near the top of web security risk lists for over twenty years, and it still appears in code reviews because string concatenation looks harmless and works in every test with normal data. Interviewers ask this find-the-bug question because it has two bugs of different kinds: one fixed by binding, and one that binding cannot fix. In this answer, we will see how the attack works on this exact method, fix the value with a bind parameter, fix the ORDER BY with an allow-list, tidy up select *, and look at how to test it.
1How the Attack Works on This Method
The method builds the SQL by gluing strings together. Whatever the caller sends as name becomes part of the SQL text, and the database cannot tell which part we wrote and which part the user wrote.
Notice how the quote in the first attack closes our string literal early, and the rest becomes a condition that is always true. The second attack is quieter: the ORDER BY clause accepts any expression, so an attacker can ask yes-or-no questions about other data and read the answers from the order of the results. With some drivers and settings, stacked statements such as ; drop table users are possible too. All of these work because user input is treated as code.
2Fix 1: Bind the Value
A bind parameter, the question mark, tells the driver that a value goes there. The driver sends the SQL text and the value separately, and the database parses the SQL before it ever sees the value, so the value can only ever be data.
The attack string is now compared, character for character, with the name column, and it matches nothing. We do not escape quotes ourselves; escaping by hand is easy to get wrong and depends on the database's rules, while binding is always correct.
3Why Binding Cannot Fix ORDER BY
It is tempting to write order by ? as well. That compiles and runs, but it does not do what we want. A bind parameter is always a value, so order by ? with the value created_at sorts every row by the constant string 'created_at', which means no useful order at all. Databases only allow parameters where a value could appear; table names, column names, keywords such as ASC and DESC, and other identifiers must be part of the SQL text.
So the identifier must come from us, not from the user. The user may choose which column, but only from a list we control, and we turn their choice into a SQL fragment we wrote.
| Part of the SQL | Bind with ? | Safe approach |
|---|---|---|
| A value: where name = ... | yes | bind parameter |
| A LIKE pattern | yes | bind the pattern, escape % and _ if literal |
| A list of values: in (...) | yes, one ? per value | NamedParameterJdbcTemplate expands a collection |
| A column in ORDER BY | no | allow-list mapped to fixed SQL |
| ASC or DESC | no | boolean or enum mapped to a keyword |
| A table name | no | allow-list, or separate queries |
4Fix 2: Map sortColumn Through an Allow-List
We map the user's choice to a fixed fragment. Anything not in the map gets a safe default or an error. Using a map rather than checking with a regular expression means the SQL that reaches the database is always text we wrote.
Notice that the concatenation is still there, but only of constants from our own map. That is the important distinction: building SQL from fixed fragments is safe, building it from input is not. The extra id column gives a stable order when two rows share a sort value, which matters for pagination. An API layer can also use an enum for the sort field, so invalid values are rejected before they reach the repository.
5Tidying Up select * and Testing the Fix
select * is not a security bug, but it is fragile in production. It returns every column, including ones the mapper does not need, such as a password hash, which then travels over the network and may end up in logs or caches. If someone adds a large column later, every query gets slower, and a row mapper that reads by position breaks when columns are reordered. Listing the columns makes the query's contract explicit.
Finally, we prove the fix with tests that run against a real database, for example with Testcontainers. One test calls the method with x' or '1'='1 and asserts an empty result; another passes an unknown sort value and asserts the default order. These tests are cheap, and they fail loudly if someone later reintroduces concatenation.
6Follow-up questions
Is escaping single quotes in name a valid fix?
It is fragile. Escaping rules differ between databases and settings, for example how backslashes are treated, and multi-byte encodings have historically allowed bypasses. It also has to be remembered at every call site. Bind parameters make the question irrelevant, because the value is never parsed as SQL.
Does JPA or Spring Data protect us automatically?
Derived query methods and JPQL with named parameters bind values, so they are safe. But JPA is just as vulnerable if we build JPQL or native SQL by concatenation, and dynamic sorting has the same identifier problem. Spring Data's Sort with property names validated against the entity, or our own allow-list, is the safe approach.
Would a WAF or input validation be enough instead?
They add depth, but they are not a fix. A web application firewall blocks known attack patterns and misses new ones, and input validation such as rejecting quotes breaks legitimate names like O'Brien. The query itself must be safe; other layers only reduce the chance that a remaining mistake is exploited.
7Conclusion
In this answer, we saw that concatenating name and sortColumn into the SQL lets user input become code, both through a classic quote-breaking value and through a quieter ORDER BY expression. We fixed the value with a bind parameter, learned that bind parameters cannot replace identifiers, and fixed the sort with an allow-list map that turns the user's choice into SQL we wrote. We also replaced select * with explicit columns and added tests with malicious input.
Open with
There are two problems here, and they need different fixes. The first is the name. It is concatenated into the SQL inside quotes, so an input like x' or '1'='1 closes the quote and adds a condition that is always true, and the query returns every user.
Then, if they want more
That is SQL injection. The fix is a bind parameter: where name = ? and pass the name as an argument to jdbcTemplate.query. The driver sends the value separately from the SQL, so it can only ever be compared as data.
The second problem is sortColumn, and binding cannot fix it, because a bind parameter can only stand for a value. Order by ? would just sort by a constant string.
Column names and keywords like ASC and DESC have to be part of the SQL text, so I would map the user's choice through an allow-list, a small map from allowed sort keys to fixed SQL fragments, with a default for anything unknown, or an enum at the API layer.
That way the only strings I concatenate are ones I wrote. I would also add id as a tie-breaker for a stable order. While I am in there, I would replace select * with an explicit column list, because it avoids pulling sensitive or large columns and keeps the row mapper stable when the table changes.
And I would add a test against a real database that passes the injection string and an invalid sort value and checks that nothing leaks.
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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.sql, package documentation
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 "Find and fix the bug in this repository method: jdbcTemplate.query("select * from users...": https://scrivio-interview-library.netlify.app/databases/jdbc/8f8fd154da/