mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-07-21 03:28:59 +02:00
✨ [BREAKING-CHANGE] Add CrowdsecAppsecBodyLimit (#208)
* ✨ Add CrowdsecAppsecBodyLimit * 🍱 fix lint * 🍱 fix lint * 🍱 fix error on main
This commit is contained in:
+9
-3
@@ -65,6 +65,7 @@ type Bouncer struct {
|
||||
appsecPath string
|
||||
appsecFailureBlock bool
|
||||
appsecUnreachableBlock bool
|
||||
appsecBodyLimit int64
|
||||
crowdsecScheme string
|
||||
crowdsecHost string
|
||||
crowdsecPath string
|
||||
@@ -154,6 +155,7 @@ func New(_ context.Context, next http.Handler, config *configuration.Config, nam
|
||||
appsecPath: config.CrowdsecAppsecPath,
|
||||
appsecFailureBlock: config.CrowdsecAppsecFailureBlock,
|
||||
appsecUnreachableBlock: config.CrowdsecAppsecUnreachableBlock,
|
||||
appsecBodyLimit: config.CrowdsecAppsecBodyLimit,
|
||||
crowdsecScheme: config.CrowdsecLapiScheme,
|
||||
crowdsecHost: config.CrowdsecLapiHost,
|
||||
crowdsecPath: config.CrowdsecLapiPath,
|
||||
@@ -593,12 +595,16 @@ func appsecQuery(bouncer *Bouncer, ip string, httpReq *http.Request) error {
|
||||
Path: bouncer.appsecPath,
|
||||
}
|
||||
var req *http.Request
|
||||
if httpReq.Body != nil && httpReq.ContentLength > 0 {
|
||||
bodyBytes, err := io.ReadAll(httpReq.Body)
|
||||
if bouncer.appsecBodyLimit > 0 && httpReq.Body != nil && httpReq.ContentLength > 0 {
|
||||
var bodyBuffer bytes.Buffer
|
||||
limitedReader := io.LimitReader(httpReq.Body, bouncer.appsecBodyLimit)
|
||||
teeReader := io.TeeReader(limitedReader, &bodyBuffer)
|
||||
bodyBytes, err := io.ReadAll(teeReader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("appsecQuery:GetBody %w", err)
|
||||
}
|
||||
httpReq.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
|
||||
// Conserve body intact after reading it for other middlewares and service
|
||||
httpReq.Body = io.NopCloser(io.MultiReader(&bodyBuffer, httpReq.Body))
|
||||
req, _ = http.NewRequest(http.MethodPost, routeURL.String(), bytes.NewBuffer(bodyBytes))
|
||||
} else {
|
||||
req, _ = http.NewRequest(http.MethodGet, routeURL.String(), nil)
|
||||
|
||||
Reference in New Issue
Block a user