fhmq / hmq

High performance mqtt broker
Apache License 2.0
1.33k stars 276 forks source link

hmq does not close connection when receiving invalid utf-8 #104

Open stapelberg opened 3 years ago

stapelberg commented 3 years ago

Steps to reproduce:

  1. Run hmq
  2. Run mosquitto_sub -t '#'
  3. Run this Go program:
    
    package main

import ( "fmt" "log"

mqtt "github.com/eclipse/paho.mqtt.golang"

)

func publishInvalid() error { opts := mqtt.NewClientOptions().AddBroker("tcp://localhost:1883") opts.SetClientID("dhcp4d") mqttClient := mqtt.NewClient(opts) if token := mqttClient.Connect(); token.Wait() && token.Error() != nil { return fmt.Errorf("MQTT connection failed: %v", token.Error()) }

token := mqttClient.Publish("foo/valid", 0, true /* retained */, []byte("payload"))
token.Wait()

token = mqttClient.Publish("foo/invalid/\xec\x8c\x04\x10\x27", 0, true /* retained */, []byte("payload"))
token.Wait()

return nil

}

func main() { if err := publishInvalid(); err != nil { log.Fatal(err) } }


Now, `mosquitto_sub` is in an infinite reconnect-loop, and hmq will print:

{"level":"info","timestamp":"2021-01-07T22:45:12.810+0100","logger":"broker","caller":"broker/broker.go:190","msg":"Start Listening client on ","hp":"0.0.0.0:1883"} {"level":"info","timestamp":"2021-01-07T22:45:13.853+0100","logger":"broker","caller":"broker/broker.go:269","msg":"read connect from ","clientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"info","timestamp":"2021-01-07T22:45:17.454+0100","logger":"broker","caller":"broker/broker.go:269","msg":"read connect from ","clientID":"dhcp4d"} {"level":"error","timestamp":"2021-01-07T22:45:17.454+0100","logger":"broker","caller":"broker/client.go:170","msg":"read packet error: ","error":"EOF","ClientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"error","timestamp":"2021-01-07T22:45:17.454+0100","logger":"broker","caller":"broker/client.go:170","msg":"read packet error: ","error":"EOF","ClientID":"dhcp4d"} {"level":"info","timestamp":"2021-01-07T22:45:18.455+0100","logger":"broker","caller":"broker/broker.go:269","msg":"read connect from ","clientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"info","timestamp":"2021-01-07T22:45:18.455+0100","logger":"broker","caller":"broker/client.go:514","msg":"process retain message: ","packet":"SUBSCRIBE: dup: false qos: 1 retain: false rLength: 6 MessageID: 2 topics: [#]","ClientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"info","timestamp":"2021-01-07T22:45:18.455+0100","logger":"broker","caller":"broker/client.go:514","msg":"process retain message: ","packet":"SUBSCRIBE: dup: false qos: 1 retain: false rLength: 6 MessageID: 2 topics: [#]","ClientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"error","timestamp":"2021-01-07T22:45:18.455+0100","logger":"broker","caller":"broker/client.go:170","msg":"read packet error: ","error":"EOF","ClientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"info","timestamp":"2021-01-07T22:45:19.457+0100","logger":"broker","caller":"broker/broker.go:269","msg":"read connect from ","clientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"error","timestamp":"2021-01-07T22:45:19.457+0100","logger":"broker","caller":"broker/client.go:170","msg":"read packet error: ","error":"EOF","ClientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"info","timestamp":"2021-01-07T22:45:19.457+0100","logger":"broker","caller":"broker/client.go:514","msg":"process retain message: ","packet":"SUBSCRIBE: dup: false qos: 1 retain: false rLength: 6 MessageID: 3 topics: [#]","ClientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"info","timestamp":"2021-01-07T22:45:19.457+0100","logger":"broker","caller":"broker/client.go:514","msg":"process retain message: ","packet":"SUBSCRIBE: dup: false qos: 1 retain: false rLength: 6 MessageID: 3 topics: [#]","ClientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"info","timestamp":"2021-01-07T22:45:20.458+0100","logger":"broker","caller":"broker/broker.go:269","msg":"read connect from ","clientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"info","timestamp":"2021-01-07T22:45:20.458+0100","logger":"broker","caller":"broker/client.go:514","msg":"process retain message: ","packet":"SUBSCRIBE: dup: false qos: 1 retain: false rLength: 6 MessageID: 4 topics: [#]","ClientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"info","timestamp":"2021-01-07T22:45:20.458+0100","logger":"broker","caller":"broker/client.go:514","msg":"process retain message: ","packet":"SUBSCRIBE: dup: false qos: 1 retain: false rLength: 6 MessageID: 4 topics: [#]","ClientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"error","timestamp":"2021-01-07T22:45:20.458+0100","logger":"broker","caller":"broker/client.go:170","msg":"read packet error: ","error":"EOF","ClientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"info","timestamp":"2021-01-07T22:45:21.459+0100","logger":"broker","caller":"broker/broker.go:269","msg":"read connect from ","clientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"info","timestamp":"2021-01-07T22:45:21.459+0100","logger":"broker","caller":"broker/client.go:514","msg":"process retain message: ","packet":"SUBSCRIBE: dup: false qos: 1 retain: false rLength: 6 MessageID: 5 topics: [#]","ClientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"info","timestamp":"2021-01-07T22:45:21.459+0100","logger":"broker","caller":"broker/client.go:514","msg":"process retain message: ","packet":"SUBSCRIBE: dup: false qos: 1 retain: false rLength: 6 MessageID: 5 topics: [#]","ClientID":"mosq-jauBjjAjwJcc1Sy865"} {"level":"error","timestamp":"2021-01-07T22:45:21.459+0100","logger":"broker","caller":"broker/client.go:170","msg":"read packet error: ","error":"EOF","ClientID":"mosq-jauBjjAjwJcc1Sy865"} ^C2021/01/07 22:45:22 signal received, broker closed. interrupt



As per https://github.com/mqttjs/mqtt-packet/issues/59, hmq should instead verify that topic names are valid utf-8, and close the connection when a client sends invalid utf-8.
lucasvmx commented 3 years ago

@chowyu08 this issue can be closed

lucasvmx commented 2 years ago

@chowyu08 this issue can be closed