Validating channel name when connecting.

This commit is contained in:
claudemiro
2015-05-17 09:37:51 -03:00
parent 485bc01abe
commit e2d07e9760
3 changed files with 38 additions and 0 deletions
+5
View File
@@ -118,6 +118,11 @@ func onMessage(conn *websocket.Conn, w http.ResponseWriter, r *http.Request, ses
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-")
isPrivate := strings.HasPrefix(channelName, "private-")
+15
View File
@@ -10,6 +10,7 @@ import (
"encoding/hex"
"fmt"
"math/rand"
"regexp"
"time"
)
@@ -29,3 +30,17 @@ func GenerateSessionID() string {
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
View File
@@ -18,3 +18,21 @@ func TestGenerateSession(t *testing.T) {
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")
}
}