Archived
[waf]支持包含二进制、不支持二进制等操作符;支持对参数值编解码
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
type Base64DecodeFilter struct {
|
||||
}
|
||||
|
||||
func (this *Base64DecodeFilter) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Base64DecodeFilter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) {
|
||||
output, err = base64.StdEncoding.DecodeString(ToString(input))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
goNext = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBase64DecodeFilter_Do(t *testing.T) {
|
||||
filter := &Base64DecodeFilter{}
|
||||
t.Log(filter.Do("123456", nil))
|
||||
|
||||
encodedString := base64.StdEncoding.EncodeToString([]byte("hello"))
|
||||
t.Log(filter.Do(encodedString, nil))
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
type Base64EncodeFilter struct {
|
||||
}
|
||||
|
||||
func (this *Base64EncodeFilter) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Base64EncodeFilter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) {
|
||||
data := ToBytes(input)
|
||||
output = base64.StdEncoding.EncodeToString(data)
|
||||
goNext = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBase64EncodeFilter_Do(t *testing.T) {
|
||||
a := assert.NewAssertion(t)
|
||||
|
||||
filter := &Base64EncodeFilter{}
|
||||
t.Log(filter.Do("hello", nil))
|
||||
t.Log(filter.Do("=", nil))
|
||||
|
||||
output, goNext, err := filter.Do("123456", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a.IsTrue(goNext)
|
||||
|
||||
outputString := output.(string)
|
||||
result, err := base64.StdEncoding.DecodeString(outputString)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("origin:", string(result))
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package filterconfigs
|
||||
|
||||
// 过滤接口
|
||||
type FilterInterface interface {
|
||||
// 初始化
|
||||
Init() error
|
||||
|
||||
// 执行过滤
|
||||
Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package filterconfigs
|
||||
|
||||
type LengthFilter struct {
|
||||
}
|
||||
|
||||
// 初始化
|
||||
func (this *LengthFilter) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 执行过滤
|
||||
func (this *LengthFilter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) {
|
||||
output = len(ToBytes(input))
|
||||
goNext = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package filterconfigs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestLengthFilter_Do(t *testing.T) {
|
||||
filter := &LengthFilter{}
|
||||
t.Log(filter.Do("hello", nil))
|
||||
t.Log(filter.Do([]byte("hello"), nil))
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Md5Filter struct {
|
||||
}
|
||||
|
||||
func (this *Md5Filter) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Md5Filter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) {
|
||||
data := ToBytes(input)
|
||||
m := md5.New()
|
||||
m.Write(data)
|
||||
result := m.Sum(nil)
|
||||
output = fmt.Sprintf("%x", result)
|
||||
goNext = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package filterconfigs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMd5Filter_Do(t *testing.T) {
|
||||
filter := &Md5Filter{}
|
||||
t.Log(filter.Do("123456", nil))
|
||||
t.Log(filter.Do(nil, nil))
|
||||
t.Log(filter.Do("", nil))
|
||||
t.Log(filter.Do("hello", nil))
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package filterconfigs
|
||||
|
||||
import "net/url"
|
||||
|
||||
type URLDecodeFilter struct {
|
||||
}
|
||||
|
||||
func (this *URLDecodeFilter) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *URLDecodeFilter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) {
|
||||
output, err = url.QueryUnescape(ToString(input))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
goNext = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package filterconfigs
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestURLDecodeFilter_Do(t *testing.T) {
|
||||
filter := &URLDecodeFilter{}
|
||||
t.Log(filter.Do("hello", nil))
|
||||
t.Log(filter.Do(url.QueryEscape("/hello/world/?a=b&c=d"), nil))
|
||||
t.Log(filter.Do("/hello/world/?a=b&c=d", nil))
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package filterconfigs
|
||||
|
||||
import "net/url"
|
||||
|
||||
type URLEncodeFilter struct {
|
||||
}
|
||||
|
||||
func (this *URLEncodeFilter) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *URLEncodeFilter) Do(input interface{}, options interface{}) (output interface{}, goNext bool, err error) {
|
||||
output = url.QueryEscape(ToString(input))
|
||||
goNext = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package filterconfigs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestURLEncodeFilter_Do(t *testing.T) {
|
||||
filter := &URLEncodeFilter{}
|
||||
t.Log(filter.Do("hello", nil))
|
||||
t.Log(filter.Do("/hello/world?a=b&c=中文&d=<symbol>", nil))
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package filterconfigs
|
||||
|
||||
import "github.com/iwind/TeaGo/logs"
|
||||
|
||||
func init() {
|
||||
for code, filter := range allFilters {
|
||||
err := filter.Init()
|
||||
if err != nil {
|
||||
logs.Println("[FILTER]init '" + code + "' failed: " + err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 所有的筛选条件
|
||||
var allFilters = map[string]FilterInterface{
|
||||
"md5": new(Md5Filter),
|
||||
"urlEncode": new(URLEncodeFilter),
|
||||
"urlDecode": new(URLDecodeFilter),
|
||||
"base64Encode": new(Base64EncodeFilter),
|
||||
"base64Decode": new(Base64DecodeFilter),
|
||||
"length": new(LengthFilter),
|
||||
}
|
||||
|
||||
// 查找Filter
|
||||
func FindFilter(code string) FilterInterface {
|
||||
return allFilters[code]
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package filterconfigs
|
||||
|
||||
import "github.com/iwind/TeaGo/types"
|
||||
|
||||
// 将输入内容转换为字节
|
||||
func ToBytes(input interface{}) []byte {
|
||||
if input == nil {
|
||||
return []byte{}
|
||||
}
|
||||
var data []byte
|
||||
var ok bool
|
||||
data, ok = input.([]byte)
|
||||
if ok {
|
||||
return data
|
||||
}
|
||||
return []byte(types.String(input))
|
||||
}
|
||||
|
||||
// 将输入内容转换为字符串
|
||||
func ToString(input interface{}) string {
|
||||
if input == nil {
|
||||
return ""
|
||||
}
|
||||
return types.String(input)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package filterconfigs
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestToBytes(t *testing.T) {
|
||||
t.Log(ToBytes("hello"))
|
||||
t.Log(ToBytes(123))
|
||||
t.Log(ToBytes([]byte{1, 2, 3}))
|
||||
}
|
||||
Reference in New Issue
Block a user