Opened 5 years ago

Closed 5 years ago

#14024 closed (duplicate)

Application freezes on access to request.POST under WSGIRequest+multipart/form-data

Reported by: zimnyx Owned by: nobody
Component: HTTP handling Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

==How to reproduce==

  1. manage.py runserver
  2. send POST using multipart/form-data encoding
  3. access request.raw_post_data, before request.POST was accessed, for example in first middleware's process_request()
  4. access request.POST -- here application freezes

==Why it happens==

Accessing WSGIRequest.raw_post_data reads data from WSGIRequest.environwsgi.input? stream, which afterwards is exhausted.
Accessing request.POST calls WSGIRequest._load_post_and_files(), which calls MultiPartParser.parse(), which tries to parse already exhausted stream from WSGIRequest.environwsgi.input?.

How to fix (fix is working, I don't claim it's a good way)

--- core/handlers/wsgi.py   (revision 13353)
+++ core/handlers/wsgi.py   (local)
@@ -132,7 +132,7 @@
         # Populates self._post and self._files
         if self.method == 'POST':
             if self.environ.get('CONTENT_TYPE', '').startswith('multipart'):
-                self._raw_post_data = ''
+                self._get_raw_post_data()
                 try:
                     self._post, self._files = self.parse_file_upload(self.META, self.environ['wsgi.input'])
                 except:
@@ -204,7 +204,8 @@
                 safe_copyfileobj(self.environ['wsgi.input'], buf,
                         size=content_length)
             self._raw_post_data = buf.getvalue()
-            buf.close()
+            buf.seek(0)
+            self.environ['wsgi.input'] = buf
             return self._raw_post_data

     GET = property(_get_get, _set_get)

Change History (1)

comment:1 Changed 5 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

Problem sounds the same as #12522, though proposed fixes are different.

Note: See TracTickets for help on using tickets.
Back to Top