SKY-D217 · Security in code
SQL injection through an ORM's raw SQL
ORMs (libraries that map database rows to objects, such as Django and peewee) also let you write SQL by hand. SKY-D217 reports raw SQL text that is pasted together from values, or that carries outside input, and is passed to Django .objects.raw(), QuerySet.extra(), RawSQL() or peewee Model.raw().
- Languages
- Python
- Quality gate
- counted toward the gate limits; a CRITICAL finding fails the gate on its own (full and diff scans)
- Finding type
- review
- Catalog name
- Raw SQL execution
Why it matters
The ORM sends this text to the database exactly as written. A visitor who searches for ' OR '1'='1 reads every row, and other payloads can change data, even though the rest of the project uses safe ORM queries.
What Skylos flags
from django.http import JsonResponse from shop.models import Book def search(request): title = request.GET.get("title", "") books = Book.objects.raw(f"SELECT * FROM shop_book WHERE title = '{title}'") return JsonResponse({"ids": [book.id for book in books]})What Skylos leaves alone
from django.http import JsonResponse from shop.models import Book def search(request): title = request.GET.get("title", "") books = Book.objects.raw("SELECT * FROM shop_book WHERE title = %s", [title]) return JsonResponse({"ids": [book.id for book in books]})Why Skylos leaves it alone
The SQL text is a fixed literal with a %s placeholder. Django sends the title separately as a parameter, so it can never change the statement. Skylos only looks at the first argument, the SQL text.
More languages
Python
Flagged
from django.http import JsonResponse from shop.models import Book def by_author(request): author = request.GET.get("author", "") books = Book.objects.extra(where=[f"author = '{author}'"]) return JsonResponse({"ids": list(books.values_list("id", flat=True))})Not flagged
from django.http import JsonResponse from shop.models import Book def by_author(request): author = request.GET.get("author", "") books = Book.objects.extra(where=["author = %s"], params=[author]) return JsonResponse({"ids": list(books.values_list("id", flat=True))})Why Skylos leaves it alone
The where entry is fixed text and the author travels in params, which Django binds as a parameter. Skylos checks select values and the where, tables and order_by entries, never params.
How Skylos decides
- Finds four sinks: a
.objects.raw()call,.extra()on a Django queryset (a chain through.objects, or any.extra()in a file that imports Django), Django'sRawSQL()resolved through its import, and.raw()in a file that imports peewee. - For
.raw()andRawSQL()it reads the first argument. For.extra()it reads eachselectvalue and each entry ofwhere,tablesandorder_by;paramsandselect_paramsare bound values and are not read. - Reports CRITICAL when that SQL text is an f-string,
+,%or.format()expression with a part that is not a fixed literal, or when it is tainted: it comes from a function parameter (such as a view'srequest), request data,input()or another outside source, directly or through assignments. - Text built only from literals is left alone. SQLAlchemy
text()and pandasread_sql()are reported once, as SKY-D211, not here.
How to fix it
Keep the SQL text fixed and pass every value in the parameter list. If the query does not need raw SQL, use the ORM's own filters, which always bind values.
from django.http import JsonResponse from shop.models import Book def search(request): title = request.GET.get("title", "")Removed: books = Book.objects.raw(f"SELECT * FROM shop_book WHERE title = '{title}'")Added: books = Book.objects.filter(title=title) return JsonResponse({"ids": [book.id for book in books]})When it can be wrong
- A helper that receives the SQL text as a parameter, such as
def run(sql): return Book.objects.raw(sql), is reported even when every caller passes a constant. Parameters count as untrusted. - An f-string that inserts a module constant, such as
f"SELECT * FROM {TABLE}", is reported: only literals written in the expression itself count as fixed text. - A column name checked against an allowlist before it is pasted into
order_byis still reported: allowlist checks are not recognised.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
Silence one line and say why. A bare
# noqadoes not hide HIGH or CRITICAL security findings.Python books = Book.objects.raw(sql) # skylos: ignore[SKY-D217] sql is from REPORTSAccept the findings you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D217"]turns the rule off for the whole project.