DetectXSS detect XSS in string
(input string, isStrict bool)
| 51 | |
| 52 | // DetectXSS detect XSS in string |
| 53 | func DetectXSS(input string, isStrict bool) bool { |
| 54 | if len(input) == 0 { |
| 55 | return false |
| 56 | } |
| 57 | |
| 58 | if detectXSSOne(input, isStrict) { |
| 59 | return true |
| 60 | } |
| 61 | |
| 62 | // 兼容 /PATH?URI |
| 63 | if (input[0] == '/' || strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://")) && len(input) < 1024 { |
| 64 | var argsIndex = strings.Index(input, "?") |
| 65 | if argsIndex > 0 { |
| 66 | var args = input[argsIndex+1:] |
| 67 | unescapeArgs, err := url.QueryUnescape(args) |
| 68 | if err == nil && args != unescapeArgs { |
| 69 | return detectXSSOne(args, isStrict) || detectXSSOne(unescapeArgs, isStrict) |
| 70 | } else { |
| 71 | return detectXSSOne(args, isStrict) |
| 72 | } |
| 73 | } |
| 74 | } else { |
| 75 | unescapedInput, err := url.QueryUnescape(input) |
| 76 | if err == nil && input != unescapedInput { |
| 77 | return detectXSSOne(unescapedInput, isStrict) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return false |
| 82 | } |
| 83 | |
| 84 | func detectXSSOne(input string, isStrict bool) bool { |
| 85 | if len(input) == 0 { |