This post is about a fix I am working on in Zalando Logbook, and about a code review that taught me more than the fix did.
The problem
Logbook's servlet integration logs HTTP requests and responses. To log a body, it first copies the whole thing into memory. That is fine for normal traffic, but issue #2441 points out the risk: a very large or never-ending body means the server keeps buffering until it runs out of memory. That is a denial-of-service problem. Someone does not need to break in to take a service down, they only need to send a huge request.
My fix
My PR (#2442) adds a size limit, maxBufferSize:
- Once a body goes over the limit, Logbook stops keeping a copy and drops what it had stored.
- The application still receives the full request and sends the full response, nothing is cut off. Only the log copy is skipped.
- For requests, the part already read is replayed first and then the rest of the stream continues through a small wrapper stream.
- The default is "no limit", so nobody's existing behavior changes unless they turn it on.
- It can be set with a system property, or with
logbook.filter.max-buffer-sizein Spring Boot.
The review: six comments, six improvements
A maintainer reviewed the PR carefully and left six comments. Each one was right, and each taught me something.
1. An integer overflow hiding in plain sight. My check was written like this:
// Before: both values are int, so the sum can overflow and turn negative, // which would let a large write slip past the limit. if (maxBufferSize >= 0 && branch.size() + len > maxBufferSize) { ... } // After: widen to long before adding. if (maxBufferSize >= 0 && (long) branch.size() + len > maxBufferSize) { ... } A limit that can be bypassed by a big enough write is not a limit. I would not have caught that without the review.
2. Thread visibility. The exceeded flag could be written by one thread and read by another in async servlet setups. The fix was to make it volatile.
3. Do not pretend something works. I had passed setReadListener through to the wrapped stream. The maintainer pointed out that once the body has been read synchronously, containers usually refuse that call, and the wrapped stream does not know about the replayed bytes anyway. The existing adapter in the project throws UnsupportedOperationException there, so I did the same. It is better to say "not supported" than to fail in a confusing way later.
4. Small input mistakes. A property value with stray spaces would throw a NumberFormatException, so I added trim() and a test for it.
5. Keep the diff clean. My README change included line-ending noise from a local tool. I reverted it so only the one new table row changed. If a reviewer cannot see what changed, they cannot trust it.
6. Help the tools. I added the new property to the Spring configuration metadata file, so IDEs offer autocompletion for it.
I replied to each comment separately, said what I changed, and added tests where it made sense.
One more lesson: sign your commits
The project's bot flagged my first commit as unsigned, because I forgot to sign it. I fixed it, but it was a good reminder: read the contributing rules before the first push, not after.
What I took from it
- A security fix needs more care than a feature, because the bug is the part someone will try to abuse.
- A good review comment is a free lesson. Replying to each one separately shows the reviewer you read all of them.
- Backward compatibility by default makes a change much easier to accept.
- Edge cases like overflow, threads and bad input are where the real work is.
As I write this, the PR is open and under review. I will share the outcome.
Proof of my work
Everything here is public, so you can check it yourself:
- zalando/logbook PR #2442 and issue #2441: the bounded buffering fix and the review thread
- zalando/logbook #2301, #2326, #2327 and #2328: my earlier Logbook changes (merged)
- PicnicSupermarket/error-prone-support #2275: extended
RequireNonNullExpressionrule (merged) - Adyen/adyen-java-api-library #1952: regex fix, shipped in SDK v42.0.0 (merged)
- Sionna Visualizer: my 6G + AI dashboard (live demo)
- My GitHub
Next post: what happens when two maintainers prefer two different fixes for the same PR.
If you are a maintainer, a recruiter or a team looking for someone who takes security and review feedback seriously, my DMs are open. Tell me in the comments about the best review comment you ever received.