Skip to content

Bound in-memory request and response buffering in servlet integrations (#2441) - #2442

Open
Vinu2111 wants to merge 2 commits into
zalando:mainfrom
Vinu2111:fix/unbounded-buffering-dos
Open

Vinu2111 wants to merge 2 commits into
zalando:mainfrom
Vinu2111:fix/unbounded-buffering-dos

Conversation

@Vinu2111

Copy link
Copy Markdown
Contributor

Fixes #2441

Description

Prevents unbounded in-memory buffering of request and response bodies in logbook-servlet and logbook-spring-boot-autoconfigure.

  • Added bounded stream reading in ByteStreams.toByteArray(InputStream, long).
  • Introduced SequenceServletInputStream to replay buffered prefix data while streaming the remaining stream without holding excess bytes in heap.
  • Extended RemoteRequest state machine with Exceeded state: when maxBufferSize is exceeded (via Content-Length or chunked stream), the body is not buffered in memory, but downstream servlets still receive the full body without disruption.
  • Bounded LocalResponse.Tee copying to discard buffered response bytes and reset the buffer if maxBufferSize is exceeded while allowing the response to complete normally.
  • Exposed maxBufferSize (-1L default / unbounded for backward compatibility) in LogbookFilter and SecureLogbookFilter configurable via system properties logbook.servlet.max-buffer-size / logbook.filter.max-buffer-size.
  • Added Spring Boot auto-configuration property logbook.filter.max-buffer-size (DataSize).

Validation

  • Validated with ./mvnw verify -pl logbook-servlet,logbook-spring-boot-autoconfigure -am -B on JDK 17.
  • 100% line and branch coverage across all new and modified classes.

@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Unsigned Commits Detected

This pull request contains commits that are not cryptographically signed:

Please sign your commits as per our contributors guidelines.

Comment thread README.md Outdated
| `logbook.client.predicate.exclude` | Exclude certain paths and methods for outgoing requests (overrides `logbook.client.predicate.include`) | `[]` |
| `logbook.filter.enabled` | Enable the [`LogbookFilter`](#servlet) | `true` |
| `logbook.filter.form-request-mode` | Determines how [form requests](#form-requests) are handled | `body` |
| `logbook.filter.max-buffer-size` | Maximum amount of request/response body data to buffer in memory before discarding capture and streaming directly | `-1` (disabled) |

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I get it right that this is the only change to README the rest is just formatting? Could you revert it? I can't even spot what's changed in the formatting. Or help me understand the reason.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, my bad on the noise there. When resolving the TOC check locally, the line endings in that block got touched. I've reverted the formatting churn completely so now only the single table row for logbook.filter.max-buffer-size is modified.

if (property == null) {
return -1L;
}
return Long.parseLong(property);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A property.trim() here might be safer to prevent NumberFormatException in case of accidental leading/trailing whitespace in the system property.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Added .trim() before parsing, and added a test case in MaxBufferSizeTest to cover accidental whitespace.

original.write(b, off, len);
branch.write(b, off, len);
if (!exceeded) {
if (maxBufferSize >= 0 && branch.size() + len > maxBufferSize) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since both branch.size() and len are int, this addition can overflow to negative if large chunks are written, bypassing the check. Should we cast to (long) branch.size() + len?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely don't want an integer overflow bypassing the bound on large writes. Cast to (long) branch.size() + len > maxBufferSize.

private final ByteArrayOutputStream branch;
private final long maxBufferSize;
@Getter
private boolean exceeded;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be volatile? With async servlets or listener threads, writes might happen on a worker thread while completion or logging is handled on another.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes total sense for async dispatches where worker writes and completion logging happen across threads. Made exceeded volatile.


@Override
public void setReadListener(final ReadListener readListener) {
second.setReadListener(readListener);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if delegating to second here works in practice. Once synchronous reads have taken place (which happened during doBuffer), servlet containers usually throw IllegalStateException if setReadListener is invoked. Also, second wouldn't know about prefix bytes in first.

In ServletInputStreamAdapter we throw UnsupportedOperationException for setReadListener. Shouldn't we do the same here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Since we've already read synchronously during buffering and second doesn't know about prefix bytes anyway, non-blocking I/O isn't viable here. Switched it to throw UnsupportedOperationException, matching ServletInputStreamAdapter.

public static class Filter {
private FormRequestMode formRequestMode = FormRequestMode.fromProperties();
@Nullable
private DataSize maxBufferSize;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add logbook.filter.max-buffer-size to src/main/resources/META-INF/spring-configuration-metadata.json so IDEs pick it up for autocompletion?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added logbook.filter.max-buffer-size under properties in spring-configuration-metadata.json with DataSize type and description.

@Vinu2111
Vinu2111 force-pushed the fix/unbounded-buffering-dos branch from 516ee0d to 413763a Compare November 20, 2026 13:15
@Vinu2111

Copy link
Copy Markdown
Contributor Author

Thanks @kasmarian for the review. Everything you raised is addressed in the earlier commit. I've pushed one small follow-up: the new README row is now aligned with its neighbours, and I removed a withFormRequestMode helper I had added to SecureLogbookFilter that isn't needed for this change. Could a maintainer also add the bugfix label so the COMPR/pr-labels check can pass? Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants