jackc/pgx · error

bad auth type

Error message

bad auth type

What it means

Returned by AuthenticationSASL.Decode in pgproto3/authentication_sasl.go:33 when the leading 4 bytes are not AuthTypeSASL (10). Redundant with frontend dispatch; firing means corruption after dispatch or Decode called on bytes whose code is not 10.

Source

Thrown at pgproto3/authentication_sasl.go:33

}

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

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

	authType := binary.BigEndian.Uint32(src)

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

	dst.AuthMechanisms = dst.AuthMechanisms[:0]
	authMechanisms := src[4:]
	for len(authMechanisms) > 1 {
		idx := bytes.IndexByte(authMechanisms, 0)
		if idx == -1 {
			return &invalidMessageFormatErr{messageType: "AuthenticationSASL", details: "unterminated string"}
		}
		dst.AuthMechanisms = append(dst.AuthMechanisms, string(authMechanisms[:idx]))
		authMechanisms = authMechanisms[idx+1:]
	}

	return nil
}

// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
func (src *AuthenticationSASL) Encode(dst []byte) ([]byte, error) {

View on GitHub (pinned to ec1a0befd2)

Solutions

  1. Dispatch through findAuthenticationMessageType instead of a fixed struct.
  2. Switch on the auth code (10 => SASL) when decoding manually.
  3. Avoid reusing the byte buffer across messages.
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

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