Skip to content

SQL Injection via RawSQL

Description

SQL Injections are a critical type of vulnerability that can lead to data or system compromise. By dynamically generating SQL query strings, user input may be able to influence the logic of the SQL statement. This could lead to an adversary accessing information they should not have access to, or in some circumstances, being able to execute OS functionality or code.

Examples

Insecure Code

python
DBObject.objects.all().annotate(val=RawSQL(sql="select id from some_secondary_table where id='" + user_supplied_id + "'"))

Secure Code

python
DBObject.objects.all().annotate(val=RawSQL(sql="select id from some_secondary_table where id=%s", params=[user_supplied_id]))

Remediation

Replace all dynamically generated SQL queries with parameterized queries. Use other QuerySet methods to achieve the same goals. If using RawSQL, ensure calls including user-supplied data pass it in to the params parameter.

Rule Details

FieldValue
IDCODE-0180
CategoryInjection
SeverityCRITICAL
CWECWE-89
ConfidenceHIGH
ImpactHIGH
LikelihoodHIGH
ExploitabilityEASY
Tagssql injection, rawsql
OWASPA1:2017-Injection, A03:2021-Injection