kibana icon indicating copy to clipboard operation
kibana copied to clipboard

[Core] Hostname is not escaped before setting header

Open dgieselaar opened this issue 3 years ago • 6 comments

Kibana sets the value of the kbn-name header to the system's hostname. If this hostname contains an invalid character (like '), all requests will fail with a 500 status code. Example of a hostname that causes the issue: Coen's-Macbook-Local-Pro.

dgieselaar avatar Aug 30 '22 14:08 dgieselaar

Pinging @elastic/kibana-core (Team:Core)

elasticmachine avatar Aug 30 '22 14:08 elasticmachine

Some pointers:

the server name comes from the server.name configuration option, which defaults to os.hostname():

https://github.com/elastic/kibana/blob/84db06c15d04bcaa011c41daf13ad1521deba604/packages/core/http/core-http-server-internal/src/http_config.ts#L37

The kbn-name header is set by an internal pre-response handler:

https://github.com/elastic/kibana/blob/84db06c15d04bcaa011c41daf13ad1521deba604/packages/core/http/core-http-server-internal/src/lifecycle_handlers.ts#L66-L71

Which, in the end, is applied to the HAPI response object using the official ResponseObject.header() API, here:

https://github.com/elastic/kibana/blob/84db06c15d04bcaa011c41daf13ad1521deba604/packages/core/http/core-http-server-internal/src/lifecycle/on_pre_response.ts#L121-L125

So the question is: why isn't HAPI itself properly handling / escaping invalid characters, and should it?

pgayvallet avatar Sep 01 '22 12:09 pgayvallet

Given that Linux hostnames must be [a-z][0-9][-] this would be limited to running Kibana on OSX https://man7.org/linux/man-pages/man7/hostname.7.html#:~:text=Each%20element%20of%20the%20hostname,not%20start%20with%20a%20hyphen.

rudolf avatar Sep 13 '22 14:09 rudolf

We should look into a few things:

  • Would upstream hapi consider this a bug that could be fixed?
  • How are these errors not getting properly surfaced?
  • Can we strip out these chars Kibana-side for the time being?

lukeelmers avatar Sep 13 '22 14:09 lukeelmers

@lukeelmers @pgayvallet regardless of whose responsibility it is to fix, if this is easy, can we patch this on our side sooner than later? This is really confusing to folks on new machines, and those are especially folks new to the company/Kibana.

dgieselaar avatar Sep 21 '22 20:09 dgieselaar

Even if not fixed, should log something to point users in the right direction.

kqualters-elastic avatar Sep 21 '22 21:09 kqualters-elastic

can we patch this on our side sooner than later?

@dgieselaar I'm hoping to have this addressed in the next 3 weeks -- we are meeting on Tuesday to confirm our plans for that timeframe.

lukeelmers avatar Sep 21 '22 22:09 lukeelmers

Okay, so with just a bit of investigation: The actual 'single quote' char used in the default computer name in macOS settings is not a single quote (ascii 39), but a (code 8217), which is absolutely not allowed in headers (or in hostnames if we follow the unix specification, FWIW...)

Screenshot 2022-09-22 at 09 24 14

FWIW, on macOS 10/11, this 'computer name' thing doesn't seem to be used as hostname (at least hostname or scutil --get HostName from a terminal doesn't reflect it). But it seems to be the case on macOS 12, apparently.

Fun thing is, when pasting this is slack, the 8217 seems to be automatically converted to 39, which leads to more misleads.

Adding

server.name: "Pierre’s MacBook Pro"

to my config reproduce the issue:

Screenshot 2022-09-22 at 09 25 03

To unblock the dev experience, I will open a PR to remove all non-ascii characters from the default value for the server.name configuration option (feels very safe, given unix hostnames don't have non-ascii chars, and, well, I don't think anyone uses a mac in production, and they would be affected if they do anyway). This will allow us to be less stressed by time to look at the issue at a whole here.

pgayvallet avatar Sep 22 '22 07:09 pgayvallet

Here, https://github.com/elastic/kibana/pull/141350

pgayvallet avatar Sep 22 '22 07:09 pgayvallet

Lowering the impact on this issue as the short term solution in https://github.com/elastic/kibana/pull/141350 has unblocked the dev experience for now.

lukeelmers avatar Sep 28 '22 22:09 lukeelmers

The workaround is sufficient. Closing this issue

rayafratkina avatar Jun 18 '24 14:06 rayafratkina