github.com/TeaOSLab/EdgeNode@v1.3.8/internal/waf/checkpoints/request_json_arg.go (about)

     1  package checkpoints
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/TeaOSLab/EdgeNode/internal/utils"
     6  	"github.com/TeaOSLab/EdgeNode/internal/waf/requests"
     7  	wafutils "github.com/TeaOSLab/EdgeNode/internal/waf/utils"
     8  	"github.com/iwind/TeaGo/maps"
     9  	"strings"
    10  )
    11  
    12  // RequestJSONArgCheckpoint ${requestJSON.arg}
    13  type RequestJSONArgCheckpoint struct {
    14  	Checkpoint
    15  }
    16  
    17  func (this *RequestJSONArgCheckpoint) RequestValue(req requests.Request, param string, options maps.Map, ruleId int64) (value any, hasRequestBody bool, sysErr error, userErr error) {
    18  	var bodyData = req.WAFGetCacheBody()
    19  	hasRequestBody = true
    20  	if len(bodyData) == 0 {
    21  		data, err := req.WAFReadBody(req.WAFMaxRequestSize()) // read body
    22  		if err != nil {
    23  			return "", hasRequestBody, err, nil
    24  		}
    25  
    26  		bodyData = data
    27  		req.WAFSetCacheBody(data)
    28  		defer req.WAFRestoreBody(data)
    29  	}
    30  
    31  	// TODO improve performance
    32  	var m any = nil
    33  	err := json.Unmarshal(bodyData, &m)
    34  	if err != nil || m == nil {
    35  		return "", hasRequestBody, nil, err
    36  	}
    37  
    38  	value = utils.Get(m, strings.Split(param, "."))
    39  	if value != nil {
    40  		return value, hasRequestBody, nil, err
    41  	}
    42  	return "", hasRequestBody, nil, nil
    43  }
    44  
    45  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) {
    46  	if this.IsRequest() {
    47  		return this.RequestValue(req, param, options, ruleId)
    48  	}
    49  	return
    50  }
    51  
    52  func (this *RequestJSONArgCheckpoint) CacheLife() wafutils.CacheLife {
    53  	return wafutils.CacheMiddleLife
    54  }