Repository navigation
pgproto3: validate the SASL initial response length in Decode - #2732
Open
waw666waw666 wants to merge 1 commit into
Open
waw666waw666 wants to merge 1 commit into
waw666waw666 wants to merge 1 commit into
Conversation
SASLInitialResponse.Decode skipped the 4-byte initial response length field without reading it, so the declared length was never checked against the number of bytes actually present. A message declaring more bytes than it carried was silently decoded with truncated data, a message declaring fewer was decoded with trailing bytes treated as SASL data, a length of -1 (no initial response) followed by extra bytes was accepted as data, and negative lengths other than -1 were accepted as well. Read the length and require it to match the remaining message size, with -1 requiring an empty remainder and yielding a nil Data. This matches how FunctionCallResponse and Bind validate their length fields in the same package.
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.
SASLInitialResponse.Decodein the pgproto3 package skipped the 4-byte initial response length field without reading it (rp += 4with the comment "The rest of the message is data so we can just skip the size"). The declared length was therefore never checked against the number of bytes actually present in the message.That silently misparses malformed messages that a pgproto3
Backendcan receive from any client:A server or proxy built on pgproto3 then hands this wrongly-framed data to its SASL implementation instead of rejecting the message up front. Other length-prefixed decoders in pgproto3 do validate:
FunctionCallResponse.Decoderequires the result size to match the remaining message exactly, andBind.Decodechecks every parameter size before slicing.This change reads the length field in
SASLInitialResponse.Decodeand requires it to match the remaining message size exactly. A length of -1 requires an empty remainder and leavesDatanil; any other negative value, or a value that differs from the number of bytes left, is an error. Messages produced by pgproto3's ownEncodealways carry a correct length, so well-formed traffic is unaffected.Tests: the new tests in pgproto3/sasl_initial_response_test.go cover a well-formed message, the -1 no-initial-response form, an encode/decode round trip, and five length-mismatch cases (declared longer than actual, declared shorter than actual, declared zero with trailing data, -1 with trailing data, and a negative length other than -1). All five mismatch cases are accepted without an error on master and rejected with this change.
Verified with:
(
TestTraceis skipped because it requires a running PostgreSQL server; it fails the same way on unmodified master in an environment without one. The pgconn tests above exercise SASLInitialResponse decoding through the mock-server authentication paths and pass.)Disclosure: this change was prepared with AI assistance. The bug, the fix, and the tests were verified locally against the protocol's length semantics and the sibling decoders in this package.