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

Packetbeat: Handle ports and IPv6 in Host header #14215

Merged
merged 4 commits into from
Oct 25, 2019

Conversation

adriansr
Copy link
Contributor

@adriansr adriansr commented Oct 24, 2019

The http parser in Packetbeat is not correctly populating events when the host header contains a port number and/or an IPv6 address:

For "Host: elasticsearch:9200":

< "destination.domain": "elasticsearch:9200"
< "url.full": "http://[elasticsearch:9200]:9200/"
> "destination.domain": "elasticsearch"
> "url.full": "http://elasticsearch:9200/"

For "Host: [::1]":

- "destination.domain": "[::1]"
 "url.full": "http://[::1]/"

For "Host: [::1]:9200":

- "destination.domain": "[::1]:9200"
<  "url.full": "http://[[::1]:9200]:9200/"
> "url.full": "http://[::1]:9200/"

@adriansr adriansr requested a review from a team as a code owner October 24, 2019 00:12
@adriansr adriansr added bug needs_backport PR is waiting to be backported to other branches. Packetbeat review labels Oct 24, 2019
@@ -95,6 +96,8 @@ func synthesizeFullURL(u *ecs.Url, port int64) string {
host := u.Domain
if port != 80 {
host = net.JoinHostPort(u.Domain, strconv.Itoa(int(u.Port)))
} else if strings.IndexByte(u.Domain, ':') != -1 {
host = "[" + u.Domain + "]"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is necessary because Golang's net/url package expects u.Domain already surrounded with square brackets when it's an IPv6, but surrounds it for you when you use net.JoinHostPort above.

Also checking for a : character to determine if the host its an IPv6 address is what the url package does internally.

@@ -701,6 +707,24 @@ func parseCookieValue(raw string) string {
return raw
}

func extractHostHeader(header string) (host string, port int) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole function can be replaced by url.Hostname() and url.Port() but those don't handles the case where an IPv6 address is not surrounded by brackets.

@adriansr
Copy link
Contributor Author

jenkins, test this

When an IPv6 address is used as host for a connection to port 80
the address in the URL won't be surrounded by square brackets which
is against RFC3986.
The HTTP parser in Packetbeat wasn't properly handling the hostname
and port from "Host:" header, generating wrong "destination.domain" and
"url.full" fields.
Copy link
Member

@andrewkroh andrewkroh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix. This explains some of the *.domain values I was seeing.

@adriansr adriansr merged commit 6acde25 into elastic:master Oct 25, 2019
adriansr added a commit to adriansr/beats that referenced this pull request Oct 25, 2019
The HTTP parser in Packetbeat wasn't properly handling the hostname
and port from "Host:" header, generating wrong "destination.domain" and
"url.full" fields.

(cherry picked from commit 6acde25)
adriansr added a commit that referenced this pull request Oct 30, 2019
The HTTP parser in Packetbeat wasn't properly handling the hostname
and port from "Host:" header, generating wrong "destination.domain" and
"url.full" fields.

(cherry picked from commit 6acde25)
jorgemarey pushed a commit to jorgemarey/beats that referenced this pull request Jun 8, 2020
The HTTP parser in Packetbeat wasn't properly handling the hostname
and port from "Host:" header, generating wrong "destination.domain" and
"url.full" fields.
leweafan pushed a commit to leweafan/beats that referenced this pull request Apr 28, 2023
…elastic#14232)

The HTTP parser in Packetbeat wasn't properly handling the hostname
and port from "Host:" header, generating wrong "destination.domain" and
"url.full" fields.

(cherry picked from commit e4bcff8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs_backport PR is waiting to be backported to other branches. Packetbeat review v7.5.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants