Validating channel name when connecting.
This commit is contained in:
@@ -118,6 +118,11 @@ func onMessage(conn *websocket.Conn, w http.ResponseWriter, r *http.Request, ses
|
|||||||
|
|
||||||
channelName := strings.TrimSpace(subscribeEvent.Data.Channel)
|
channelName := strings.TrimSpace(subscribeEvent.Data.Channel)
|
||||||
|
|
||||||
|
if !utils.IsChannelNameValid(channelName) {
|
||||||
|
emitWSError(newGenericError(fmt.Sprintf("This channel name is not valid")), conn)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
isPresence := strings.HasPrefix(channelName, "presence-")
|
isPresence := strings.HasPrefix(channelName, "presence-")
|
||||||
isPrivate := strings.HasPrefix(channelName, "private-")
|
isPrivate := strings.HasPrefix(channelName, "private-")
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,3 +30,17 @@ func GenerateSessionID() string {
|
|||||||
|
|
||||||
return fmt.Sprintf("%d.%d", rand.Intn(MAX), rand.Intn(MAX))
|
return fmt.Sprintf("%d.%d", rand.Intn(MAX), rand.Intn(MAX))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsChannelNameValid(channelName string) bool {
|
||||||
|
matched, err := regexp.MatchString("^[A-Za-z0-9_\\-=@,.;]+$", channelName)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if matched {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,3 +18,21 @@ func TestGenerateSession(t *testing.T) {
|
|||||||
t.Errorf("Must match ^\\d+\\.\\d+$, value: '%s'", sessionID)
|
t.Errorf("Must match ^\\d+\\.\\d+$, value: '%s'", sessionID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsValidChannelName(t *testing.T) {
|
||||||
|
if IsChannelNameValid("#@#hhh**sasas") {
|
||||||
|
t.Errorf("Invalid Channel Name")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !IsChannelNameValid("private-hello") {
|
||||||
|
t.Errorf("Must be Valid Channel Name")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !IsChannelNameValid("presence-hello") {
|
||||||
|
t.Errorf("Must be Valid Channel Name")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !IsChannelNameValid("public") {
|
||||||
|
t.Errorf("Must be Valid Channel Name")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user