File tree Expand file tree Collapse file tree
django/db/backends/postgresql Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ from django .db .models .sql .compiler import ( # isort:skip
2+ SQLAggregateCompiler ,
3+ SQLCompiler as BaseSQLCompiler ,
4+ SQLDeleteCompiler ,
5+ SQLInsertCompiler ,
6+ SQLUpdateCompiler ,
7+ )
8+
9+ __all__ = [
10+ "SQLAggregateCompiler" ,
11+ "SQLCompiler" ,
12+ "SQLDeleteCompiler" ,
13+ "SQLInsertCompiler" ,
14+ "SQLUpdateCompiler" ,
15+ ]
16+
17+
18+ class SQLCompiler (BaseSQLCompiler ):
19+ def quote_name_unless_alias (self , name ):
20+ if "$" in name :
21+ raise ValueError (
22+ "Dollar signs are not permitted in column aliases on PostgreSQL."
23+ )
24+ return super ().quote_name_unless_alias (name )
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ def get_json_dumps(encoder):
2424
2525
2626class DatabaseOperations (BaseDatabaseOperations ):
27+ compiler_module = "django.db.backends.postgresql.compiler"
2728 cast_char_field_without_max_length = "varchar"
2829 explain_prefix = "EXPLAIN"
2930 explain_options = frozenset (
Original file line number Diff line number Diff line change @@ -7,6 +7,14 @@ Django 4.2.27 release notes
77Django 4.2.27 fixes one security issue with severity "high", one security issue
88with 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+
1018Bugfixes
1119========
1220
Original file line number Diff line number Diff line change @@ -7,6 +7,14 @@ Django 5.1.15 release notes
77Django 5.1.15 fixes one security issue with severity "high", one security issue
88with 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+
1018Bugfixes
1119========
1220
Original file line number Diff line number Diff line change 22from decimal import Decimal
33
44from django .core .exceptions import FieldDoesNotExist , FieldError
5+ from django .db import connection
56from django .db .models import (
67 BooleanField ,
78 Case ,
@@ -1454,3 +1455,14 @@ def test_alias_filtered_relation_sql_injection(self):
14541455 )
14551456 with self .assertRaisesMessage (ValueError , msg ):
14561457 Book .objects .alias (** {crafted_alias : FilteredRelation ("authors" )})
1458+
1459+ def test_alias_filtered_relation_sql_injection_dollar_sign (self ):
1460+ qs = Book .objects .alias (
1461+ ** {"crafted_alia$" : FilteredRelation ("authors" )}
1462+ ).values ("name" , "crafted_alia$" )
1463+ if connection .vendor == "postgresql" :
1464+ msg = "Dollar signs are not permitted in column aliases on PostgreSQL."
1465+ with self .assertRaisesMessage (ValueError , msg ):
1466+ list (qs )
1467+ else :
1468+ self .assertEqual (qs .first ()["name" ], self .b1 .name )
You can’t perform that action at this time.
0 commit comments