Skip to content

Commit

Permalink
fix: check tags
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Oct 5, 2024
1 parent 5f5a514 commit 69c70de
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
6 changes: 1 addition & 5 deletions internal/collections/infra/mysql_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,7 @@ func (r mysqlRepo) reCountSubjectTags(ctx context.Context, tx *query.Query,
var countMap = make(map[string]uint32)

for _, tag := range tagList {
if len(tag.Tag.Name) == 0 {
continue
}

if dam.ZeroWithPattern.MatchString(tag.Tag.Name) {
if !dam.ValidateTag(tag.Tag.Name) {
continue
}

Expand Down
19 changes: 18 additions & 1 deletion internal/pkg/dam/dam.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,21 @@ func AllPrintableChar(text string) bool {
return true
}

var ZeroWithPattern = regexp.MustCompile(`\p{Cf}|\p{Cc}|\p{Co}`)
var ZeroWidthPattern = regexp.MustCompile(`\p{Cf}|\p{Cc}|\p{Co}`)
var ExtraSpacePattern = regexp.MustCompile("[\u3000 ]")

func ValidateTag(t string) bool {
if len(t) == 0 {
return false
}

if !AllPrintableChar(t) {
return false
}

if ExtraSpacePattern.MatchString(t) {
return false
}

return true
}
15 changes: 4 additions & 11 deletions web/req/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
package req

import (
"fmt"
"strings"
"unicode/utf8"

"github.com/samber/lo"

"github.com/bangumi/server/internal/collections/domain/collection"
"github.com/bangumi/server/internal/pkg/dam"
"github.com/bangumi/server/internal/pkg/null"
Expand Down Expand Up @@ -47,15 +46,9 @@ func (v *SubjectEpisodeCollectionPatch) Validate() error {
}
}

if len(v.Tags) > 0 {
if !lo.EveryBy(v.Tags, dam.AllPrintableChar) {
return res.BadRequest("invisible character are included in tags")
}

if lo.ContainsBy(v.Tags, func(item string) bool {
return len(item) == 0
}) {
return res.BadRequest("zero length tags are included in tags")
for _, tag := range v.Tags {
if !dam.ValidateTag(tag) {
return res.BadRequest(fmt.Sprintf("invalid tag: %q", tag))
}
}

Expand Down

0 comments on commit 69c70de

Please sign in to comment.