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

Empty X-Forwarded-Host header #1968

Closed
adriangonz opened this issue May 9, 2024 · 1 comment · Fixed by #1984
Closed

Empty X-Forwarded-Host header #1968

adriangonz opened this issue May 9, 2024 · 1 comment · Fixed by #1984
Assignees
Labels
backend Issues related to the backend
Milestone

Comments

@adriangonz
Copy link

Problem

In Go, trying to access the Host header directly of a request object through r.Header.Get("Host") always return an empty string - regardless of whether the header is actually set or not. Instead, you need to use the r.Host field (which under the hood will just read the actual header). This leads to the X-Forwarded-Host header being left empty when requests are proxied to the cluster API Server:

r.Header.Set("X-Forwarded-Host", r.Header.Get("Host"))

This can cause problems when the API Server is fronted by some reverse proxies (e.g. NGINX) which do a strict implementation of the HTTP 1.1 protocol - where the Host (or X-Forwarded-Host) has to always be present. Otherwise, every request sends back a 400 response with an error along the following lines:

400 Bad Request: missing required Host header error

Proposed Solution

To work around the above, it should be enough to just read the Host header directly via r.Host, i.e. doing something like (note that r.Host is already set on the line above):

r.Header.Set("X-Forwarded-Host", r.Host)
@joaquimrocha
Copy link
Collaborator

joaquimrocha commented May 10, 2024

Thanks a lot @adriangonz !
@yolossn Can you check this one out? I think this code was last touched by you.

@joaquimrocha joaquimrocha added this to the v0.24.0 milestone May 17, 2024
@knrt10 knrt10 added the backend Issues related to the backend label May 20, 2024
knrt10 added a commit that referenced this issue May 20, 2024
The header was not getting set correctly and returning an empty string

Fixes: #1968
Signed-off-by: Kautilya Tripathi <[email protected]>
knrt10 added a commit that referenced this issue May 21, 2024
The header was not getting set correctly and returning an empty string

Fixes: #1968
Signed-off-by: Kautilya Tripathi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Issues related to the backend
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants