fix(hls-pull): cap segment + playlist body size so one upstream can't OOM the host#17
Merged
Merged
Conversation
… OOM the host fetchSegmentOnce read the segment body with io.ReadAll and no bound, and the M3U8 parser scanned the playlist body with only a 256 KB per-line cap. A pull HLS source — or any host its segment URI / a 3xx redirect points to (combines with the SSRF vector) — that returns a multi-GB body forced unbounded allocation; the only limit was the 60 s segment timeout (~7.5 GB at 1 Gbps). One malicious/compromised/redirected upstream could OOM-kill the shared process and take every stream on the host down, with no config write access. Add response-body caps. readCapped reads at most cap+1 bytes through an io.LimitReader and returns errBodyTooLarge when exceeded, so an oversized body is rejected WITHOUT allocating it; fetchSegmentOnce uses it with a 64 MiB default and fetchSegmentWithRetry treats over-limit as a failed (then skipped) segment rather than killing the stream. parseM3U8 wraps its body in an io.LimitReader (8 MiB), which bounds the scanner input and transitively the parsed segment/variant slice. Both caps are package vars so ops can tune and tests can shrink them. Tests: TestReadCapped (under/at/over cap; a counting reader proves no more than cap+1 bytes are consumed), TestFetchSegmentOnce_RejectsOversizedSegment (httptest with a shrunk cap), TestParseM3U8_PlaylistBodyCapped.
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
Problem (audit A-7, HIGH)
fetchSegmentOnceread the segment body withio.ReadAll(resp.Body)and no bound, andparseM3U8scanned the playlist body with only a 256 KB per-line cap. A pull HLS source — or any host its segment URI / a 3xx redirect points to (combines with the SSRF vector S-4) — that returns a multi-GB body forced unbounded allocation; the only limit was the 60 s segment timeout (~7.5 GB at 1 Gbps). One malicious / compromised / redirected upstream could OOM-kill the shared process and take every stream on the host down, with no config write access.Fix
readCapped(r, max): reads at mostmax+1bytes through anio.LimitReaderand returnserrBodyTooLargewhen exceeded — so an oversized body is rejected without allocating it (the OOM the cap exists to prevent).fetchSegmentOnceusesreadCapped(resp.Body, hlsMaxSegmentBytes)(64 MiB default);fetchSegmentWithRetryalready treats the error as a failed → skipped segment, so one bad segment doesn't kill the stream.parseM3U8wraps its body inio.LimitReader(body, hlsMaxPlaylistBytes)(8 MiB), bounding the scanner input and transitively the parsed segment/variant slice.Test
TestReadCapped(under / at / over cap; a counting reader proves ≤cap+1bytes are consumed),TestFetchSegmentOnce_RejectsOversizedSegment(httptest server + shrunk cap →errBodyTooLarge; under-cap succeeds),TestParseM3U8_PlaylistBodyCapped.go test -race ./internal/ingestor/pull/green for the new tests,golangci-lint0 issues, fullgo build ./...green. (The pre-existingTestRTMPReader_PullsFromMediaMTXintegration test requires a local MediaMTX/Docker/ffmpeg and is unrelated to this change.)