Reason for vulnerability: This code directly concatenates user input into the SQL query, which allows an attacker to inject malicious SQL code.
Fixed Code:
javaCopy codeString query = "SELECT * FROM users WHERE username = ? AND password = ?";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
Reason for fix: Using PreparedStatement with parameterized queries prevents SQL injection by treating user input as data, not code.
Vulnerable Code
import java.sql.*;
public class UserDao {
public User getUser(String username, String password) throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/users", "root", "password");
String sql = "SELECT * FROM users WHERE username = '" + username + "' AND password = '" + password + "'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
// Process result set and return user
}
}
Reason for Vulnerability:
This code constructs an SQL query by directly concatenating user input, allowing an attacker to manipulate the query structure.
Fixed Code:
javaCopyimport java.sql.*;
public class UserDao {
public User getUser(String username, String password) throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/users", "root", "password");
String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
// Process result set and return user
}
}
Reason for Fix:
The fixed code uses a PreparedStatement with parameterized queries, which separates SQL code from user input, preventing SQL injection attacks.
Java Example
Vulnerable Code:
javaCopy@Repository
public class ProductRepository {
@PersistenceContext
private EntityManager entityManager;
public List<Product> searchProducts(String category) {
String jpql = "SELECT p FROM Product p WHERE p.category = '" + category + "'";
return entityManager.createQuery(jpql, Product.class).getResultList();
}
}
Reason for Vulnerability:
This JPA query is constructed by directly concatenating user input, allowing potential manipulation of the query structure.
Fixed Code:
javaCopy@Repository
public class ProductRepository {
@PersistenceContext
private EntityManager entityManager;
public List<Product> searchProducts(String category) {
String jpql = "SELECT p FROM Product p WHERE p.category = :category";
return entityManager.createQuery(jpql, Product.class)
.setParameter("category", category)
.getResultList();
}
}
Reason for Fix:
The fixed code uses a parameterized JPQL query, which binds the user input as a parameter, preventing SQL injection attacks in JPA queries.
Python Example
Vulnerable Code:
pythonCopyimport sqlite3
def get_user(username):
conn = sqlite3.connect('users.db')
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM users WHERE username = '{username}'")
return cursor.fetchone()
Reason for Vulnerability:
This code uses string formatting to construct the SQL query, allowing an attacker to inject malicious SQL code.