티스토리 수익 글 보기

티스토리 수익 글 보기

[6.0.x] Fixed CVE-2025-13372 — Protected FilteredRelation against SQ… · django/django@56aea00 · GitHub
Skip to content

Commit 56aea00

Browse files
jacobtylerwallsnessita
authored andcommitted
[6.0.x] Fixed CVE-2025-13372 — Protected FilteredRelation against SQL injection in column aliases on PostgreSQL.
Follow-up to CVE-2025-57833. Thanks Stackered for the report, and Simon Charette and Mariusz Felisiak for the reviews. Backport of 5b90ca1 from main.
1 parent c95abc5 commit 56aea00

File tree

5 files changed

+45
1
lines changed

5 files changed

+45
1
lines changed

django/db/backends/postgresql/compiler.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.db.models.sql.compiler import ( # isort:skip
22
SQLAggregateCompiler,
3-
SQLCompiler,
3+
SQLCompiler as BaseSQLCompiler,
44
SQLDeleteCompiler,
55
SQLInsertCompiler as BaseSQLInsertCompiler,
66
SQLUpdateCompiler,
@@ -25,6 +25,15 @@ def __str__(self):
2525
return "UNNEST(%s)" % ", ".join(self)
2626

2727

28+
class SQLCompiler(BaseSQLCompiler):
29+
def quote_name_unless_alias(self, name):
30+
if "$" in name:
31+
raise ValueError(
32+
"Dollar signs are not permitted in column aliases on PostgreSQL."
33+
)
34+
return super().quote_name_unless_alias(name)
35+
36+
2837
class SQLInsertCompiler(BaseSQLInsertCompiler):
2938
def assemble_as_sql(self, fields, value_rows):
3039
# Specialize bulk-insertion of literal values through UNNEST to

docs/releases/4.2.27.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ Django 4.2.27 release notes
77
Django 4.2.27 fixes one security issue with severity "high", one security issue
88
with severity "moderate", and one bug in 4.2.26.
99

10+
CVE-2025-13372: Potential SQL injection in ``FilteredRelation`` column aliases on PostgreSQL
11+
============================================================================================
12+
13+
:class:`.FilteredRelation` was subject to SQL injection in column aliases,
14+
using a suitably crafted dictionary, with dictionary expansion, as the
15+
``**kwargs`` passed to :meth:`.QuerySet.annotate` or :meth:`.QuerySet.alias` on
16+
PostgreSQL.
17+
1018
Bugfixes
1119
========
1220

docs/releases/5.1.15.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ Django 5.1.15 release notes
77
Django 5.1.15 fixes one security issue with severity "high", one security issue
88
with severity "moderate", and one bug in 5.1.14.
99

10+
CVE-2025-13372: Potential SQL injection in ``FilteredRelation`` column aliases on PostgreSQL
11+
============================================================================================
12+
13+
:class:`.FilteredRelation` was subject to SQL injection in column aliases,
14+
using a suitably crafted dictionary, with dictionary expansion, as the
15+
``**kwargs`` passed to :meth:`.QuerySet.annotate` or :meth:`.QuerySet.alias` on
16+
PostgreSQL.
17+
1018
Bugfixes
1119
========
1220

docs/releases/5.2.9.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ Django 5.2.9 release notes
77
Django 5.2.9 fixes one security issue with severity "high", one security issue
88
with severity "moderate", and several bugs in 5.2.8.
99

10+
CVE-2025-13372: Potential SQL injection in ``FilteredRelation`` column aliases on PostgreSQL
11+
============================================================================================
12+
13+
:class:`.FilteredRelation` was subject to SQL injection in column aliases,
14+
using a suitably crafted dictionary, with dictionary expansion, as the
15+
``**kwargs`` passed to :meth:`.QuerySet.annotate` or :meth:`.QuerySet.alias` on
16+
PostgreSQL.
17+
1018
Bugfixes
1119
========
1220

tests/annotations/tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,3 +1540,14 @@ def test_alias_filtered_relation_sql_injection(self):
15401540
)
15411541
with self.assertRaisesMessage(ValueError, msg):
15421542
Book.objects.alias(**{crafted_alias: FilteredRelation("authors")})
1543+
1544+
def test_alias_filtered_relation_sql_injection_dollar_sign(self):
1545+
qs = Book.objects.alias(
1546+
**{"crafted_alia$": FilteredRelation("authors")}
1547+
).values("name", "crafted_alia$")
1548+
if connection.vendor == "postgresql":
1549+
msg = "Dollar signs are not permitted in column aliases on PostgreSQL."
1550+
with self.assertRaisesMessage(ValueError, msg):
1551+
list(qs)
1552+
else:
1553+
self.assertEqual(qs.first()["name"], self.b1.name)

0 commit comments

Comments
 (0)