Fixing SQL Injection

Everything you need to know in order to fix your code

Tools recognizing this:

Opengrep Fortify Checkmarx SonarQube Snyk Semgrep CodeQL

What is SQL Injection

SQL Injection is a critical security vulnerability that occurs when an application incorporates untrusted user input into SQL queries without proper validation or sanitization.

The user input can potentially include malicious payload that manipulates a database query to allow attacker unauthorized actions:

  • Access, modify, or delete sensitive data from the database

  • Bypass authentication mechanisms

  • Execute administrative operations on the database

  • In some cases, execute commands on the operating system

One Simple Example

Consider this classic example of a login query:

String query = "SELECT * FROM users WHERE username = '" + username + "' AND password = '" + password + "';";

An attacker could provide this input for the username:

admin' --

The resulting query becomes:

SELECT * FROM users WHERE username = 'admin' --' AND password = 'anything';

This allows the attacker to log in as the admin user without knowing their password. The -- makes the rest of the query a comment, effectively bypassing the password check.

Real-world Occurrences of SQL Injection

Sony Pictures Data Breach (2011)

The attack on Sony’s network was not just a breach of data but an outright assault on the company’s digital infrastructure.

According to The Washington Post, approximately 77 million PlayStation Network accounts were compromised, revealing personal details of millions of users​​. The financial repercussions were equally severe, with the breach costing Sony an estimated $170 million.

This incident highlighted the vulnerability of even the most sophisticated digital networks to SQL Injection attacks, underscoring the critical need for robust cybersecurity measures. It served as a wake-up call to the entire digital entertainment industry, emphasizing the importance of safeguarding user data against evolving cyber threats.

TalkTalk Data Breach (2015)

The year 2015 marked a significant event in the history of cyberattacks with the TalkTalk breach. Nearly 157,000 customers of the UK-based telecom company TalkTalk had their personal details compromised, as reported by BBC News​​. This cyber-attack didn’t just breach customer accounts; it shook the foundation of trust between consumers and digital service providers.

Fixing SQL Injections

The most efficient way to fix an SQL Injection issue in your code is using Prepared Statements with parameterized queries.

Prepared Statements are precompiled SQL templates that separate the query from the user input, meaning they take user-provided values only after the query structure is already determined. By doing so, they prevent attackers from injecting malicious SQL code, since the parameters are treated strictly as data rather than executable instructions.

Code Samples

Vulnerable Code

String userId = request.getParameter("userId"); 
String query = "SELECT * FROM users WHERE id = ; 
Statement stmt = connection.createStatement(); 
ResultSet rs = stmt.executeQuery(query);

Fixed Code

String userId = request.getParameter("userId");
String query = "SELECT * FROM users WHERE id = ?";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, userId);
ResultSet rs = pstmt.executeQuery();

Fix Explanation

The vulnerable code directly concatenates user input into the SQL query, allowing the query to be manipulated. The fix uses PreparedStatement with parameterized queries. Parameters are bound using type-safe methods (setString, setInt, etc.). The Prepared Statement automatically handles proper escaping and prevents SQL injection.

Last updated

Was this helpful?