File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed
Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 ( ) ;
You can’t perform that action at this time.
0 commit comments