Skip to content

[Bug]: Bound unbounded servlet response-body caching during OpenAPI validation #434

Backlog

Description

@visheshupadhyay

Summary

While tracing how servlet responses are validated, I found that the starter uses Spring's ContentCachingResponseWrapper. It keeps the whole response body in memory until validation has finished. That is fine for ordinary JSON responses, but a large download or generated response has no upper memory limit.

This change adds a limit. We still cache and validate normal responses as before, but once a response becomes too large, we stop retaining it in memory. The client still receives the complete response.

Affected code

  • spring-boot-starter/spring-boot-starter-web/src/main/java/com/getyourguide/openapi/validation/factory/ContentCachingWrapperFactory.java
  • spring-boot-starter/spring-boot-starter-web/src/main/java/com/getyourguide/openapi/validation/filter/BoundedContentCachingResponseWrapper.java
  • spring-boot-starter/spring-boot-starter-web/src/main/java/com/getyourguide/openapi/validation/filter/OpenApiValidationInterceptor.java

Impact

Before this change, one unusually large response could make the application retain its entire body in heap memory. A few such responses at the same time could put real pressure on the JVM and, in the worst case, lead to an out-of-memory failure.

With this change, each response retains at most the configured cache limit. If it crosses that limit, we continue delivering it normally, but skip response-body validation. That is intentional: validating only the first part of a response could report a misleading result.

Fix

BoundedContentCachingResponseWrapper holds response bytes only up to a fixed limit. At the first write beyond that limit, it sends the already cached bytes and every later byte directly to the underlying servlet response.

The interceptor sees that overflow and deliberately skips response-body validation. Small responses keep the existing behaviour: their complete body is cached, validated, copied to the client, and receives the same automatic Content-Length handling as Spring's original wrapper.

100 MB cache-limit decision

This contribution proposes 100 MiB (100 * 1024 * 1024 bytes) as the production default. It felt like a reasonable starting point: large enough that normal, legitimately large API responses can still be validated, while still putting a firm cap on memory retained per response.

That said, 100 MiB is not meant to be a final policy decision. Every service has a different memory budget and response-size profile, and I am open to change the value based on the maintainers preference for a smaller or larger value. The wrapper accepts the limit as a constructor argument, so changing the default does not require changing the overflow logic.
PS: I think its better if we can add this value in application properties.

For the regression tests, the same behavior is exercised with a 1 KB limit. This keeps the tests fast and lightweight while proving the exact boundary conditions that also apply at 100 MiB. We are fully open to changing the production default based on maintainer feedback or project guidance.

Regression coverage

ContentCachingWrapperFactoryTest covers the practical cases we care about:

  • the 100 MiB production default;
  • ordinary JSON response caching and delivery;
  • a response exactly at the cache limit;
  • a response one byte above the limit;
  • large binary output;
  • many small output chunks;
  • output written via Writer rather than OutputStream;
  • explicit and automatic Content-Length behavior.

OpenApiValidationInterceptorTest also verifies the key safety decision: a truncated cached response is never sent to response validation, while request validation still runs.

Both focused test classes and the complete spring-boot-starter-web module check pass.

Terminal Screenshots

FullTestRunBeforeChanges
FullTestRunBeforeChanges
TestRunWithBug
TestRunWithBug
TestRunWithBugFix
TestRunWithBugFix
FullTestRunAfterChanges
FullTestRunAfterChanges

Activity

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