summaryrefslogtreecommitdiffstats
path: root/meta-python/recipes-devtools/python/python3-django/CVE-2024-42005.patch
blob: e6b58fca79017f31b17b31c58732cc002ad0654c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
From f4af67b9b41e0f4c117a8741da3abbd1c869ab28 Mon Sep 17 00:00:00 2001
From: Simon Charette <charette.s@gmail.com>
Date: Thu, 25 Jul 2024 18:19:13 +0200
Subject: [PATCH] Fixed CVE-2024-42005 -- Mitigated QuerySet.values()  SQL
 injection attacks against JSON fields.

Thanks Eyal (eyalgabay) for the report.

CVE: CVE-2024-42005

Upstream-Status: Backport [https://github.com/django/django/commit/f4af67b9b41e0f4c117a8741da3abbd1c869ab28]

Signed-off-by: Soumya Sambu <soumya.sambu@windriver.com>
---
 django/db/models/sql/query.py             |  2 ++
 tests/expressions/models.py               |  7 +++++++
 tests/expressions/test_queryset_values.py | 17 +++++++++++++++--
 3 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 1e823cf..9b054bd 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -2019,6 +2019,8 @@ class Query:
             self.clear_select_fields()

         if fields:
+            for field in fields:
+                self.check_alias(field)
             field_names = []
             extra_names = []
             annotation_names = []
diff --git a/tests/expressions/models.py b/tests/expressions/models.py
index 33f7850..fb80938 100644
--- a/tests/expressions/models.py
+++ b/tests/expressions/models.py
@@ -97,3 +97,10 @@ class UUID(models.Model):

     def __str__(self):
         return "%s" % self.uuid
+
+
+class JSONFieldModel(models.Model):
+    data = models.JSONField(null=True)
+
+    class Meta:
+        required_db_features = {"supports_json_field"}
diff --git a/tests/expressions/test_queryset_values.py b/tests/expressions/test_queryset_values.py
index 0804531..bd52b8e 100644
--- a/tests/expressions/test_queryset_values.py
+++ b/tests/expressions/test_queryset_values.py
@@ -1,8 +1,8 @@
 from django.db.models.aggregates import Sum
 from django.db.models.expressions import F
-from django.test import TestCase
+from django.test import TestCase, skipUnlessDBFeature

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


 class ValuesExpressionsTests(TestCase):
@@ -36,6 +36,19 @@ class ValuesExpressionsTests(TestCase):
         with self.assertRaisesMessage(ValueError, msg):
             Company.objects.values(**{crafted_alias: F("ceo__salary")})

+    @skipUnlessDBFeature("supports_json_field")
+    def test_values_expression_alias_sql_injection_json_field(self):
+        crafted_alias = """injected_name" from "expressions_company"; --"""
+        msg = (
+            "Column aliases cannot contain whitespace characters, quotation marks, "
+            "semicolons, or SQL comments."
+        )
+        with self.assertRaisesMessage(ValueError, msg):
+            JSONFieldModel.objects.values(f"data__{crafted_alias}")
+
+        with self.assertRaisesMessage(ValueError, msg):
+            JSONFieldModel.objects.values_list(f"data__{crafted_alias}")
+
     def test_values_expression_group_by(self):
         # values() applies annotate() first, so values selected are grouped by
         # id, not firstname.
--
2.40.0