mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-03 12:20:27 +08:00
[WAF]增加若干个编解码方法
This commit is contained in:
20
pkg/serverconfigs/filterconfigs/filter_html_escape.go
Normal file
20
pkg/serverconfigs/filterconfigs/filter_html_escape.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"html"
|
||||
)
|
||||
|
||||
type HTMLEscapeFilter struct {
|
||||
}
|
||||
|
||||
// 初始化
|
||||
func (this *HTMLEscapeFilter) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 执行过滤
|
||||
func (this *HTMLEscapeFilter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) {
|
||||
s := types.String(input)
|
||||
return html.EscapeString(s), true, nil
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package filterconfigs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestHTMLEscapeFilter_Do(t *testing.T) {
|
||||
filter := &HTMLEscapeFilter{}
|
||||
t.Log(filter.Do("Hello", nil))
|
||||
t.Log(filter.Do("<script></script>", nil))
|
||||
}
|
||||
20
pkg/serverconfigs/filterconfigs/filter_html_unescape.go
Normal file
20
pkg/serverconfigs/filterconfigs/filter_html_unescape.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"html"
|
||||
)
|
||||
|
||||
type HTMLUnescapeFilter struct {
|
||||
}
|
||||
|
||||
// 初始化
|
||||
func (this *HTMLUnescapeFilter) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 执行过滤
|
||||
func (this *HTMLUnescapeFilter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) {
|
||||
s := types.String(input)
|
||||
return html.UnescapeString(s), true, nil
|
||||
}
|
||||
10
pkg/serverconfigs/filterconfigs/filter_html_unescape_test.go
Normal file
10
pkg/serverconfigs/filterconfigs/filter_html_unescape_test.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package filterconfigs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestHTMLUnescapeFilter_Do(t *testing.T) {
|
||||
filter := &HTMLUnescapeFilter{}
|
||||
t.Log(filter.Do("Hello", nil))
|
||||
t.Log(filter.Do("<script>", nil))
|
||||
t.Log(filter.Do("<script>", nil))
|
||||
}
|
||||
25
pkg/serverconfigs/filterconfigs/filter_unicode_decode.go
Normal file
25
pkg/serverconfigs/filterconfigs/filter_unicode_decode.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type UnicodeDecodeFilter struct {
|
||||
}
|
||||
|
||||
// 初始化
|
||||
func (this *UnicodeDecodeFilter) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 执行过滤
|
||||
func (this *UnicodeDecodeFilter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) {
|
||||
s := types.String(input)
|
||||
result, err := strconv.Unquote("\"" + strings.ReplaceAll(s, "\"", "\\\"") + "\"")
|
||||
if err != nil {
|
||||
return input, true, nil
|
||||
}
|
||||
return result, true, nil
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package filterconfigs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestUnicodeDecodeFilter_Do(t *testing.T) {
|
||||
filter := &UnicodeDecodeFilter{}
|
||||
t.Log(filter.Do(`"\u5947\u5c4f`, nil))
|
||||
t.Log(filter.Do(`"Hello`, nil))
|
||||
t.Log(filter.Do(`真实的存在`, nil))
|
||||
}
|
||||
29
pkg/serverconfigs/filterconfigs/filter_unicode_encode.go
Normal file
29
pkg/serverconfigs/filterconfigs/filter_unicode_encode.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type UnicodeEncodeFilter struct {
|
||||
}
|
||||
|
||||
// 初始化
|
||||
func (this *UnicodeEncodeFilter) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 执行过滤
|
||||
func (this *UnicodeEncodeFilter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) {
|
||||
s := []rune(types.String(input))
|
||||
result := strings.Builder{}
|
||||
for _, r := range s {
|
||||
if r < 128 {
|
||||
result.WriteRune(r)
|
||||
} else {
|
||||
result.WriteString("\\u" + strconv.FormatInt(int64(r), 16))
|
||||
}
|
||||
}
|
||||
return result.String(), true, nil
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package filterconfigs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestUnicodeEncodeFilter_Do(t *testing.T) {
|
||||
filter := &UnicodeEncodeFilter{}
|
||||
t.Log(filter.Do("Hello", nil))
|
||||
t.Log(filter.Do("我是中文", nil))
|
||||
t.Log(filter.Do("我是中文和英文Mixed", nil))
|
||||
t.Log(filter.Do("我有特殊字符|'\"", nil))
|
||||
}
|
||||
@@ -13,16 +13,20 @@ func init() {
|
||||
|
||||
// 所有的筛选条件
|
||||
var allFilters = map[string]FilterInterface{
|
||||
"md5": new(Md5Filter),
|
||||
"urlEncode": new(URLEncodeFilter),
|
||||
"urlDecode": new(URLDecodeFilter),
|
||||
"base64Encode": new(Base64EncodeFilter),
|
||||
"base64Decode": new(Base64DecodeFilter),
|
||||
"length": new(LengthFilter),
|
||||
"hex2dec": new(Hex2DecFilter),
|
||||
"dec2hex": new(Dec2HexFilter),
|
||||
"sha1": new(Sha1Filter),
|
||||
"sha256": new(Sha256Filter),
|
||||
"md5": new(Md5Filter),
|
||||
"urlEncode": new(URLEncodeFilter),
|
||||
"urlDecode": new(URLDecodeFilter),
|
||||
"base64Encode": new(Base64EncodeFilter),
|
||||
"base64Decode": new(Base64DecodeFilter),
|
||||
"unicodeEncode": new(UnicodeEncodeFilter),
|
||||
"unicodeDecode": new(UnicodeDecodeFilter),
|
||||
"htmlEscape": new(HTMLEscapeFilter),
|
||||
"htmlUnescape": new(HTMLUnescapeFilter),
|
||||
"length": new(LengthFilter),
|
||||
"hex2dec": new(Hex2DecFilter),
|
||||
"dec2hex": new(Dec2HexFilter),
|
||||
"sha1": new(Sha1Filter),
|
||||
"sha256": new(Sha256Filter),
|
||||
}
|
||||
|
||||
// 查找Filter
|
||||
|
||||
Reference in New Issue
Block a user