Reject a hostile Content-Length instead of panicking - #24
Open
objevovat wants to merge 1 commit into
Open
Conversation
A receiver answering with "Content-Length: -1" crashes the sender. The value is parsed with Sscanf into an int, is not checked, and reaches make([]byte, contentLength) in readPlaintextHTTPResponse, which panics with "makeslice: len out of range". Nothing recovers it. Reproduced end to end over a net.Pipe standing in for the receiver: the panic happens inside readPlaintextHTTPResponse, not in a helper. Also bounds the positive case. Content-Length is attacker- or bug-controlled and was used unbounded, so "Content-Length: 2147483647" asks the sender for a 2 GB allocation before a single body byte arrives. Control-channel bodies here are small plists; the 8 MB limit is far above anything legitimate. The other four make([]byte, n) sites in this package are fine and are left alone -- readEncryptedFrame already bounds its uint16 length, hkdfSHA512 and padTo take caller-supplied sizes, and audio.go's n comes from a Read. Tests cover negative, large-negative and oversized headers, plus a well-formed response to show the normal path still works. Removing the guard makes the first two fail with the original panic.
objevovat
force-pushed
the
reject-bad-content-length
branch
from
July 31, 2026 21:10
23fa3f9 to
2348596
Compare
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.
The bug
A receiver answering with
Content-Length: -1crashes the sender.parseHTTPHeaderreads the value withSscanfinto anintand doesn't check it. InreadPlaintextHTTPResponsethe zero case is handled, but a negative value falls through to:Nothing recovers it. I reproduced this end to end over a
net.Pipestanding in for the receiver — the panic happens insidereadPlaintextHTTPResponse, not in a helper.Also bounded the positive case
Content-Lengthis receiver-controlled and was used unbounded, soContent-Length: 2147483647asks the sender for a 2 GB allocation before a single body byte arrives. Control-channel bodies here are small plists, so the 8 MB limit is far above anything legitimate and far below anything that hurts.Scope
Only these two sites. I checked the other four
make([]byte, n)in the package and left them alone:readEncryptedFramealready bounds itsuint16length and rejects anything over 16384hkdfSHA512andpadTotake caller-supplied sizes, not network valuesaudio.go'sncomes from aRead, so it can't be negativeTests
Negative, large-negative and oversized headers, plus a well-formed response to show the normal path still reads its body. Removing the guard makes the first two fail with the original panic, so the tests genuinely cover it.
go test ./...is green.How I found it
I was fuzzing the receiver-facing parsers —
parseHTTPHeader,parseTXT,parseFeatures,parseCaptureFrames,parseXrandrGeometry— for about 11.8 million executions total. All clean; none of them crash. This one isn't reachable by fuzzing those functions in isolation, because the panic is at the use of the parsed value rather than in the parse. It turned up reading the allocation sites afterwards.Happy to contribute the fuzz targets separately if you'd want them in the tree.