SQL Injection (SQLi)
Last updated
Last updated
SQL injection (SQLi) is a vulnerability that allows an attacker to interfere with queries that an application makes to its database. It generally allows an attacker to inject SQL and gain the ability to read, modify and delete data.
For example, consider an application that displays products in different categories. The following URL will return a list of products on the Gifts
category:
The application makes the following SQL query to retrieve data from a database:
The restriction released = 1
is being used to hide products that are not released. If the category
variable is directly passed to the SQL query, an attacker can craft the following URL:
This results in the SQL query:
Since --
is a comment in SQL, an attacker can retrieve all products, whether they are released or not.
You can find more details at PortSwigger Web Security Academy: SQL injection.
This page contains recommendations for the implementation of protection against SQL injection (SQLi) attacks.
Do not use string formatting or concatenation to assemble SQL queries.
Use prepared statements (or parametrized queries) to assemble SQL queries.
Use a database driver from the following list of available drivers: https://github.com/golang/go/wiki/SQLDrivers.
Example for PostgreSQL
Remember that placeholders are database specific:
MySQL
Placeholder
Example
PostgreSQL
Placeholder
Example
Oracle
Placeholder
Example