Skip to content

fix: refuse a PUBLISH whose topic contains a wildcard - #2124

Open
Xnadir wants to merge 5 commits into
mqttjs:mainfrom
Xnadir:fix/publish-wildcard-topic
Open

Xnadir wants to merge 5 commits into
mqttjs:mainfrom
Xnadir:fix/publish-wildcard-topic

Conversation

@Xnadir

@Xnadir Xnadir commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

The Topic Name of a PUBLISH must not contain wildcard characters [MQTT-3.3.2-2]. subscribe() validates its topic filters, but publish() sent any topic as it was. A broker may close the connection on such a PUBLISH, and with QoS 1/2 the packet stays in the outgoing store and is sent again after every reconnect:

const client = mqtt.connect('mqtt://localhost', { reconnectPeriod: 200 }) // Aedes
client.on('connect', () => client.publish('a/+', 'x', { qos: 1 }, console.log))
// main: the broker closes every new connection, `connect` is never emitted again
//       and the callback is never called
// here: the callback gets `Invalid topic a/+` and nothing is sent

publish() now gives the callback Invalid topic <topic> for a topic with + or #, and sends and stores nothing, like subscribe() does for an invalid topic filter. Without a callback the publish is dropped, as subscribe() does; happy to emit error there instead if you prefer.

Tests

New test in test/node/abstract_client.ts, so it runs for MQTT 3.1.1, 5, TLS, WebSocket and the unique message id provider:

  • should return an error (via callback) and send nothing for a topic with a wildcard

It fails on main and passes with this change. Run locally: client.ts, client_mqtt5.ts, websocket_client.ts, secure_client.ts, unique_message_id_provider_client.ts. Everything passes except tests that also fail on main on Windows ("should accept the connack of a new connection after a disconnect", and in secure_client.ts "should tear the connection down before emitting a connack timeout"). eslint and tsc --noEmit are clean.

This change was written with the help of Claude Code (AI). I reviewed it and ran the tests above.

🤖 Generated with Claude Code

Xnadir and others added 2 commits November 26, 2026 08:08
The Topic Name of a PUBLISH must not contain `+` or `#`
[MQTT-3.3.2-2]. `publish()` did not check it, so `publish('a/+', ...)`
went to the broker, which may close the connection on it (Aedes
does). With QoS 1/2 the packet stays in the outgoing store and
is sent again after each reconnect, so such a broker closes every new
connection before `connect` is emitted, and the publish callback is
never called.

`publish()` now gives the callback `Invalid topic <topic>` and sends
nothing, like `subscribe()` does for an invalid topic filter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.73%. Comparing base (61d7b34) to head (d48ba01).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2124      +/-   ##
==========================================
- Coverage   88.81%   88.73%   -0.09%     
==========================================
  Files          26       26              
  Lines        1905     1909       +4     
  Branches      453      455       +2     
==========================================
+ Hits         1692     1694       +2     
- Misses        173      175       +2     
  Partials       40       40              
Flag Coverage Δ
unittests 88.73% <100.00%> (-0.09%) ⬇️

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.

Xnadir and others added 3 commits November 26, 2026 23:57
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…topic

# Conflicts:
#	test/node/abstract_client.ts
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.

2 participants