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

Skip to content

Commit 9e19acc

Browse files
committed
[3.2.x] Fixed CVE-2022-28347 -- Protected QuerySet.explain(**options) against SQL injection on PostgreSQL.
Backport of 6723a26 from main.
1 parent 2044dac commit 9e19acc

File tree

6 files changed

+78
-9
lines changed

6 files changed

+78
-9
lines changed

django/db/backends/postgresql/features.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
5454
only_supports_unbounded_with_preceding_and_following = True
5555
supports_aggregate_filter_clause = True
5656
supported_explain_formats = {'JSON', 'TEXT', 'XML', 'YAML'}
57-
validates_explain_options = False # A query will error on invalid options.
5857
supports_deferrable_unique_constraints = True
5958
has_json_operators = True
6059
json_key_contains_list_matching_requires_list = True

django/db/backends/postgresql/operations.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
class DatabaseOperations(BaseDatabaseOperations):
88
cast_char_field_without_max_length = 'varchar'
99
explain_prefix = 'EXPLAIN'
10+
explain_options = frozenset(
11+
[
12+
"ANALYZE",
13+
"BUFFERS",
14+
"COSTS",
15+
"SETTINGS",
16+
"SUMMARY",
17+
"TIMING",
18+
"VERBOSE",
19+
"WAL",
20+
]
21+
)
1022
cast_data_types = {
1123
'AutoField': 'integer',
1224
'BigAutoField': 'bigint',
@@ -258,15 +270,20 @@ def subtract_temporals(self, internal_type, lhs, rhs):
258270
return super().subtract_temporals(internal_type, lhs, rhs)
259271

260272
def explain_query_prefix(self, format=None, **options):
261-
prefix = super().explain_query_prefix(format)
262273
extra = {}
263-
if format:
264-
extra['FORMAT'] = format
274+
# Normalize options.
265275
if options:
266-
extra.update({
267-
name.upper(): 'true' if value else 'false'
276+
options = {
277+
name.upper(): "true" if value else "false"
268278
for name, value in options.items()
269-
})
279+
}
280+
for valid_option in self.explain_options:
281+
value = options.pop(valid_option, None)
282+
if value is not None:
283+
extra[valid_option.upper()] = value
284+
prefix = super().explain_query_prefix(format, **options)
285+
if format:
286+
extra['FORMAT'] = format
270287
if extra:
271288
prefix += ' (%s)' % ', '.join('%s %s' % i for i in extra.items())
272289
return prefix

django/db/models/sql/query.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
# SQL comments are forbidden in column aliases.
5151
FORBIDDEN_ALIAS_PATTERN = _lazy_re_compile(r"['`\"\]\[;\s]|--|/\*|\*/")
5252

53+
# Inspired from
54+
# https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
55+
EXPLAIN_OPTIONS_PATTERN = _lazy_re_compile(r"[\w\-]+")
56+
5357

5458
def get_field_names_from_opts(opts):
5559
return set(chain.from_iterable(
@@ -558,6 +562,12 @@ def has_results(self, using):
558562

559563
def explain(self, using, format=None, **options):
560564
q = self.clone()
565+
for option_name in options:
566+
if (
567+
not EXPLAIN_OPTIONS_PATTERN.fullmatch(option_name) or
568+
"--" in option_name
569+
):
570+
raise ValueError(f"Invalid option name: {option_name!r}.")
561571
q.explain_query = True
562572
q.explain_format = format
563573
q.explain_options = options

docs/releases/2.2.28.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ CVE-2022-28346: Potential SQL injection in ``QuerySet.annotate()``, ``aggregate(
1313
:meth:`~.QuerySet.extra` methods were subject to SQL injection in column
1414
aliases, using a suitably crafted dictionary, with dictionary expansion, as the
1515
``**kwargs`` passed to these methods.
16+
17+
CVE-2022-28347: Potential SQL injection via ``QuerySet.explain(**options)`` on PostgreSQL
18+
=========================================================================================
19+
20+
:meth:`.QuerySet.explain` method was subject to SQL injection in option names,
21+
using a suitably crafted dictionary, with dictionary expansion, as the
22+
``**options`` argument.

docs/releases/3.2.13.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ CVE-2022-28346: Potential SQL injection in ``QuerySet.annotate()``, ``aggregate(
1515
aliases, using a suitably crafted dictionary, with dictionary expansion, as the
1616
``**kwargs`` passed to these methods.
1717

18+
CVE-2022-28347: Potential SQL injection via ``QuerySet.explain(**options)`` on PostgreSQL
19+
=========================================================================================
20+
21+
:meth:`.QuerySet.explain` method was subject to SQL injection in option names,
22+
using a suitably crafted dictionary, with dictionary expansion, as the
23+
``**options`` argument.
24+
1825
Bugfixes
1926
========
2027

tests/queries/test_explain.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def test_basic(self):
3434

3535
@skipUnlessDBFeature('validates_explain_options')
3636
def test_unknown_options(self):
37-
with self.assertRaisesMessage(ValueError, 'Unknown options: test, test2'):
38-
Tag.objects.all().explain(test=1, test2=1)
37+
with self.assertRaisesMessage(ValueError, "Unknown options: TEST, TEST2"):
38+
Tag.objects.all().explain(**{"TEST": 1, "TEST2": 1})
3939

4040
def test_unknown_format(self):
4141
msg = 'DOES NOT EXIST is not a recognized format.'
@@ -68,6 +68,35 @@ def test_postgres_options(self):
6868
option = '{} {}'.format(name.upper(), 'true' if value else 'false')
6969
self.assertIn(option, captured_queries[0]['sql'])
7070

71+
def test_option_sql_injection(self):
72+
qs = Tag.objects.filter(name="test")
73+
options = {"SUMMARY true) SELECT 1; --": True}
74+
msg = "Invalid option name: 'SUMMARY true) SELECT 1; --'"
75+
with self.assertRaisesMessage(ValueError, msg):
76+
qs.explain(**options)
77+
78+
def test_invalid_option_names(self):
79+
qs = Tag.objects.filter(name="test")
80+
tests = [
81+
'opt"ion',
82+
"o'ption",
83+
"op`tion",
84+
"opti on",
85+
"option--",
86+
"optio\tn",
87+
"o\nption",
88+
"option;",
89+
"你 好",
90+
# [] are used by MSSQL.
91+
"option[",
92+
"option]",
93+
]
94+
for invalid_option in tests:
95+
with self.subTest(invalid_option):
96+
msg = f"Invalid option name: {invalid_option!r}"
97+
with self.assertRaisesMessage(ValueError, msg):
98+
qs.explain(**{invalid_option: True})
99+
71100
@unittest.skipUnless(connection.vendor == 'mysql', 'MySQL specific')
72101
def test_mysql_text_to_traditional(self):
73102
# Ensure these cached properties are initialized to prevent queries for

0 commit comments

Comments
 (0)