{"id":"229595e08e973e5a","repo":"apache/kafka","slug":"invalid-receive-size-receivesize","errorCode":null,"errorMessage":"Invalid receive (size = ${receiveSize})","messagePattern":"Invalid receive \\(size = (.+?)\\)","errorType":"validation","errorClass":"InvalidReceiveException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/network/NetworkReceive.java","lineNumber":93,"sourceCode":"    }\n\n    @Override\n    public boolean complete() {\n        return !size.hasRemaining() && buffer != null && !buffer.hasRemaining();\n    }\n\n    public long readFrom(ScatteringByteChannel channel) throws IOException {\n        int read = 0;\n        if (size.hasRemaining()) {\n            int bytesRead = channel.read(size);\n            if (bytesRead < 0)\n                throw new EOFException();\n            read += bytesRead;\n            if (!size.hasRemaining()) {\n                size.rewind();\n                int receiveSize = size.getInt();\n                if (receiveSize < 0)\n                    throw new InvalidReceiveException(\"Invalid receive (size = \" + receiveSize + \")\");\n                if (maxSize != UNLIMITED && receiveSize > maxSize)\n                    throw new InvalidReceiveException(\"Invalid receive (size = \" + receiveSize + \" larger than \" + maxSize + \")\");\n                requestedBufferSize = receiveSize; // may be 0 for some payloads (SASL)\n                if (receiveSize == 0) {\n                    buffer = EMPTY_BUFFER;\n                }\n            }\n        }\n        if (buffer == null && requestedBufferSize != -1) { // we know the size we want but haven't been able to allocate it yet\n            buffer = memoryPool.tryAllocate(requestedBufferSize);\n            if (buffer == null)\n                log.trace(\"Broker low on memory - could not allocate buffer of size {} for source {}\", requestedBufferSize, source);\n        }\n        if (buffer != null) {\n            int bytesRead = channel.read(buffer);\n            if (bytesRead < 0)\n                throw new EOFException();\n            read += bytesRead;","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/network/NetworkReceive.java#L75-L111","documentation":"Thrown by NetworkReceive.readFrom as an InvalidReceiveException when the 4-byte size prefix read from the channel decodes to a negative value. The Kafka wire protocol frames every message as a 4-byte big-endian length N followed by N bytes of payload; a negative N is structurally impossible for a valid Kafka frame and signals a corrupted or non-Kafka byte stream. The guard exists so the broker/client fails fast instead of trying to allocate or interpret garbage.","triggerScenarios":"A client (Kafka or otherwise) connects to a Kafka port and sends bytes whose first 4 bytes decode to a negative int; e.g. a plain HTTP/Telnet/redis-cli probe against the Kafka port, a partially-truncated SSL handshake sent as plaintext, or a binary protocol mismatch (pointing a non-Kafka client at the broker).","commonSituations":"Running 'curl http://broker:9092' or 'nc broker 9092' to test connectivity; a load balancer/proxy doing a TCP health check that sends junk; sending plaintext to a SASL_SSL/SSL listener (or vice versa); mixed-up container port mappings exposing a different service on 9092.","solutions":["Confirm the connecting client speaks the Kafka protocol and targets the correct listener (PLAINTEXT vs SSL vs SASL_SSL).","Stop any HTTP/Telnet/health-check probes hitting the Kafka port; use a Kafka-aware tool (kafka-broker-api-versions, kcat) to test.","If SSL is configured, verify the client is actually performing a TLS handshake rather than writing plaintext."],"exampleFix":"// before: plaintext probe against an SSL listener\n$ nc broker 9092  # sends raw bytes -> negative size prefix\n\n// after: test with the correct protocol\n$ kafka-broker-api-versions --bootstrap-server broker:9092 --command-config client-ssl.properties","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    long read = networkReceive.readFrom(channel);\n} catch (InvalidReceiveException e) {\n    // Peer sent a negative size: protocol violation. Drop the channel.\n    log.warn(\"Invalid receive from {}: closing channel\", networkReceive.source(), e);\n    Utils.closeQuietly(channel, \"channel\");\n    disconnect(networkReceive.source());\n} catch (EOFException | IOException e) {\n    handleDisconnect(networkReceive.source(), e);\n}","preventionTips":["Only connect NetworkReceive/Selector to trusted Kafka brokers; a negative size prefix is always malformed or hostile.","Keep client and broker versions aligned so the size-delimited framing is mutually understood.","Treat InvalidReceiveException as a connection-fatal event: never retry on the same channel.","Monitor this exception as a signal of corruption, version skew, or man-in-the-middle tampering."],"tags":["network","wire-protocol","invalid-receive","connection"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}