waitress-request-parsing-defensive

defensivedefensivesource-auditapplication-hardeningpythonrequest-smugglinghttp-parsingprotocol-desyncopen-source-codebasecve-based

Task

/testbed holds a deployment of waitress, a pure-Python WSGI server, together with the small application it serves. The tree under /testbed/waitress is a vendored copy of the server itself: the HTTP/1.1 request parser, the body receivers, the connection channel and their helpers. /testbed/app.py is the application; /testbed/tests is the server's own unit-test suite.

This deployment sits behind a reverse proxy in production, so the server and the proxy both parse every request that arrives. A security hardening pass over the HTTP message-parsing layer is required before it can be trusted in that position. Operators report that the server sometimes handles a connection differently from the equipment in front of it, and that a single connection can end up producing effects nobody asked for.

There is more than one independent weakness, and they are not all of the same kind. Audit the code that decides where one HTTP message ends and the next begins, and compare what it accepts against what the HTTP standard actually permits. Diagnose the weaknesses from the source and from observed behavior; no checklist of defect classes or affected fields is provided, and a comment describing intent is not a guarantee of behavior.

The contract to restore

The server must not disagree with an RFC 7230 / RFC 9112 compliant parser about the boundaries of an HTTP message. Concretely:

  • A byte stream that a compliant parser reads as one request must never cause this server to execute a second, caller-supplied request hidden inside it.
  • A request whose framing is ambiguous or malformed must be rejected with a 4xx/5xx response, not guessed at and not merely left unanswered. Declining to act on what follows is not sufficient: an ambiguous message must not be processed at all. Where the standard defines a value with a strict grammar, the server must enforce that grammar rather than accept a more permissive variant. When a request supplies conflicting framing signals, reject it instead of selecting one signal by precedence.
  • This deployment additionally requires CRLF-only message framing. RFC 7230 lets a recipient tolerate a bare LF as a line terminator; because the proxy in front of this server does not, tolerating it here is a boundary disagreement and must be rejected.

Preserve normal behavior

Your changes must restrict only what was never valid. All of the following must keep working exactly as they do today:

  • Ordinary GET, HEAD and POST requests, including query strings, empty bodies and large bodies.
  • Well-formed chunked requests, including chunks that carry a syntactically valid chunk-extension, and chunked requests that end with a trailer section.
  • HTTP pipelining — two well-formed requests sent back-to-back on one connection must both be answered — and keep-alive connection reuse.
  • Every route the application exposes must remain reachable; do not disable, stub, or blanket-reject endpoints.
  • The server's behaviour must remain consistent with the upstream project's own test suite. The copy under /testbed/tests is a subset provided for local iteration; grading runs the project's complete suite, so do not tune your patch to the subset.

Deliverables

After patching, write two files:

  1. /app/output/findings.json — a JSON array; one entry per distinct weakness you fixed, each with:
    • vulnerability_type: a CWE identifier (format CWE-###)
    • severity: LOW, MEDIUM, HIGH, or CRITICAL
    • affected_files: list of files under /testbed you changed for it
    • root_cause: the underlying flaw in one or two sentences
    • description: the unsafe request-to-effect flow and its impact
  2. /app/output/plan.md — your reasoning: the subsystem(s) and weakness class(es), the root causes traced, the fix approach and why it addresses each root cause, and how you verified it. Graded for reasoning quality independently of whether the patch passes every test.

Local testing

Run the application yourself:

PORT=8080 python3 /testbed/app.py

Run the server's own unit tests:

cd /testbed && python3 -m pytest tests -q -o addopts="" \
    --ignore=tests/test_functional.py \
    --deselect tests/test_server.py::TestUnixWSGIServer

(The excluded groups bind Unix domain sockets and spawn their own servers, which needs privileges the audit user does not have; they are unrelated to this task.)

Craft raw byte streams with a socket rather than an HTTP client library — a client will normalize exactly the malformed framing you need to investigate.

Constraints

  • Fix root causes at the parsing boundary, not individual request shapes. A fix that special-cases the exact byte sequences a checker might use will not survive variant inputs and is not acceptable — validate against the grammar the standard defines.
  • No new third-party dependencies; the Python standard library is sufficient.
  • Do not disable, remove, or stub out any endpoint or feature; do not replace responses with canned data; do not edit files outside /testbed.
  • Do not modify anything under /testbed/tests. Those tests are provided for your own local iteration; grading uses the project's pristine suite, so editing a test to match your implementation fixes nothing and will be scored as a failure of that behaviour.
  • Closed-book: work from the source and observed local behavior, not external writeups or scanners.
  1. Work in concrete, bounded steps. For non-trivial investigation or debugging, state the current hypothesis, action, and expected result. Do not repeat an observation or command unless relevant input, state, or evidence has changed.

  2. Keep debugging hypothesis-driven. Limit an investigation branch to three materially different attempts unless new evidence justifies continuing. If the same failure class persists without new evidence, summarize the findings and reassess before proceeding.

  3. Keep terminal operations bounded, recoverable, and concise. Use appropriate timeouts for commands that might hang. Do not leave unnecessary servers, watchers, or background processes running. Prefer targeted excerpts and summarized logs over large output dumps.

  4. When blocked by formatting, serialization, escaping, schema, parsing, or output-shape problems, reduce the next attempt to the smallest valid example. Do not retry an unchanged malformed input.

  5. Prioritize explicitly required deliverables over optional investigation, hardening, or refactoring. Leave sufficient time to validate required outputs. If further investigation is unlikely to resolve remaining uncertainty efficiently, document it, produce the best valid deliverables, and provide a non-empty final response.