Patchwork D2768: hgweb: use a capped reader for WSGI input stream

login
register
mail settings
Submitter phabricator
Date March 10, 2018, 1:23 a.m.
Message ID <differential-rev-PHID-DREV-wr5kjvkhtjtxia7gcqwn-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/29224/
State Superseded
Headers show

Comments

phabricator - March 10, 2018, 1:23 a.m.
indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  Per PEP-3333, the input stream from WSGI should respect EOF and
  prevent reads past the end of the request body. However, not all
  WSGI servers guarantee this. Notably, our BaseHTTPServer based
  built-in HTTP server doesn't. Instead, it exposes the raw socket
  and you can read() from it all you want, getting the connection in
  a bad state by doing so.
  
  We have a "cappedreader" utility class that proxies a file object
  and prevents reading past a limit.
  
  This commit converts the WSGI input stream into a capped reader when
  the input length is advertised via Content-Length headers.
  
  "cappedreader" only exposes a read() method. PEP-3333 states that
  the input stream MUST also support readline(), readlines(hint), and
  __iter__(). However, since our code only calls read and we're not
  implementing a spec conforming WSGI server (just a WSGI application
  at this point), we don't need to support these additional methods.
  So the limited functionality of "cappedreader" is sufficient for our
  WSGI application.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D2768

AFFECTED FILES
  mercurial/hgweb/request.py

CHANGE DETAILS




To: indygreg, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/mercurial/hgweb/request.py b/mercurial/hgweb/request.py
--- a/mercurial/hgweb/request.py
+++ b/mercurial/hgweb/request.py
@@ -234,6 +234,14 @@ 
             raise RuntimeError("Unknown and unsupported WSGI version %d.%d"
                                % version)
         self.inp = wsgienv[r'wsgi.input']
+
+        if r'HTTP_CONTENT_LENGTH' in wsgienv:
+            self.inp = util.cappedreader(self.inp,
+                                         int(wsgienv[r'HTTP_CONTENT_LENGTH']))
+        elif r'CONTENT_LENGTH' in wsgienv:
+            self.inp = util.cappedreader(self.inp,
+                                         int(wsgienv[r'CONTENT_LENGTH']))
+
         self.err = wsgienv[r'wsgi.errors']
         self.threaded = wsgienv[r'wsgi.multithread']
         self.multiprocess = wsgienv[r'wsgi.multiprocess']