Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added protections to AttributeErrors raised within properties #9455

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions rest_framework/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ def query_params(self):
@property
def data(self):
if not _hasattr(self, '_full_data'):
self._load_data_and_files()
with wrap_attributeerrors():
self._load_data_and_files()
return self._full_data

@property
Expand Down Expand Up @@ -420,13 +421,14 @@ def __getattr__(self, attr):
_request = self.__getattribute__("_request")
return getattr(_request, attr)
except AttributeError:
return self.__getattribute__(attr)
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{attr}'")

@property
def POST(self):
# Ensure that request.POST uses our request parsing.
if not _hasattr(self, '_data'):
self._load_data_and_files()
with wrap_attributeerrors():
self._load_data_and_files()
if is_form_media_type(self.content_type):
return self._data
return QueryDict('', encoding=self._request._encoding)
Expand All @@ -437,7 +439,8 @@ def FILES(self):
# Different from the other two cases, which are not valid property
# names on the WSGIRequest class.
if not _hasattr(self, '_files'):
self._load_data_and_files()
with wrap_attributeerrors():
self._load_data_and_files()
return self._files

def force_plaintext_errors(self, value):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ def test_standard_behaviour_determines_non_form_content_PUT(self):
request.parsers = (PlainTextParser(), )
assert request.data == content

def test_calling_data_fails_when_attribute_error_is_raised(self):
"""
Ensure attribute errors raised when parsing are properly re-raised.
"""
expected_message = "Internal error"

class BrokenParser:
media_type = "application/json"

def parse(self, *args, **kwargs):
raise AttributeError(expected_message)

http_request = factory.post('/', data={}, format="json")
request = Request(http_request)
request.parsers = (BrokenParser,)

with self.assertRaisesMessage(WrappedAttributeError, expected_message):
request.data


class MockView(APIView):
authentication_classes = (SessionAuthentication,)
Expand Down
Loading