mirror of
https://github.com/django/django.git
synced 2025-10-09 06:49:12 +00:00
[4.2.x] Fixed CVE-2025-59681 -- Protected QuerySet.annotate(), alias(), aggregate(), and extra() against SQL injection in column aliases on MySQL/MariaDB.
Thanks sw0rd1ight for the report. Follow up to 93cae5cb2f9a4ef1514cf1a41f714fef08005200. Backport of 41b43c74bda19753c757036673ea9db74acf494a from main.
This commit is contained in:
parent
7c7d2a4a10
commit
38d9ef8c7b
@ -46,9 +46,9 @@ from django.utils.tree import Node
|
||||
|
||||
__all__ = ["Query", "RawQuery"]
|
||||
|
||||
# Quotation marks ('"`[]), whitespace characters, semicolons, or inline
|
||||
# Quotation marks ('"`[]), whitespace characters, semicolons, hashes, or inline
|
||||
# SQL comments are forbidden in column aliases.
|
||||
FORBIDDEN_ALIAS_PATTERN = _lazy_re_compile(r"['`\"\]\[;\s]|--|/\*|\*/")
|
||||
FORBIDDEN_ALIAS_PATTERN = _lazy_re_compile(r"['`\"\]\[;\s]|#|--|/\*|\*/")
|
||||
|
||||
# Inspired from
|
||||
# https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
|
||||
@ -1123,8 +1123,8 @@ class Query(BaseExpression):
|
||||
def check_alias(self, alias):
|
||||
if FORBIDDEN_ALIAS_PATTERN.search(alias):
|
||||
raise ValueError(
|
||||
"Column aliases cannot contain whitespace characters, quotation marks, "
|
||||
"semicolons, or SQL comments."
|
||||
"Column aliases cannot contain whitespace characters, hashes, "
|
||||
"quotation marks, semicolons, or SQL comments."
|
||||
)
|
||||
|
||||
def add_annotation(self, annotation, alias, select=True):
|
||||
|
@ -7,4 +7,11 @@ Django 4.2.25 release notes
|
||||
Django 4.2.25 fixes one security issue with severity "high" and one security
|
||||
issue with severity "low" in 4.2.24.
|
||||
|
||||
...
|
||||
CVE-2025-59681: Potential SQL injection in ``QuerySet.annotate()``, ``alias()``, ``aggregate()``, and ``extra()`` on MySQL and MariaDB
|
||||
======================================================================================================================================
|
||||
|
||||
:meth:`.QuerySet.annotate`, :meth:`~.QuerySet.alias`,
|
||||
:meth:`~.QuerySet.aggregate`, and :meth:`~.QuerySet.extra` methods were subject
|
||||
to SQL injection in column aliases, using a suitably crafted dictionary, with
|
||||
dictionary expansion, as the ``**kwargs`` passed to these methods (follow up to
|
||||
:cve:`2022-28346`).
|
||||
|
@ -2090,8 +2090,8 @@ class AggregateTestCase(TestCase):
|
||||
def test_alias_sql_injection(self):
|
||||
crafted_alias = """injected_name" from "aggregation_author"; --"""
|
||||
msg = (
|
||||
"Column aliases cannot contain whitespace characters, quotation marks, "
|
||||
"semicolons, or SQL comments."
|
||||
"Column aliases cannot contain whitespace characters, hashes, quotation "
|
||||
"marks, semicolons, or SQL comments."
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
Author.objects.aggregate(**{crafted_alias: Avg("age")})
|
||||
|
@ -1116,8 +1116,8 @@ class NonAggregateAnnotationTestCase(TestCase):
|
||||
def test_alias_sql_injection(self):
|
||||
crafted_alias = """injected_name" from "annotations_book"; --"""
|
||||
msg = (
|
||||
"Column aliases cannot contain whitespace characters, quotation marks, "
|
||||
"semicolons, or SQL comments."
|
||||
"Column aliases cannot contain whitespace characters, hashes, quotation "
|
||||
"marks, semicolons, or SQL comments."
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
Book.objects.annotate(**{crafted_alias: Value(1)})
|
||||
@ -1125,8 +1125,8 @@ class NonAggregateAnnotationTestCase(TestCase):
|
||||
def test_alias_filtered_relation_sql_injection(self):
|
||||
crafted_alias = """injected_name" from "annotations_book"; --"""
|
||||
msg = (
|
||||
"Column aliases cannot contain whitespace characters, quotation marks, "
|
||||
"semicolons, or SQL comments."
|
||||
"Column aliases cannot contain whitespace characters, hashes, quotation "
|
||||
"marks, semicolons, or SQL comments."
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
Book.objects.annotate(**{crafted_alias: FilteredRelation("author")})
|
||||
@ -1143,13 +1143,14 @@ class NonAggregateAnnotationTestCase(TestCase):
|
||||
"ali/*as",
|
||||
"alias*/",
|
||||
"alias;",
|
||||
# [] are used by MSSQL.
|
||||
# [] and # are used by MSSQL.
|
||||
"alias[",
|
||||
"alias]",
|
||||
"ali#as",
|
||||
]
|
||||
msg = (
|
||||
"Column aliases cannot contain whitespace characters, quotation marks, "
|
||||
"semicolons, or SQL comments."
|
||||
"Column aliases cannot contain whitespace characters, hashes, quotation "
|
||||
"marks, semicolons, or SQL comments."
|
||||
)
|
||||
for crafted_alias in tests:
|
||||
with self.subTest(crafted_alias):
|
||||
@ -1428,8 +1429,8 @@ class AliasTests(TestCase):
|
||||
def test_alias_sql_injection(self):
|
||||
crafted_alias = """injected_name" from "annotations_book"; --"""
|
||||
msg = (
|
||||
"Column aliases cannot contain whitespace characters, quotation marks, "
|
||||
"semicolons, or SQL comments."
|
||||
"Column aliases cannot contain whitespace characters, hashes, quotation "
|
||||
"marks, semicolons, or SQL comments."
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
Book.objects.alias(**{crafted_alias: Value(1)})
|
||||
@ -1437,8 +1438,8 @@ class AliasTests(TestCase):
|
||||
def test_alias_filtered_relation_sql_injection(self):
|
||||
crafted_alias = """injected_name" from "annotations_book"; --"""
|
||||
msg = (
|
||||
"Column aliases cannot contain whitespace characters, quotation marks, "
|
||||
"semicolons, or SQL comments."
|
||||
"Column aliases cannot contain whitespace characters, hashes, quotation "
|
||||
"marks, semicolons, or SQL comments."
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
Book.objects.alias(**{crafted_alias: FilteredRelation("authors")})
|
||||
|
@ -37,8 +37,8 @@ class ValuesExpressionsTests(TestCase):
|
||||
def test_values_expression_alias_sql_injection(self):
|
||||
crafted_alias = """injected_name" from "expressions_company"; --"""
|
||||
msg = (
|
||||
"Column aliases cannot contain whitespace characters, quotation marks, "
|
||||
"semicolons, or SQL comments."
|
||||
"Column aliases cannot contain whitespace characters, hashes, quotation "
|
||||
"marks, semicolons, or SQL comments."
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
Company.objects.values(**{crafted_alias: F("ceo__salary")})
|
||||
@ -47,8 +47,8 @@ class ValuesExpressionsTests(TestCase):
|
||||
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."
|
||||
"Column aliases cannot contain whitespace characters, hashes, quotation "
|
||||
"marks, semicolons, or SQL comments."
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
JSONFieldModel.objects.values(f"data__{crafted_alias}")
|
||||
|
@ -1943,8 +1943,8 @@ class Queries5Tests(TestCase):
|
||||
def test_extra_select_alias_sql_injection(self):
|
||||
crafted_alias = """injected_name" from "queries_note"; --"""
|
||||
msg = (
|
||||
"Column aliases cannot contain whitespace characters, quotation marks, "
|
||||
"semicolons, or SQL comments."
|
||||
"Column aliases cannot contain whitespace characters, hashes, quotation "
|
||||
"marks, semicolons, or SQL comments."
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
Note.objects.extra(select={crafted_alias: "1"})
|
||||
|
Loading…
x
Reference in New Issue
Block a user