Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8008288

Browse files
committed
[4.0.x] Fixed CVE-2022-28346 -- Protected QuerySet.annotate(), aggregate(), and extra() against SQL injection in column aliases.
Thanks Splunk team: Preston Elder, Jacob Davis, Jacob Moore, Matt Hanson, David Briggs, and a security researcher: Danylo Dmytriiev (DDV_UA) for the report. Backport of 93cae5c from main.
1 parent 78e553b commit 8008288

File tree

8 files changed

+108
-0
lines changed

8 files changed

+108
-0
lines changed

django/db/models/sql/query.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@
4040
from django.db.models.sql.datastructures import BaseTable, Empty, Join, MultiJoin
4141
from django.db.models.sql.where import AND, OR, ExtraWhere, NothingNode, WhereNode
4242
from django.utils.functional import cached_property
43+
from django.utils.regex_helper import _lazy_re_compile
4344
from django.utils.tree import Node
4445

4546
__all__ = ["Query", "RawQuery"]
4647

48+
# Quotation marks ('"`[]), whitespace characters, semicolons, or inline
49+
# SQL comments are forbidden in column aliases.
50+
FORBIDDEN_ALIAS_PATTERN = _lazy_re_compile(r"['`\"\]\[;\s]|--|/\*|\*/")
51+
4752

4853
def get_field_names_from_opts(opts):
4954
return set(
@@ -1077,8 +1082,16 @@ def join_parent_model(self, opts, model, alias, seen):
10771082
alias = seen[int_model] = join_info.joins[-1]
10781083
return alias or seen[None]
10791084

1085+
def check_alias(self, alias):
1086+
if FORBIDDEN_ALIAS_PATTERN.search(alias):
1087+
raise ValueError(
1088+
"Column aliases cannot contain whitespace characters, quotation marks, "
1089+
"semicolons, or SQL comments."
1090+
)
1091+
10801092
def add_annotation(self, annotation, alias, is_summary=False, select=True):
10811093
"""Add a single annotation expression to the Query."""
1094+
self.check_alias(alias)
10821095
annotation = annotation.resolve_expression(
10831096
self, allow_joins=True, reuse=None, summarize=is_summary
10841097
)
@@ -2234,6 +2247,7 @@ def add_extra(self, select, select_params, where, params, tables, order_by):
22342247
else:
22352248
param_iter = iter([])
22362249
for name, entry in select.items():
2250+
self.check_alias(name)
22372251
entry = str(entry)
22382252
entry_params = []
22392253
pos = entry.find("%s")

docs/releases/2.2.28.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ Django 2.2.28 release notes
55
*April 11, 2022*
66

77
Django 2.2.28 fixes two security issues with severity "high" in 2.2.27.
8+
9+
CVE-2022-28346: Potential SQL injection in ``QuerySet.annotate()``, ``aggregate()``, and ``extra()``
10+
====================================================================================================
11+
12+
:meth:`.QuerySet.annotate`, :meth:`~.QuerySet.aggregate`, and
13+
:meth:`~.QuerySet.extra` methods were subject to SQL injection in column
14+
aliases, using a suitably crafted dictionary, with dictionary expansion, as the
15+
``**kwargs`` passed to these methods.

docs/releases/3.2.13.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ Django 3.2.13 release notes
77
Django 3.2.13 fixes two security issues with severity "high" in
88
3.2.12 and a regression in 3.2.4.
99

10+
CVE-2022-28346: Potential SQL injection in ``QuerySet.annotate()``, ``aggregate()``, and ``extra()``
11+
====================================================================================================
12+
13+
:meth:`.QuerySet.annotate`, :meth:`~.QuerySet.aggregate`, and
14+
:meth:`~.QuerySet.extra` methods were subject to SQL injection in column
15+
aliases, using a suitably crafted dictionary, with dictionary expansion, as the
16+
``**kwargs`` passed to these methods.
17+
1018
Bugfixes
1119
========
1220

docs/releases/4.0.4.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ Django 4.0.4 release notes
77
Django 4.0.4 fixes two security issues with severity "high" and two bugs in
88
4.0.3.
99

10+
CVE-2022-28346: Potential SQL injection in ``QuerySet.annotate()``, ``aggregate()``, and ``extra()``
11+
====================================================================================================
12+
13+
:meth:`.QuerySet.annotate`, :meth:`~.QuerySet.aggregate`, and
14+
:meth:`~.QuerySet.extra` methods were subject to SQL injection in column
15+
aliases, using a suitably crafted dictionary, with dictionary expansion, as the
16+
``**kwargs`` passed to these methods.
17+
1018
Bugfixes
1119
========
1220

tests/aggregation/tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,15 @@ def test_exists_none_with_aggregate(self):
20292029
)
20302030
self.assertEqual(len(qs), 6)
20312031

