#40 Merged

Fix for issue #1068 - unable to post more than 16KB over SSL

  1. David Andrzejewski

This hopefully resolves issue #1068. All I did was ensure that SSL_fileobject.recv() so that it never returns more than the number of bytes it was asked for, which is what the assert statement on line 1055 of wsgiserver2.py is checking on - that's where the failure occurs.

I'm not really sure if this is the best or most efficient way to do it, but this definitely appears to resolve it on my end, and it does not cause additional unit test failures.

I did not add unit tests for this because no unit tests are run in HTTPS mode, however I think someone with the required expertise should consider having the unit tests run in that mode.

  • Issues #1068: File upload crashes when using HTTPS resolved

Comments (5)

  1. Joel Rivera

    I have come to the realization after spending a shameful amount of time looking into this issue that the method SSL_fileobject.recv needs to be just to be just one line:

        def recv(self, size, *args, **kwargs):
            return self._safe_call(True, super(SSL_fileobject, self).recv, size, *args, **kwargs)

    Why are we buffering a call with a fixed size as an argument? I have make a couple of test with some 500MB files and is working fine, the call to self._sock.pending() is not required for the purpose of that method.

    BUT after thinking a little more I see that probably is a good idea to buffer the method and ask for pending plus verifying the size as you suggest. Probably the examples that I'm making are not a real use case because of the low latency of the localhost. Anyway the real issue is indeed in this method not in the caller in wsgiserver2.py, the thing that's bothers me is that on python3 is working fine and the almost exactly the same implementation (except for some extra locking ) in CP3.1 is also working fine.

    I'm going to look for a way to test the SSL part.

    This is the documentation of OpenSSLConection.

  2. David Andrzejewski author

    That is interesting that it works on Python 3.

    Anyway, I'm glad you think this is the right way to go.

    Per the OpenSSLConnection documentation (emphasis mine):

    recv(bufsize) Receive data from the Connection. The return value is a string representing the data received. The maximum amount of data to be received at once, is specified by bufsize.

    And that's what appears to be the problem - at least to my inexperienced eyes - more than the maximum amount of data might be retrieved for some reason. As you've seen, this will check for that and stop appending to the buffer if it's going to exceed the maximum.

  3. Joel Rivera

    Finally everything has fall into place, I do have a reason why it is working on CP3.1 and Python3 (this is pretty simple).

    1. This use to work on CP3.1 because the body of the request was read with readline which does not verify the length of the data that recv returns, also it always call recv with a fixed argument, the default read size of socket self._rbufsize which in my platform is 8192 in CP3.1 recv does return more than gets asked, almost always 2 * 8192 bytes, because of a coincidence of the record size of openssl -> http://repo.or.cz/w/mirror-openssl.git/blob/HEAD:/ssl/ssl2.h#l141

    2. This works in Python3 because the wsgiserver3.py does not use pyopenssl, and therefore does not usessl_pyopenssl.py :|, simple as that.

    The reason to not include any call to self._sock.pending to the fix is because that method only verifies that the underlying ssl connection does not have any buffered bytes of previously fetched records (e.g. records of 16383 bytes), and most important the buffering between records is already handled by SSL_read which is wrapped by pyOpenSSL at the SSLConnection.recv. In particular that recv method does not support the -1 and the existence of it is just to be thread safe, not to add more buffering magic.

    So the fix need to be just like this:

        def recv(self, size):
            return self._safe_call(True, super(SSL_fileobject, self).recv, size)

    David Andrzejewski It would be great If you could make the change to looks like that.

    And just for anyone interested in how is the call stack comparing CP3.1 to CP3.2:


    -> self.__bootstrap_inner()
    -> self.run()
    -> conn.communicate()
    -> req.respond()
    -> self._respond()
    -> response = self.wsgi_app(self.environ, self.start_response)
    -> return app(environ, start_response)
    -> return self.wsgiapp(environ, start_response)
    -> return head(environ, start_response)
    -> return self.response_class(environ, start_response, self.cpapp)
    -> self.setapp()
    -> s, h, b = self.get_response()
    -> response = self.request.run(meth, path, qs, rproto, headers, rfile)
    -> self.respond(pi)
    -> self.process_body()
    -> keep_blank_values=1)
    -> cgi.FieldStorage.__init__(self, *args, **kwds)
    -> self.read_multi(environ, keep_blank_values, strict_parsing)
    -> environ, keep_blank_values, strict_parsing)
    -> cgi.FieldStorage.__init__(self, *args, **kwds)
    -> self.read_single()
    -> self.read_lines()
    -> self.read_lines_to_outerboundary()
    -> line = self.fp.readline(1<<16)
    -> data = self.rfile.readline(size)
    -> data = self.recv(self._rbufsize)
    > /tmp/cherrypy/env/lib/python2.7/site-packages/cherrypy/wsgiserver/__init__.py(1130)recv()
    -> buf = []


    -> self.__bootstrap_inner()
    -> self.run()
    -> conn.communicate()
    -> req.respond()
    -> self.server.gateway(self).respond()
    -> response = self.req.server.wsgi_app(self.env, self.start_response)
    -> return app(environ, start_response)
    -> return self.wsgiapp(environ, start_response)
    -> return head(environ, start_response)
    -> return _TrappedResponse(self.nextapp, environ, start_response, self.throws)
    -> self.response = self.trap(self.nextapp, self.environ, self.start_response)
    -> return func(*args, **kwargs)
    -> return self.nextapp(environ, start_response)
    -> return self.response_class(environ, start_response, self.cpapp)
    -> self.run()
    -> request.run(meth, path, qs, rproto, headers, rfile)
    -> self.respond(pi)
    -> self.body.process()
    -> super(RequestBody, self).process()
    -> proc(self)
    -> process_multipart(entity)
    -> part.process()
    -> self.default_proc()
    -> self.file = self.read_into_file()
    -> self.read_lines_to_boundary(fp_out=fp_out)
    -> line = self.fp.readline(1<<16)
    -> data = self.read(chunksize)
    -> data = self.fp.read(chunksize)
    -> data = self.rfile.read(size)
    -> data = self.recv(left)
    > /mnt/files/repos/cherrypy/cherrypy/wsgiserver/ssl_pyopenssl.py(101)recv()
    -> buf = []