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

Fix host parsing in httpfltr #487

Merged
merged 1 commit into from
Dec 7, 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
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)
}