DetectSQLInjection detect sql injection in string
(input string, isStrict bool)
| 50 | |
| 51 | // DetectSQLInjection detect sql injection in string |
| 52 | func DetectSQLInjection(input string, isStrict bool) bool { |
| 53 | if len(input) == 0 { |
| 54 | return false |
| 55 | } |
| 56 | |
| 57 | if !isStrict { |
| 58 | if len(input) > 1024 { |
| 59 | if !utf8.ValidString(input[:1024]) && !utf8.ValidString(input[:1023]) && !utf8.ValidString(input[:1022]) { |
| 60 | return false |
| 61 | } |
| 62 | } else { |
| 63 | if !utf8.ValidString(input) { |
| 64 | return false |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if detectSQLInjectionOne(input) { |
| 70 | return true |
| 71 | } |
| 72 | |
| 73 | // 兼容 /PATH?URI |
| 74 | if (input[0] == '/' || strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://")) && len(input) < 1024 { |
| 75 | var argsIndex = strings.Index(input, "?") |
| 76 | if argsIndex > 0 { |
| 77 | var args = input[argsIndex+1:] |
| 78 | unescapeArgs, err := url.QueryUnescape(args) |
| 79 | if err == nil && args != unescapeArgs { |
| 80 | return detectSQLInjectionOne(args) || detectSQLInjectionOne(unescapeArgs) |
| 81 | } else { |
| 82 | return detectSQLInjectionOne(args) |
| 83 | } |
| 84 | } |
| 85 | } else { |
| 86 | unescapedInput, err := url.QueryUnescape(input) |
| 87 | if err == nil && input != unescapedInput { |
| 88 | return detectSQLInjectionOne(unescapedInput) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return false |
| 93 | } |
| 94 | |
| 95 | func detectSQLInjectionOne(input string) bool { |
| 96 | if len(input) == 0 { |