Files
apinto/utils/json_path.go
2025-12-19 18:06:10 +08:00

58 lines
1.2 KiB
Go
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package utils
import (
"fmt"
"regexp"
"strings"
)
var (
allowedPattern = regexp.MustCompile(`^\$\.[a-zA-Z0-9_.*\[\]]+$`)
maxDepth = 6
maxArrayCount = 3
maxPathLength = 100
)
func ValidateJSONPath(path string) error {
path = strings.TrimSpace(path)
// 1⃣ 长度限制
if len(path) == 0 || len(path) > maxPathLength {
return fmt.Errorf("invalid path length")
}
// 2⃣ 字符白名单
if !allowedPattern.MatchString(path) {
return fmt.Errorf("invalid characters in path")
}
// 3⃣ 必须 $. 开头
if !strings.HasPrefix(path, "$.") {
return fmt.Errorf("path must start with $.")
}
// 4⃣ 禁止递归
if strings.Contains(path, "..") {
return fmt.Errorf("recursive path not allowed")
}
// 5⃣ 深度限制
depth := strings.Count(path, ".")
if depth > maxDepth {
return fmt.Errorf("path depth exceeds limit")
}
// 6⃣ 数组节点限制
arrayCount := strings.Count(path, "[*]")
if arrayCount > maxArrayCount {
return fmt.Errorf("too many array selectors")
}
// 7⃣ 非法组合
if strings.Contains(path, "[") && !strings.Contains(path, "[*]") {
return fmt.Errorf("only [*] array selector allowed")
}
return nil
}