Skip to content

Commit

Permalink
Fix host parsing in httpfltr
Browse files Browse the repository at this point in the history
  • Loading branch information
grcevski committed Dec 5, 2023
1 parent 289dec9 commit 7c94c46
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
15 changes: 14 additions & 1 deletion pkg/internal/ebpf/httpfltr/httpfltr.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (event *BPFHTTPInfo) method() string {
}

func (event *BPFHTTPInfo) hostFromBuf() (string, int) {
buf := string(event.Buf[:])
buf := cstr(event.Buf[:])
idx := strings.Index(buf, "Host: ")

if idx < 0 {
Expand All @@ -348,6 +348,10 @@ func (event *BPFHTTPInfo) hostFromBuf() (string, int) {

rIdx := strings.Index(buf, "\r")

if rIdx < 0 {
rIdx = len(buf)
}

host, portStr, err := net.SplitHostPort(buf[:rIdx])

if err != nil {
Expand Down Expand Up @@ -401,3 +405,12 @@ func serviceInfo(pid uint32) svc.ID {

return result
}

func cstr(chars []uint8) string {
addrLen := bytes.IndexByte(chars[:], 0)
if addrLen < 0 {
addrLen = len(chars)
}

return string(chars[:addrLen])
}
61 changes: 56 additions & 5 deletions pkg/internal/ebpf/httpfltr/httpfltr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,62 @@ func TestToRequestTraceNoConnection(t *testing.T) {
assert.Equal(t, expected, result)
}

func cstr(chars []uint8) string {
addrLen := bytes.IndexByte(chars[:], 0)
if addrLen < 0 {
addrLen = len(chars)
func TestToRequestTrace_BadHost(t *testing.T) {
var record BPFHTTPInfo
record.Type = 1
record.StartMonotimeNs = 123456
record.EndMonotimeNs = 789012
record.Status = 200
record.ConnInfo.D_port = 0
record.ConnInfo.S_port = 0
record.ConnInfo.S_addr = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 192, 168, 0, 1}
record.ConnInfo.D_addr = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 8, 8, 8, 8}
copy(record.Buf[:], "GET /hello HTTP/1.1\r\nHost: example.c")

buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, &record)
assert.NoError(t, err)

result, _, err := ReadHTTPInfoIntoSpan(&ringbuf.Record{RawSample: buf.Bytes()})
assert.NoError(t, err)

expected := request.Span{
Host: "",
Peer: "",
Path: "/hello",
Method: "GET",
Status: 200,
Type: request.EventTypeHTTP,
RequestStart: 123456,
Start: 123456,
End: 789012,
HostPort: 0,
ServiceID: svc.ID{SDKLanguage: svc.InstrumentableGeneric},
}
assert.Equal(t, expected, result)

s, p := record.hostFromBuf()
assert.Equal(t, s, "")
assert.Equal(t, p, -1)

var record1 BPFHTTPInfo
copy(record1.Buf[:], "GET /hello HTTP/1.1\r\nHost: example.c:23")

s, p = record1.hostFromBuf()
assert.Equal(t, s, "example.c")
assert.Equal(t, p, 23)

var record2 BPFHTTPInfo
copy(record2.Buf[:], "GET /hello HTTP/1.1\r\nHost: ")

s, p = record2.hostFromBuf()
assert.Equal(t, s, "")
assert.Equal(t, p, -1)

var record3 BPFHTTPInfo
copy(record3.Buf[:], "GET /hello HTTP/1.1\r\nHost")

return string(chars[:addrLen])
s, p = record3.hostFromBuf()
assert.Equal(t, s, "")
assert.Equal(t, p, -1)
}

0 comments on commit 7c94c46

Please sign in to comment.