mosquitto icon indicating copy to clipboard operation
mosquitto copied to clipboard

Mosquitto_pub reuses topic alias after reconnect, which is not allowed

Open halfgaar opened this issue 2 years ago • 1 comments

Mosquitto_pub reuses topic alias after reconnect, which is not allowed.

To reproduce, do:

( while true; do echo "$(date)" ; sleep 1; done ) | ./mosquitto_pub -D publish topic-alias 1 -l -d -t a/b/c/d

Here's a session, in which one/two/three/four/five is specified once, then reused:

$ ( while true; do echo "$(date)" ; sleep 1; done ) | ./mosquitto_pub -D publish topic-alias 1 -l -d -t one/two/three/four/five
Client (null) sending CONNECT
Client RgI04TXDn6no7UyHMmXlvnR received CONNACK (0)
Client RgI04TXDn6no7UyHMmXlvnR sending PUBLISH (d0, q0, r0, m1, 'one/two/three/four/five', ... (29 bytes))
Client RgI04TXDn6no7UyHMmXlvnR sending PUBLISH (d0, q0, r0, m2, '(null)', ... (29 bytes))
Client RgI04TXDn6no7UyHMmXlvnR sending PUBLISH (d0, q0, r0, m3, '(null)', ... (29 bytes))
Client RgI04TXDn6no7UyHMmXlvnR sending PUBLISH (d0, q0, r0, m4, '(null)', ... (29 bytes))
Client RgI04TXDn6no7UyHMmXlvnR sending PUBLISH (d0, q0, r0, m5, '(null)', ... (29 bytes))

-- Stop server
-- Start server

-- It will handshake again:
Client RgI04TXDn6no7UyHMmXlvnR sending CONNECT
Client RgI04TXDn6no7UyHMmXlvnR received CONNACK (0)

-- And reuse the topic alias. The server rejects the client, because the topic is empty all the time
Client RgI04TXDn6no7UyHMmXlvnR sending PUBLISH (d0, q0, r0, m10, '(null)', ... (29 bytes))
Client RgI04TXDn6no7UyHMmXlvnR sending CONNECT
Client RgI04TXDn6no7UyHMmXlvnR received CONNACK (0)
Client RgI04TXDn6no7UyHMmXlvnR sending PUBLISH (d0, q0, r0, m11, '(null)', ... (29 bytes))
Client RgI04TXDn6no7UyHMmXlvnR sending CONNECT
Client RgI04TXDn6no7UyHMmXlvnR received CONNACK (0)
Client RgI04TXDn6no7UyHMmXlvnR sending PUBLISH (d0, q0, r0, m12, '(null)', ... (29 bytes))
Client RgI04TXDn6no7UyHMmXlvnR sending CONNECT
Client RgI04TXDn6no7UyHMmXlvnR received CONNACK (0)
Client RgI04TXDn6no7UyHMmXlvnR sending PUBLISH (d0, q0, r0, m13, '(null)', ... (29 bytes))
Client RgI04TXDn6no7UyHMmXlvnR sending CONNECT
Client RgI04TXDn6no7UyHMmXlvnR received CONNACK (0)
Client RgI04TXDn6no7UyHMmXlvnR sending PUBLISH (d0, q0, r0, m14, '(null)', ... (29 bytes))

The MQTT 5.0 specs say:

Topic Alias mappings exist only within a Network Connection and last only for the lifetime of that Network Connection. A receiver MUST NOT carry forward any Topic Alias mappings from one Network Connection to another.

./mosquitto_pub --version
mosquitto_pub version 2.0.12 running on libmosquitto 2.0.12.

BTW: why is everything 29 bytes? Is that correct?

halfgaar avatar Mar 27 '22 20:03 halfgaar

Peeking in the source, I find that there is a first_publish variable that is never reset. Perhaps simply resetting it in the connect callback will fix it.

BTW: why is everything 29 bytes? Is that correct?

Mine publishes 32 bytes. I wouldn't expect $(date) to vary in length much.

Daedaluz avatar Mar 28 '22 13:03 Daedaluz