From d3371a724069658774e57e470c83c0d3987d2a8d Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 17 Aug 2024 22:25:11 +0800 Subject: [PATCH] :bug: Validate alias --- pkg/internal/services/posts.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 34b4069..477369e 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -3,6 +3,7 @@ package services import ( "errors" "fmt" + "regexp" "strconv" "time" @@ -270,6 +271,13 @@ func EnsurePostCategoriesAndTags(item models.Post) (models.Post, error) { } func NewPost(user models.Account, item models.Post) (models.Post, error) { + if item.Alias != nil { + re := regexp.MustCompile(`^[a-z0-9.-]+$`) + if !re.MatchString(*item.Alias) { + return item, fmt.Errorf("invalid post alias, learn more about alias rule on our wiki") + } + } + if item.Realm != nil { item.AreaAlias = &item.Realm.Alias } else { @@ -325,6 +333,13 @@ func NewPost(user models.Account, item models.Post) (models.Post, error) { } func EditPost(item models.Post) (models.Post, error) { + if item.Alias != nil { + re := regexp.MustCompile(`^[a-z0-9.-]+$`) + if !re.MatchString(*item.Alias) { + return item, fmt.Errorf("invalid post alias, learn more about alias rule on our wiki") + } + } + if item.Realm != nil { item.AreaAlias = &item.Realm.Alias } else {