DetectSQLInjectionCache detect sql injection in string with cache
(input string, isStrict bool, cacheLife utils.CacheLife)
| 22 | |
| 23 | // DetectSQLInjectionCache detect sql injection in string with cache |
| 24 | func DetectSQLInjectionCache(input string, isStrict bool, cacheLife utils.CacheLife) bool { |
| 25 | var l = len(input) |
| 26 | |
| 27 | if l == 0 { |
| 28 | return false |
| 29 | } |
| 30 | |
| 31 | if cacheLife <= 0 || l < 128 || l > utils.MaxCacheDataSize { |
| 32 | return DetectSQLInjection(input, isStrict) |
| 33 | } |
| 34 | |
| 35 | var hash = xxhash.Sum64String(input) |
| 36 | var key = "WAF@SQLI@" + strconv.FormatUint(hash, 10) |
| 37 | var item = utils.SharedCache.Read(key) |
| 38 | if item != nil { |
| 39 | return item.Value == 1 |
| 40 | } |
| 41 | |
| 42 | var result = DetectSQLInjection(input, isStrict) |
| 43 | if result { |
| 44 | utils.SharedCache.Write(key, 1, fasttime.Now().Unix()+cacheLife) |
| 45 | } else { |
| 46 | utils.SharedCache.Write(key, 0, fasttime.Now().Unix()+cacheLife) |
| 47 | } |
| 48 | return result |
| 49 | } |
| 50 | |
| 51 | // DetectSQLInjection detect sql injection in string |
| 52 | func DetectSQLInjection(input string, isStrict bool) bool { |