The Checkmarx throws the error because the values you are setting to the query parameters are not validated for its type.

For example, let us assume the query formed with your PreparedStatement is as below and the value you want to pass to the query parameter is 'Test'

Select * from XYZ where COL1 = ?

If your code is compromised and if the intruder passes 'Test' OR 1 = 1 in the query parameter, then the condition will always be true and it would return all the records from the Table.

So, before executing the query you should validate all your inputs.

Hope this helps

Answer from Prasann on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 76104183 › how-to-resolve-a-second-order-sql-injection-issue-being-thrown-by-checkmarx
java - How to resolve a Second Order SQL Injection issue being thrown by Checkmarx - Stack Overflow
This may enable a Second-Order SQL Injection attack · It seems to have an issue with the + in between CONTACT_ID_TABLE and FROM_SYSIBM. As I mentioned all of the similar solutions I found involved parameters being passed in to the queries, here we aren't doing that so I don't think that I need something quite as complicated, though I could be wrong. ... Sounds to me like a false positive Checkmarx finding, similar to this one.
🌐
Lucentsky
lucentsky.com › en › cxsast › sql-injection
How to fix SQL injection in Checkmarx CxSAST reports | Lucent Sky
Learn how to fix SQL injection found by Checkmarx CxSAST fast and efficiently, with examples in C#, Java, and other languages.
Top answer
1 of 2
39

A second order SQL injection is an injection where the payload is already stored in the database (instead of say being delivered in a GET parameter). In that sense it is somewhat similar to stored XSS (and ordinary "first order" SQL injection would be analogous to reflected XSS).

How does it work? Lets say you let users pick any username. So an attacker could choose the name '; DROP TABLE Users; --. If you naively concatenate this username into your SQL query to retrieve information about that user you have a problem:

sql = "SELECT * FROM Users WHERE UserName = '" + $username + "'";

So, how do you deal with this?

Always use parametrized querires, always, always, always. Treat all variables as untrusted user data even if they originate from the database. Just pretend everything is GET parameters, and behave accordingly by binding them as parameters.

You can also sanitize and limit the input (e.g. only allow alphanumeric usernames) before it is stored in the database as well as after it is retrieved from the database. But I would not rely on that as my only line of defence, so use parametrized queries as well.

2 of 2
1

There is nothing 'special' here. The so called 'second order' SQL injection is just the same SQL injection with the minor difference that the content is coming from within the database rather than from data entered directly by the user. The same rules apply

  • always sanitise input data regardless of where it comes from (the user, a file a database etc)

  • Never use string concatenation to build up executable commands. Use prepared statements etc.

The rule of thumb is to never trust any input data regardless of how secure you think it might be. You cannot trust what the user might enter and you need to assume that even your own data repositories (i.e. your database) may have been compromised in some way or have had 'bad data' etnered into it. Write your code with the assumption your running in a hostile environment as the reality is, you are.

BTW I see that Oracle's documentation has not improved! That is a really badly worded and poorly explained blurb regarding SQl injection.

