mirror of
https://gitee.com/fit2cloud-feizhiyun/1Panel.git
synced 2026-11-20 07:49:30 +08:00
fix: Fix orderby dependency injection issue (#11858)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user