Quick revision
The short answer
All four findings are the same mistake: data is mixed into text that another interpreter will read, so input can change that text's structure. Newlines in logged input forge log lines, so we use structured JSON logging or strip control characters and never log secrets. Echoed parameters cause reflected XSS, so the template engine escapes by default or the OWASP Java Encoder encodes for the exact context. Concatenated JSON lets input add fields, so we serialize with Jackson. Home-made SQL escaping misses cases, so we use PreparedStatement or JPA bind parameters and a whitelist for identifiers such as sort columns.
What to remember
In this answer, we saw that all four findings are injection: input pasted into text for another interpreter can change its structure. Logged newlines forge lines, so we use structured JSON logging, strip control characters and never log secrets. Echoed parameters cause reflected XSS, so templates escape by default and the OWASP Encoder encodes per context. Concatenated JSON allows injected fields, so Jackson serializes it, and home-made SQL escaping misses backslashes, unquoted contexts and character sets, so we bind parameters and whitelist identifiers. The common rule is library encoding for the actual interpreter, at output.
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.
All four findings are the same mistake: data is mixed into text that another interpreter will read, so input can change that text's structure. Newlines in logged input forge log lines, so we use structured JSON logging or strip control characters and never log secrets. Echoed parameters cause reflected XSS, so the template engine escapes by default or the OWASP Java Encoder encodes for the exact context. Concatenated JSON lets input add fields, so we serialize with Jackson. Home-made SQL escaping misses cases, so we use PreparedStatement or JPA bind parameters and a whitelist for identifiers such as sort columns.
Why this question is asked
Security reviews often return a list of findings that look unrelated: a logging issue, a web issue, a JSON issue and a database issue. Treating them as four separate bugs leads to four separate patches, and the next review finds four new ones. Interviewers ask this scenario to see whether we can explain each risk concretely and recognise the pattern behind them. In this answer, we will recap what injection means, go through each finding with a small attack and its fix, and end with the rule that covers all of them.
1A Quick Recap: Data That Becomes Code
Every one of these outputs is read by an interpreter: a log viewer reads lines, a browser reads HTML, a JSON parser reads JSON and the database reads SQL. Each interpreter has characters that define structure, such as a newline, an angle bracket, a quote or a semicolon. When we build the output by pasting user input into a string, a user who includes those characters can change the structure, not just the data. That is injection. The fix is never to guess which characters are dangerous, but to hand the data to a library that keeps data and structure apart for that interpreter.
2Finding 1: Unfiltered Input in Logs
Plain-text logs are one event per line. If we log a user name that contains a newline, the user can add a line that looks like a genuine event.
Forged lines can hide an attack, frame another user or break log parsers. Structured logging is the better fix, because the JSON encoder escapes every control character and the log platform reads fields rather than lines. The second rule for logs is about what we never write: passwords, tokens, full card numbers and session ids must not appear, so we mask them at the source and use logging filters as a backstop.
3Finding 2: Request Parameters Echoed Into HTML
An error page that says Product <whatever was in the id parameter> not found, built by concatenation, lets an attacker craft a link that runs script in the victim's browser. This is reflected XSS: the payload travels in the link and is reflected straight back.
Notice that the encoder has one method per context. Text inside an element, inside an attribute and inside a script block are parsed differently by the browser, so a value that is safe in one place can be dangerous in another. JSON APIs should also send Content-Type: application/json with X-Content-Type-Options: nosniff, so a browser never treats an error body as HTML.
4Finding 3: JSON Built With Concatenation
Hand-built JSON breaks on the first name with a quote or backslash, and worse, it lets input add fields of its own.
Jackson escapes quotes, backslashes and control characters, so the whole input stays inside the name value. In Spring MVC we rarely call it ourselves: returning the record from a controller lets the framework serialize it the same way.
5Finding 4: SQL Built With String.format and a Home-Made Escape
A function that doubles single quotes looks safe, but it fails in several known ways. In MySQL with its default settings a backslash also escapes, so input ending in a backslash can break out of the string. Values placed without quotes, such as WHERE id = %s or ORDER BY %s, need no quote at all to inject. Some multi-byte character sets can hide a quote inside a character the function does not recognise.
| Sink | Risk | Correct fix |
|---|---|---|
| Logs | Forged lines via CR and LF; leaked secrets | Structured JSON logging; mask secrets |
| HTML error page | Reflected XSS | Auto-escaping templates; OWASP Encoder per context |
| Hand-built JSON | Broken output; injected fields | Serialize objects with Jackson |
SQL via String.format | SQL injection despite escaping | Bind parameters; whitelist identifiers |
Bind parameters are safe because the value never becomes part of the SQL text; the driver sends it separately from the statement, so there is nothing to escape. Identifiers such as column names and sort directions cannot be bound, which is why we map the allowed inputs to fixed column names. The same holds for JPA: JPQL built by concatenation is just as injectable, so we use :name parameters or the Criteria API.
6The Rule Behind All Four
The common rule is to use the encoding that the target interpreter needs, applied by a library at the point of output, instead of escaping by hand. Logs get a JSON encoder, HTML gets the template engine or a context-specific encoder, JSON gets a serializer, and SQL gets bind parameters, which avoid encoding altogether. Hand-written escaping fails because it encodes for the interpreter we imagine rather than the one we actually have, with its character sets, modes and contexts. So the review outcome is not four patches but a rule for the codebase, and a static analysis check for each sink so that new code follows it.
7Follow-up questions
How would you test for XSS in API responses?
At three levels. In unit and slice tests with MockMvc, we send hostile values such as <script>alert(1)</script> and "><img src=x onerror=alert(1)> in every parameter and assert that responses have Content-Type application/json and the nosniff header, and that any HTML error page shows the payload escaped. In the pipeline, a DAST scanner such as OWASP ZAP crawls the running service and tries payloads automatically. And for clients that render API data, we test that the front end inserts values as text, because JSON itself is not HTML: a JSON response containing angle brackets is safe until some page inserts it with innerHTML.
Is it enough to validate input instead of encoding output?
No, though validation helps. Input validation rejects values that are clearly wrong, such as a non-numeric id, and that removes many attacks early. But many legitimate values contain dangerous characters, such as the name O'Brien or a comment that mentions <b>, and the same value may end up in SQL, HTML and a log line, each with different rules. Only encoding at output, for the specific context, is correct in every case; validation is an extra layer.
Does using JPA make SQL injection impossible?
Only if we use it as intended. Repository methods, JPQL with named parameters and the Criteria API bind values safely. But a JPQL or native query built by concatenating input, for example in a custom repository with entityManager.createQuery("... where name = '" + name + "'"), is as injectable as raw JDBC. Sorting is a common trap: Spring Data's Sort with property names from the request should be checked against an allowed list, too.
8Conclusion
In this answer, we saw that all four findings are injection: input pasted into text for another interpreter can change its structure. Logged newlines forge lines, so we use structured JSON logging, strip control characters and never log secrets. Echoed parameters cause reflected XSS, so templates escape by default and the OWASP Encoder encodes per context. Concatenated JSON allows injected fields, so Jackson serializes it, and home-made SQL escaping misses backslashes, unquoted contexts and character sets, so we bind parameters and whitelist identifiers. The common rule is library encoding for the actual interpreter, at output.
Open with
All four are the same underlying problem: user input pasted into text that another interpreter reads, so it can change the structure, not just the data. For logs, a newline in the input lets someone forge log lines, hiding an attack or framing another user.
Then, if they want more
I'd switch to structured JSON logging, which escapes control characters, or at least replace CR and LF before logging free text, and make sure secrets like passwords and tokens are never logged. For the HTML error messages, echoing a parameter is reflected XSS: a crafted link runs script in the victim's browser.
The fix is to render through a template engine that escapes by default, like Thymeleaf's th:text, or use the OWASP Java Encoder for the specific context, because element content, attributes and JavaScript strings are parsed differently. APIs should send application/json with nosniff.
For JSON, concatenation breaks on quotes and backslashes and lets input add fields like a role; serialize objects with Jackson instead. For SQL, a home-made escape function misses cases: MySQL backslash escapes, character set tricks and unquoted contexts like an id or an ORDER BY column, which need no quote to inject.
So values go through PreparedStatement or JPA bind parameters, where the value never becomes SQL text, and identifiers like sort columns are mapped from a whitelist. The common rule is to use the encoding the target interpreter needs, done by a library at output, never hand-written escaping.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3.4
- 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
- The Java Language Specification, Java SE 21, Java SE 21 API 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 "A security review of a Spring Boot service finds: user input written to logs without...": https://scrivio-interview-library.netlify.app/java/strings/12d941dda4/