Skip to content

fix: reject an unsolicited AUTH instead of crashing the process (GHSA-fwrw-4mhv-wxvm) - #2063

Merged
robertsLando merged 2 commits into
mainfrom
fix/ghsa-fwrw-auth
Sep 16, 2026
Merged

robertsLando merged 2 commits into
mainfrom
fix/ghsa-fwrw-auth

Conversation

@robertsLando

Copy link
Copy Markdown
Member

Summary

Fixes GHSA-fwrw-4mhv-wxvm (high). A broker could terminate an MQTT.js client process with a single unsolicited AUTH packet — an uncaught TypeError in the stream write path, where no application code can catch it. Enhanced authentication did not have to be configured for this to be reachable.

The trigger and the full analysis are in the advisory and stay there until it publishes.

Stacked on #2062 (and #2061, #2060 beneath it); those merge first.

Fix

src/lib/handlers/auth.ts, src/lib/handlers/index.ts, src/lib/client.ts.

An AUTH is now rejected, and the connection dropped, when:

  • the protocol is not 5 — AUTH does not exist before MQTT 5;
  • the client sent no Authentication Method in CONNECT. "If the Client does not include an Authentication Method in the CONNECT, the Server MUST NOT send an AUTH packet" [MQTT-4.12.0-6];
  • the AUTH's Authentication Method differs from the one CONNECT carried [MQTT-4.12.0-5] — without this a broker can switch method mid-exchange and steer a custom handleAuth down a weaker path;
  • reason code 24 arrives but the handler has nothing to continue the exchange with (this is the crash);
  • the reason code is not one a broker may send. §3.15.2.1 defines exactly three, and 25 is client-only.

Reason code 0 ends the exchange successfully and keeps the connection up.

The Authentication Method is latched from the packet actually written, not read from options per packet, so a caller mutating options mid-exchange cannot move the goalposts.

The teardown is skipped when the exchange belongs to a connection that has already been replaced. client.handleAuth is application-overridable and answers through a callback — an implementation that fetches a token has every reason to be slow — so it can resume long after its connection is gone, and tearing down then would destroy a healthy stream over a dead exchange.

Consumer-visible changes

  • A client with no properties.authenticationMethod that receives an AUTH now emits error and drops the connection, where before it crashed the process.
  • An AUTH whose reason code is neither 0 nor 24 is rejected rather than passed to handleAuth.
  • Reason code 0 is treated as a successful end to the exchange; previously it produced an error with an empty reason string.

Verification

Full node suite on this branch: 978 tests, 973 pass, 0 fail, 5 skipped. Typecheck, lint and build clean.

Merge note: resolves the _discardParsedPackets / PacketPump divergence between this fix and #2060 in favour of the pump, keeping this branch's _authenticationMethod alongside it.

@codecov

codecov Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.10%. Comparing base (3db16f5) to head (9ddcf10).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/lib/handlers/auth.ts 92.30% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2063      +/-   ##
==========================================
+ Coverage   83.91%   85.10%   +1.19%     
==========================================
  Files          26       26              
  Lines        1672     1692      +20     
  Branches      388      393       +5     
==========================================
+ Hits         1403     1440      +37     
+ Misses        236      218      -18     
- Partials       33       34       +1     
Flag Coverage Δ
unittests 85.10% <93.33%> (+1.19%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Base automatically changed from fix/ghsa-gfxc-connack-props to main September 16, 2026 09:07
…-fwrw-4mhv-wxvm)

An AUTH with reason code 24 reached the default handleAuth, which
answered with no packet, and _sendPacket was handed undefined: an
uncaught TypeError in the stream write path, terminating the process.
No enhanced authentication had to be configured for this to be reachable.

An AUTH is now rejected when the protocol is not 5, when no
Authentication Method was sent in CONNECT (MQTT-4.12.0-6), when the
method differs from the one CONNECT carried (MQTT-4.12.0-5), when rc 24
arrives with nothing to continue the exchange, and when the reason code
is not one a broker may send (3.15.2.1). Reason code 0 ends the exchange
and keeps the connection up.

The method is latched from the packet actually written, so a caller
mutating options mid-exchange cannot move the goalposts.