🌐
Offensive360
offensive360.com › second-order-sql-injection-attack
Second-Order SQL Injection (2nd Order SQLi): How It Works & Fixes | Offensive360
March 1, 2024 - Hardcoded credentials (CWE-798) are flagged by Checkmarx, Veracode, and Fortify as critical findings. Learn the exact remediation steps, how SAST scanners detect them, and how to fix hard-coded credentials across Python, Java, C#, and Node.js. HTML injection lets attackers embed fake forms, links & overlays on real pages — enabling phishing and credential theft even without JavaScript.
🌐
Stack Overflow
stackoverflow.com › questions › 75281835 › getting-second-order-sql-injection-in-spring-hibernate
Getting Second Order SQL Injection in Spring Hibernate - Stack Overflow
Is there any thing apart from this to prevent it :- Query query = entityManager.createNativeQuery("select order_id from order where order_name=?"); List<String> dataSet = query.setParameter(1,order_name).getResultList(); as after preventing still checkmarx showing the same for sanity and validate the data which return from getResultList() method. ... whatever returned from getResultList() method, is not related to SQL, let alone injections. N'est-ce pas? ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s... Release notes and bug fixes for beta.stackoverflow.com
🌐
PortSwigger
portswigger.net › kb › issues › 00100210_sql-injection-second-order
SQL injection (second order) - PortSwigger
However, if the data being incorporated into queries is numeric, then the defense may fail, because numeric data may not be encapsulated within quotes, in which case only a space is required to break out of the data context and interfere with the query. Further, in second-order SQL injection attacks, data that has been safely escaped when initially inserted into the database is subsequently read from the database and then passed back to it again.
Find elsewhere
🌐
Medium
infosecwriteups.com › the-wrath-of-second-order-sql-injection-c9338a51c6d
What is a Second-Order SQL Injection and how can you exploit it successfully?
July 14, 2020 - An automated tool is not smart enough to identify the change in application behavior in any of the subsequent responses caused by the malicious injection in one of the previous queries. This kind of vulnerability happens because a good programmer maybe will patch his code to prevent SQL injections in forms where the user can input something BUT he will not do the same thing where a user doesn’t have any sort of interaction with the application database.
🌐
Checkmarx
checkmarx.com › glossary › what is sql injection, and how can today’s organizations prevent it with application security?
SQL Injection | Checkmarx.com
July 29, 2025 - If a problem or risk is found, the fix is explained in natural language, alongside the exact code snippet to make the change. Looking to prevent the risk of SQL attacks in your environment? Learn more about Checkmarx by requesting a demo. ... Protect from SQL Injection Attacks Today!
🌐
Atlassian
checkmarx.atlassian.net › wiki › pages › viewpage.action
https://checkmarx.atlassian.net/wiki/pages/viewpag...
August 3, 2021 - Log in to Jira, Confluence, and all other Atlassian Cloud products here. Not an Atlassian user? Sign up for free.
🌐
www.esecforte.com
esecforte.com › home › our blog › blog › second order sql injection
Second Order SQL Injection Attack | Second-Order Code injection
June 30, 2022 - Basically second order SQL injections take place when one functionality of a web application takes a user input from a user, escapes (not strips) all SQL metacharacters and inserts that data input into a database.
🌐
Medium
medium.com › @appsecwarrior › static-application-security-testing-sast-checkmarx-one-dd83e2a24b25
Static Application Security Testing (SAST)-Checkmarx One | by appsecwarrior | Medium
August 9, 2024 - This ensures that the userId value is properly escaped and cannot be used to inject malicious SQL code. ... Mitigating second order SQL injection vulnerabilities involves a combination of input validation, output encoding, and parameterized queries.
🌐
Source
source.checkmarx.com › reference
SQL Injection (SQLi) Cheat Sheet, Attack Examples & Protection - Reference - Source
June 28, 2021 - SQL Injection, sometimes shortened to SQLi, is perhaps the most commonly employed hacking technique today, constantly making headlines and appearing in vulnerability reports. These malicious injections have been regularly starring in the OWASP Top-10 lists for years and they took the first ...
🌐
Offensive360
offensive360.com › blog › what-is-second-order-sql-injection-owasp
Second-Order SQL Injection: OWASP Definition, Checkmarx Detection & Fixes | Offensive360
1 month ago - Second-order (2nd order) SQL injection per OWASP WSTG-INPV-05: why it evades scanners, how Checkmarx flags it, and parameterized query fixes in Java, Python, PHP & C#.
🌐
Atlassian
checkmarx.atlassian.net › wiki › spaces › KC › pages › 900366423
Scan Results Example (v8.8.0) - Checkmarx Knowledge Center - Confluence
July 19, 2022 - The Queries pane (bottom-left) shows that 27 instances of the SQL_Injection vulnerability were found. Clicking () takes you to the Codebashing, where you can learn more about the selected vulnerability, why it happens, and how to eliminate it. ... Codebashing provides developers with a new in-context learning platform that sharpens the skills they need to fix vulnerabilities and write secure code.