SQL Injection
When you start developing any endpoint with database queries, the first thing everyone says is: be careful with SQL injections, use standards to prevent them, and bla bla.
But to prevent something, you need to understand how it works on the other side. If you just follow best practices, you will probably prevent it, but it’s way cooler when you know what’s really happening.
For this example, we will use Javascript as the programming language and PostgreSQL as our database
So basically, for this example, let’s consider a simple table called contacts with this structure
And we have an endpoint responsible for create contacts in this table, something like a POST in /contacts where we send the data to create:
curl --location 'http://localhost:3000/contacts' \
--header 'Content-Type: application/json' \
--data-raw '{
"name":"Alex",
"email":"alex_goodguy@mail.com",
"phone":"1273698127391"
}'And, your create method is something like:
When you sent to your senior developer review…….few moments later……….🚨 ⚠️ 📢 ❗ 🚨SQL INJECTION!!! REFACTOR RIGHT NOW🚨 ⚠️ 📢 ❗ 🚨
But you are asking, why? why my code is fragile against SQL injections ? and wtf is this anyway?
Let’s see what OWASP has to say to us:
A SQL injection attack consists of insertion or “injection” of a SQL query via the input data from the client to the application. A successful SQL injection exploit can read sensitive data from the database, modify database data (Insert/Update/Delete), execute administration operations on the database (such as shutdown the DBMS), recover the content of a given file present on the DBMS file system and in some cases issue commands to the operating system. SQL injection attacks are a type of injection attack, in which SQL commands are injected into data-plane input in order to affect the execution of predefined SQL commands.
It’s a excellent definition, but I little harsh to understand don’t you think?
Basically, someone with bad intentions can inject SQL instructions in our payload and do bad stuffs in your database, like dropping everything. So let’s get down to it and continue our example, considering the client want’s to drop our contacts database.
If the user pass in our name parameter a command like:
‘ ’,’ ’,’ ‘); DROP TABLE contacts; --curl --location 'http://localhost:3000/contacts' \
--header 'Content-Type: application/json' \
--data-raw '{
"name":"' ',' ',' '); DROP TABLE contacts; --",
"email":"alex_badguy@mail.com",
"phone":"1273698127391"
}'BOOOMMM💥💥💥💥, all the data fromcontacts is over =(. But why?
Get Alex Ercolin’s stories in your inbox
Join Medium for free to get updates from this writer.
When this payload came for our create function(image 1.0) will create the following SQL command
INSERT INTO contacts(name, email, phone, category_id)
VALUES(' ',' ',' '); DROP TABLE contacts RETURNING *;Let’s analyze, when you see this command, the first thing you might think is will throw a syntax error, but unfortunately no.. SQL supports multiple queries separated by ; , this is called SQL Batch Execution or Stacked Queries. So SQL, basically each instruction is independent and will be handled in sequence.
So.. how to refactor our code to prevent it from happening?
And why specifically just doing these small things prevents such horrible event?
Because we are using parameterized query or prepared statement, so SQL will handle the data as data and not code, so:
1.SQL will pre-compile the query
INSERT INTO contacts(name, email, phone, category_id)
VALUES($1, $2, $3, $4) RETURNING *And now, $1, $2, $3, $4 are interpreted as values who will be inserted after, but not as SQL code.
2.The database insert the values as data
Now, the database know that query structure before, so new queries won’t be accepted.
If the user try again using:
curl --location 'http://localhost:3000/contacts' \
--header 'Content-Type: application/json' \
--data-raw '{
"name":"' ',' ',' '); DROP TABLE contacts; --",
"email":"alex_badguy@mail.com",
"phone":"1273698127391"
}'The query mounted will be:
INSERT INTO contacts(name, email, phone, category_id)
VALUES('''; DROP TABLE contacts; --', 'alex_badguy@mail.com', '1273698127391') RETURNING *So now, the DROP TABLE command is just a string inside VALUES , and will not run against you database and you are safe. 😮💨😮💨😮💨
OBS: Off course, beside that, we can sanitize all the data, configure our database to not accept multiple queries in one array, implement proper user permissions — a very common but not always sufficient measure is to set up database users with “operations-only” permissions and so on. But for the sake of explanation, we considered all the data from the payload going directly to our create function.






