Skip to content

Commit 850f082

Browse files
authored
fix: Don't allow publishing wildcards in MQTT topic (#27025)
1 parent 17adc44 commit 850f082

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/mqtt.ts

+6
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,12 @@ export default class MQTT {
176176
skipLog = false,
177177
skipReceive = true,
178178
): Promise<void> {
179+
if (topic.includes('+') || topic.includes('#')) {
180+
// https://github.com/Koenkk/zigbee2mqtt/issues/26939#issuecomment-2772309646
181+
logger.error(`Topic '${topic}' includes wildcard characters, skipping publish.`);
182+
return;
183+
}
184+
179185
const defaultOptions = {qos: 0 as const, retain: false};
180186
topic = `${base}/${topic}`;
181187

test/controller.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,20 @@ describe('Controller', () => {
283283
controller.mqtt.client.reconnecting = false;
284284
});
285285

286+
it('Should not allow publishing wildcard characters in topic', async () => {
287+
await controller.start();
288+
await flushPromises();
289+
mockMQTTPublishAsync.mockClear();
290+
// @ts-expect-error private
291+
await controller.mqtt.publish('z2m/#/status', 'empty');
292+
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(0);
293+
expect(mockLogger.error).toHaveBeenCalledWith(`Topic 'z2m/#/status' includes wildcard characters, skipping publish.`);
294+
// @ts-expect-error private
295+
await controller.mqtt.publish('z2m/+/status', 'empty');
296+
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(0);
297+
expect(mockLogger.error).toHaveBeenCalledWith(`Topic 'z2m/+/status' includes wildcard characters, skipping publish.`);
298+
});
299+
286300
it('Load empty state when state file does not exist', async () => {
287301
data.removeState();
288302
await controller.start();

0 commit comments

Comments
 (0)