The teardown is skipped when the exchange belongs to a connection that
has already been replaced: handleAuth is application-overridable and
answers through a callback, so it can resume long after its connection
is gone.
handleAuth is application-overridable and answers through a callback, so
a rejection can land after the client has moved on. Without the
isCurrent guard the teardown destroys the healthy stream that replaced
it; the test holds the exchange open across a reconnect and asserts the
live connection survives.
@robertsLando
robertsLando merged commit b511e7b into main Sep 16, 2026
10 checks passed
@robertsLando
robertsLando deleted the fix/ghsa-fwrw-auth branch September 16, 2026 09:09
robertsLando added a commit that referenced this pull request Sep 16, 2026
GHSA-8phv-jwjm-93rr) (#2064)

## Summary

Fixes **GHSA-8phv-jwjm-93rr** (medium). A broker could make an MQTT.js
client re-run its whole connection setup mid-session: in-flight QoS 1
messages resent under their original ids, subscriptions replayed, and
the message id state reset underneath a live session.

The trigger and the full analysis are in the advisory and stay there
until it publishes.

Stacked on #2063 (and #2062, #2061, #2060 beneath it); those merge
first.

## Fix

`src/lib/handlers/connack.ts`, `src/lib/handlers/index.ts`,
`src/lib/client.ts`.

- [MQTT-3.2.0-2] allows one CONNACK per network connection. A second one
is now a protocol error and the connection is dropped.
- The gate is "a CONNACK was already handled on this connection"
(`connackReceived`), **not** `client.connected`. A refused CONNACK never
sets `connected`, so gating on it would let a broker follow a refusal
with an accepting CONNACK — in the same chunk, ahead of the teardown —
and end up with a connected client the application had already been told
had failed.
- A refused CONNACK now always tears the connection down. Previously,
whenever `reconnectOnConnackError` was off — the default — the socket
was left open: the connack timer was already cleared, no keepalive
manager existed because the client never became `connected`, and no
reconnect was armed, while the broker kept streaming PUBLISHes that were
delivered as `message` events to an application that had just been told
"Connection refused". `reconnectOnConnackError` now gates only the
retry, which is what its name says.
- `work()` drops whatever is still queued on a connection the client has
moved on from. A CONNACK parked behind a slow `handleMessage` was
otherwise judged against the connection that *replaced* it — `connect()`
resets `connackReceived` for the new connection, so it passed the gate
and `_onConnect` ran against a session it never belonged to. Dropping at
the pump covers every handler at once, rather than adding a liveness
check to each teardown site.

## Consumer-visible changes

- A refused CONNACK now closes the socket in all cases. With
`reconnectOnConnackError: false` (the default) the client no longer sits
on an open connection after a refusal. Applications that relied on
receiving messages after a refusal were relying on a bug.
- Event order on a refusal is preserved for `reconnectOnConnackError`
users: `error`, then `offline`, then `close`.

## Verification

Full node suite on this branch: **1009 tests, 1004 pass, 0 fail, 5
skipped**. Typecheck, lint and build clean.

One existing test changed meaning: a topic-alias violation queued on a
connection that has since been replaced is now never examined, so it
emits nothing instead of an error. Its security assertions — that the
dead connection cannot touch the live one — are unchanged.
robertsLando added a commit that referenced this pull request Sep 16, 2026
…h8jm-hm87-fqw3) (#2065)

## Summary

Fixes **GHSA-h8jm-hm87-fqw3** (low). The client advertised a Receive
Maximum and then ignored it on inbound QoS 2: a broker could grow the
client's incoming store without bound. Reconnecting did not clear it,
because a resumed session keeps its store.

The trigger and the full analysis are in the advisory and stay there
until it publishes.

Last of six; stacked on #2064 (and #2063, #2062, #2061, #2060 beneath
it).

## Fix

`src/lib/client.ts`, `src/lib/handlers/publish.ts`,
`src/lib/handlers/pubrel.ts`.

- Inbound QoS 2 message ids are tracked against the advertised quota. A
broker that exceeds it is disconnected with reason code `0x93` (Receive
Maximum exceeded), which §3.3.4 [MQTT-3.3.4-9] is exactly the code for.
- A redelivery of a message already held takes no second slot, so DUP
redelivery still works [MQTT-4.3.3-10], and the PUBREL handler answers a
re-sent PUBREL whether or not the store still holds the entry
[MQTT-4.3.3-11].
- The quota is advertised as the value actually enforced. The two differ
when the option failed validation, and a rejected `receiveMaximum: 0` on
the wire is itself a protocol error (§3.2.2.3.3) the broker would blame
us for.
- The `receiveMaximum` option is validated and latched at construction,
so a caller mutating `options` mid-session cannot disable the check, and
a non-number cannot silently defeat it (every comparison against `NaN`
is false).

### On slot lifetime

Slots are deliberately **kept** across a reconnect. §4.9 describes the
flow-control quota as re-initialized per network connection, and read
literally that would mean releasing them. They are not released here,
because these slots are the bound on the *incoming store*, which is
session state: releasing them per connection lets a broker reset the
quota by dropping the TCP connection and grow the store across
reconnects — the vulnerability itself.

The trade-off is that a broker which resumes a session, leaves earlier
QoS 2 exchanges unfinished, and then fills the connection to
`receiveMaximum` can be disconnected. That requires the broker to leave
exchanges unresolved, and the store bound is the property worth keeping.
Splitting the two limits — per-connection flow control for the `0x93`
decision, a separate store cap refusing the excess with `PUBREC` `0x97`
— is a possible follow-up, deliberately not done in a security fix.

## Consumer-visible changes

- A broker exceeding the advertised `receiveMaximum` on inbound QoS 2
now gets disconnected instead of being allowed to grow the store.
Reconnect behaviour is unchanged.
- `properties.receiveMaximum` outside 1..65535, or not an integer, is
ignored with a log line and 65535 is enforced and advertised instead.

## Verification

Full node suite on this branch: **1030 tests, 1025 pass, 0 fail, 5
skipped**. Typecheck, lint and build clean.

With all six merged, the tree is byte-identical to the integration
branch the whole set was tested on.
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.

1 participant