The analyzer has detected an SQL command that uses data received from an external source, without a prior check. This can cause an SQL injection if the data is compromised.
An SQL injection is identified as a separate risk category in the OWASP Top 10 Application Security Risks 2017: A1:2017-Injection.
The example:
public static void getFoo(Connection conn, String bar) throws SQLException {
var st = conn.createStatement();
var rs = st.executeQuery("SELECT * FROM foo WHERE bar = '" + bar + "'");
// ....
}
In this case, we receive the value of the 'bar' variable from a public method, 'bar'. Since the method is public, it may receive unverified data from external sources (such as controllers, forms, etc.). It is dangerous to use data without any check—this way attackers get to use different ways to inject commands.
For example, an attacker can enter a special command instead of the expected value of the username. This way all users' data will be extracted from the base and then processed.
The example of such a compromised string:
' OR '1'='1
To protect against such queries, check the input data or, for example, use parameterized commands.
public static void getFoo(Connection conn, String bar) throws SQLException {
var sql = "SELECT * FROM foo WHERE bar = ?";
var st = conn.prepareStatement(sql);
st.setString(1, bar);
var rs = st.executeQuery();
// ....
}
A safer alternative is to use an ORM, especially when direct execution of SQL queries is not strictly required.