Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Validate wall slugs on server

Anand Baburajanc54aae4fa089
diff --git a/server/wall/repo/common.go b/server/wall/repo/common.go
index 1bd2c2bd8a..e4eac20c6d 100644
--- a/server/wall/repo/common.go
+++ b/server/wall/repo/common.go
@@ -2,12 +2,20 @@ package repo
import (
"database/sql"
+ "regexp"
"strings"
"github.com/ente-io/museum/ente"
"github.com/ente-io/stacktrace"
)
+const (
+ minWallSlugLength = 3
+ maxWallSlugLength = 30
+)
+
+var wallSlugPattern = regexp.MustCompile(`^[a-z0-9][a-z0-9._-]*$`)
+
func normalizeSlug(input string) string {
return strings.ToLower(strings.TrimSpace(input))
}
@@ -147,6 +155,12 @@ func validateWallSlug(input string) (string, error) {
if slug == "" {
return "", ente.NewBadRequestWithMessage("wallSlug is required")
}
+ if len(slug) < minWallSlugLength || len(slug) > maxWallSlugLength {
+ return "", ente.NewBadRequestWithMessage("wallSlug must be 3-30 characters")
+ }
+ if !wallSlugPattern.MatchString(slug) {
+ return "", ente.NewBadRequestWithMessage("wallSlug can only contain lowercase letters, numbers, dots, dashes, or underscores, and must start with a letter or number")
+ }
if isReservedWallSlug(slug) {
return "", ente.NewBadRequestWithMessage("wallSlug is reserved")
}
diff --git a/server/wall/repo/common_test.go b/server/wall/repo/common_test.go
new file mode 100644
index 0000000000..df1245b4ca
--- /dev/null
+++ b/server/wall/repo/common_test.go
@@ -0,0 +1,53 @@
+package repo
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestValidateWallSlugMatchesClientRules(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ input string
+ normalized string
+ }{
+ {name: "letters", input: "alice", normalized: "alice"},
+ {name: "numbers", input: "user123", normalized: "user123"},
+ {name: "allowed separators", input: "my.wall-name_1", normalized: "my.wall-name_1"},
+ {name: "normalizes case and spaces", input: " Alice_123 ", normalized: "alice_123"},
+ {name: "thirty chars", input: strings.Repeat("a", 30), normalized: strings.Repeat("a", 30)},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ normalized, err := ValidateWallSlug(tc.input)
+ require.NoError(t, err)
+ require.Equal(t, tc.normalized, normalized)
+ })
+ }
+}
+
+func TestValidateWallSlugRejectsInvalidClientSlugs(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ input string
+ message string
+ }{
+ {name: "empty", input: " ", message: "wallSlug is required"},
+ {name: "too short", input: "ab", message: "wallSlug must be 3-30 characters"},
+ {name: "too long", input: strings.Repeat("a", 31), message: "wallSlug must be 3-30 characters"},
+ {name: "leading dot", input: ".alice", message: "wallSlug can only contain"},
+ {name: "leading dash", input: "-alice", message: "wallSlug can only contain"},
+ {name: "leading underscore", input: "_alice", message: "wallSlug can only contain"},
+ {name: "space", input: "ali ce", message: "wallSlug can only contain"},
+ {name: "slash", input: "ali/ce", message: "wallSlug can only contain"},
+ {name: "control character", input: "ali\nce", message: "wallSlug can only contain"},
+ {name: "unicode confusable", input: "paypa\u217C", message: "wallSlug can only contain"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ _, err := ValidateWallSlug(tc.input)
+ require.Error(t, err)
+ require.Contains(t, err.Error(), tc.message)
+ })
+ }
+}