Skip to content

Commit

Permalink
Multipart forms and RawPostDataException (#592)
Browse files Browse the repository at this point in the history
* Multipart forms only allow reading response.body once. When silk reads it, it breaks any other readers including middleware and views. This skips the request body for those forms.

* Add test for multipart forms not reading the post body.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
bazubii and pre-commit-ci[bot] committed Oct 12, 2022
1 parent e2cf24f commit b15da76
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
21 changes: 21 additions & 0 deletions project/tests/test_multipart_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from unittest.mock import Mock

from django.test import TestCase
from django.urls import reverse

from silk.model_factory import RequestModelFactory, multipart_form


class TestMultipartForms(TestCase):

def test_no_max_request(self):
mock_request = Mock()
mock_request.META = {'CONTENT_TYPE': multipart_form}
mock_request.GET = {}
mock_request.path = reverse('silk:requests')
mock_request.method = 'post'
mock_request.body = Mock()
request_model = RequestModelFactory(mock_request).construct_request_model()
self.assertFalse(request_model.body)
self.assertEqual(b"Raw body not available for multipart_form data, Silk is not showing file uploads.", request_model.raw_body)
mock_request.body.assert_not_called()
7 changes: 6 additions & 1 deletion silk/model_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
'text/javascript',
'text/x-javascript',
'text/x-json']
content_type_form = ['multipart/form-data',
multipart_form = 'multipart/form-data'
content_type_form = [multipart_form,
'application/x-www-form-urlencoded']
content_type_html = ['text/html']
content_type_css = ['text/css']
Expand Down Expand Up @@ -162,6 +163,10 @@ def _body(self, raw_body, content_type):

def body(self):
content_type, char_set = self.content_type()
if content_type == multipart_form:
raw_body = b"Raw body not available for multipart_form data, Silk is not showing file uploads."
body = ''
return body, raw_body
try:
raw_body = self.request.body
except RequestDataTooBig:
Expand Down

0 comments on commit b15da76

Please sign in to comment.