jackc/pgx · error

authentication message too short

Error message

authentication message too short

What it means

Returned by AuthenticationSASLFinal.Decode in pgproto3/authentication_sasl_final.go:26 when the body is < 4 bytes. SASLFinal carries the 4-byte auth code (AuthTypeSASLFinal = 12) followed by the variable server-final SCRAM message, so anything shorter than 4 bytes cannot be type-checked. Indicates truncation/corruption or Decode on partial bytes.

Source

Thrown at pgproto3/authentication_sasl_final.go:26

	"github.com/jackc/pgx/v5/internal/pgio"
)

// AuthenticationSASLFinal is a message sent from the backend indicating a SASL authentication has completed.
type AuthenticationSASLFinal struct {
	Data []byte
}

// 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...)

View on GitHub (pinned to ec1a0befd2)

Solutions

  1. Keep the connection stable through the final SCRAM step.
  2. Remove intermediaries that truncate the server-final message.
  3. Validate len(body) >= 4 before decoding in custom code.
Defensive patterns

Strategy: try-catch

Try / catch

conn, err := pgconn.Connect(ctx, connString)
if err != nil {
    if strings.Contains(err.Error(), "authentication message too short") {
        return fmt.Errorf("truncated SASL final frame from %s: %w", connString, err)
    }
    return err
}

Prevention

When it happens

Trigger: The server-final SCRAM frame arrives truncated at the end of the SCRAM exchange; connection dropped just before auth completes; proxy truncates the frame; direct Decode on undersized input.

Common situations: SCRAM-SHA-256 over an unstable link or truncating proxy; fuzz input.

Related errors


AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04). Data as JSON: /data/errors/c8c0f9c56a9c1eb0.json. Report an issue: GitHub.