Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

appsec: support server.response.headers.no_cookies WAF address #2347

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions internal/appsec/dyngo/instrumentation/httpsec/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ type (
// HandlerOperationRes is the HTTP handler operation results.
HandlerOperationRes struct {
// Status corresponds to the address `server.response.status`.
Status int
Status int
Headers map[string][]string
}

// SDKBodyOperationArgs is the SDK body operation arguments.
Expand Down Expand Up @@ -132,12 +133,7 @@ func WrapHandler(handler http.Handler, span ddtrace.Span, pathParams map[string]
r = r.WithContext(ctx)

defer func() {
var status int
if mw, ok := w.(interface{ Status() int }); ok {
status = mw.Status()
}

events := op.Finish(HandlerOperationRes{Status: status})
events := op.Finish(MakeHandlerOperationRes(w))

// Execute the onBlock functions to make sure blocking works properly
// in case we are instrumenting the Gin framework
Expand Down Expand Up @@ -173,16 +169,8 @@ func WrapHandler(handler http.Handler, span ddtrace.Span, pathParams map[string]

// MakeHandlerOperationArgs creates the HandlerOperationArgs value.
func MakeHandlerOperationArgs(r *http.Request, clientIP netip.Addr, pathParams map[string]string) HandlerOperationArgs {
headers := make(http.Header, len(r.Header))
for k, v := range r.Header {
k := strings.ToLower(k)
if k == "cookie" {
// Do not include cookies in the request headers
continue
}
headers[k] = v
}
cookies := makeCookies(r) // TODO(Julio-Guerra): avoid actively parsing the cookies thanks to dynamic instrumentation
headers := headersRemoveCookies(r.Header)
headers["host"] = []string{r.Host}
return HandlerOperationArgs{
Method: r.Method,
Expand All @@ -201,7 +189,21 @@ func MakeHandlerOperationRes(w http.ResponseWriter) HandlerOperationRes {
if mw, ok := w.(interface{ Status() int }); ok {
status = mw.Status()
}
return HandlerOperationRes{Status: status}
return HandlerOperationRes{Status: status, Headers: headersRemoveCookies(w.Header())}
}

// Remove cookies from the request headers and return the map of headers
// Used from `server.request.headers.no_cookies` and server.response.headers.no_cookies` addresses for the WAF
func headersRemoveCookies(headers http.Header) map[string][]string {
headersNoCookies := make(http.Header, len(headers))
for k, v := range headers {
k := strings.ToLower(k)
if k == "cookie" {
continue
}
headersNoCookies[k] = v
}
return headersNoCookies
}

// Return the map of parsed cookies if any and following the specification of
Expand Down
28 changes: 27 additions & 1 deletion internal/appsec/testdata/user_rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,33 @@
"address": "server.request.query"
}
],
"regex": "^match$"
"regex": "^match-request-query$"
},
"operator": "match_regex"
}
],
"transformers": [],
"on_match": [
"block"
]
},
{
"id": "headers-003",
"name": "query match",
"tags": {
"type": "security_scanner",
"category": "attack_attempt",
"confidence": "1"
},
"conditions": [
{
"parameters": {
"inputs": [
{
"address": "server.response.headers.no_cookies"
}
],
"regex": "match-response-header"
},
"operator": "match_regex"
}
Expand Down
26 changes: 16 additions & 10 deletions internal/appsec/waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ func newHTTPWAFEventListener(handle *wafHandle, addresses map[string]struct{}, t
values[serverResponseStatusAddr] = fmt.Sprintf("%d", res.Status)
}

if _, ok := addresses[serverResponseHeadersNoCookiesAddr]; ok && res.Headers != nil {
values[serverResponseHeadersNoCookiesAddr] = res.Headers
}

// Run the WAF, ignoring the returned actions - if any - since blocking after the request handler's
// response is not supported at the moment.
wafResult := runWAF(wafCtx, waf.RunAddressData{Persistent: values}, timeout)
Expand Down Expand Up @@ -391,16 +395,17 @@ func runWAF(wafCtx *waf.Context, values waf.RunAddressData, timeout time.Duratio

// HTTP rule addresses currently supported by the WAF
const (
serverRequestMethodAddr = "server.request.method"
serverRequestRawURIAddr = "server.request.uri.raw"
serverRequestHeadersNoCookiesAddr = "server.request.headers.no_cookies"
serverRequestCookiesAddr = "server.request.cookies"
serverRequestQueryAddr = "server.request.query"
serverRequestPathParamsAddr = "server.request.path_params"
serverRequestBodyAddr = "server.request.body"
serverResponseStatusAddr = "server.response.status"
httpClientIPAddr = "http.client_ip"
userIDAddr = "usr.id"
serverRequestMethodAddr = "server.request.method"
serverRequestRawURIAddr = "server.request.uri.raw"
serverRequestHeadersNoCookiesAddr = "server.request.headers.no_cookies"
serverRequestCookiesAddr = "server.request.cookies"
serverRequestQueryAddr = "server.request.query"
serverRequestPathParamsAddr = "server.request.path_params"
serverRequestBodyAddr = "server.request.body"
serverResponseStatusAddr = "server.response.status"
serverResponseHeadersNoCookiesAddr = "server.response.headers.no_cookies"
httpClientIPAddr = "http.client_ip"
userIDAddr = "usr.id"
)

// List of HTTP rule addresses currently supported by the WAF
Expand All @@ -413,6 +418,7 @@ var httpAddresses = []string{
serverRequestPathParamsAddr,
serverRequestBodyAddr,
serverResponseStatusAddr,
serverResponseHeadersNoCookiesAddr,
httpClientIPAddr,
userIDAddr,
}
Expand Down
14 changes: 12 additions & 2 deletions internal/appsec/waf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,13 @@ func TestUserRules(t *testing.T) {

// Start and trace an HTTP server
mux := httptrace.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World!\n"))
})
mux.HandleFunc("/response-header", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("match-response-header", "match-response-header")
w.WriteHeader(204)
})

srv := httptest.NewServer(mux)
defer srv.Close()
Expand All @@ -106,13 +110,19 @@ func TestUserRules(t *testing.T) {
}{
{
name: "custom-001",
url: "/hello",
rule: "custom-001",
},
{
name: "custom-action",
url: "?match=match",
url: "/hello?match=match-request-query",
rule: "query-002",
},
{
name: "response-headers",
url: "/response-header",
rule: "headers-003",
},
} {
t.Run(tc.name, func(t *testing.T) {
mt := mocktracer.Start()
Expand Down
Loading