javaCopy codeString query = "SELECT * FROM users WHERE username = '" + username + "' AND password = '" + password + "'";
Statement stmt =connection.createStatement();ResultSet rs =stmt.executeQuery(query);
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
importjava.sql.*;publicclassUserDao {publicUsergetUser(String username,String password) throwsSQLException {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.*;publicclassUserDao {publicUsergetUser(String username,String password) throwsSQLException {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@RepositorypublicclassProductRepository { @PersistenceContextprivateEntityManager entityManager;publicList<Product> searchProducts(String category) {String jpql ="SELECT p FROM Product p WHERE p.category = '"+ category +"'";returnentityManager.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@RepositorypublicclassProductRepository { @PersistenceContextprivateEntityManager entityManager;publicList<Product> searchProducts(String category) {String jpql ="SELECT p FROM Product p WHERE p.category = :category";returnentityManager.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 sqlite3defget_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.
Fixed Code:
pythonCopyimport sqlite3defget_user(username): conn = sqlite3.connect('users.db') cursor = conn.cursor() cursor.execute("SELECT * FROM users WHERE username = ?", (username,))return cursor.fetchone()
Reason for Fix:
The fixed code uses parameterized queries, which treat user input as data rather than part of the SQL command, preventing SQL injection attacks.
Vulnerable Code:
pythonCopy codequery ="SELECT * FROM users WHERE username = '%s'"% usernamecursor.execute(query)
Reason for vulnerability: User input is directly used in the SQL query, allowing SQL injection.
Fixed Code:
pythonCopy codequery ="SELECT * FROM users WHERE username = %s"cursor.execute(query, (username,))
Reason for fix: Use parameterized queries to prevent SQL injection.