jackc/pgx · error

bad auth type

Error message

bad auth type

What it means

Returned by AuthenticationSASLFinal.Decode in pgproto3/authentication_sasl_final.go:32 when the leading 4 bytes are not AuthTypeSASLFinal (12). Defensive check redundant with frontend dispatch; firing means corruption after dispatch or Decode called on bytes whose code is not 12.

Source

Thrown at pgproto3/authentication_sasl_final.go:32

}

// Backend identifies this message as sendable by the PostgreSQL backend.
func (*AuthenticationSASLFinal) Backend() {}

// Backend identifies this message as an authentication response.
func (*AuthenticationSASLFinal) 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 *AuthenticationSASLFinal) Decode(src []byte) error {
	if len(src) < 4 {
		return errors.New("authentication message too short")
	}

	authType := binary.BigEndian.Uint32(src)

	if authType != AuthTypeSASLFinal {
		return errors.New("bad auth type")
	}

	dst.Data = src[4:]

	return nil
}

// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
func (src *AuthenticationSASLFinal) Encode(dst []byte) ([]byte, error) {
	dst, sp := beginMessage(dst, 'R')
	dst = pgio.AppendUint32(dst, AuthTypeSASLFinal)
	dst = append(dst, src.Data...)
	return finishMessage(dst, sp)
}

// MarshalJSON implements encoding/json.Unmarshaler.
func (src AuthenticationSASLFinal) MarshalJSON() ([]byte, error) {
	return json.Marshal(struct {

View on GitHub (pinned to ec1a0befd2)

Solutions

  1. Dispatch via findAuthenticationMessageType rather than a fixed struct.
  2. Switch on the auth code (12 => SASLFinal) when decoding manually.
  3. Do not alias/reuse the message buffer across decodes.
Defensive patterns

Strategy: validation

Validate before calling

if len(body) >= 4 && binary.BigEndian.Uint32(body) == pgproto3.AuthTypeSASLFinal {
    var m pgproto3.AuthenticationSASLFinal
    return m.Decode(body)
}

Type guard

func isSASLFinalFrame(body []byte) bool {
    return len(body) >= 4 && binary.BigEndian.Uint32(body) == pgproto3.AuthTypeSASLFinal
}

Prevention

When it happens

Trigger: Manual/proxy code decoding a non-SASLFinal frame with the SASLFinal 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/0f03ccd804be5d91.json. Report an issue: GitHub.