티스토리 수익 글 보기

티스토리 수익 글 보기

[1.11.x] Fixed CVE-2020-7471 — Properly escaped StringAgg(delimiter)… · django/django@001b063 · GitHub
Skip to content
/ django Public

Commit 001b063

Browse files
committed
[1.11.x] Fixed CVE-2020-7471 — Properly escaped StringAgg(delimiter) parameter.
1 parent 7fd1ca3 commit 001b063

File tree

4 files changed

+22
2
lines changed

4 files changed

+22
2
lines changed

django/contrib/postgres/aggregates/general.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.contrib.postgres.fields import JSONField
2+
from django.db.models import Value
23
from django.db.models.aggregates import Aggregate
34

45
__all__ = [
@@ -43,11 +44,12 @@ def convert_value(self, value, expression, connection, context):
4344

4445
class StringAgg(Aggregate):
4546
function = 'STRING_AGG'
46-
template = "%(function)s(%(distinct)s%(expressions)s, '%(delimiter)s')"
47+
template = '%(function)s(%(distinct)s%(expressions)s)'
4748

4849
def __init__(self, expression, delimiter, distinct=False, **extra):
4950
distinct = 'DISTINCT ' if distinct else ''
50-
super(StringAgg, self).__init__(expression, delimiter=delimiter, distinct=distinct, **extra)
51+
delimiter_expr = Value(str(delimiter))
52+
super(StringAgg, self).__init__(expression, delimiter_expr, distinct=distinct, **extra)
5153

5254
def convert_value(self, value, expression, connection, context):
5355
if not value:

docs/releases/1.11.28.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
============================
2+
Django 1.11.28 release notes
3+
============================
4+
5+
*February 3, 2020*
6+
7+
Django 1.11.28 fixes a security issue in 1.11.27.
8+
9+
CVE-2020-7471: Potential SQL injection via ``StringAgg(delimiter)``
10+
===================================================================
11+
12+
:class:`~django.contrib.postgres.aggregates.StringAgg` aggregation function was
13+
subject to SQL injection, using a suitably crafted ``delimiter``.

docs/releases/index.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ versions of the documentation contain the release notes for any later releases.
2626
.. toctree::
2727
:maxdepth: 1
2828

29+
1.11.28
2930
1.11.27
3031
1.11.26
3132
1.11.25

tests/postgres_tests/test_aggregates.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ def test_string_agg_requires_delimiter(self):
108108
with self.assertRaises(TypeError):
109109
AggregateTestModel.objects.aggregate(stringagg=StringAgg('char_field'))
110110

111+
def test_string_agg_delimiter_escaping(self):
112+
values = AggregateTestModel.objects.aggregate(stringagg=StringAgg('char_field', delimiter="'"))
113+
self.assertEqual(values, {'stringagg': "Foo1'Foo2'Foo3'Foo4"})
114+
111115
def test_string_agg_charfield(self):
112116
values = AggregateTestModel.objects.aggregate(stringagg=StringAgg('char_field', delimiter=';'))
113117
self.assertEqual(values, {'stringagg': 'Foo1;Foo2;Foo3;Foo4'})

0 commit comments

Comments
 (0)