fix(jwks): enforce maxResponseSize while reading the response - #32
Merged
Conversation
The size limit was applied to the `Content-Length` header and then re-checked after `res.text()` had resolved. A response without that header — any chunked reply, which the server chooses — skipped the first check, and by the time the second one ran the whole body was already in memory. The limit described a response we had finished buffering rather than one we refused to buffer. `readCapped` reads the body stream with a running byte count and stops at the first chunk that crosses the limit, cancelling the remainder so the connection is not left draining. Falls back to `text()` when the response exposes no stream. Measured against a server streaming 40 MiB with a 64 KiB limit configured: heap growth drops from ~42 MiB to ~1 MiB, and the client disconnects instead of reading to completion.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Independent of the Redis series — can merge in any order relative to it.
maxResponseSizewas checked against theContent-Lengthheader, and then again afterres.text()had already resolved. A response that omits that header — any chunked reply, which the server chooses — skipped the first check, and by the time the second ran the whole body was in memory. The limit described a response we had finished buffering rather than one we refused to buffer.readCappednow reads the body stream with a running byte count and stops at the first chunk that crosses the limit, cancelling the remainder so the connection is not left draining. Falls back totext()when the response exposes no stream.Measured — 40 MiB streamed, 64 KiB limit configured:
Regression test covers a chunked response with no
Content-Length, and asserts the stream is cancelled rather than drained.