From e5c4a6a8d1e06a21bd3e3139d89f02c33dc9d275 Mon Sep 17 00:00:00 2001 From: Mayank Pande Date: Thu, 4 Apr 2024 23:06:50 +0530 Subject: [PATCH] fix: fix for host header parsing --- containers/container.go | 4 ++-- containers/l7.go | 6 +++--- ebpftracer/l7/http.go | 32 ++++++++++++++++++++++++-------- ebpftracer/l7/l7_test.go | 6 ------ 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/containers/container.go b/containers/container.go index 01ed8a6f..17f64a26 100644 --- a/containers/container.go +++ b/containers/container.go @@ -610,9 +610,9 @@ func (c *Container) onL7Request(pid uint32, fd uint64, timestamp uint64, r *l7.R } if conn.dstWorkload.Namespace == "external" && (r.Protocol == l7.ProtocolHTTP || r.Protocol == l7.ProtocolHTTP2) { - request, error := l7.ParseHttpRequest(string(r.Payload)) + host, error := l7.ParseHostFromHttpRequest(string(r.Payload)) if error == nil { - conn.dstWorkload.Name = request.Host + conn.dstWorkload.Name = host } } stats := c.l7Stats.get(r.Protocol, conn.Dest, conn.ActualDest, r, conn.srcWorkload, conn.dstWorkload, conn.actualDestWorkload) diff --git a/containers/l7.go b/containers/l7.go index 2ec65278..2fb5be74 100644 --- a/containers/l7.go +++ b/containers/l7.go @@ -72,9 +72,9 @@ func (s L7Stats) get(protocol l7.Protocol, destination, actualDestination netadd constLabels["path"] = path constLabels["method"] = method if dstWorkload.Namespace == "external" { - request, error := l7.ParseHttpRequest(string(r.Payload)) - if error == nil { - constLabels["destination_workload_name"] = request.Host + host, err := l7.ParseHostFromHttpRequest(string(r.Payload)) + if err == nil { + constLabels["destination_workload_name"] = host } } hOpts := L7Latency[protocol] diff --git a/ebpftracer/l7/http.go b/ebpftracer/l7/http.go index a5f31be8..64ff3dfc 100644 --- a/ebpftracer/l7/http.go +++ b/ebpftracer/l7/http.go @@ -1,11 +1,10 @@ package l7 import ( - "bufio" "bytes" "encoding/base64" + "errors" "log" - "net/http" "regexp" "strings" ) @@ -51,14 +50,31 @@ func ParseHttpAndRest(payload []byte) (string, string, string, string) { return string(method), string(uri), string(headers), string(d) } -func ParseHttpRequest(request string) (*http.Request, error) { - request = strings.ReplaceAll(request, "\\n", "\r\n") - req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(request))) - if err != nil { - return nil, err +func ParseHostFromHttpRequest(request string) (string, error) { + // Split the request string into lines + lines := strings.Split(request, "\n") + + // Search for the Host header within the lines + var host string + for _, line := range lines { + if strings.HasPrefix(line, "Host:") { + parts := strings.SplitN(line, ": ", 2) + if len(parts) == 2 { + host = parts[1] + break + } + } + } + + // Check if the Host header is found + if host == "" { + err := errors.New("host header not found") + return "", err } - return req, nil + + return host, nil } + func SanitizeString(input string) string { // Regular expression patterns to match various sensitive data formats sensitivePatterns := []*regexp.Regexp{ diff --git a/ebpftracer/l7/l7_test.go b/ebpftracer/l7/l7_test.go index aa47237b..6c1f60a7 100644 --- a/ebpftracer/l7/l7_test.go +++ b/ebpftracer/l7/l7_test.go @@ -88,9 +88,3 @@ func TestParseMongo(t *testing.T) { binary.LittleEndian.PutUint32(payload[mongoHeaderLength+mongoSectionKindLength:], dataSize+1) assert.Equal(t, ``, ParseMongo(payload)) } - -func TestParseRequest(t *testing.T) { - m, _ := ParseHttpRequest(`HEAD /1 HTTP/1.1\nHost: 127.0.0.1\nUser-Agent: curl/8.0.1\nAccept: */*\n\nxzxxxxxxzx`) - assert.Equal(t, "HEAD", m.Method) - assert.Equal(t, "127.0.0.1", m.Host) -}