Django, SQL injection in column aliases, (Moderate)

Listen to this Post

The vulnerability arises in Django’s FilteredRelation feature when used with QuerySet.annotate() or QuerySet.alias() on PostgreSQL databases. When a crafted dictionary is passed as kwargs to these methods, improper handling of column aliases can lead to SQL injection. Specifically, an attacker can inject malicious SQL code into the alias names via dictionary expansion, allowing them to manipulate database queries. This occurs because user-supplied input in the alias keys is not sufficiently sanitized before being incorporated into the raw SQL query. The issue affects FilteredRelation objects where the alias is dynamically generated from untrusted data. By exploiting this, an attacker could read, modify, or delete data in the database, depending on the query structure. The vulnerability is limited to PostgreSQL backends and requires specific conditions where annotate() or alias() is called with unsafe kwargs. Versions 4.2a1 to 4.2.26, 5.1a1 to 5.1.14, and 5.2a1 to 5.2.8 are affected. Patched versions properly escape alias names to prevent injection.
Platform: Django
Version: 4.2-5.2
Vulnerability: SQL injection
Severity: Moderate
date: Dec 2, 2025

Prediction: Patched Dec 2, 2025

What Undercode Say:

Analytics

Showing bash commands and codes related to the blog

Example vulnerable code snippet in Django

from django.db.models import FilteredRelation, Q

from myapp.models import Author

queryset = Author.objects.annotate(

{‘injected_alias’: FilteredRelation(‘book’, condition=Q(book__=’test’))}

)

Malicious alias could inject SQL via dictionary key

Check Django version

python -m django –version

Update Django to patched version

pip install Django==5.2.9

How Exploit:

Craft dictionary with SQL injection in alias key passed to annotate() on PostgreSQL.

Protection from this CVE

Update to patched versions.

Impact:

Data manipulation via SQL injection.

🎯Let’s Practice Exploiting & Learn Patching For Free:

Sources:

Reported By: github.com
Extra Source Hub:
Undercode

🔐JOIN OUR CYBER WORLD [ CVE News • HackMonitor • UndercodeNews ]

💬 Whatsapp | 💬 Telegram

📢 Follow DailyCVE & Stay Tuned:

𝕏 formerly Twitter 🐦 | @ Threads | 🔗 Linkedin Featured Image

Scroll to Top