Skip to content

Commit f74b3ae

Browse files
felixxmcarltongibson
authored andcommitted
[2.1.x] Fixed CVE-2019-14234 -- Protected JSONField/HStoreField key and index lookups against SQL injection.
Thanks to Sage M. Abdullah for the report and initial patch. Thanks Florian Apolloner for reviews.
1 parent 5ff8e79 commit f74b3ae

File tree

6 files changed

+52
-8
lines changed

6 files changed

+52
-8
lines changed

django/contrib/postgres/fields/hstore.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def __init__(self, key_name, *args, **kwargs):
8686

8787
def as_sql(self, compiler, connection):
8888
lhs, params = compiler.compile(self.lhs)
89-
return "(%s -> '%s')" % (lhs, self.key_name), params
89+
return '(%s -> %%s)' % lhs, [self.key_name] + params
9090

9191

9292
class KeyTransformFactory:

django/contrib/postgres/fields/jsonb.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,10 @@ def as_sql(self, compiler, connection):
109109
if len(key_transforms) > 1:
110110
return "(%s %s %%s)" % (lhs, self.nested_operator), [key_transforms] + params
111111
try:
112-
int(self.key_name)
112+
lookup = int(self.key_name)
113113
except ValueError:
114-
lookup = "'%s'" % self.key_name
115-
else:
116-
lookup = "%s" % self.key_name
117-
return "(%s %s %s)" % (lhs, self.operator, lookup), params
114+
lookup = self.key_name
115+
return '(%s %s %%s)' % (lhs, self.operator), [lookup] + params
118116

119117

120118
class KeyTextTransform(KeyTransform):

docs/releases/1.11.23.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@ Remember that absolutely NO guarantee is provided about the results of
3636
``strip_tags()`` being HTML safe. So NEVER mark safe the result of a
3737
``strip_tags()`` call without escaping it first, for example with
3838
:func:`django.utils.html.escape`.
39+
40+
CVE-2019-14234: SQL injection possibility in key and index lookups for ``JSONField``/``HStoreField``
41+
====================================================================================================
42+
43+
:lookup:`Key and index lookups <jsonfield.key>` for
44+
:class:`~django.contrib.postgres.fields.JSONField` and :lookup:`key lookups
45+
<hstorefield.key>` for :class:`~django.contrib.postgres.fields.HStoreField`
46+
were subject to SQL injection, using a suitably crafted dictionary, with
47+
dictionary expansion, as the ``**kwargs`` passed to ``QuerySet.filter()``.

docs/releases/2.1.11.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@ Remember that absolutely NO guarantee is provided about the results of
3636
``strip_tags()`` being HTML safe. So NEVER mark safe the result of a
3737
``strip_tags()`` call without escaping it first, for example with
3838
:func:`django.utils.html.escape`.
39+
40+
CVE-2019-14234: SQL injection possibility in key and index lookups for ``JSONField``/``HStoreField``
41+
====================================================================================================
42+
43+
:lookup:`Key and index lookups <jsonfield.key>` for
44+
:class:`~django.contrib.postgres.fields.JSONField` and :lookup:`key lookups
45+
<hstorefield.key>` for :class:`~django.contrib.postgres.fields.HStoreField`
46+
were subject to SQL injection, using a suitably crafted dictionary, with
47+
dictionary expansion, as the ``**kwargs`` passed to ``QuerySet.filter()``.

tests/postgres_tests/test_hstore.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import json
22

33
from django.core import checks, exceptions, serializers
4+
from django.db import connection
45
from django.forms import Form
5-
from django.test.utils import isolate_apps, modify_settings
6+
from django.test.utils import (
7+
CaptureQueriesContext, isolate_apps, modify_settings,
8+
)
69

710
from . import PostgreSQLTestCase
811
from .models import HStoreModel, PostgreSQLModel
@@ -189,6 +192,18 @@ def test_usage_in_subquery(self):
189192
self.objs[:2]
190193
)
191194

195+
def test_key_sql_injection(self):
196+
with CaptureQueriesContext(connection) as queries:
197+
self.assertFalse(
198+
HStoreModel.objects.filter(**{
199+
"field__test' = 'a') OR 1 = 1 OR ('d": 'x',
200+
}).exists()
201+
)
202+
self.assertIn(
203+
"""."field" -> 'test'' = ''a'') OR 1 = 1 OR (''d') = 'x' """,
204+
queries[0]['sql'],
205+
)
206+
192207

193208
@isolate_apps('postgres_tests')
194209
class TestChecks(PostgreSQLTestCase):

tests/postgres_tests/test_json.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
from django.core import checks, exceptions, serializers
66
from django.core.serializers.json import DjangoJSONEncoder
7+
from django.db import connection
78
from django.db.models import Q
89
from django.forms import CharField, Form, widgets
9-
from django.test.utils import isolate_apps
10+
from django.test.utils import CaptureQueriesContext, isolate_apps
1011
from django.utils.html import escape
1112

1213
from . import PostgreSQLTestCase
@@ -299,6 +300,18 @@ def test_regex(self):
299300
def test_iregex(self):
300301
self.assertTrue(JSONModel.objects.filter(field__foo__iregex=r'^bAr$').exists())
301302

303+
def test_key_sql_injection(self):
304+
with CaptureQueriesContext(connection) as queries:
305+
self.assertFalse(
306+
JSONModel.objects.filter(**{
307+
"""field__test' = '"a"') OR 1 = 1 OR ('d""": 'x',
308+
}).exists()
309+
)
310+
self.assertIn(
311+
"""."field" -> 'test'' = ''"a"'') OR 1 = 1 OR (''d') = '"x"' """,
312+
queries[0]['sql'],
313+
)
314+
302315

303316
@isolate_apps('postgres_tests')
304317
class TestChecks(PostgreSQLTestCase):

0 commit comments

Comments
 (0)