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

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