Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

fix(chat): enforce reserved username matching

Gabe Kangase706360c6090
diff --git a/services/chat/events.go b/services/chat/events.go
index 080b7e62d6..81ba26a4bb 100644
--- a/services/chat/events.go
+++ b/services/chat/events.go
@@ -43,20 +43,16 @@ func (s *Service) userNameChanged(eventData chatClientEvent) {
return
}
- for _, blockedName := range blocklist {
- normalizedName := strings.TrimSpace(blockedName)
- normalizedName = strings.ToLower(normalizedName)
- if strings.Contains(normalizedName, proposedUsername) {
- // Denied.
- log.Debugln(logSanitize(eventData.client.User.DisplayName), "blocked from changing name to", logSanitize(proposedUsername), "due to blocked name", normalizedName)
- message := fmt.Sprintf("You cannot change your name to **%s**.", proposedUsername)
- s.sendActionToClient(eventData.client, message)
-
- // Resend the client's user so their username is in sync.
- eventData.client.sendConnectedClientInfo()
+ if blockedName, forbidden := ForbiddenUsername(proposedUsername, blocklist); forbidden {
+ // Denied.
+ log.Debugln(logSanitize(eventData.client.User.DisplayName), "blocked from changing name to", logSanitize(proposedUsername), "due to blocked name", blockedName)
+ message := fmt.Sprintf("You cannot change your name to **%s**.", proposedUsername)
+ s.sendActionToClient(eventData.client, message)
- return
- }
+ // Resend the client's user so their username is in sync.
+ eventData.client.sendConnectedClientInfo()
+
+ return
}
// Check if the name is not already assigned to a registered user.
diff --git a/services/chat/username.go b/services/chat/username.go
new file mode 100644
index 0000000000..aad16fc479
--- /dev/null
+++ b/services/chat/username.go
@@ -0,0 +1,16 @@
+package chat
+
+import "strings"
+
+// ForbiddenUsername reports whether a proposed display name contains one of
+// the configured reserved names, case-insensitively.
+func ForbiddenUsername(proposed string, blocklist []string) (string, bool) {
+ proposed = strings.ToLower(strings.TrimSpace(proposed))
+ for _, blocked := range blocklist {
+ blocked = strings.ToLower(strings.TrimSpace(blocked))
+ if blocked != "" && strings.Contains(proposed, blocked) {
+ return blocked, true
+ }
+ }
+ return "", false
+}
diff --git a/services/chat/username_test.go b/services/chat/username_test.go
new file mode 100644
index 0000000000..e4f24998b9
--- /dev/null
+++ b/services/chat/username_test.go
@@ -0,0 +1,17 @@
+package chat
+
+import "testing"
+
+func TestForbiddenUsernameMatchesReservedNameInsideCandidate(t *testing.T) {
+ for _, name := range []string{"owncast", "OwnCast_Admin", "SYSTEM-status"} {
+ if blocked, ok := ForbiddenUsername(name, []string{"owncast", "admin", "system"}); !ok || blocked == "" {
+ t.Errorf("ForbiddenUsername(%q) = %q, %t; want a blocklist match", name, blocked, ok)
+ }
+ }
+
+ for _, name := range []string{"own", "moderator", "viewer"} {
+ if blocked, ok := ForbiddenUsername(name, []string{"owncast", "admin", "system"}); ok {
+ t.Errorf("ForbiddenUsername(%q) = %q, true; want allowed", name, blocked)
+ }
+ }
+}
diff --git a/webserver/handlers/chat.go b/webserver/handlers/chat.go
index 744698a74c..22c9edbe11 100644
--- a/webserver/handlers/chat.go
+++ b/webserver/handlers/chat.go
@@ -8,6 +8,7 @@ import (
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/models"
+ "github.com/owncast/owncast/services/chat"
"github.com/owncast/owncast/utils"
"github.com/owncast/owncast/webserver/handlers/generated"
"github.com/owncast/owncast/webserver/router/middleware"
@@ -80,8 +81,10 @@ func (h *Handlers) RegisterAnonymousChatUser(w http.ResponseWriter, r *http.Requ
// Sanitize before the empty check so a proposed name that sanitizes to
// nothing (e.g. only HTML tags or whitespace) still falls back to a
- // generated name instead of failing registration.
proposedNewDisplayName = utils.MakeSafeStringOfLength(proposedNewDisplayName, config.MaxChatDisplayNameLength)
+ if _, forbidden := chat.ForbiddenUsername(proposedNewDisplayName, h.configRepository.GetForbiddenUsernameList()); forbidden {
+ proposedNewDisplayName = ""
+ }
if proposedNewDisplayName == "" {
proposedNewDisplayName = utils.MakeSafeStringOfLength(h.generateDisplayName(), config.MaxChatDisplayNameLength)
}