Skip to content

pgproto3: validate the SASL initial response length in Decode - #2732

Open
waw666waw666 wants to merge 1 commit into
jackc:masterfrom
waw666waw666:fix/pgproto3-sasl-initial-response-length
Open

waw666waw666 wants to merge 1 commit into
jackc:masterfrom
waw666waw666:fix/pgproto3-sasl-initial-response-length

Conversation

@waw666waw666

Copy link
Copy Markdown
Contributor

SASLInitialResponse.Decode in the pgproto3 package skipped the 4-byte initial response length field without reading it (rp += 4 with 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 Backend can receive from any client:

  • a length larger than the remaining bytes decodes truncated data as if it were complete;
  • a length smaller than the remaining bytes treats the trailing bytes as SASL data;
  • a length of -1, which per the protocol means the mechanism sends no initial response, followed by extra bytes accepts those bytes as data;
  • a negative length other than -1 is accepted as well.

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.Decode requires the result size to match the remaining message exactly, and Bind.Decode checks every parameter size before slicing.

This change reads the length field in SASLInitialResponse.Decode and requires it to match the remaining message size exactly. A length of -1 requires an empty remainder and leaves Data nil; any other negative value, or a value that differs from the number of bytes left, is an error. Messages produced by pgproto3's own Encode always 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:

go test ./pgproto3/ -run TestSASLInitialResponse -count=1
go test ./pgproto3/ -skip TestTrace -count=1
go test ./pgconn/ -run 'TestConnectRequireAuthCompletion|TestConnectAuthenticationOk|TestConnectChannelBinding|TestConnectOAuthServerErrors|TestScram' -count=1
go vet ./pgproto3/

(TestTrace is 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant