jackc/pgx · error
bad auth type
Error message
bad auth type
What it means
Returned by AuthenticationMD5Password.Decode in pgproto3/authentication_md5_password.go:32 when the leading 4 bytes are not AuthTypeMD5Password (5). Redundant with frontend dispatch; firing means corruption after dispatch or Decode called on bytes whose code is not 5.
Source
Thrown at pgproto3/authentication_md5_password.go:32
}
// Backend identifies this message as sendable by the PostgreSQL backend.
func (*AuthenticationMD5Password) Backend() {}
// Backend identifies this message as an authentication response.
func (*AuthenticationMD5Password) AuthenticationResponse() {}
// Decode decodes src into dst. src must contain the complete message with the exception of the initial 1 byte message
// type identifier and 4 byte message length.
func (dst *AuthenticationMD5Password) Decode(src []byte) error {
if len(src) != 8 {
return errors.New("bad authentication message size")
}
authType := binary.BigEndian.Uint32(src)
if authType != AuthTypeMD5Password {
return errors.New("bad auth type")
}
copy(dst.Salt[:], src[4:8])
return nil
}
// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
func (src *AuthenticationMD5Password) Encode(dst []byte) ([]byte, error) {
dst, sp := beginMessage(dst, 'R')
dst = pgio.AppendUint32(dst, AuthTypeMD5Password)
dst = append(dst, src.Salt[:]...)
return finishMessage(dst, sp)
}
// MarshalJSON implements encoding/json.Marshaler.
func (src AuthenticationMD5Password) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {View on GitHub (pinned to ec1a0befd2)
Solutions
- Dispatch through findAuthenticationMessageType instead of a fixed struct.
- Switch on the auth code (5 => MD5) when decoding manually.
- Avoid reusing the byte buffer across messages.
Defensive patterns
Strategy: validation
Validate before calling
if len(body) >= 4 && binary.BigEndian.Uint32(body) == pgproto3.AuthTypeMD5Password {
var m pgproto3.AuthenticationMD5Password
return m.Decode(body)
} Type guard
func isMD5AuthFrame(body []byte) bool {
return len(body) >= 4 && binary.BigEndian.Uint32(body) == pgproto3.AuthTypeMD5Password
} Prevention
- Dispatch through findAuthenticationMessageType instead of a fixed struct.
- Switch on the auth code (5 => MD5) before Decode.
- Avoid reusing the byte buffer across messages.
When it happens
Trigger: Manual/proxy code decoding a non-MD5 frame with the MD5 struct; buffer mutation; fuzz input.
Common situations: Test/proxy hard-coding the wrong struct; corrupted reused buffer.
Related errors
AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04).
Data as JSON: /data/errors/b5e23884d75d9b98.json.
Report an issue: GitHub.