Skip to main content
CVE Vulnerability Database
Vulnerability Database/CVE-2022-28346

CVE-2022-28346: Django SQL Injection Vulnerability

CVE-2022-28346 is a SQL injection vulnerability in Django affecting QuerySet methods through crafted dictionary expansion. This article covers the technical details, affected versions, security impact, and mitigation.

Published:

CVE-2022-28346 Overview

CVE-2022-28346 is a SQL Injection vulnerability affecting Django, the popular Python web framework. The vulnerability exists in the QuerySet.annotate(), aggregate(), and extra() methods, which are susceptible to SQL injection attacks through crafted dictionary arguments passed via **kwargs. An attacker can exploit this flaw by injecting malicious SQL code into column aliases when dictionary expansion is used.

Critical Impact

This vulnerability allows remote attackers to execute arbitrary SQL commands against the database backend without authentication. Successful exploitation could lead to complete database compromise, including unauthorized data access, data modification, or data destruction.

Affected Products

  • Django versions 2.2 before 2.2.28
  • Django versions 3.2 before 3.2.13
  • Django versions 4.0 before 4.0.4
  • Debian Linux 9.0 and 11.0 (packages using vulnerable Django versions)

Discovery Timeline

  • 2022-04-11 - Django Project releases security advisory and patches
  • 2022-04-12 - CVE-2022-28346 published to NVD
  • 2024-11-21 - Last updated in NVD database

Technical Details for CVE-2022-28346

Vulnerability Analysis

This SQL Injection vulnerability (CWE-89) stems from insufficient sanitization of column alias names in Django's QuerySet methods. The annotate(), aggregate(), and extra() methods accept keyword arguments that define column aliases for aggregated or annotated fields. When an attacker controls the keys of a dictionary that is expanded using the **kwargs syntax, they can inject malicious SQL code into the resulting query.

The vulnerability is particularly dangerous because Django's ORM is widely trusted to provide protection against SQL injection attacks. Developers may not expect that column alias names require additional sanitization, making this an unexpected attack vector in otherwise secure code.

Root Cause

The root cause lies in how Django processes dictionary keys when they are used as column aliases in SQL queries. When a dictionary is passed to methods like annotate() or aggregate() using **kwargs expansion, Django uses the dictionary keys directly as column aliases in the generated SQL without proper escaping or validation. This allows an attacker who can control dictionary keys to inject arbitrary SQL syntax.

The issue specifically affects scenarios where user-controlled data influences the structure of dictionaries that are subsequently expanded in QuerySet method calls. This pattern can occur in applications that dynamically build queries based on user input or configuration data.

Attack Vector

The attack is network-based and requires no authentication or user interaction, making it highly exploitable in web applications. An attacker can craft malicious requests where user input eventually becomes dictionary keys that are expanded in vulnerable QuerySet method calls.

For example, if an application allows users to specify field names for aggregation and those names are used as dictionary keys without validation, an attacker could inject SQL code through specially crafted field names. The injected code would execute within the context of the database connection used by the Django application.

The vulnerability manifests when crafted dictionaries are passed to Django ORM methods. For detailed technical analysis, refer to the Django Weblog Security Releases advisory.

Detection Methods for CVE-2022-28346

Indicators of Compromise

  • Unusual SQL query patterns in database logs containing unexpected column aliases or SQL syntax in alias positions
  • Error logs showing SQL syntax errors with suspicious content in alias fields
  • Database audit logs revealing queries with injected commands in annotation or aggregation clauses
  • Web application logs showing requests with special characters or SQL keywords in form fields or URL parameters that map to aggregation fields

Detection Strategies

  • Implement Web Application Firewall (WAF) rules to detect SQL injection patterns in request parameters
  • Monitor Django application logs for ORM errors or unexpected query patterns involving annotate(), aggregate(), or extra() methods
  • Deploy database activity monitoring to detect anomalous query structures or privilege escalation attempts
  • Use Static Application Security Testing (SAST) tools to identify code patterns where user input flows into dictionary keys used in QuerySet methods

Monitoring Recommendations

  • Enable detailed SQL query logging in development and staging environments to identify potentially vulnerable query patterns
  • Implement runtime application self-protection (RASP) to detect SQL injection attempts in real-time
  • Set up alerts for database errors related to SQL syntax issues that may indicate exploitation attempts
  • Review Django debug logs for QuerySet operations that include unexpected characters in column aliases

How to Mitigate CVE-2022-28346

Immediate Actions Required

  • Upgrade Django to patched versions: 2.2.28, 3.2.13, or 4.0.4 or later immediately
  • Audit application code for patterns where user input may influence dictionary keys used in annotate(), aggregate(), or extra() calls
  • Implement input validation to ensure that any user-controlled data used in QuerySet method arguments contains only expected characters
  • Consider implementing a Web Application Firewall as a temporary protective measure while upgrades are deployed

Patch Information

The Django Project has released security patches addressing this vulnerability. Affected users should upgrade to the following fixed versions:

  • Django 2.2.28 for the 2.2 LTS branch
  • Django 3.2.13 for the 3.2 LTS branch
  • Django 4.0.4 for the 4.0 release series

Detailed patch information and release notes are available in the Django Security Release Notes. Linux distribution users should also apply updates from their package managers, as documented in the Debian Security Advisory DSA-5254 and Fedora Package Announcements.

Workarounds

  • Validate and sanitize all dictionary keys before using them in QuerySet method calls with **kwargs expansion
  • Implement an allowlist of permitted column alias names and reject any input not matching the allowlist
  • Avoid using user-controlled data as dictionary keys in Django ORM queries; use static keys and map user input to predetermined values
  • Consider using parameterized queries or Django's F() expressions instead of dynamic annotation names where possible
bash
# Upgrade Django to patched version
pip install --upgrade Django>=3.2.13

# Verify installed version
python -c "import django; print(django.VERSION)"

# For production deployments, update requirements.txt
# Django>=3.2.13,<4.0  # For 3.2 LTS users
# Django>=4.0.4        # For 4.0 users

Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.

Default Legacy - Prefooter | Experience the World’s Most Advanced Cybersecurity Platform

Experience the Most Advanced Cybersecurity Platform

See how the world’s most intelligent, autonomous cybersecurity platform can protect your organization today and into the future.