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 http status phrase parsing not allow spaces (#4795) #5312

Merged
merged 2 commits into from
Oct 12, 2017
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
2 changes: 2 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ https://github.com/elastic/beats/compare/v6.0.0-beta2...master[Check the HEAD di

*Packetbeat*

- Fix http status phrase parsing not allow spaces. {pull}5312[5312]

*Winlogbeat*

==== Added
Expand Down
14 changes: 7 additions & 7 deletions packetbeat/protos/http/http_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,15 @@ func parseResponseStatus(s []byte) (uint16, []byte, error) {
if p == -1 {
return 0, nil, errors.New("Not able to identify status code")
}

code, _ := parseInt(s[0:p])

p = bytes.LastIndexByte(s, ' ')
if p == -1 {
return uint16(code), nil, errors.New("Not able to identify status code")
statusCode, err := parseInt(s[0:p])
if err != nil {
return 0, nil, fmt.Errorf("Unable to parse status code from [%s]", s)
}
phrase := s[p+1:]
return uint16(code), phrase, nil
if len(phrase) == 0 {
return 0, nil, fmt.Errorf("Unable to parse status phrase from [%s]", s)
}
return uint16(statusCode), phrase, nil
}

func parseVersion(s []byte) (uint8, uint8, error) {
Expand Down
45 changes: 45 additions & 0 deletions packetbeat/protos/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,51 @@ func TestHttpParser_301_response(t *testing.T) {
assert.Equal(t, 290, msg.contentLength)
}

func TestHttpParser_PhraseContainsSpaces(t *testing.T) {
if testing.Verbose() {
logp.LogInit(logp.LOG_DEBUG, "", false, true, []string{"http"})
}
response_404 := "HTTP/1.1 404 Not Found\r\n" +
"Server: Apache-Coyote/1.1\r\n" +
"Content-Type: text/html;charset=utf-8\r\n" +
"Content-Length: 18\r\n" +
"Date: Mon, 31 Jul 2017 11:31:53 GMT\r\n" +
"\r\n" +
"Http Response Body"

r, ok, complete := testParse(nil, response_404)
assert.True(t, ok)
assert.True(t, complete)
assert.Equal(t, 18, r.contentLength)
assert.Equal(t, "Not Found", string(r.statusPhrase))
assert.Equal(t, 404, int(r.statusCode))

response_500 := "HTTP/1.1 500 Internal Server Error\r\n" +
"Server: Apache-Coyote/1.1\r\n" +
"Content-Type: text/html;charset=utf-8\r\n" +
"Content-Length: 2\r\n" +
"Date: Mon, 30 Jul 2017 00:00:00 GMT\r\n" +
"\r\n" +
"xx"
r, ok, complete = testParse(nil, response_500)
assert.True(t, ok)
assert.True(t, complete)
assert.Equal(t, 2, r.contentLength)
assert.Equal(t, "Internal Server Error", string(r.statusPhrase))
assert.Equal(t, 500, int(r.statusCode))

broken := "HTTP/1.1 500 \r\n" +
"Server: Apache-Coyote/1.1\r\n" +
"Content-Type: text/html;charset=utf-8\r\n" +
"Content-Length: 2\r\n" +
"Date: Mon, 30 Jul 2017 00:00:00 GMT\r\n" +
"\r\n" +
"xx"
r, ok, complete = testParse(nil, broken)
assert.False(t, ok)
assert.False(t, complete)
}

func TestEatBodyChunked(t *testing.T) {
if testing.Verbose() {
logp.LogInit(logp.LOG_DEBUG, "", false, true, []string{"http", "httpdetailed"})
Expand Down