cherrypy / cheroot

Cheroot is the high-performance, pure-Python HTTP server used by CherryPy. Docs -->
https://cheroot.cherrypy.dev
BSD 3-Clause "New" or "Revised" License
185 stars 90 forks source link

CherryPy/Cheroot SSL Built-in Adapter Causing Port Contention with Netcat #709

Open PraveenKumarVN opened 6 months ago

PraveenKumarVN commented 6 months ago

I'm encountering an issue with CherryPy's built-in SSL adapter where it seems to cause port contention with netcat. When I run a CherryPy server with SSL enabled using the built-in adapter, and establish a TCP connection using netcat nc localhost 8080 without performing TLS handshake, not able to access the application via browser or any other client until I complete the TLS handshake in netcat. However, if I switch to using PyOpenSSL, able to access the application via other clients while TLS handshake is not yet done in TCP connection via netcat.

I'm not sure why the built-in SSL adapter is causing this behavior, but it seems to be related to how it manages SSL connections or handles socket bindings.

Has anyone encountered a similar issue with CherryPy's built-in SSL adapter? Any insights or suggestions on how to resolve or further debug this issue would be greatly appreciated.

Thank you!

❓ I'm submitting a ...

🐞 Describe the bug. What is the current behavior?

Here's a summary of the setup and behavior:

Please provide the steps to reproduce and if possible a screenshots and logs of the problem. If you can, show us your code.

class YourApp:
    @cherrypy.expose
    def index(self):
        return "Hello, world!"

if __name__ == '__main__':
    # Configuration
    cherrypy.config.update({
        'server.thread_pool': 10,  # Number of threads in the pool
        'server.socket_host': '0.0.0.0',  # Bind to all interfaces
        'server.socket_port': 8080,  # Port to listen on
        'server.socket_timeout': 60
    })

    cherrypy.server.ssl_module = 'builtin'  # Use the built-in SSL module
    cherrypy.server.ssl_certificate = 'cert.pem'  # Path to your SSL certificate
    cherrypy.server.ssl_private_key = 'key.pem'

    cherrypy.quickstart(YourApp(), '/')

πŸ’‘ Expected behavior

App should server requests even if a connection handshake is in progress

πŸ“‹ Details

πŸ“‹ Environment

πŸ“‹ Additional context

webknjaz commented 6 months ago

Could you attach that Cheroot-only reproducer that I was asking for in https://github.com/cherrypy/cheroot/pull/649#issuecomment-2102276242?

PraveenKumarVN commented 6 months ago

Hi @webknjaz Please find below the reproducer only using cheroot

from cheroot import wsgi
from cheroot.ssl.builtin import BuiltinSSLAdapter

def my_crazy_app(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type', 'text/plain')]
    start_response(status, response_headers)
    return [b'Hello world!']

addr = '0.0.0.0', 8070
certfile = 'cert.pem'
keyfile = 'key.pem'

server = wsgi.Server(addr, my_crazy_app)
server.ssl_adapter = BuiltinSSLAdapter(certfile, keyfile, None)
server.start()
PraveenKumarVN commented 2 months ago

@webknjaz Any update on this?

webknjaz commented 2 weeks ago

Sorry, I didn't have time to look into it, but if you're able to debug this further, it'd be nice. I wonder if this is related to the stdlib ssl module or to our code… Are you sure it's handshake? Have you tried looking into the network exchange via Wireshark?