Skip to content

Commit 69c69b8

Browse files
committed
Fixed #3751 -- Stopped listing all related field choices through metadata.
Listing related fields can leak sensitive data and result in poor performance when dealing with large result sets. Large result sets should be exposed by a dedicated endpoint instead.
1 parent 6b1125a commit 69c69b8

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

rest_framework/metadata.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ def get_field_info(self, field):
137137
elif getattr(field, 'fields', None):
138138
field_info['children'] = self.get_serializer_info(field)
139139

140-
if not field_info.get('read_only') and hasattr(field, 'choices'):
140+
if (not field_info.get('read_only') and
141+
not isinstance(field, serializers.RelatedField) and
142+
hasattr(field, 'choices')):
141143
field_info['choices'] = [
142144
{
143145
'value': choice_value,

tests/test_metadata.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from rest_framework.request import Request
1212
from rest_framework.test import APIRequestFactory
1313

14+
from .models import BasicModel
15+
1416
request = Request(APIRequestFactory().options('/'))
1517

1618

@@ -261,10 +263,21 @@ def get_serializer(self):
261263
view = ExampleView.as_view(versioning_class=scheme)
262264
view(request=request)
263265

266+
267+
class TestSimpleMetadataFieldInfo(TestCase):
264268
def test_null_boolean_field_info_type(self):
265269
options = metadata.SimpleMetadata()
266270
field_info = options.get_field_info(serializers.NullBooleanField())
267-
assert field_info['type'] == 'boolean'
271+
self.assertEqual(field_info['type'], 'boolean')
272+
273+
def test_related_field_choices(self):
274+
options = metadata.SimpleMetadata()
275+
BasicModel.objects.create()
276+
with self.assertNumQueries(0):
277+
field_info = options.get_field_info(
278+
serializers.RelatedField(queryset=BasicModel.objects.all())
279+
)
280+
self.assertNotIn('choices', field_info)
268281

269282

270283
class TestModelSerializerMetadata(TestCase):

0 commit comments

Comments
 (0)