T09 · Insecure Skill Coding Practices
- Location
mcp_server.py:336- Finding
Unvalidated Negative Content-Length Can Cause Server-Wide Denial of Service
- Content
View full analysis
MAX_BODY_BYTES: self._send_json(build_error_response(-32000, "Request body too large", jsonrpc="2.0"), status=413) return raw_body = self.rfile.read(content_length).decode("utf-8") ``` ### Technical Analysis The request handler verifies that `Content-Length` does not exceed `MAX_BODY_BYTES`, but it does not reject negative values. An attacker can therefore supply a header such as `Content-Length: -1`. In Python, calling `read(-1)` requests data until the input stream reaches end-of-file rather than reading zero bytes. If the remote client keeps the connection open, the request handler can remain blocked while waiting for EOF. The application uses the single-threaded `HTTPServer`: ```python server = HTTPServer((DEFAULT_HOST, DEFAULT_PORT), MCPHandler) ``` Consequently, one blocked request handler prevents the server from accepting and processing other requests. This makes the flaw capable of affecting the availability of the entire MCP service rather than only the attacker's connection. The same request-processing path also lacks defensive type validation after JSON decoding. Although the handler expects a JSON object, arrays or scalar JSON values can reach `handle_mcp_request()`, where calls such as `request.get(...)` raise an unhandled exception. This reinforces the need for strict request validation and centralized exception handling. ### Attack Path 1. The attacker establishes a TCP connection to the publicly reachable MCP HTTP endpoint. 2. The attacker submits a POST request containing a negative length ...[truncated 1104 chars]- Remediation
View remediation
MAX_BODY_BYTES: self._send_json( build_error_response(-32000, "Request body too large", jsonrpc="2.0"), status=413, ) return ``` 2. Configure socket read timeouts so slow or deliberately incomplete clients cannot hold handlers indefinitely. 3. Replace `HTTPServer` with `ThreadingHTTPServer` to prevent one blocked client from stopping all request processing. Threading is defense in depth and must not replace validation or timeouts. 4. Confirm that parsed JSON is an object before calling `handle_mcp_request()`: ```python request = json.loads(raw_body) if raw_body else {} if not isinstance(request, dict): self._send_json( build_error_response(-32600, "Request must be a JSON object", jsonrpc="2.0"), status=400, ) return ``` 5. Add centralized exception handling around request dispatch so malformed input produces a controlled JSON-RPC error rather than an uncaught handler exception. 6. Add regression tests for negative, missing, nonnumeric, oversized, and inconsistent `Content-Length` headers, as well as JSON arrays, strings, numbers, booleans, and null values. ]] ...[truncated 1 chars]
