Skip to content

Commit

Permalink
Fix http parsing of repeated headers
Browse files Browse the repository at this point in the history
Packetbeat HTTP protocol parser was not concatenating properly repeated
headers in a request or response. This caused corruption in the header
in the form of null bytes (\u0000).
  • Loading branch information
adriansr committed Feb 8, 2018
1 parent d1b53f4 commit f77a6cd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ https://github.com/elastic/beats/compare/v6.0.0-beta2...master[Check the HEAD di
- Fix http status phrase parsing not allow spaces. {pull}5312[5312]
- Fix http parse to allow to parse get request with space in the URI. {pull}5495[5495]
- Fix mysql SQL parser to trim `\r` from Windows Server `SELECT\r\n\t1`. {pull}5572[5572]
- Fix corruption when parsing repeated headers in an HTTP request or response. {pull}6325[6325]

*Winlogbeat*

Expand Down
4 changes: 2 additions & 2 deletions packetbeat/protos/http/http_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ func (parser *parser) parseHeader(m *message, data []byte) (bool, bool, int) {
if val, ok := m.headers[string(headerName)]; ok {
composed := make([]byte, len(val)+len(headerVal)+2)
off := copy(composed, val)
off = copy(composed[off:], []byte(", "))
copy(composed[off:], headerVal)
copy(composed[off:], []byte(", "))
copy(composed[off+2:], headerVal)

m.headers[string(headerName)] = composed
} else {
Expand Down
22 changes: 22 additions & 0 deletions packetbeat/protos/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,28 @@ func Test_gap_in_body_http1dot0(t *testing.T) {
assert.Equal(t, false, complete)
}

func TestHttpParser_composedHeaders(t *testing.T) {
data := "HTTP/1.1 200 OK\r\n" +
"Content-Length: 0\r\n" +
"Date: Tue, 14 Aug 2012 22:31:45 GMT\r\n" +
"Set-Cookie: aCookie=yummy\r\n" +
"Set-Cookie: anotherCookie=why%20not\r\n" +
"\r\n"
http := httpModForTests(nil)
http.parserConfig.sendHeaders = true
http.parserConfig.sendAllHeaders = true
message, ok, complete := testParse(http, data)

assert.True(t, ok)
assert.True(t, complete)
assert.False(t, message.isRequest)
assert.Equal(t, 200, int(message.statusCode))
assert.Equal(t, "OK", string(message.statusPhrase))
header, ok := message.headers["set-cookie"]
assert.True(t, ok)
assert.Equal(t, "aCookie=yummy, anotherCookie=why%20not", string(header))
}

func testCreateTCPTuple() *common.TCPTuple {
t := &common.TCPTuple{
IPLength: 4,
Expand Down

0 comments on commit f77a6cd

Please sign in to comment.