Bound proxy HTTP response body read to a fixed size limit - #1202
Open
iaohkut-from-NightWolf-Team wants to merge 1 commit into
Open
Conversation
CallHTTP buffered the entire proxy backend response via io.ReadAll with no upper bound. A slow, huge, or misbehaving proxy backend response can force unbounded memory growth on every proxied call (connect, refresh, publish, subscribe, rpc, map_publish, map_remove, shared_poll_refresh), since they all share this code path. Bound it the same way other request bodies are already bounded elsewhere in the codebase (unisse/unihttpstream MaxRequestBodySize pattern), using io.LimitReader plus an explicit over-limit check since LimitReader silently truncates rather than erroring. Co-Authored-By: iaohkut <thb2601@gmail.com>
2 tasks
Member
|
Hi @iaohkut-from-NightWolf-Team, thx! Can we make const 16 MB? For this exact defense-in-depth hardening and this part this value seems better fit. I don't think we need to have this configurable, so larger value seems safe. |
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.
Summary
CallHTTPininternal/proxy/http.goreads the entire response body from a proxy backend viaio.ReadAll(resp.Body)with no upper bound. Every HTTP proxy call type (connect, refresh, publish, subscribe, sub_refresh, rpc, map_publish, map_remove, shared_poll_refresh) shares this same function, so a slow-but-huge or fast-but-massive response from the configured proxy backend gets fully buffered into memory regardless of size. The clientTimeoutonly bounds wall-clock time, not response size.This is defense-in-depth hardening, not an exploit against a normal client — the proxy
Endpointis fixed at startup from operator config and is never influenced by request/client data. But if a configured proxy backend is buggy, misconfigured, or itself compromised, it can currently force unbounded memory growth in Centrifugo, since many concurrent client connections can each be mid-proxy-call at once.Other request-body reads in this codebase already follow a bounded-size pattern (
unisse/unihttpstream'sMaxRequestBodySize+http.MaxBytesReader), so this brings the proxy response path in line with that existing convention.Change
maxProxyResponseBodySizeis a new 5MB constant in the same file. The explicit over-limit check is needed becauseio.LimitReadertruncates silently rather than erroring — readinglimit+1bytes and checking the actual length is what turns that into a real error instead of silently accepting a truncated response.Test plan
go build ./internal/proxy/...go test ./internal/proxy/...— existing tests pass unchanged