2023-12-07 20:25:35 +08:00
|
|
|
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
|
|
|
|
|
|
|
|
package injectionutils
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
#cgo CFLAGS: -I./libinjection/src
|
|
|
|
|
|
|
|
|
|
#include <libinjection.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
*/
|
|
|
|
|
import "C"
|
|
|
|
|
import (
|
|
|
|
|
"net/url"
|
|
|
|
|
"strings"
|
|
|
|
|
"unsafe"
|
|
|
|
|
)
|
|
|
|
|
|
2023-12-08 10:15:18 +08:00
|
|
|
// DetectXSS detect XSS in string
|
|
|
|
|
func DetectXSS(input string) bool {
|
2023-12-07 20:25:35 +08:00
|
|
|
if len(input) == 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 10:15:18 +08:00
|
|
|
if detectXSSOne(input) {
|
2023-12-07 20:25:35 +08:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 兼容 /PATH?URI
|
2023-12-08 10:15:18 +08:00
|
|
|
if (input[0] == '/' || strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "https://")) && len(input) < 4096 {
|
2023-12-07 20:25:35 +08:00
|
|
|
var argsIndex = strings.Index(input, "?")
|
|
|
|
|
if argsIndex > 0 {
|
|
|
|
|
var args = input[argsIndex+1:]
|
|
|
|
|
unescapeArgs, err := url.QueryUnescape(args)
|
|
|
|
|
if err == nil && args != unescapeArgs {
|
2023-12-08 10:15:18 +08:00
|
|
|
return detectXSSOne(args) || detectXSSOne(unescapeArgs)
|
2023-12-07 20:25:35 +08:00
|
|
|
} else {
|
2023-12-08 10:15:18 +08:00
|
|
|
return detectXSSOne(args)
|
2023-12-07 20:25:35 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 10:15:18 +08:00
|
|
|
func detectXSSOne(input string) bool {
|
2023-12-07 20:25:35 +08:00
|
|
|
if len(input) == 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cInput = C.CString(input)
|
|
|
|
|
defer C.free(unsafe.Pointer(cInput))
|
|
|
|
|
|
2023-12-08 10:15:18 +08:00
|
|
|
return C.libinjection_xss(cInput, C.size_t(len(input))) == 1
|
2023-12-07 20:25:35 +08:00
|
|
|
}
|