Unbounded memory growth from fragmented WebSocket messages (DoS)
Description
Vulnerability Details
File: http-netty/src/main/java/io/micronaut/http/netty/websocket/AbstractNettyWebSocketHandler.java Lines: 339-362 (handleWebSocketFrame) CWE: CWE-400 / CWE-770 — Uncontrolled Resource Consumption / Allocation of Resources Without Limits or Throttling Severity: High CVSS: 7.5 — CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H
Root Cause
WebSocket messages can be fragmented across multiple frames (FIN=0 on all but the last, FIN=1 on the final one). NettyServerWebSocketUpgradeHandler configures Netty's WebSocketServerHandshakerFactory with a maxFramePayloadLength (default 65536, or a per-@OnMessage(maxPayloadLength=...) override) — this bounds each individual frame's payload at the Netty decoder level, correctly.
However, Micronaut reassembles fragmented messages by hand into a CompositeByteBuf (frameBuffer), rather than using Netty's own WebSocketFrameAggregator (the standard utility that enforces a maximum aggregated message size across all fragments):
ByteBuf msgContent = msg.content().retain();
if (!msg.isFinalFragment()) {
frameBuffer.updateAndGet((buffer) -> {
if (buffer == null) {
buffer = ctx.alloc().compositeBuffer();
}
buffer.addComponent(true, msgContent);
return buffer;
});
return;
}There is no check on the number of fragments or the cumulative size of frameBuffer before appending the next one. maxFramePayloadLength says nothing about how many frames may be queued before a final fragment arrives — and a malicious client is free to simply never send one.
Affects both the WebSocket server and the WebSocket client (NettyWebSocketClientHandler extends the same base class). Confirmed present, unchanged, on v5.1.10, v4.10.26, v3.10.10, and 5.2.x (5.2.0-SNAPSHOT @ e20b89086582e81d301b91eec92d883e098eb845).
Attack Scenario
- Open a single WebSocket connection to any
@ServerWebSocketendpoint. - Send a non-final (
FIN=0) frame with payload just undermaxFramePayloadLength— accepted by Netty's per-frame check. - Repeat indefinitely with
ContinuationWebSocketFrames, never sending a final (FIN=1) fragment. - Each frame is individually valid, so
frameBuffergrows without bound as long as frames keep arriving. - A handful of concurrent connections doing this can exhaust the JVM heap, crashing the whole server process for all users, not just the attacker's own connection.
Impact
Denial of Service via memory exhaustion; ability to open a WebSocket connection is the only precondition (often unauthenticated, depending on the application).
Recommended Fix
Track the cumulative reassembled-message size as fragments arrive, and reject/close the connection with CloseReason.MESSAGE_TO_BIG once it exceeds the same maxFramePayloadLength already used for individual frames.
I have this implemented and verified on a local branch (against 5.2.x / HEAD e20b89086582e81d301b91eec92d883e098eb845): compute the existing per-handler maxFramePayloadLength (same value already derived from @OnMessage(maxPayloadLength=...) at handshake time) once in the constructor, and check it against the reassembled buffer's cumulative size before each non-final-fragment append, releasing all buffered data and closing the connection instead of continuing to grow it once exceeded.
Verification
Verified by complete reading of the fragment-accumulation code path and confirming (a) no bound exists anywhere between fragment receipt and buffer growth, and (b) Netty's own WebSocketFrameAggregator (the standard mitigation) is never used anywhere in this codebase. A full runtime PoC (opening a live connection and observing heap growth) wasn't performed given time constraints on isolating a standalone build of this class (it's compiled against Micronaut's own DI/annotation-processing internals) — the missing bound is structural and unambiguous from the code alone.
Fix:
MN5: github.com/