Skip to content

Commit

Permalink
appsec: get the original UA from headers (crowdsecurity#2809)
Browse files Browse the repository at this point in the history
  • Loading branch information
blotus authored Mar 8, 2024
1 parent a928b4d commit e8ff13b
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions pkg/appsec/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import (
)

const (
URIHeaderName = "X-Crowdsec-Appsec-Uri"
VerbHeaderName = "X-Crowdsec-Appsec-Verb"
HostHeaderName = "X-Crowdsec-Appsec-Host"
IPHeaderName = "X-Crowdsec-Appsec-Ip"
APIKeyHeaderName = "X-Crowdsec-Appsec-Api-Key"
URIHeaderName = "X-Crowdsec-Appsec-Uri"
VerbHeaderName = "X-Crowdsec-Appsec-Verb"
HostHeaderName = "X-Crowdsec-Appsec-Host"
IPHeaderName = "X-Crowdsec-Appsec-Ip"
APIKeyHeaderName = "X-Crowdsec-Appsec-Api-Key"
UserAgentHeaderName = "X-Crowdsec-Appsec-User-Agent"
)

type ParsedRequest struct {
Expand Down Expand Up @@ -311,18 +312,28 @@ func NewParsedRequestFromRequest(r *http.Request, logger *logrus.Entry) (ParsedR
logger.Debugf("missing '%s' header", HostHeaderName)
}

userAgent := r.Header.Get(UserAgentHeaderName) //This one is optional

// delete those headers before coraza process the request
delete(r.Header, IPHeaderName)
delete(r.Header, HostHeaderName)
delete(r.Header, URIHeaderName)
delete(r.Header, VerbHeaderName)
delete(r.Header, UserAgentHeaderName)

originalHTTPRequest := r.Clone(r.Context())
originalHTTPRequest.Body = io.NopCloser(bytes.NewBuffer(body))
originalHTTPRequest.RemoteAddr = clientIP
originalHTTPRequest.RequestURI = clientURI
originalHTTPRequest.Method = clientMethod
originalHTTPRequest.Host = clientHost
if userAgent != "" {
originalHTTPRequest.Header.Set("User-Agent", userAgent)
r.Header.Set("User-Agent", userAgent) //Override the UA in the original request, as this is what will be used by the waf engine
} else {
//If we don't have a forwarded UA, delete the one that was set by the bouncer
originalHTTPRequest.Header.Del("User-Agent")
}

parsedURL, err := url.Parse(clientURI)
if err != nil {
Expand Down

0 comments on commit e8ff13b

Please sign in to comment.