File CVE-2025-64459.patch of Package python-Django.19209

From 9595b2bbcd1b0cf2e4c9aed0152665a16d037717 Mon Sep 17 00:00:00 2001
From: Jacob Walls <jacobtylerwalls@gmail.com>
Date: Wed, 24 Sep 2025 15:54:51 -0400
Subject: [PATCH 2/3] [4.2.x] Fixed CVE-2025-62769 -- Prevented SQL injections
 in Q/QuerySet via the _connector kwarg.

Thanks cyberstan for the report, Sarah Boyce, Adam Johnson, Simon
Charette, and Jake Howard for the reviews.
---
 django/db/models/query_utils.py | 4 ++++
 docs/releases/4.2.26.txt        | 7 +++++++
 tests/queries/test_q.py         | 5 +++++
 3 files changed, 16 insertions(+)

Index: Django-2.2.28/django/db/models/query_utils.py
===================================================================
--- Django-2.2.28.orig/django/db/models/query_utils.py
+++ Django-2.2.28/django/db/models/query_utils.py
@@ -54,8 +54,12 @@ class Q(tree.Node):
     OR = 'OR'
     default = AND
     conditional = True
+    connectors = (None, AND, OR)
 
     def __init__(self, *args, _connector=None, _negated=False, **kwargs):
+        if _connector not in self.connectors:
+            connector_reprs = ", ".join(f"{conn!r}" for conn in self.connectors[1:])
+            raise ValueError(f"_connector must be one of {connector_reprs}, or None.")
         super().__init__(children=[*args, *sorted(kwargs.items())], connector=_connector, negated=_negated)
 
     def _combine(self, other, conn):
Index: Django-2.2.28/tests/queries/test_q.py
===================================================================
--- Django-2.2.28.orig/tests/queries/test_q.py
+++ Django-2.2.28/tests/queries/test_q.py
@@ -103,3 +103,8 @@ class QTests(SimpleTestCase):
         q = q1 & q2
         path, args, kwargs = q.deconstruct()
         self.assertEqual(Q(*args, **kwargs), q)
+
+    def test_connector_validation(self):
+        msg = f"_connector must be one of {Q.AND!r}, {Q.OR!r}, or None."
+        with self.assertRaisesMessage(ValueError, msg):
+            Q(_connector="evil")
Index: Django-2.2.28/django/db/models/query.py
===================================================================
--- Django-2.2.28.orig/django/db/models/query.py
+++ Django-2.2.28/django/db/models/query.py
@@ -42,6 +42,8 @@ class BaseIterable:
         self.chunked_fetch = chunked_fetch
         self.chunk_size = chunk_size
 
+PROHIBITED_FILTER_KWARGS = frozenset(["_connector", "_negated"])
+
 
 class ModelIterable(BaseIterable):
     """Iterable that yields a model instance for each row."""
@@ -902,6 +904,10 @@ class QuerySet:
         if args or kwargs:
             assert self.query.can_filter(), \
                 "Cannot filter a query once a slice has been taken."
+            invalid_kwargs = PROHIBITED_FILTER_KWARGS.intersection(kwargs)
+            if invalid_kwargs:
+                invalid_kwargs_str = ", ".join(f"'{k}'" for k in sorted(invalid_kwargs))
+                raise TypeError(f"The following kwargs are invalid: {invalid_kwargs_str}")
 
         clone = self._chain()
         if negate:
Index: Django-2.2.28/tests/queries/tests.py
===================================================================
--- Django-2.2.28.orig/tests/queries/tests.py
+++ Django-2.2.28/tests/queries/tests.py
@@ -3865,6 +3865,14 @@ class TestInvalidValuesRelation(SimpleTe
             Annotation.objects.filter(tag__in=[123, 'abc'])
 
 
+class TestInvalidFilterArguments(TestCase):
+    def test_filter_rejects_invalid_arguments(self):
+        school = School.objects.create()
+        msg = "The following kwargs are invalid: '_connector', '_negated'"
+        with self.assertRaisesMessage(TypeError, msg):
+            School.objects.filter(pk=school.pk, _negated=True, _connector="evil")
+
+
 class TestTicket24605(TestCase):
     def test_ticket_24605(self):
         """
openSUSE Build Service is sponsored by