Scrivio Interview library
LeadershipPrincipal8 min read · updated 12 November 2026

Code volume is growing fast, including code produced with AI assistants, and security reviews cannot keep up. How would you keep injection and similar vulnerability classes out of your Java codebase over the coming years?

We cannot review our way out of a growing codebase, so we make the safe way of writing data access the only easy way, and let automation catch what slips through. That means libraries that always use bind parameters and allowlisted identifiers, static analysis tuned for injection on every pull request, the same review and test bar for AI-generated code as for human code, dynamic testing and pen tests for what static tools miss, and metrics per vulnerability class so we know whether any of it works.

Why this question is asked

Injection is one of the oldest vulnerability classes, and it keeps coming back because it is so easy to write. One string concatenation in a repository method is enough. When code volume doubles, partly because AI assistants produce code quickly and confidently, the number of places where that mistake can happen doubles too, while the number of security reviewers stays the same. Interviewers ask this at a senior level to see whether we think in systems rather than heroics: a plan that still works when nobody reads every line. In this answer, we will recap what injection is and why review alone fails, then go through secure defaults, automated checks in the pull request, how to treat generated code, dynamic testing, and how to measure whether the programme is working.

1A Quick Recap: What Injection Is and Why Review Does Not Scale

Injection happens when data supplied by a user is joined into a command that another system interprets, such as SQL, JPQL (the query language of JPA, which works on entities instead of tables), LDAP filters or shell commands. The interpreter cannot tell where our command ends and the user's data begins, so a crafted value like ' OR '1'='1 changes the meaning of the query.

The classic fix is a bind parameter, a placeholder such as ? or :name that the driver sends separately from the query text, so the value can never become part of the command. The fix is well known. The problem is consistency: in an estate of a few million lines, a single developer under deadline, or a code assistant copying an old pattern, writes the concatenated version, and a human reviewer looking at a 600-line pull request misses it.

So the question is really about changing the odds. We want the safe version to be the default, the unsafe version to be hard to write and loud when written, and an independent check for whatever still gets through.

Java
// The pattern we want to make impossible to merge
String jpql = "select o from Order o where o.customer = '" + name + "'";
em.createQuery(jpql, Order.class).getResultList(); // name = "x' or '1'='1" returns every order
// The pattern we want to be the only easy path
em.createQuery("select o from Order o where o.customer = :name", Order.class)
.setParameter("name", name)
.getResultList(); // the value is data, never part of the query

2Make the Safe Pattern the Default

The cheapest control is the one developers never have to remember. Most Spring teams already use Spring Data repositories, where derived queries such as findByCustomerName and @Query methods with named parameters always bind values. We lean into that: the platform starter ships repository base classes and query helpers, and documentation and templates show only the parameterised form.

The harder case is dynamic structure, such as a sort column or a table name chosen by the user. Bind parameters cannot hold identifiers, which is why people fall back to concatenation. So the platform provides an allowlist helper: the caller passes a requested column name, and the helper maps it to one of a fixed set of known columns or rejects it. Spring Data's Sort with a whitelist of property names, or the Criteria API, cover most of these needs without strings.

Notice that this also helps the AI assistants. They imitate the code around them. If the codebase is full of safe repository methods and allowlisted sorting, the suggestions they produce follow the same shape.

Java
public final class SortColumns {
private static final Map<String, String> ALLOWED = Map.of(
"date", "createdAt", "total", "totalAmount", "status", "status");
public static Sort of(String requested, Sort.Direction dir) {
String property = ALLOWED.get(requested);
if (property == null) throw new IllegalArgumentException("unknown sort"); // 400 for the client
return Sort.by(dir, property);
}
}
SortColumns.of("total", Sort.Direction.DESC); // Sort: totalAmount DESC
SortColumns.of("1; drop table x", Sort.Direction.ASC); // IllegalArgumentException

3Automated Checks in Every Pull Request

The second layer is static analysis (tools that read source code without running it, such as Semgrep, CodeQL or SpotBugs with Find Security Bugs) with rules for injection, run on every pull request, not in a nightly report nobody reads. A rule like "a string built with + or format() reaches createQuery, createNativeQuery, JdbcTemplate.query or Statement.execute" catches the majority of real cases.

But there is a catch: a scanner that raises 200 findings per week, most of them false, gets ignored or switched off within a quarter. So we tune it. We start with a small set of high-confidence rules that block the merge, add our own rules for our own helper classes, and send lower-confidence rules to a dashboard rather than to the developer. We track the false positive rate and treat a noisy rule as a bug in the rule. A target such as fewer than one dismissed finding in five keeps developers trusting the tool.

Step throughWhat happens to a pull request
  1. Developer opens pull requesthuman or assistant-written, same path
  2. Unit and integration testsincluding tests for hostile input
  3. Static analysis with tuned rulesinjection sinks, unsafe query builders
  4. High-confidence finding?
    YesBlock merge, show fix patternNoContinue
  5. Human reviewfocus on design and risky areas
  6. Merge and deploy to testdynamic scan runs on the deployed service

