Skip to content
Permalink
Browse files
ARTEMIS-3764 wrong CONNACK return code for MQTT5
This bug is causing tests in o.a.a.a.t.i.m.s.c.ConnectTestsWithSecurity
to fail.

This commit fixes the problem by setting the session's version earlier
in the logic handling the CONNECT packet so that the proper CONNACK
return code can be supplied to the remote client in case of
authentication failure.
  • Loading branch information
jbertram authored and clebertsuconic committed Apr 6, 2022
1 parent 8252642 commit b48037a1e683f4e926c0a6a797faf2dcfcd2df3e
Showing 2 changed files with 5 additions and 7 deletions.
@@ -58,7 +58,6 @@ public MQTTConnectionManager(MQTTSession session) {
}

void connect(MqttConnectMessage connect, String validatedUser) throws Exception {
session.setVersion(MQTTVersion.getVersion(connect.variableHeader().version()));
if (session.getVersion() == MQTTVersion.MQTT_5) {
session.getConnection().setProtocolVersion(Byte.toString(MqttVersion.MQTT_5.protocolLevel()));
String authenticationMethod = MQTTUtil.getProperty(String.class, connect.variableHeader().properties(), AUTHENTICATION_METHOD);
@@ -39,7 +39,6 @@
import io.netty.handler.codec.mqtt.MqttUnsubAckMessage;
import io.netty.handler.codec.mqtt.MqttUnsubAckPayload;
import io.netty.handler.codec.mqtt.MqttUnsubscribeMessage;
import io.netty.handler.codec.mqtt.MqttVersion;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import org.apache.activemq.artemis.api.core.ActiveMQSecurityException;
@@ -223,6 +222,7 @@ void handleAuth(MqttMessage auth) throws Exception {
}

void handleConnect(MqttConnectMessage connect) throws Exception {
session.setVersion(MQTTVersion.getVersion(connect.variableHeader().version()));
/*
* Perform authentication *before* attempting redirection because redirection may be based on the user's role.
*/
@@ -243,12 +243,11 @@ void handleConnect(MqttConnectMessage connect) throws Exception {

if (connection.getTransportConnection().getRouter() == null || !protocolManager.getRoutingHandler().route(connection, session, connect)) {
/* [MQTT-3.1.2-2] Reject unsupported clients. */
int packetVersion = connect.variableHeader().version();
if (packetVersion != MqttVersion.MQTT_3_1.protocolLevel() &&
packetVersion != MqttVersion.MQTT_3_1_1.protocolLevel() &&
packetVersion != MqttVersion.MQTT_5.protocolLevel()) {
if (session.getVersion() != MQTTVersion.MQTT_3_1 &&
session.getVersion() != MQTTVersion.MQTT_3_1_1 &&
session.getVersion() != MQTTVersion.MQTT_5) {

if (packetVersion <= MqttVersion.MQTT_3_1_1.protocolLevel()) {
if (session.getVersion().getVersion() <= MQTTVersion.MQTT_3_1_1.getVersion()) {
// See MQTT-3.1.2-2 at http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718030
sendConnack(MQTTReasonCodes.UNACCEPTABLE_PROTOCOL_VERSION_3);
} else {

0 comments on commit b48037a

Please sign in to comment.