mirror of
https://github.com/go-gitea/gitea
synced 2025-07-09 20:17:21 +00:00
Validate hex colors when creating/editing labels (#34623)
Resolves #34618. --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@ -7,10 +7,10 @@ import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
"sync"
|
||||
|
||||
// colorPattern is a regexp which can validate label color
|
||||
var colorPattern = regexp.MustCompile("^#?(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})$")
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
// Label represents label information loaded from template
|
||||
type Label struct {
|
||||
@ -21,6 +21,10 @@ type Label struct {
|
||||
ExclusiveOrder int `yaml:"exclusive_order,omitempty"`
|
||||
}
|
||||
|
||||
var colorPattern = sync.OnceValue(func() *regexp.Regexp {
|
||||
return regexp.MustCompile(`^#([\da-fA-F]{3}|[\da-fA-F]{6})$`)
|
||||
})
|
||||
|
||||
// NormalizeColor normalizes a color string to a 6-character hex code
|
||||
func NormalizeColor(color string) (string, error) {
|
||||
// normalize case
|
||||
@ -31,8 +35,8 @@ func NormalizeColor(color string) (string, error) {
|
||||
color = "#" + color
|
||||
}
|
||||
|
||||
if !colorPattern.MatchString(color) {
|
||||
return "", fmt.Errorf("bad color code: %s", color)
|
||||
if !colorPattern().MatchString(color) {
|
||||
return "", util.NewInvalidArgumentErrorf("invalid color: %s", color)
|
||||
}
|
||||
|
||||
// convert 3-character shorthand into 6-character version
|
||||
|
Reference in New Issue
Block a user