diff --git a/pkg/internal/ebpf/httpfltr/httpfltr.go b/pkg/internal/ebpf/httpfltr/httpfltr.go index 5997361d0..53af45381 100644 --- a/pkg/internal/ebpf/httpfltr/httpfltr.go +++ b/pkg/internal/ebpf/httpfltr/httpfltr.go @@ -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 { @@ -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 { @@ -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]) +} diff --git a/pkg/internal/ebpf/httpfltr/httpfltr_test.go b/pkg/internal/ebpf/httpfltr/httpfltr_test.go index 073231c68..65327c4c4 100644 --- a/pkg/internal/ebpf/httpfltr/httpfltr_test.go +++ b/pkg/internal/ebpf/httpfltr/httpfltr_test.go @@ -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) }