brianc/node-postgres · error · Error

SASL: SCRAM-SERVER-FIRST-MESSAGE: iteration missing

Error message

SASL: SCRAM-SERVER-FIRST-MESSAGE: iteration missing

What it means

Thrown by parseServerFirstMessage() when the server's first SASL message lacks the i= attribute (the iteration count). Per RFC 5802, the server must specify the PBKDF2 iteration count as a positive integer. Without it, the client cannot perform the key derivation with the correct work factor.

Solutions

  1. Verify the target is a standard PostgreSQL server with full SCRAM-SHA-256 support.
  2. Remove intermediaries that might truncate the SASL message.
  3. Test with psql using the same connection string.
  4. Update node-postgres to the latest version.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await client.connect()
} catch (err) {
  if (err.message.includes('iteration missing')) {
    throw new Error('SCRAM handshake failed: server sent no iteration count — verify server is PostgreSQL 10+')
  }
  throw err
}

Prevention

When it happens

Trigger: At sasl.js:207-208, attrPairs.get('i') returns a falsy value (undefined). The parsed attribute Map has no 'i' key — the server omitted the iteration count.

Common situations: Malformed or truncated server first message; connecting to a non-conformant server that doesn't fully implement SCRAM-SHA-256; a proxy truncating the authentication exchange; network data loss cutting the message short.

Related errors


AI-assisted analysis of brianc/node-postgres@ff9d775abd (2026-08-11). Data as JSON: /api/errors/90484998f8ecca96. Report an issue: GitHub.

Appendix: source

Thrown at packages/pg/lib/crypto/sasl.js:209

function parseServerFirstMessage(data) {
  const attrPairs = parseAttributePairs(data)

  const nonce = attrPairs.get('r')
  if (!nonce) {
    throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: nonce missing')
  } else if (!isPrintableChars(nonce)) {
    throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: nonce must only contain printable characters')
  }
  const salt = attrPairs.get('s')
  if (!salt) {
    throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: salt missing')
  } else if (!isBase64(salt)) {
    throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: salt must be base64')
  }
  const iterationText = attrPairs.get('i')
  if (!iterationText) {
    throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: iteration missing')
  } else if (!/^[1-9][0-9]*$/.test(iterationText)) {
    throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: invalid iteration count')
  }
  const iteration = parseInt(iterationText, 10)

  return {
    nonce,
    salt,
    iteration,
  }
}

function parseServerFinalMessage(serverData) {
  const attrPairs = parseAttributePairs(serverData)
  const error = attrPairs.get('e')
  const serverSignature = attrPairs.get('v')

  if (error) {

View on GitHub (pinned to ff9d775abd)