Skip to content

Commit 32ebcbf

Browse files
charettessarahboyce
authored andcommitted
[5.0.x] Fixed CVE-2024-42005 -- Mitigated QuerySet.values() SQL injection attacks against JSON fields.
Thanks Eyal (eyalgabay) for the report.
1 parent 523da87 commit 32ebcbf

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

django/db/models/sql/query.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,6 +2446,8 @@ def set_values(self, fields):
24462446
self.has_select_fields = True
24472447

24482448
if fields:
2449+
for field in fields:
2450+
self.check_alias(field)
24492451
field_names = []
24502452
extra_names = []
24512453
annotation_names = []

docs/releases/4.2.15.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ CVE-2024-41991: Potential denial-of-service vulnerability in ``django.utils.html
3030
subject to a potential denial-of-service attack via certain inputs with a very
3131
large number of Unicode characters.
3232

33+
CVE-2024-42005: Potential SQL injection in ``QuerySet.values()`` and ``values_list()``
34+
======================================================================================
35+
36+
:meth:`.QuerySet.values` and :meth:`~.QuerySet.values_list` methods on models
37+
with a ``JSONField`` were subject to SQL injection in column aliases, via a
38+
crafted JSON object key as a passed ``*arg``.
39+
3340
Bugfixes
3441
========
3542

docs/releases/5.0.8.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ CVE-2024-41991: Potential denial-of-service vulnerability in ``django.utils.html
3030
subject to a potential denial-of-service attack via certain inputs with a very
3131
large number of Unicode characters.
3232

33+
CVE-2024-42005: Potential SQL injection in ``QuerySet.values()`` and ``values_list()``
34+
======================================================================================
35+
36+
:meth:`.QuerySet.values` and :meth:`~.QuerySet.values_list` methods on models
37+
with a ``JSONField`` were subject to SQL injection in column aliases, via a
38+
crafted JSON object key as a passed ``*arg``.
39+
3340
Bugfixes
3441
========
3542

tests/expressions/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,10 @@ class UUIDPK(models.Model):
107107
class UUID(models.Model):
108108
uuid = models.UUIDField(null=True)
109109
uuid_fk = models.ForeignKey(UUIDPK, models.CASCADE, null=True)
110+
111+
112+
class JSONFieldModel(models.Model):
113+
data = models.JSONField(null=True)
114+
115+
class Meta:
116+
required_db_features = {"supports_json_field"}

tests/expressions/test_queryset_values.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.db.models import F, Sum
2-
from django.test import TestCase
2+
from django.test import TestCase, skipUnlessDBFeature
33

4-
from .models import Company, Employee
4+
from .models import Company, Employee, JSONFieldModel
55

66

77
class ValuesExpressionsTests(TestCase):
@@ -43,6 +43,19 @@ def test_values_expression_alias_sql_injection(self):
4343
with self.assertRaisesMessage(ValueError, msg):
4444
Company.objects.values(**{crafted_alias: F("ceo__salary")})
4545

46+
@skipUnlessDBFeature("supports_json_field")
47+
def test_values_expression_alias_sql_injection_json_field(self):
48+
crafted_alias = """injected_name" from "expressions_company"; --"""
49+
msg = (
50+
"Column aliases cannot contain whitespace characters, quotation marks, "
51+
"semicolons, or SQL comments."
52+
)
53+
with self.assertRaisesMessage(ValueError, msg):
54+
JSONFieldModel.objects.values(f"data__{crafted_alias}")
55+
56+
with self.assertRaisesMessage(ValueError, msg):
57+
JSONFieldModel.objects.values_list(f"data__{crafted_alias}")
58+
4659
def test_values_expression_group_by(self):
4760
# values() applies annotate() first, so values selected are grouped by
4861
# id, not firstname.

0 commit comments

Comments
 (0)