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

Cheroot cannot send a 400 response for requests with invalid chunked message bodies #718

Open kenballus opened 5 months ago

kenballus commented 5 months ago

❓ I'm submitting a ...

🐞 Describe the bug. What is the current behavior? When a Cheroot-based WSGI server receives a request with an invalid chunked message body, it raises an exception upon attempting to access environ["wsgi.input"]. Unless I'm mistaken, the issue with this approach is twofold:

❓ What is the motivation / use case for changing the behavior? Prevention of request smuggling.

πŸ’‘ To Reproduce Steps to reproduce the behavior:

  1. Start a Cheroot-Based WSGI server that accesses wsgi.input outside of a try block.
  2. Send it the following:
    POST / HTTP/1.1\r\n
    Host: a\r\n
    Transfer-Encoding: chunked\r\n
    \r\n
    0 abc\r\n
    \r\n
    GET / HTTP/1.1\r\n
    Host: a\r\n
    \r\n
  3. Observe that it 500s.
  4. Add a try block around the access to wsgi.input, and respond 400 in the except block.
  5. Observe that the server responds 400 (as expected), but then responds 200 to the second request, even though a 400 should have caused the connection to be closed.

πŸ’‘ Expected behavior Requests with invalid chunked message bodies should get 400s.

πŸ“‹ Environment

kenballus commented 5 months ago

Other WSGI servers either automatically close the connection upon receipt of requests with invalid chunked message bodies (Waitress, Tornado) or close the connection automatically when a 400 response is sent manually (gunicorn).