brutella / hc

hc is a lightweight framework to develop HomeKit accessories in Go.
Apache License 2.0
1.74k stars 189 forks source link

NewIPTransport add validation #195

Closed xxandev closed 1 year ago

xxandev commented 3 years ago

(a-z, A-Z, 0-9, \s, -) if other characters are used, the accessory is started but cannot be added

Сurrent:

func NewIPTransport(config Config, a *accessory.Accessory, as ...*accessory.Accessory) (*ipTransport, error) {
    name := a.Info.Name.GetValue()
    if len(name) == 0 {
        log.Info.Panic("Invalid empty name for first accessory")
    }
    cfg := defaultConfig(name)
...

Desired:

func NewIPTransport(config Config, a *accessory.Accessory, as ...*accessory.Accessory) (*ipTransport, error) {
    name := a.Info.Name.GetValue()
    if len(name) == 0 {
        return nil, errors.New("accessory name cannot be empty")
    }
    validName := regexp.MustCompile(`^[a-zA-Z0-9\s\-]+$`).MatchString
    if validName(name) {
        return nil, errors.New("invalid accessory name, should only contain (a-z, A-Z, 0-9, \\s, -)")
    }
    cfg := defaultConfig(name)
...
brutella commented 3 years ago

I've just tried to add an accessory with the name Brücke which contains the umlaut ö. The bridge shows up as Brucke because RemoveAccentsFromString() does its job to convert ö to o. I was able to add it to HomeKit with no problems.

xxandev commented 3 years ago

doesn't add, in my home accessory.Info{Name: "Люстра", SerialNumber: "test1sw"} accessory.Info{Name: "#test", SerialNumber: "test2sw"} accessory.Info{Name: "测试", SerialNumber: "test3sw"} accessory.Info{Name: "@send mess", SerialNumber: "test4sw"} accessory.Info{Name: "switch 21/1", SerialNumber: "test5sw"}

https://github.com/alpr777/homekit/tree/main/example/switch

joeshaw commented 3 years ago

Related: #192