Files
EdgeNode/internal/waf/checkpoints/request_json_arg.go

55 lines
1.5 KiB
Go
Raw Normal View History

2020-10-08 15:06:42 +08:00
package checkpoints
import (
"encoding/json"
"github.com/TeaOSLab/EdgeNode/internal/utils"
"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
2023-10-11 12:21:10 +08:00
wafutils "github.com/TeaOSLab/EdgeNode/internal/waf/utils"
"github.com/iwind/TeaGo/maps"
2020-10-08 15:06:42 +08:00
"strings"
)
2021-07-18 15:51:49 +08:00
// RequestJSONArgCheckpoint ${requestJSON.arg}
2020-10-08 15:06:42 +08:00
type RequestJSONArgCheckpoint struct {
Checkpoint
}
2023-10-11 12:21:10 +08:00
func (this *RequestJSONArgCheckpoint) RequestValue(req requests.Request, param string, options maps.Map, ruleId int64) (value any, hasRequestBody bool, sysErr error, userErr error) {
2021-07-18 15:51:49 +08:00
var bodyData = req.WAFGetCacheBody()
2022-07-16 17:05:37 +08:00
hasRequestBody = true
2021-07-18 15:51:49 +08:00
if len(bodyData) == 0 {
data, err := req.WAFReadBody(req.WAFMaxRequestSize()) // read body
2020-10-08 15:06:42 +08:00
if err != nil {
2022-07-16 17:05:37 +08:00
return "", hasRequestBody, err, nil
2020-10-08 15:06:42 +08:00
}
2021-07-18 15:51:49 +08:00
bodyData = data
req.WAFSetCacheBody(data)
defer req.WAFRestoreBody(data)
2020-10-08 15:06:42 +08:00
}
// TODO improve performance
2023-10-11 12:21:10 +08:00
var m any = nil
2021-07-18 15:51:49 +08:00
err := json.Unmarshal(bodyData, &m)
2020-10-08 15:06:42 +08:00
if err != nil || m == nil {
2022-07-16 17:05:37 +08:00
return "", hasRequestBody, nil, err
2020-10-08 15:06:42 +08:00
}
value = utils.Get(m, strings.Split(param, "."))
if value != nil {
2022-07-16 17:05:37 +08:00
return value, hasRequestBody, nil, err
2020-10-08 15:06:42 +08:00
}
2022-07-16 17:05:37 +08:00
return "", hasRequestBody, nil, nil
2020-10-08 15:06:42 +08:00
}
2023-10-11 12:21:10 +08:00
func (this *RequestJSONArgCheckpoint) ResponseValue(req requests.Request, resp *requests.Response, param string, options maps.Map, ruleId int64) (value any, hasRequestBody bool, sysErr error, userErr error) {
2020-10-08 15:06:42 +08:00
if this.IsRequest() {
return this.RequestValue(req, param, options, ruleId)
2020-10-08 15:06:42 +08:00
}
return
}
2023-10-11 12:21:10 +08:00
func (this *RequestJSONArgCheckpoint) CacheLife() wafutils.CacheLife {
return wafutils.CacheMiddleLife
}