Fix MQTT client: clear credentials and will on connection teardown#402
Open
EdouardMALOT wants to merge 1 commit into
Open
Fix MQTT client: clear credentials and will on connection teardown#402EdouardMALOT wants to merge 1 commit into
EdouardMALOT wants to merge 1 commit into
Conversation
nxd_mqtt_client_login_set stores pointers into the caller's buffers, and only _nxd_mqtt_process_disconnect reset them. Failed connects (CONNACK refused, TCP or TLS establish failure) tear down through _nxd_mqtt_client_connection_end and skip that reset, so a later connect without credentials sends CONNECT with the username flag set but no username. Brokers drop it as a protocol error. Move the cleanup into _nxd_mqtt_client_connection_end, which every teardown path goes through except the early TCP-failure branch of _nxd_mqtt_client_connect. That branch gets the same cleanup inline, plus a use_tls reset: it has the same stale-flag problem that eclipse-threadx#400 fixes in connection_end, on a path eclipse-threadx#400 does not cover.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The credentials and will pointers stored in the client (
nxd_mqtt_client_username/password/will_topic/will_message, set bynxd_mqtt_client_login_set()andnxd_mqtt_client_will_message_set()) must be cleared on every connection teardown, error paths included. Today only the clean-disconnect path clears them, in_nxd_mqtt_process_disconnect(); the failed-connect teardowns (CONNACK refused, TCP or TLS establish failure) go through_nxd_mqtt_client_connection_end()and keep the stale pointers. If the application then reconfigures the same client for an anonymous connection (no newlogin_set()call) and its old buffers have been zeroed or reused, the next CONNECT goes out with the username flag set but no valid username. Mosquitto drops it as a protocol error (CONNECT with username flag but no username) andnxd_mqtt_client_secure_connect()fails withNXD_MQTT_COMMUNICATION_FAILURE(0x10007), on every retry, until the device reboots.Observed during TLS/MQTT test-bench runs: connect attempts with credentials failed while the server certificate was temporarily invalid, the configuration was then switched back to an anonymous account, and the live client could never reconnect.
Changes
addons/mqtt/nxd_mqtt_client.c_nxd_mqtt_process_disconnect()into_nxd_mqtt_client_connection_end(), so every teardown path resets it._nxd_mqtt_process_disconnect()calls_nxd_mqtt_client_connection_end(), so the clean-disconnect behavior is unchanged._nxd_mqtt_client_connect(), the only teardown that does not go through_nxd_mqtt_client_connection_end(). Also clearnxd_mqtt_client_use_tlsthere: that branch has the same stale-flag problem that Fix MQTT client: reset use_tls flag in connection_end (TLS to plain reconnect) #400 fixes in_nxd_mqtt_client_connection_end(), on a path Fix MQTT client: reset use_tls flag in connection_end (TLS to plain reconnect) #400 does not cover. (The two PRs touch different hunks and merge independently.)Behavioral note: credentials were already cleared on every clean disconnect by
_nxd_mqtt_process_disconnect(), so the effective contract is already "calllogin_set()before each connect that needs credentials". This change only extends the existing cleanup to the failure paths.Test plan
CONNECTwith the username flag and no username, and is dropped by the broker as a protocol error_nxd_mqtt_process_disconnect,_nxd_mqtt_process_connackerror path, TCP/TLS establish-process failures, and the early TCP-failure branch of_nxd_mqtt_client_connectall reset the credentials with this change