6 steps in order. Play them, or step through them one at a time.

Automation handles the repeatable checks so human reviewers can spend their time on design and unusual code.

4Generated Code Gets the Same Bar, Not a Lower One

Code from an assistant is not more trustworthy or less trustworthy by nature; it is code whose author did not understand our context. The rule we set is simple: the developer who commits it owns it, and it goes through exactly the same tests, scanners and review as code they typed. We do not create a separate fast lane because the code looked complete.

A few practices make this work. We ask for tests with hostile inputs on any new data access code, so a quote character or a very long value is part of the test suite. We give assistants context that steers them, such as repository conventions in the project and approved helper classes. And we watch the trend: if findings per thousand changed lines rise in teams that adopt assistants heavily, that is a signal to add rules or training, not to ban the tools.

5Dynamic Testing and Pen Tests for What Static Tools Miss

Static analysis sees code paths, but not everything about the running system. It may miss an injection that goes through a reflection-based framework, a stored value that is later used in a report query, or a native query built in a library. Dynamic testing (DAST, which sends attack payloads to a running service) catches some of these, and we run it automatically against test environments for internet-facing services.

For the highest-risk systems, such as payments or anything with customer data, we add periodic penetration tests by people who think like attackers. Their findings are valuable twice: once as bugs to fix, and again as input for new static rules, so the same mistake is caught automatically next time.

6Measuring Whether It Works

Without measurement, a security programme becomes a list of tools we bought. We count findings by vulnerability class (injection, deserialization, access control and so on) and by where they were found: in the pull request, in dynamic testing, in a pen test or in production. The healthy trend is that the total goes down over a year and that most remaining findings are caught early, in the pull request, rather than late.

We also track time to fix and recurrence. If injection findings in one service keep returning, the fix is probably a missing helper or a team that needs pairing, not another reminder email. For example, a goal could be: no injection found in production for two years, 90 percent of injection findings caught before merge, and median time to fix under five days.

Metrics we review every quarter
MetricWhy it mattersExample target
Findings per class per quarterShows whether a class is decliningInjection down year on year
Share caught before mergeEarly catches are cheap90 percent or more
Median time to fixShows ownership and capacityUnder 5 days for high
False positive rate of blocking rulesKeeps developers trusting the toolUnder 20 percent
Recurrence in the same servicePoints to missing defaultsClose to zero

7Follow-up questions

Should we ban AI coding assistants until security catches up?

In most organisations that would push people to use them unofficially, which is worse. A better answer is to allow approved tools, keep generated code under the same tests and scanners, and give the assistants good context such as the project's safe helpers. Then measure findings in teams that use them; the data, not fear, should drive further rules.

Is an ORM enough to prevent SQL injection?

No. JPA and Hibernate protect us only when we use parameters. createQuery and createNativeQuery accept any string, so concatenating user input into JPQL or SQL is just as dangerous as with plain JDBC. Identifiers such as sort columns also need allowlisting because they cannot be bound.

8Conclusion

In this answer, we saw that review alone cannot keep injection out of a fast-growing codebase. We make the safe pattern the default through data access helpers with bind parameters and allowlisted identifiers, run tuned static analysis on every pull request, hold generated code to the same standard as human code, use dynamic testing and pen tests to find what static tools miss, and measure findings per vulnerability class over time to prove that the controls are working.

What to say in the interviewAbout 90 seconds aloud

Open with

My starting point is that we will never have enough reviewers to read every line, especially with assistants producing more code, so I would change the system rather than ask people to try harder.

Then, if they want more

First, I make the safe pattern the default. The platform starter gives teams Spring Data repositories with named parameters, and a small helper for dynamic parts like sort columns that maps user input to an allowlist, because identifiers cannot be bound. If the codebase is full of safe examples, both developers and assistants copy them.

Second, every pull request runs static analysis with injection rules, such as string concatenation reaching createQuery or JdbcTemplate. I keep the blocking rules few and accurate, because a noisy scanner gets ignored, and I track the false positive rate.

Third, generated code goes through exactly the same tests and review as human code, and the person who commits it owns it. I ask for tests with hostile input on new data access code. Fourth, I run dynamic scans against test environments and pen tests on high-risk systems, and I turn every pen test finding into a new static rule.

Finally, I measure: findings by class, how many were caught before merge, time to fix and recurrence. If injection is declining and most of what remains is caught in the pull request, the programme is working; if not, the data tells me where to add defaults or training.

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
OWASP Top Ten
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 "Code volume is growing fast, including code produced with AI assistants, and security...": https://scrivio-interview-library.netlify.app/spring-boot/web-security-threats/7d532fe067/