Skip to content

fix: parse a minimal Success AUTH packet (Remaining Length 0) - #160

Merged
robertsLando merged 1 commit into
mqttjs:masterfrom
spokodev:fix/auth-zero-remaining-length
Jun 24, 2026
Merged

robertsLando merged 1 commit into
mqttjs:masterfrom
spokodev:fix/auth-zero-remaining-length

Conversation

@spokodev

Copy link
Copy Markdown
Contributor

MQTT 5.0 §3.15.2.2.1 says the AUTH Reason Code and Property Length can be omitted when the Reason Code is 0x00 (Success) and there are no Properties — giving an AUTH packet with Remaining Length 0, i.e. the two bytes f0 00. The re-authentication flow (§4.12) ends with the server sending such a Success AUTH, which a compliant peer may encode in this minimal form.

The parser rejects it:

const parser = mqtt.parser({ protocolVersion: 5 })
parser.on('error', e => console.log(e.message))
parser.parse(Buffer.from([0xf0, 0x00]))
// => "Invalid auth reason code"

Cause

_parseAuth() reads the reason code unconditionally:

packet.reasonCode = this._parseByte()
if (!constants.MQTT5_AUTH_CODES[packet.reasonCode]) {
  return this._emitError(new Error('Invalid auth reason code'))
}

With Remaining Length 0 there is no reason-code byte, so _parseByte() returns undefined, MQTT5_AUTH_CODES[undefined] is falsy, and the packet is dropped. The sibling _parseDisconnect() already handles the analogous DISCONNECT e0 00 case by guarding the read with if (this._list.length > 0) and defaulting the reason code to 0; AUTH is the only reason-code packet missing that guard.

Impact

A spec-compliant minimal Success AUTH from a broker or client doing enhanced/re-authentication is rejected as an error, breaking the auth handshake when interoperating with peers that use the minimal encoding (affects MQTT.js and Aedes). generate() is unaffected — it always emits the longer f0 02 00 00 form — which is why a self round-trip never exposed this; the minimal form only arrives from an external encoder.

Fix

Mirror the existing _parseDisconnect guard: only read/validate the reason code when bytes remain, otherwise default it to 0. Non-empty AUTH packets (including invalid reason codes) behave exactly as before.

Tests

Added a parse-only case for f0 00 asserting { cmd: 'auth', reasonCode: 0, length: 0 }. The full suite passes (668) and standard is clean.

MQTT 5.0 section 3.15.2.2.1 allows the AUTH reason code and property
length to be omitted when the reason code is 0x00 (Success) and there are
no properties, giving a packet with Remaining Length 0 (bytes `f0 00`).
The parser read the reason code unconditionally, so for an empty payload
`_parseByte()` returned undefined, failed the reason-code validation, and
the packet was rejected with "Invalid auth reason code".

Guard the reason-code read with the same remaining-length check that
`_parseDisconnect` already uses, defaulting the reason code to 0 when the
payload is empty.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
mcollina requested a review from robertsLando June 24, 2026 10:14
@robertsLando
robertsLando merged commit 48c52be into mqttjs:master Jun 24, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants