Quick revision
The short answer
Repeated SQL injection findings show that knowing about a risk does not stop people from making the mistake under deadline pressure and constant staff turnover. We remove the class of bug by changing the system: make the safe path the easy path with query APIs that bind parameters by default, add static analysis in CI that flags string-built SQL and blocks merges, use least-privilege database users so a successful injection does little damage, and track findings over time, treating recurring ones as a platform gap rather than an individual failure.
What to remember
In this answer, we saw that recurring SQL injection shows the limits of relying on developer knowledge under pressure and turnover, and that the bug class is removed by changing the system. We made the safe path the easy path with query APIs that bind by default, enforced it with blocking static analysis in CI, contained damage with least-privilege database users, and tracked findings over time so that recurring ones become platform work rather than blame.
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.
Repeated SQL injection findings show that knowing about a risk does not stop people from making the mistake under deadline pressure and constant staff turnover. We remove the class of bug by changing the system: make the safe path the easy path with query APIs that bind parameters by default, add static analysis in CI that flags string-built SQL and blocks merges, use least-privilege database users so a successful injection does little damage, and track findings over time, treating recurring ones as a platform gap rather than an individual failure.
Why this question is asked
Every security team has seen this pattern: training is delivered, a guideline is published, and a year later the audit finds the same bug in new code. Leaders then face a choice between blaming developers and changing the environment that keeps producing the bug. Interviewers ask this at senior levels because the answer shows how we think about reliability at organisation scale, which is the same thinking we use for outages. In this answer, we will see why knowledge alone fails, then go through making the safe path easy, enforcing it in CI, limiting the damage, and measuring the trend.
1Why Knowledge Alone Does Not Work
Almost every developer can explain SQL injection. Yet the bug keeps appearing, for ordinary reasons. Under delivery pressure, people take the quickest working path, and string concatenation is the quickest way to add a filter or a sort column. New people join every month, and each one brings habits from somewhere else. Code is copied from older parts of the codebase that predate the rules. And the bug is invisible in normal testing: concatenated SQL works perfectly with normal data.
So a defect that depends on every developer remembering a rule every time will keep happening at a predictable rate. Aviation and medicine learned this long ago: rather than relying on memory, they design checklists and equipment so the wrong action is hard to take. That is the shift we want: from asking people to be careful to building a system in which the careful path is the default.
2Make the Safe Path the Easy Path
First we look at where findings come from. Usually it is a small number of patterns: dynamic search filters, user-chosen sort columns, LIKE searches, IN lists, and native queries in JPA repositories. For each pattern, the platform provides a ready-made safe way that is easier than concatenating. That might be a small internal library for dynamic filters on top of NamedParameterJdbcTemplate or JdbcClient, a sort helper that maps API sort keys through an allow-list, standard support for Spring Data Specifications or jOOQ, and examples in the service template.
The design goal is that the shortest code is also the safe code. If the safe helper needs three lines and concatenation needs one, people will concatenate. If the helper is one line and already imported in the template, people will use it. We also remove unsafe examples from internal wikis and old shared libraries, because copied code is a major source of new bugs.
- Safe APIs by defaultquery helpers, allow-list sorting, query builders
- Templates and examplesnew services start with the safe patterns
- Static analysis in CIflags string-built SQL, blocks the merge
- Least-privilege database usersan injection that slips through can do little
- Tracking and reviewrecurring findings become platform work
5 layers, from the top down. Step through them one at a time.
Each layer catches what the one above it missed.
3Enforce It With Static Analysis in CI
Next, we make the mistake visible before it merges. Static analysis tools can detect SQL built from non-constant strings and passed to JDBC, JdbcTemplate, JPA's createNativeQuery and similar sinks. Tools in common use include CodeQL, Semgrep, SpotBugs with the Find Security Bugs plugin, and Sonar rules; the choice matters less than running one consistently. We add a custom rule for our own data-access helpers so their raw-string escape hatches are covered too.
The check runs on every pull request and blocks the merge on a finding. Blocking is important; a warning in a report is ignored under deadline pressure. To keep that acceptable, we tune the rules to keep false positives low, provide a documented way to mark a reviewed false positive with a reason that a security champion approves, and start by blocking only new code, so teams are not buried by old findings on day one. Existing findings go into a tracked backlog with owners and dates.
4Limit the Damage With Least Privilege
No prevention is perfect, so we also limit what a successful injection can do. Each service connects with its own database user that can only touch its own tables, with only the operations it needs. The user never owns the schema; migrations run under a separate account at deploy time. Reporting and search paths can use read-only users. Rights such as creating functions, reading server files or running COPY to the file system are never granted to application users.
With that in place, an injection in the order service cannot read the customer service's data or drop tables. It is still a serious bug, but it is a contained one. This is the same idea as blast-radius thinking in reliability: assume something will fail, and make sure the failure stays small.
5Track Findings and Treat Recurrence as a Platform Gap
Finally, we measure. We track new SQL injection findings per month, by source (audit, scanner, penetration test, bug bounty), by pattern and by team, along with time to fix. The trend tells us whether the system is working. If findings keep appearing in one pattern, say native queries with dynamic sorting, that is a gap in our safe APIs or our scanner rules, and the fix is platform work, not another reminder email.
How we talk about findings matters. If a finding means public blame, people hide problems and argue with scanners. If a finding means we found a hole in our defences, people report them and help close them. We run short blameless reviews for any finding that reaches production, asking why the safe path was not taken and why CI did not catch it, and we share the improvements. Training still has a place, but as a short, practical introduction to the safe APIs rather than as the main defence.
6Follow-up questions
What if a team says the safe API is too limited for their query?
That is useful feedback. We help them write the query safely, often with fixed fragments and bound values, and we add the missing capability to the shared API if others will need it. A documented exception path with review keeps them unblocked. If many teams hit the same limit, the API is the problem, not the teams.
How do you introduce a blocking check without stopping delivery?
We run the scanner in report-only mode first, measure findings and false positives, tune the rules, and fix the worst existing issues. Then we block only on new findings in changed code, with a clear false-positive process. Once the existing backlog is small, we extend blocking to the whole codebase.
Does an ORM solve this on its own?
It removes most of the risk for ordinary queries, because generated SQL binds values. But native queries, string-built JPQL and dynamic sorting are just as injectable through an ORM, and they are exactly where findings cluster. So an ORM is one of the safe paths, not a replacement for the scanner and the other layers.
7Conclusion
In this answer, we saw that recurring SQL injection shows the limits of relying on developer knowledge under pressure and turnover, and that the bug class is removed by changing the system. We made the safe path the easy path with query APIs that bind by default, enforced it with blocking static analysis in CI, contained damage with least-privilege database users, and tracked findings over time so that recurring ones become platform work rather than blame.
Open with
It tells me that training and awareness are not enough. Nearly every developer knows what SQL injection is, but under deadline pressure, with new people joining and old code being copied, someone will concatenate a string, and it works fine in every normal test.
Then, if they want more
A bug that depends on everyone remembering a rule every time will keep happening at a steady rate. So I would change the system rather than the reminders.
First, make the safe path the easy path: look at where the findings come from, usually dynamic filters, sort columns, LIKE and native queries, and provide helpers or query builders that bind values by default and are shorter to use than concatenation, built into the service template.
Second, enforce it in CI with static analysis, CodeQL, Semgrep or Find Security Bugs for example, that flags SQL built from non-constant strings and blocks the merge, with a reviewed false-positive path, starting with new code so we do not stop delivery.
Third, limit the blast radius: each service gets a database user that can only touch its own tables, migrations run under a separate account, and read paths can use read-only users. Fourth, track findings over time by pattern and team, and when a pattern recurs, treat it as a gap in our platform, not a personal failure, with blameless reviews.
That way the number should trend towards zero, and when one slips through, it does little damage.
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 "SQL injection keeps appearing in your code audits years after it became a well-known...": https://scrivio-interview-library.netlify.app/databases/jdbc/5f5424914d/