100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Fix anonymous MQTT authentication with empty passwords (#3244)
Panos Kalogeropoulosa7db73c13aa0
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java b/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.javaindex e8b6983aa..3bac95150 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java@@ -47,6 +47,7 @@ import org.openremote.container.security.TokenPrincipal;import org.openremote.manager.security.RemotingConnectionPrincipal;import org.openremote.model.protocol.mqtt.Topic;import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.util.TextUtil;/*** An {@link ActiveMQSecurityManager5} implementation that authenticates the user by either@@ -88,7 +89,7 @@ public class ActiveMQORSecurityManager implements ActiveMQSecurityManager5 {// Create a connection principal to track the remoting connection associated with this subjectprincipals.add(new RemotingConnectionPrincipal(remotingConnection));- if (password == null) {+ if (TextUtil.isNullOrEmpty(password)) {// Replicate anonymous guest userprincipals.add(new UserPrincipal(ANONYMOUS_USERNAME));principals.add(new RolePrincipal(ANONYMOUS_USERNAME));diff --git a/manager/test/src/groovy/org/openremote/manager/mqtt/ActiveMQORSecurityManagerTest.groovy b/manager/test/src/groovy/org/openremote/manager/mqtt/ActiveMQORSecurityManagerTest.groovynew file mode 100644index 000000000..f7386d1d2--- /dev/null+++ b/manager/test/src/groovy/org/openremote/manager/mqtt/ActiveMQORSecurityManagerTest.groovy@@ -0,0 +1,63 @@+/*+ * Copyright 2026, OpenRemote Inc.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * SPDX-License-Identifier: AGPL-3.0-or-later+ */+package org.openremote.manager.mqtt++import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection+import org.apache.activemq.artemis.spi.core.security.jaas.RolePrincipal+import org.apache.activemq.artemis.spi.core.security.jaas.UserPrincipal+import org.openremote.container.security.IdentityService+import org.openremote.manager.security.RemotingConnectionPrincipal+import spock.lang.Specification++import javax.security.auth.Subject+import java.util.concurrent.ExecutorService++class ActiveMQORSecurityManagerTest extends Specification {++ def "authenticates a connection with an empty password as anonymous"() {+ given:+ def brokerService = Stub(MQTTBrokerService)+ def executorService = Stub(ExecutorService)+ def identityService = Mock(IdentityService)+ def securityManager = new ActiveMQORSecurityManager(brokerService, executorService, identityService)+ Subject connectionSubject = null+ def connection = Stub(RemotingConnection) {+ getSubject() >> { connectionSubject }+ setSubject(_ as Subject) >> { Subject subject -> connectionSubject = subject }+ getRemoteAddress() >> "tcp://127.0.0.1:1883"+ getClientID() >> "862000000006159"+ }++ when:+ def subject = securityManager.authenticate("broker.example/862000000006159", "", connection, null)++ then:+ subject.is(connectionSubject)+ subject.principals.any {+ it instanceof UserPrincipal && it.name == ActiveMQORSecurityManager.ANONYMOUS_USERNAME+ }+ subject.principals.any {+ it instanceof RolePrincipal && it.name == ActiveMQORSecurityManager.ANONYMOUS_USERNAME+ }+ subject.principals.any {+ it instanceof RemotingConnectionPrincipal+ }+ 0 * identityService.authenticate(_, _, _)+ }+}