sql_inject_detector.go 917 字节
package domain

import (
	"fmt"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/utils"
	"strings"
)

var sqlKeys = []string{
	"'",
	"`",
	//",",
	"--",
	"/*",
	//";",
	"(",
	")",
	"#",
	"*",
	"or",
	"=",
	"having ",
	"union",
	"sleep",
	"as ",
	"from",
	"where",
	"exists",
	"and",
	"&&",
	"or",
	"||",
	"not",
	"in",
	"like",
	"is",
	"between",
	"union",
	"all",
	"having",
	"order",
	"group",
	"by",
	"print",
	"sleep",
}

var ErrorSqlInject = fmt.Errorf("!!! 请检查输入的内容,存在注入风险")

func SqlDetections(args ...interface{}) error {
	for _, item := range args {
		if err := sqlDetection(item); err != nil {
			return err
		}
	}
	return nil
}

func sqlDetection(val interface{}) error {
	sVal := strings.ToLower(utils.AssertString(val))
	for _, kw := range sqlKeys {
		if strings.Contains(sVal, kw) {
			return ErrorSqlInject
		}
	}
	return nil
}