티스토리 수익 글 보기

티스토리 수익 글 보기

[5.1.x] Fixed CVE-2025-64459 — Prevented SQL injections in Q/QuerySe… · django/django@72d2c87 · GitHub
Skip to content

Commit 72d2c87

Browse files
jacobtylerwallsnessita
authored andcommitted
[5.1.x] Fixed CVE-2025-64459 — 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. Backport of c880530 from main.
1 parent 3790593 commit 72d2c87

File tree

4 files changed

+23
0
lines changed

4 files changed

+23
0
lines changed

django/db/models/query_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ class Q(tree.Node):
4747
XOR = "XOR"
4848
default = AND
4949
conditional = True
50+
connectors = (None, AND, OR, XOR)
5051

5152
def __init__(self, *args, _connector=None, _negated=False, **kwargs):
53+
if _connector not in self.connectors:
54+
connector_reprs = ", ".join(f"{conn!r}" for conn in self.connectors[1:])
55+
raise ValueError(f"_connector must be one of {connector_reprs}, or None.")
5256
super().__init__(
5357
children=[*args, *sorted(kwargs.items())],
5458
connector=_connector,

docs/releases/4.2.26.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ Windows. As a consequence, :class:`~django.http.HttpResponseRedirect`,
1616
:func:`redirect() <django.shortcuts.redirect>` were subject to a potential
1717
denial-of-service attack via certain inputs with a very large number of Unicode
1818
characters (follow up to :cve:`2025-27556`).
19+
20+
CVE-2025-64459: Potential SQL injection via ``_connector`` keyword argument
21+
===========================================================================
22+
23+
:meth:`.QuerySet.filter`, :meth:`~.QuerySet.exclude`, :meth:`~.QuerySet.get`,
24+
and :class:`~.Q` were subject to SQL injection using a suitably crafted
25+
dictionary, with dictionary expansion, as the ``_connector`` argument.

docs/releases/5.1.14.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ Windows. As a consequence, :class:`~django.http.HttpResponseRedirect`,
1616
:func:`redirect() <django.shortcuts.redirect>` were subject to a potential
1717
denial-of-service attack via certain inputs with a very large number of Unicode
1818
characters (follow up to :cve:`2025-27556`).
19+
20+
CVE-2025-64459: Potential SQL injection via ``_connector`` keyword argument
21+
===========================================================================
22+
23+
:meth:`.QuerySet.filter`, :meth:`~.QuerySet.exclude`, :meth:`~.QuerySet.get`,
24+
and :class:`~.Q` were subject to SQL injection using a suitably crafted
25+
dictionary, with dictionary expansion, as the ``_connector`` argument.

tests/queries/test_q.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ def test_create_helper(self):
264264
Q(*items, _connector=connector),
265265
)
266266

267+
def test_connector_validation(self):
268+
msg = f"_connector must be one of {Q.AND!r}, {Q.OR!r}, {Q.XOR!r}, or None."
269+
with self.assertRaisesMessage(ValueError, msg):
270+
Q(_connector="evil")
271+
267272
def test_referenced_base_fields(self):
268273
# Make sure Q.referenced_base_fields retrieves all base fields from
269274
# both filters and F expressions.

0 commit comments

Comments
 (0)