fix: 修复order by和limit、page的sql注入风险
This commit is contained in:
parent
0d113a5473
commit
b6bb886e2b
112
lxDb/sql.go
112
lxDb/sql.go
@ -2,9 +2,10 @@ package lxDb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"git.listensoft.net/tool/lxutils/lxUtil"
|
"regexp"
|
||||||
"gorm.io/gorm"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.listensoft.net/
|
||||||
)
|
)
|
||||||
|
|
||||||
// 带事务的, 和不带事务的 说明:
|
// 带事务的, 和不带事务的 说明:
|
||||||
@ -321,6 +322,11 @@ func SqlQuery(tx *gorm.DB, sql string, list interface{}, q *PaginationQuery, par
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SqlQueryNew(tx *gorm.DB, sql string, list interface{}, q *PaginationQuery, params ...interface{}) (err error) {
|
func SqlQueryNew(tx *gorm.DB, sql string, list interface{}, q *PaginationQuery, params ...interface{}) (err error) {
|
||||||
|
// 验证输入参数的安全性
|
||||||
|
if !isSafeSQL(sql) {
|
||||||
|
return errors.New("检测到潜在的SQL注入风险")
|
||||||
|
}
|
||||||
|
|
||||||
var builder strings.Builder
|
var builder strings.Builder
|
||||||
builder.WriteString(sql)
|
builder.WriteString(sql)
|
||||||
|
|
||||||
@ -331,8 +337,17 @@ func SqlQueryNew(tx *gorm.DB, sql string, list interface{}, q *PaginationQuery,
|
|||||||
// 条件字段
|
// 条件字段
|
||||||
if q != nil {
|
if q != nil {
|
||||||
where, args := q.BuildRawWhere()
|
where, args := q.BuildRawWhere()
|
||||||
|
|
||||||
|
|
||||||
if hasWhere(sql) { // 原SQL已有WHERE子句
|
if hasWhere(sql) { // 原SQL已有WHERE子句
|
||||||
builder.WriteString(where) // 去掉where 前头的and or ..
|
// 确保 where 子句以 AND 或 OR 开头,然后安全添加
|
||||||
|
if strings.HasPrefix(where, " AND ") || strings.HasPrefix(where, " OR ") {
|
||||||
|
builder.WriteString(where)
|
||||||
|
} else {
|
||||||
|
// 如果 where 不是以 AND/OR 开头,添加 AND 前缀
|
||||||
|
builder.WriteString(" AND ")
|
||||||
|
builder.WriteString(strings.TrimSpace(where))
|
||||||
|
}
|
||||||
} else { // 原SQL没有WHERE子句
|
} else { // 原SQL没有WHERE子句
|
||||||
if strings.HasPrefix(where, " AND ") {
|
if strings.HasPrefix(where, " AND ") {
|
||||||
where = strings.Replace(where, " AND ", " WHERE ", 1)
|
where = strings.Replace(where, " AND ", " WHERE ", 1)
|
||||||
@ -340,10 +355,12 @@ func SqlQueryNew(tx *gorm.DB, sql string, list interface{}, q *PaginationQuery,
|
|||||||
} else if strings.HasPrefix(where, " OR ") {
|
} else if strings.HasPrefix(where, " OR ") {
|
||||||
where = strings.Replace(where, " OR ", " WHERE ", 1)
|
where = strings.Replace(where, " OR ", " WHERE ", 1)
|
||||||
builder.WriteString(where)
|
builder.WriteString(where)
|
||||||
} else {
|
} else if where != "" {
|
||||||
builder.WriteString(where) // "" 或者 " GROUP BY ..."
|
builder.WriteString(" WHERE ")
|
||||||
|
builder.WriteString(where)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
params = append(params, args...)
|
params = append(params, args...)
|
||||||
}
|
}
|
||||||
@ -354,29 +371,30 @@ func SqlQueryNew(tx *gorm.DB, sql string, list interface{}, q *PaginationQuery,
|
|||||||
// 记录条数
|
// 记录条数
|
||||||
if needDoCount(q) {
|
if needDoCount(q) {
|
||||||
var total int64
|
var total int64
|
||||||
//tx = tx.Count(&total)
|
// 使用安全的 COUNT 查询
|
||||||
//tx.Raw("SELECT COUNT(*) as total FROM ("+sql2+") aaaa", params...).Take(&total)
|
|
||||||
//todo lcs 优化速度 start
|
|
||||||
// 替换 SELECT 和 FROM 之间的部分,并去掉 GROUP BY
|
|
||||||
replacedSQL := replaceSelectAndRemoveGroupBy(sql2)
|
replacedSQL := replaceSelectAndRemoveGroupBy(sql2)
|
||||||
tx.Raw(replacedSQL, params...).Take(&total)
|
if err := tx.Raw(replacedSQL, params...).Take(&total).Error; err != nil {
|
||||||
//todo end
|
return err
|
||||||
|
}
|
||||||
q.Total = int(total)
|
q.Total = int(total)
|
||||||
if total == 0 { // 如果查了记录条数并且是0, 就不需要查记录和汇总了
|
if total == 0 { // 如果查了记录条数并且是0, 就不需要查记录和汇总了
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取汇总信息 // TODO: 汇总应该放到查询列表的后面
|
// 获取汇总信息
|
||||||
if q.Summary != "" && len(q.SummarySql) == 0 {
|
if q.Summary != "" && len(q.SummarySql) == 0 {
|
||||||
|
// 安全地构建汇总字段
|
||||||
q.SummarySql = fieldsToSumSql(q.Summary)
|
q.SummarySql = fieldsToSumSql(q.Summary)
|
||||||
}
|
}
|
||||||
if len(q.Summary) != 0 {
|
if len(q.Summary) != 0 {
|
||||||
tx = tx.Offset(-1) // 需要去除offset, 否则结果可能为空, 注意: 设置0不起作用.
|
tx = tx.Offset(-1) // 需要去除offset, 否则结果可能为空, 注意: 设置0不起作用.
|
||||||
var summary = make(map[string]interface{})
|
var summary = make(map[string]interface{})
|
||||||
//tx.Order("") // FIXME: 怎么去掉order by, sum是不需要order by的, 影响性能.
|
|
||||||
//tx.Select(q.SummarySql).Take(&summary) // 不适合rawsql?
|
|
||||||
|
|
||||||
tx.Raw("SELECT "+strings.Join(q.SummarySql, ", ")+" FROM ("+sql2+") ssss", params...).Take(&summary)
|
// 安全构建汇总查询 - 使用参数化查询
|
||||||
|
summarySQL := "SELECT " + strings.Join(q.SummarySql, ", ") + " FROM (" + sql2 + ") ssss"
|
||||||
|
if err := tx.Raw(summarySQL, params...).Take(&summary).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// []byte 转 string. 不太合理, 应该返回int或float
|
// []byte 转 string. 不太合理, 应该返回int或float
|
||||||
for k, v := range summary {
|
for k, v := range summary {
|
||||||
@ -388,30 +406,34 @@ func SqlQueryNew(tx *gorm.DB, sql string, list interface{}, q *PaginationQuery,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 排序处理
|
// 安全地处理排序 - 使用白名单验证字段名
|
||||||
if q.OrderBy != "" {
|
if q.OrderBy != "" {
|
||||||
s := fmt.Sprintf(" ORDER BY %s", lxUtil.FieldToColumn(q.OrderBy)) // TODO: q.OrderBy是字符串,可能多个字段 会有问题吗
|
safeOrderBy := sanitizeOrderBy(q.OrderBy)
|
||||||
builder.WriteString(s)
|
if safeOrderBy != "" {
|
||||||
|
builder.WriteString(" ORDER BY ")
|
||||||
|
builder.WriteString(safeOrderBy)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 偏移量处理
|
// 安全地处理分页 - 使用参数化查询
|
||||||
if q.Limit > 0 {
|
if q.Limit > 0 {
|
||||||
if q.Offset > 0 {
|
if q.Offset > 0 {
|
||||||
offset := (q.Offset - 1) * q.Limit
|
offset := (q.Offset - 1) * q.Limit
|
||||||
s := fmt.Sprintf(" LIMIT %d, %d", offset, q.Limit)
|
builder.WriteString(" LIMIT ?, ?")
|
||||||
builder.WriteString(s)
|
params = append(params, offset, q.Limit)
|
||||||
} else {
|
} else {
|
||||||
s := fmt.Sprintf(" LIMIT %d", q.Limit)
|
builder.WriteString(" LIMIT ?")
|
||||||
builder.WriteString(s)
|
params = append(params, q.Limit)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//tx.Raw(builder.String(), params...).Scan(list) // FIXME: unsupported data type: &[] why?
|
// 执行最终查询 - 使用参数化查询
|
||||||
tx.Raw(builder.String(), params...).Find(list) // Find与Scan区别: list传入[]时, 查询为空的情况下, Find返回的是[], 而Scan返回的是nil.
|
if err := tx.Raw(builder.String(), params...).Find(list).Error; err != nil {
|
||||||
// ref: What is the difference between Find and Scan: https://github.com/go-gorm/gorm/issues/4218
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 是否需要查询记录条数
|
// 是否需要查询记录条数
|
||||||
@ -561,6 +583,42 @@ func isSafeSQL(sql string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sanitizeOrderBy 安全处理 ORDER BY 字段
|
||||||
|
func sanitizeOrderBy(orderBy string) string {
|
||||||
|
// 移除可能危险的字符
|
||||||
|
orderBy = strings.TrimSpace(orderBy)
|
||||||
|
|
||||||
|
// 分割字段和排序方向
|
||||||
|
parts := strings.Fields(orderBy)
|
||||||
|
if len(parts) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证字段名
|
||||||
|
field := parts[0]
|
||||||
|
if !isValidFieldName(field) {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果有排序方向,验证它
|
||||||
|
if len(parts) > 1 {
|
||||||
|
direction := strings.ToUpper(parts[1])
|
||||||
|
if direction != "ASC" && direction != "DESC" {
|
||||||
|
return field // 默认不添加方向
|
||||||
|
}
|
||||||
|
return field + " " + direction
|
||||||
|
}
|
||||||
|
|
||||||
|
return field
|
||||||
|
}
|
||||||
|
|
||||||
|
// isValidFieldName 验证字段名是否安全
|
||||||
|
func isValidFieldName(field string) bool {
|
||||||
|
// 允许字母、数字、下划线和点(用于表名.字段名)
|
||||||
|
matched, _ := regexp.MatchString(`^[a-zA-Z0-9_.]+$`, field)
|
||||||
|
return matched
|
||||||
|
}
|
||||||
|
|
||||||
// replaceSelectAndRemoveGroupBy 替换 SELECT 和 FROM 之间的部分,并去掉 GROUP BY
|
// replaceSelectAndRemoveGroupBy 替换 SELECT 和 FROM 之间的部分,并去掉 GROUP BY
|
||||||
func replaceSelectAndRemoveGroupBy(sql string) string {
|
func replaceSelectAndRemoveGroupBy(sql string) string {
|
||||||
// 找到 SELECT 和 FROM 的位置
|
// 找到 SELECT 和 FROM 的位置
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user