SQL Injection ('SQLi')
ID |
java.sql_injection |
Severity |
critical |
Resource |
Injection |
Language |
Java |
Tags |
CWE:564, CWE:89, NIST.SP.800-53, OWASP:2021:A3, PCI-DSS:6.5.1 |
Description
Improper neutralization of special elements in SQL Commands ('SQL Injection' aka 'SQL').
SQL Injection vulnerabilities are predominant in applications that incorporate user input directly into SQL queries without adequate validation or sanitization. This allows malicious users to inject SQL commands that can alter the functionality of the original query.
By launching an SQL injection attack, threat actors may leak sensitive data from the database, alter data for their profit, delete data for denial of service, extract passwords and other credentials for offline dictionary attacks, or gain access to other systems within the network exploiting trust relationships.
Rationale
As with most injection vulnerabilities, inserting external input into SQL code is common practice when dealing with dynamic queries, as the engine may not provide options to code a table or column name(s), values for the IN operator, or dynamic WHERE conditions.
However, the concatenation of unsanitized input into SQL code almost always allows for the modification of the intended query.
In Java, a susceptible code example might look like this:
public List<User> getUserByName(String name) throws SQLException {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
List<User> users = new ArrayList<>();
String query = "SELECT * FROM users WHERE username = '" + name + "'";
try {
connection = DriverManager.getConnection(DB_URL, USER, PASS);
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setUsername(resultSet.getString("username"));
users.add(user);
}
} finally {
if (resultSet != null) resultSet.close();
if (statement != null) statement.close();
if (connection != null) connection.close();
}
return users;
}
In this instance, the input name
is concatenated directly into SQL query strings, potentially allowing execution of arbitrary SQL code if the input is not stringently controlled.
Remediation
To prevent SQL Injection vulnerabilities, the recommended approach is to use parameterized queries. These structures inherently separate SQL code from data input, neutralizing the potential for input to alter query logic maliciously.
Furthermore, consider these additional precautions:
-
Implement strong input validation and sanitization to enforce expected data patterns and strip potentially harmful characters.
-
Utilize ORM frameworks that abstract direct SQL query writing, inherently minimizing the risk of injection vulnerabilities.
-
Regularly conduct security testing, including SAST reviews, to detect and address potential vulnerabilities early in the development lifecycle.
-
Educate development teams on secure coding practices to integrate security awareness into the development culture.
Here is the revised, secure code example:
public List<User> getUserByName(String name) throws SQLException {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List<User> users = new ArrayList<>();
String query = "SELECT * FROM users WHERE username = ?";
try {
connection = DriverManager.getConnection(DB_URL, USER, PASS);
preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, name);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
User user = new User();
user.setId(resultSet.getInt("id"));
user.setUsername(resultSet.getString("username"));
users.add(user);
}
} finally {
if (resultSet != null) resultSet.close();
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
}
return users;
}
This fix ensures that input values are treated strictly as data, preventing them from being able to modify the query structure.
Configuration
The detector has the following configurable parameters:
-
sources
, that indicates the source kinds to check. -
neutralizations
, that indicates the neutralization kinds to check.
References
-
CWE-89 : Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection').
-
OWASP Top 10 2021 - A03 : Injection.
-
OWASP Cheat Sheets Series: SQL Injection Prevention.
-
CERT IDS00-J : Prevent SQL injection
-
https://www.securecoding.cert.org/confluence/display/java/IDS00-J.+Prevent+SQL+injection