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

56 lines
1.4 KiB
Go
Raw Permalink Normal View History

2020-10-08 15:06:42 +08:00
package checkpoints
import (
2024-07-27 15:42:50 +08:00
"net/url"
2020-10-08 15:06:42 +08:00
"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
2023-10-11 12:21:10 +08:00
"github.com/TeaOSLab/EdgeNode/internal/waf/utils"
"github.com/iwind/TeaGo/maps"
2020-10-08 15:06:42 +08:00
)
2021-07-18 15:51:49 +08:00
// RequestFormArgCheckpoint ${requestForm.arg}
2020-10-08 15:06:42 +08:00
type RequestFormArgCheckpoint struct {
Checkpoint
}
2023-10-11 12:21:10 +08:00
func (this *RequestFormArgCheckpoint) RequestValue(req requests.Request, param string, options maps.Map, ruleId int64) (value any, hasRequestBody bool, sysErr error, userErr error) {
2022-07-16 17:05:37 +08:00
hasRequestBody = true
2022-03-16 17:06:26 +08:00
if this.RequestBodyIsEmpty(req) {
value = ""
return
}
2021-07-18 15:51:49 +08:00
if req.WAFRaw().Body == nil {
2020-10-08 15:06:42 +08:00
value = ""
return
}
2021-07-18 15:51:49 +08:00
var bodyData = req.WAFGetCacheBody()
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)
req.WAFRestoreBody(data)
2020-10-08 15:06:42 +08:00
}
// TODO improve performance
2021-07-18 15:51:49 +08:00
values, _ := url.ParseQuery(string(bodyData))
2022-07-16 17:05:37 +08:00
return values.Get(param), hasRequestBody, nil, nil
2020-10-08 15:06:42 +08:00
}
2023-10-11 12:21:10 +08:00
func (this *RequestFormArgCheckpoint) 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 *RequestFormArgCheckpoint) CacheLife() utils.CacheLife {
return utils.CacheMiddleLife
}