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 missing host header in http check #15337

Merged
merged 7 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions client/serviceregistration/checks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ func (c *checker) checkHTTP(ctx context.Context, qc *QueryContext, q *Query) *st
return qr
}
request.Header = q.Headers

for key, values := range q.Headers {
if strings.EqualFold(key, "Host") && len(values) > 0 {
request.Host = values[0]
delete(q.Headers, key)
}
}
SamMousa marked this conversation as resolved.
Show resolved Hide resolved

request.Body = io.NopCloser(strings.NewReader(q.Body))
request = request.WithContext(ctx)

Expand Down
36 changes: 36 additions & 0 deletions client/serviceregistration/checks/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,14 @@ func TestChecker_Do_HTTP_extras(t *testing.T) {
method string
body []byte
headers map[string][]string
host string
)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
method = r.Method
body, _ = io.ReadAll(r.Body)
headers = maps.Clone(r.Header)
host = r.Host
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
Expand Down Expand Up @@ -269,6 +271,25 @@ func TestChecker_Do_HTTP_extras(t *testing.T) {
[2]string{"Authorization", "Basic ZWxhc3RpYzpjaGFuZ2VtZQ=="},
),
},
{
name: "host header",
method: "GET",
headers: makeHeaders(encoding, agent,
[2]string{"Host", "hello"},
[2]string{"Test-Abc", "hello"},
),
},
{
name: "host header",
Copy link
Member

Choose a reason for hiding this comment

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

nit: this is the second test case named "host header"; making them unique helps quickly tracking down failures

method: "GET",
body: "",
// This is needed to prevent header normalization by http.Header.Set
headers: func() map[string][]string {
h := makeHeaders(encoding, agent, [2]string{"Test-Abc", "hello"})
h["hoST"] = []string{"heLLO"}
return h
}(),
},
Comment on lines +287 to +292
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We still test sending in weird data although we don't expect our code to ever send us this data since everything is normalized at the edge.

{
name: "with body",
method: "POST",
Expand Down Expand Up @@ -312,9 +333,24 @@ func TestChecker_Do_HTTP_extras(t *testing.T) {
must.Eq(t, http.StatusOK, result.StatusCode,
must.Sprintf("test.URL: %s", ts.URL),
must.Sprintf("headers: %v", tc.headers),
must.Sprintf("received headers: %v", tc.headers),
)
must.Eq(t, tc.method, method)
must.Eq(t, tc.body, string(body))

hostSent := false

for key, values := range tc.headers {
if strings.EqualFold(key, "Host") && len(values) > 0 {
must.Eq(t, values[0], host)
hostSent = true

}
}
if !hostSent {
must.Eq(t, nil, tc.headers["Host"])
}

must.Eq(t, tc.headers, headers)
})
}
Expand Down