Skip to content

Unbounded in-memory buffering of request/response bodies allows remote memory exhaustion (DoS) #2441

Open

Description

@achhibi

Summary

LogbookFilter (and the other server integrations) buffer the entire request body in heap before the application sees the request, with no size limit. An unauthenticated client can crash a JVM with a single chunked POST, even against a route that does not exist or never reads the body. Response bodies are buffered the same way.

This looks like CWE-400 / CWE-770 (allocation of resources without limits). The README already acknowledges the behavior under logbook.write.max-body-size ("Logbook will still buffer the full body"), but there is currently no way to bound the buffer at all.

Affected code

  • logbook-servlet – RemoteRequest.doBuffer() calls ByteStreams.toByteArray(request.getInputStream()) with no limit. Buffering is eager (withBody()), so it happens before request filters/body replacers run: an application/octet-stream upload is fully read into memory and only then replaced by <binary> in the log.
  • logbook-servlet – LocalResponse.Tee copies every byte written to the response into a ByteArrayOutputStream (large downloads, streaming responses).
  • Same pattern in logbook-spring-webflux (DataBufferUtils.join(body) without maxByteCount), logbook-netty (Buffering), logbook-jaxrs and logbook-jdkserver (ByteStreams.toByteArray).

Why it matters

  • The filter runs before the servlet, so the body is buffered even for 404s and for endpoints that never call getInputStream().
  • Tomcat does not limit non-form request bodies by default (maxPostSize only applies to form parameter parsing).
  • With the Spring Boot starter, secureLogbookFilter discards request bodies (SecurityStrategy) but still buffers response bodies without limit; logbookFilter is exposed on any route reachable without authentication, or everywhere when Spring Security is absent.
  • SecurityStrategy, WithoutBodyStrategy or a custom Strategy are only workarounds: they disable body logging entirely.

Reproduction

Minimal test in logbook-servlet: send a 256 MiB body without Content-Length (simulating a chunked upload) through LogbookFilter with an active sink, to a FilterChain that answers 404 without touching the body.

final MockHttpServletRequest request = new MockHttpServletRequest("POST", "/does-not-exist") {
    @Override
    public ServletInputStream getInputStream() {
        return hugeStream; // 256 MiB, never reads Content-Length
    }
};
request.setContentType("application/json");

new LogbookFilter(logbook).doFilter(request, new MockHttpServletResponse(),
        (req, res) -> ((HttpServletResponse) res).setStatus(404));

Observed:

JVM heap Result
-Xmx1g all 256 MiB consumed before the chain is invoked, ~767 MiB heap growth
-Xmx400m java.lang.OutOfMemoryError: Java heap space inside the filter

The same happens with a real curl -T /dev/zero -H 'Transfer-Encoding: chunked' against any Logbook-enabled endpoint when the Logbook logger is at TRACE.

Suggested fix

Bound the amount of body kept in memory, independently of logbook.write.max-body-size (which only truncates after the full read):

  • read at most limit + 1 bytes; if the limit is exceeded, hand the already-read prefix plus the remaining stream to the application (SequenceInputStream-like ServletInputStream) and do not log the body;
  • bound the response Tee the same way, discarding the copy once the limit is exceeded;
  • expose the limit on LogbookFilter/SecureLogbookFilter (e.g. withMaxBufferSize(long)), via a system property for plain servlet users and via logbook.filter.max-buffer-size (DataSize) for the Spring Boot starter;
  • pick a secure default (e.g. 1 MiB) with -1 to restore the unbounded behavior;
  • apply the same bound in the WebFlux (DataBufferUtils.join(body, maxByteCount) or a copying cap), Netty, JAX-RS and JDK server integrations.

Activity

  1. Vinu2111 commented on Nov 20, 2026

    @Vinu2111
    Contributor

    I have opened PR #2442 addressing this for logbook-servlet and logbook-spring-boot-autoconfigure.

    It bounds in-memory buffering for both requests and responses without disrupting downstream stream delivery, backed by 100% line and branch test coverage, keeping -1 (unbounded) as default for backwards compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions