Skip to content

armgit / ente

Public Imported from GitHub
0

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

Add reserved wall slug validation

Anand Baburajan16b8e91eb779
diff --git a/server/wall/controller/friends_test.go b/server/wall/controller/friends_test.go
index 43a4f1ba99..ee5d6d2d1d 100644
--- a/server/wall/controller/friends_test.go
+++ b/server/wall/controller/friends_test.go
@@ -19,12 +19,12 @@ func TestFriendRelationshipReportsSelfFriendAndEmpty(t *testing.T) {
friends, repos, ctx := setupFriendsControllerTest(t)
aliceID := insertWallControllerUser(t, repos, "[email protected]", "alice-public")
bobID := insertWallControllerUser(t, repos, "[email protected]", "bob-public")
- devID := insertWallControllerUser(t, repos, "[email protected]", "dev-public")
+ charlieID := insertWallControllerUser(t, repos, "[email protected]", "charlie-public")
aliceWall, err := repos.Walls.CreateWall(ctx, aliceID, "alice", "alice-wall-key", "alice-profile")
require.NoError(t, err)
bobWall, err := repos.Walls.CreateWall(ctx, bobID, "bob", "bob-wall-key", "bob-profile")
require.NoError(t, err)
- devWall, err := repos.Walls.CreateWall(ctx, devID, "dev", "dev-wall-key", "dev-profile")
+ charlieWall, err := repos.Walls.CreateWall(ctx, charlieID, "charlie", "charlie-wall-key", "charlie-profile")
require.NoError(t, err)
require.NoError(t, repos.Friends.AddFriend(ctx, aliceID, aliceWall.WallID, bobWall.WallID, "bob-share-key", bobWall.CurrentVersion, "alice-share-key", aliceWall.CurrentVersion))
@@ -36,7 +36,7 @@ func TestFriendRelationshipReportsSelfFriendAndEmpty(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "friend", resp.Relationship)
- resp, err = friends.Relationship(newWallControllerContext(aliceID), models.FriendRelationshipRequest{TargetWallID: devWall.WallID})
+ resp, err = friends.Relationship(newWallControllerContext(aliceID), models.FriendRelationshipRequest{TargetWallID: charlieWall.WallID})
require.NoError(t, err)
require.Empty(t, resp.Relationship)
}
diff --git a/server/wall/repo/common.go b/server/wall/repo/common.go
index da3a1a1bee..f9275c4318 100644
--- a/server/wall/repo/common.go
+++ b/server/wall/repo/common.go
@@ -12,6 +12,161 @@ func normalizeSlug(input string) string {
return strings.ToLower(strings.TrimSpace(input))
}
+var reservedWallSlugs = map[string]struct{}{
+ "about": {},
+ "abuse": {},
+ "account": {},
+ "accounts": {},
+ "add": {},
+ "admin": {},
+ "administrator": {},
+ "admins": {},
+ "all": {},
+ "anonymous": {},
+ "api": {},
+ "app": {},
+ "apps": {},
+ "asset": {},
+ "assets": {},
+ "auth": {},
+ "authentication": {},
+ "avatar": {},
+ "blog": {},
+ "cdn": {},
+ "contact": {},
+ "create": {},
+ "css": {},
+ "dashboard": {},
+ "delete": {},
+ "deleted": {},
+ "dev": {},
+ "developer": {},
+ "developers": {},
+ "docs": {},
+ "edit": {},
+ "email": {},
+ "ente": {},
+ "enteadmin": {},
+ "entecom": {},
+ "entegg": {},
+ "enteio": {},
+ "entesupport": {},
+ "enterprise": {},
+ "faq": {},
+ "favicon": {},
+ "feed": {},
+ "feedback": {},
+ "friend": {},
+ "friends": {},
+ "ftp": {},
+ "guest": {},
+ "help": {},
+ "home": {},
+ "hostmaster": {},
+ "html": {},
+ "image": {},
+ "images": {},
+ "imap": {},
+ "img": {},
+ "invite": {},
+ "join": {},
+ "js": {},
+ "json": {},
+ "legal": {},
+ "like": {},
+ "likes": {},
+ "link": {},
+ "links": {},
+ "login": {},
+ "logout": {},
+ "mail": {},
+ "mailer": {},
+ "me": {},
+ "mod": {},
+ "moderator": {},
+ "museum": {},
+ "new": {},
+ "no-reply": {},
+ "no_reply": {},
+ "noreply": {},
+ "notifications": {},
+ "null": {},
+ "official": {},
+ "owner": {},
+ "passkeys": {},
+ "pop": {},
+ "post": {},
+ "postmaster": {},
+ "posts": {},
+ "privacy": {},
+ "private": {},
+ "profile": {},
+ "public": {},
+ "recover": {},
+ "register": {},
+ "registration": {},
+ "remove": {},
+ "robots": {},
+ "root": {},
+ "search": {},
+ "security": {},
+ "self": {},
+ "settings": {},
+ "setup-profile": {},
+ "setup_profile": {},
+ "share": {},
+ "shares": {},
+ "signin": {},
+ "signout": {},
+ "signup": {},
+ "sitemap": {},
+ "smtp": {},
+ "ssh": {},
+ "staff": {},
+ "static": {},
+ "status": {},
+ "support": {},
+ "system": {},
+ "team": {},
+ "terms": {},
+ "tos": {},
+ "two-factor": {},
+ "two_factor": {},
+ "undefined": {},
+ "unknown": {},
+ "update": {},
+ "upload": {},
+ "uploads": {},
+ "user": {},
+ "username": {},
+ "users": {},
+ "verify": {},
+ "view": {},
+ "wall": {},
+ "web": {},
+ "webmaster": {},
+ "www": {},
+ "xml": {},
+}
+
+func validateWallSlug(input string) (string, error) {
+ slug := normalizeSlug(input)
+ if slug == "" {
+ return "", ente.NewBadRequestWithMessage("wallSlug is required")
+ }
+ if isReservedWallSlug(slug) {
+ return "", ente.NewBadRequestWithMessage("wallSlug is reserved")
+ }
+ return slug, nil
+}
+
+func isReservedWallSlug(slug string) bool {
+ if _, ok := reservedWallSlugs[slug]; ok {
+ return true
+ }
+ return strings.HasPrefix(slug, "ente-") || strings.HasPrefix(slug, "ente_")
+}
+
func nullString(value string) sql.NullString {
value = strings.TrimSpace(value)
if value == "" {
diff --git a/server/wall/repo/module_test.go b/server/wall/repo/module_test.go
index abadbe6c80..3a0cf82d02 100644
--- a/server/wall/repo/module_test.go
+++ b/server/wall/repo/module_test.go
@@ -38,6 +38,34 @@ func insertWallUser(t *testing.T, module *Module, email string, publicKey string
return userID
}
+func TestCreateWallRejectsReservedSlugs(t *testing.T) {
+ ctx := context.Background()
+ module := newWallTestModule(t)
+ userID := insertWallUser(t, module, "[email protected]", "reserved-public")
+
+ for _, slug := range []string{"admin", " EnteCom ", "ente_com", "ente-com", "ente_gg", "ente-photos", "ente_social", "entegg", "images", "two-factor"} {
+ _, err := module.Walls.CreateWall(ctx, userID, slug, "wall-key", "profile")
+ require.Error(t, err)
+ require.Contains(t, err.Error(), "wallSlug is reserved")
+ }
+}
+
+func TestUpdateSlugRejectsReservedSlug(t *testing.T) {
+ ctx := context.Background()
+ module := newWallTestModule(t)
+ userID := insertWallUser(t, module, "[email protected]", "rename-public")
+ wall, err := module.Walls.CreateWall(ctx, userID, "rename-user", "wall-key", "profile")
+ require.NoError(t, err)
+
+ _, err = module.Walls.UpdateSlug(ctx, userID, wall.WallID, "support")
+ require.Error(t, err)
+ require.Contains(t, err.Error(), "wallSlug is reserved")
+
+ unchanged, err := module.Walls.GetWallByID(ctx, wall.WallID)
+ require.NoError(t, err)
+ require.Equal(t, "rename-user", unchanged.WallSlug)
+}
+
func TestWallModuleLifecycle(t *testing.T) {
ctx := context.Background()
module := newWallTestModule(t)
@@ -742,19 +770,19 @@ func TestListPostLikersPaginates(t *testing.T) {
aliceID := insertWallUser(t, module, "[email protected]", "alice-public")
bobID := insertWallUser(t, module, "[email protected]", "bob-public")
- devID := insertWallUser(t, module, "[email protected]", "dev-public")
+ charlieID := insertWallUser(t, module, "[email protected]", "charlie-public")
aliceWall, err := module.Walls.CreateWall(ctx, aliceID, "alice", "alice-wall-key", "alice-profile")
require.NoError(t, err)
bobWall, err := module.Walls.CreateWall(ctx, bobID, "bob", "bob-wall-key", "bob-profile")
require.NoError(t, err)
- devWall, err := module.Walls.CreateWall(ctx, devID, "dev", "dev-wall-key", "dev-profile")
+ charlieWall, err := module.Walls.CreateWall(ctx, charlieID, "charlie", "charlie-wall-key", "charlie-profile")
require.NoError(t, err)
postID, err := module.Posts.CreatePost(ctx, aliceID, aliceWall.WallID, "post-key", nil, aliceWall.CurrentVersion, nil)
require.NoError(t, err)
require.NoError(t, module.Posts.SetLike(ctx, postID, bobID, true))
- require.NoError(t, module.Posts.SetLike(ctx, postID, devID, true))
+ require.NoError(t, module.Posts.SetLike(ctx, postID, charlieID, true))
setPostLikeCreatedAt(t, module, 3000, postID, bobID)
- setPostLikeCreatedAt(t, module, 2000, postID, devID)
+ setPostLikeCreatedAt(t, module, 2000, postID, charlieID)
page, nextCursor, err := module.Posts.ListPostLikers(ctx, postID, "", 1)
require.NoError(t, err)
@@ -766,8 +794,8 @@ func TestListPostLikersPaginates(t *testing.T) {
page, nextCursor, err = module.Posts.ListPostLikers(ctx, postID, nextCursor, 1)
require.NoError(t, err)
require.Len(t, page, 1)
- require.Equal(t, devID, page[0].Actor.UserID)
- require.Equal(t, devWall.WallID, page[0].Actor.WallID)
+ require.Equal(t, charlieID, page[0].Actor.UserID)
+ require.Equal(t, charlieWall.WallID, page[0].Actor.WallID)
require.Empty(t, nextCursor)
}
diff --git a/server/wall/repo/walls.go b/server/wall/repo/walls.go
index 3ef61a08d8..3e394769b8 100644
--- a/server/wall/repo/walls.go
+++ b/server/wall/repo/walls.go
@@ -10,6 +10,10 @@ import (
)
func (r *WallsRepository) CreateWall(ctx context.Context, ownerID int64, wallSlug, encryptedWallKey, encryptedProfile string) (*WallRecord, error) {
+ normalizedWallSlug, err := validateWallSlug(wallSlug)
+ if err != nil {
+ return nil, err
+ }
wallID := base.MustNewID("wal")
tx, err := r.DB.BeginTx(ctx, nil)
if err != nil {
@@ -20,7 +24,7 @@ func (r *WallsRepository) CreateWall(ctx context.Context, ownerID int64, wallSlu
if _, err := tx.ExecContext(ctx, `
INSERT INTO walls (wall_id, owner_id, wall_slug, encrypted_wall_key, encrypted_profile, current_version)
VALUES ($1, $2, $3, $4, $5, 1)
- `, wallID, ownerID, normalizeSlug(wallSlug), encryptedWallKey, encryptedProfile); err != nil {
+ `, wallID, ownerID, normalizedWallSlug, encryptedWallKey, encryptedProfile); err != nil {
return nil, wrapUnique(err, "wall already exists")
}
if _, err := tx.ExecContext(ctx, `
@@ -172,13 +176,17 @@ func (r *WallsRepository) UpdateProfile(ctx context.Context, ownerID int64, wall
}
func (r *WallsRepository) UpdateSlug(ctx context.Context, ownerID int64, wallID, wallSlug string) (*WallRecord, error) {
+ normalizedWallSlug, err := validateWallSlug(wallSlug)
+ if err != nil {
+ return nil, err
+ }
rec, err := scanWallRecord(r.DB.QueryRowContext(ctx, `
UPDATE walls
SET wall_slug = $1
WHERE owner_id = $2 AND wall_id = $3
RETURNING wall_id, owner_id, wall_slug, encrypted_wall_key, encrypted_profile, current_version,
avatar_object_key, avatar_bucket_id, avatar_size, created_at, updated_at
- `, normalizeSlug(wallSlug), ownerID, wallID))
+ `, normalizedWallSlug, ownerID, wallID))
if err != nil {
if strings.Contains(strings.ToLower(err.Error()), "duplicate key value") {
return nil, stacktrace.Propagate(err, "wall slug already exists")