2032+
def test_alias_sql_injection(self):
2033+
crafted_alias = """injected_name" from "aggregation_author"; --"""
2034+
msg = (
2035+
"Column aliases cannot contain whitespace characters, quotation marks, "
2036+
"semicolons, or SQL comments."
2037+
)
2038+
with self.assertRaisesMessage(ValueError, msg):
2039+
Author.objects.aggregate(**{crafted_alias: Avg("age")})
2040+
20322041
def test_exists_extra_where_with_aggregate(self):
20332042
qs = Book.objects.all().annotate(
20342043
count=Count("id"),

tests/annotations/tests.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,40 @@ def test_annotation_aggregate_with_m2o(self):
10551055
],
10561056
)
10571057

1058+
def test_alias_sql_injection(self):
1059+
crafted_alias = """injected_name" from "annotations_book"; --"""
1060+
msg = (
1061+
"Column aliases cannot contain whitespace characters, quotation marks, "
1062+
"semicolons, or SQL comments."
1063+
)
1064+
with self.assertRaisesMessage(ValueError, msg):
1065+
Book.objects.annotate(**{crafted_alias: Value(1)})
1066+
1067+
def test_alias_forbidden_chars(self):
1068+
tests = [
1069+
'al"ias',
1070+
"a'lias",
1071+
"ali`as",
1072+
"alia s",
1073+
"alias\t",
1074+
"ali\nas",
1075+
"alias--",
1076+
"ali/*as",
1077+
"alias*/",
1078+
"alias;",
1079+
# [] are used by MSSQL.
1080+
"alias[",
1081+
"alias]",
1082+
]
1083+
msg = (
1084+
"Column aliases cannot contain whitespace characters, quotation marks, "
1085+
"semicolons, or SQL comments."
1086+
)
1087+
for crafted_alias in tests:
1088+
with self.subTest(crafted_alias):
1089+
with self.assertRaisesMessage(ValueError, msg):
1090+
Book.objects.annotate(**{crafted_alias: Value(1)})
1091+
10581092

10591093
class AliasTests(TestCase):
10601094
@classmethod
@@ -1318,3 +1352,12 @@ def test_values_alias(self):
13181352
with self.subTest(operation=operation):
13191353
with self.assertRaisesMessage(FieldError, msg):
13201354
getattr(qs, operation)("rating_alias")
1355+
1356+
def test_alias_sql_injection(self):
1357+
crafted_alias = """injected_name" from "annotations_book"; --"""
1358+
msg = (
1359+
"Column aliases cannot contain whitespace characters, quotation marks, "
1360+
"semicolons, or SQL comments."
1361+
)
1362+
with self.assertRaisesMessage(ValueError, msg):
1363+
Book.objects.alias(**{crafted_alias: Value(1)})

tests/expressions/test_queryset_values.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ def test_values_expression(self):
3434
[{"salary": 10}, {"salary": 20}, {"salary": 30}],
3535
)
3636

37+
def test_values_expression_alias_sql_injection(self):
38+
crafted_alias = """injected_name" from "expressions_company"; --"""
39+
msg = (
40+
"Column aliases cannot contain whitespace characters, quotation marks, "
41+
"semicolons, or SQL comments."
42+
)
43+
with self.assertRaisesMessage(ValueError, msg):
44+
Company.objects.values(**{crafted_alias: F("ceo__salary")})
45+
3746
def test_values_expression_group_by(self):
3847
# values() applies annotate() first, so values selected are grouped by
3948
# id, not firstname.

tests/queries/tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,15 @@ def test_extra_select_literal_percent_s(self):
18921892
Note.objects.extra(select={"foo": "'bar %%s'"})[0].foo, "bar %s"
18931893
)
18941894

1895+
def test_extra_select_alias_sql_injection(self):
1896+
crafted_alias = """injected_name" from "queries_note"; --"""
1897+
msg = (
1898+
"Column aliases cannot contain whitespace characters, quotation marks, "
1899+
"semicolons, or SQL comments."
1900+
)
1901+
with self.assertRaisesMessage(ValueError, msg):
1902+
Note.objects.extra(select={crafted_alias: "1"})
1903+
18951904
def test_queryset_reuse(self):
18961905
# Using querysets doesn't mutate aliases.
18971906
authors = Author.objects.filter(Q(name="a1") | Q(name="nonexistent"))

0 commit comments

Comments
 (0)