mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-23 19:20:24 +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))
|
||||||
|
}
|
||||||
@@ -18,6 +18,10 @@ var allFilters = map[string]FilterInterface{
|
|||||||
"urlDecode": new(URLDecodeFilter),
|
"urlDecode": new(URLDecodeFilter),
|
||||||
"base64Encode": new(Base64EncodeFilter),
|
"base64Encode": new(Base64EncodeFilter),
|
||||||
"base64Decode": new(Base64DecodeFilter),
|
"base64Decode": new(Base64DecodeFilter),
|
||||||
|
"unicodeEncode": new(UnicodeEncodeFilter),
|
||||||
|
"unicodeDecode": new(UnicodeDecodeFilter),
|
||||||
|
"htmlEscape": new(HTMLEscapeFilter),
|
||||||
|
"htmlUnescape": new(HTMLUnescapeFilter),
|
||||||
"length": new(LengthFilter),
|
"length": new(LengthFilter),
|
||||||
"hex2dec": new(Hex2DecFilter),
|
"hex2dec": new(Hex2DecFilter),
|
||||||
"dec2hex": new(Dec2HexFilter),
|
"dec2hex": new(Dec2HexFilter),
|
||||||
|
|||||||
Reference in New Issue
Block a user