fix: Fix orderby dependency injection issue (#11858)

This commit is contained in:
ssongliu
2026-02-10 18:35:06 +08:00
committed by GitHub
parent a95d9aa27a
commit 32bc8d922b
5 changed files with 48 additions and 11 deletions
+3 -5
View File
@@ -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 {
+2 -6
View File
@@ -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)
}
+7
View File
@@ -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"
+2
View File
@@ -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()
+34
View File
@@ -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)
}