From 32bc8d922b517ca5c7a9c9f8723edd2171adfda4 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Tue, 10 Feb 2026 18:35:06 +0800 Subject: [PATCH] fix: Fix orderby dependency injection issue (#11858) --- agent/app/repo/common.go | 8 +++----- agent/utils/re/re.go | 8 ++------ core/app/repo/common.go | 7 +++++++ core/server/server.go | 2 ++ core/utils/re/re.go | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 core/utils/re/re.go diff --git a/agent/app/repo/common.go b/agent/app/repo/common.go index 156402e2c..4f47e97ef 100644 --- a/agent/app/repo/common.go +++ b/agent/app/repo/common.go @@ -3,11 +3,11 @@ package repo import ( "context" "fmt" - "regexp" "time" "github.com/1Panel-dev/1Panel/agent/constant" "github.com/1Panel-dev/1Panel/agent/global" + "github.com/1Panel-dev/1Panel/agent/utils/re" "gorm.io/gorm" ) @@ -152,13 +152,11 @@ func WithByCreatedAt(startTime, endTime time.Time) DBOption { } } -var validColumnName = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`) - func WithOrderBy(orderStr string) DBOption { if orderStr == "createdAt" { orderStr = "created_at" } - if !validColumnName.MatchString(orderStr) { + if !re.GetRegex(re.OrderByValidationPattern).MatchString(orderStr) { orderStr = "created_at" } return func(g *gorm.DB) *gorm.DB { @@ -169,7 +167,7 @@ func WithOrderRuleBy(orderBy, order string) DBOption { if orderBy == "createdAt" { orderBy = "created_at" } - if !validColumnName.MatchString(orderBy) { + if !re.GetRegex(re.OrderByValidationPattern).MatchString(orderBy) { orderBy = "created_at" } switch order { diff --git a/agent/utils/re/re.go b/agent/utils/re/re.go index 680170fde..3c556e597 100644 --- a/agent/utils/re/re.go +++ b/agent/utils/re/re.go @@ -28,12 +28,11 @@ const ( MysqlGroupPattern = `\[*\]` AnsiEscapePattern = "\x1b\\[[0-9;?]*[A-Za-z]|\x1b=|\x1b>" RecycleBinFilePattern = `_1p_file_1p_(.+)_p_(\d+)_(\d+)` + OrderByValidationPattern = `^[a-zA-Z_][a-zA-Z0-9_]*$` ) var regexMap = make(map[string]*regexp.Regexp) -// InitRegex compiles all regex patterns and stores them in the map. -// This function should be called once at program startup. func Init() { patterns := []string{ NumberAlphaPattern, @@ -58,6 +57,7 @@ func Init() { MysqlGroupPattern, AnsiEscapePattern, RecycleBinFilePattern, + OrderByValidationPattern, } for _, pattern := range patterns { @@ -65,8 +65,6 @@ func Init() { } } -// GetRegex retrieves a compiled regex by its pattern string. -// Panics if the pattern is not found in the map. func GetRegex(pattern string) *regexp.Regexp { regex, exists := regexMap[pattern] if !exists { @@ -75,8 +73,6 @@ func GetRegex(pattern string) *regexp.Regexp { return regex } -// RegisterRegex registers a regex pattern and stores it in the map. -// This function should be called once at program startup. func RegisterRegex(pattern string) { regexMap[pattern] = regexp.MustCompile(pattern) } diff --git a/core/app/repo/common.go b/core/app/repo/common.go index aad17fe10..1c6415f6b 100644 --- a/core/app/repo/common.go +++ b/core/app/repo/common.go @@ -5,6 +5,7 @@ import ( "github.com/1Panel-dev/1Panel/core/constant" "github.com/1Panel-dev/1Panel/core/global" + "github.com/1Panel-dev/1Panel/core/utils/re" "gorm.io/gorm" ) @@ -65,6 +66,9 @@ func WithOrderBy(orderStr string) global.DBOption { if orderStr == "createdAt" { orderStr = "created_at" } + if !re.GetRegex(re.OrderByValidationPattern).MatchString(orderStr) { + orderStr = "created_at" + } return func(g *gorm.DB) *gorm.DB { return g.Order(orderStr) } @@ -74,6 +78,9 @@ func WithOrderRuleBy(orderBy, order string) global.DBOption { if orderBy == "createdAt" { orderBy = "created_at" } + if !re.GetRegex(re.OrderByValidationPattern).MatchString(orderBy) { + orderBy = "created_at" + } switch order { case constant.OrderDesc: order = "desc" diff --git a/core/server/server.go b/core/server/server.go index a4ffd1255..abb115d8a 100644 --- a/core/server/server.go +++ b/core/server/server.go @@ -32,9 +32,11 @@ import ( "github.com/1Panel-dev/1Panel/core/init/session/psession" "github.com/1Panel-dev/1Panel/core/init/validator" "github.com/1Panel-dev/1Panel/core/init/viper" + "github.com/1Panel-dev/1Panel/core/utils/re" ) func Start() { + re.Init() viper.Init() log.Init() db.Init() diff --git a/core/utils/re/re.go b/core/utils/re/re.go new file mode 100644 index 000000000..ea8db84ea --- /dev/null +++ b/core/utils/re/re.go @@ -0,0 +1,34 @@ +package re + +import ( + "fmt" + "regexp" +) + +const ( + OrderByValidationPattern = `^[a-zA-Z_][a-zA-Z0-9_]*$` +) + +var regexMap = make(map[string]*regexp.Regexp) + +func Init() { + patterns := []string{ + OrderByValidationPattern, + } + + for _, pattern := range patterns { + regexMap[pattern] = regexp.MustCompile(pattern) + } +} + +func GetRegex(pattern string) *regexp.Regexp { + regex, exists := regexMap[pattern] + if !exists { + panic(fmt.Sprintf("regex pattern not found: %s", pattern)) + } + return regex +} + +func RegisterRegex(pattern string) { + regexMap[pattern] = regexp.MustCompile(pattern) +}