A database query is built by joining strings at run time

tx0/java:query-from-stringsrecommendedwarningsecurity, cwe-89

What it finds

statement.executeQuery("SELECT * FROM users WHERE name = '" + name + "'") builds a query from strings at run time. A value with a quote in it ends the literal and adds SQL of its own (CWE-89). A PreparedStatement with ? placeholders, or named parameters of the persistence API, passes values apart from the query text.

Reported are the query arguments of executeQuery, executeUpdate, executeLargeUpdate, execute, addBatch, prepareStatement, prepareCall, createQuery, createNativeQuery and nativeSQL that are a concatenation with a part that is not a constant, or a String.format.

From the rule's source, query_from_strings.rs.

Example from the rule's tests
import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement; class Users {  void find(Connection c, Statement s, String name) throws SQLException {    s.executeQuery("SELECT * FROM users WHERE name = '" + name + "'");tx0 reports: the query is put together from strings at run time    String sql = "DELETE FROM users WHERE id = " + name;    s.executeUpdate(sql);tx0 reports: the query is put together    c.prepareStatement("SELECT * FROM users WHERE name = ?");    s.execute("SELECT 1");  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:query-from-strings" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:query-from-strings] reason="..."

tx0 explain tx0/java:query-from-strings shows the same in the terminal. Found a false positive? Report it.