Skip to content

Commit ede3e27

Browse files
neildgopherbot
authored andcommitted
[release-branch.go1.20] net/http: permit requests with invalid Host headers
Historically, the Transport has silently truncated invalid Host headers at the first '/' or ' ' character. CL 506996 changed this behavior to reject invalid Host headers entirely. Unfortunately, Docker appears to rely on the previous behavior. When sending a HTTP/1 request with an invalid Host, send an empty Host header. This is safer than truncation: If you care about the Host, then you should get the one you set; if you don't care, then an empty Host should be fine. Continue to fully validate Host headers sent to a proxy, since proxies generally can't productively forward requests without a Host. For #60374 Fixes #61431 Fixes #61826 Change-Id: If170c7dd860aa20eb58fe32990fc93af832742b6 Reviewed-on: https://go-review.googlesource.com/c/go/+/511155 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]> Run-TryBot: Damien Neil <[email protected]> (cherry picked from commit b9153f6) Reviewed-on: https://go-review.googlesource.com/c/go/+/518756 Auto-Submit: Dmitri Shuralyov <[email protected]> Reviewed-by: Russ Cox <[email protected]> Run-TryBot: Roland Shoemaker <[email protected]>
1 parent 201f8b4 commit ede3e27

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/net/http/request.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,29 @@ func (r *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, waitF
586586
if err != nil {
587587
return err
588588
}
589+
// Validate that the Host header is a valid header in general,
590+
// but don't validate the host itself. This is sufficient to avoid
591+
// header or request smuggling via the Host field.
592+
// The server can (and will, if it's a net/http server) reject
593+
// the request if it doesn't consider the host valid.
589594
if !httpguts.ValidHostHeader(host) {
590-
return errors.New("http: invalid Host header")
595+
// Historically, we would truncate the Host header after '/' or ' '.
596+
// Some users have relied on this truncation to convert a network
597+
// address such as Unix domain socket path into a valid, ignored
598+
// Host header (see https://go.dev/issue/61431).
599+
//
600+
// We don't preserve the truncation, because sending an altered
601+
// header field opens a smuggling vector. Instead, zero out the
602+
// Host header entirely if it isn't valid. (An empty Host is valid;
603+
// see RFC 9112 Section 3.2.)
604+
//
605+
// Return an error if we're sending to a proxy, since the proxy
606+
// probably can't do anything useful with an empty Host header.
607+
if !usingProxy {
608+
host = ""
609+
} else {
610+
return errors.New("http: invalid Host header")
611+
}
591612
}
592613

593614
// According to RFC 6874, an HTTP client, proxy, or other

src/net/http/request_test.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -766,16 +766,23 @@ func TestRequestWriteBufferedWriter(t *testing.T) {
766766
}
767767
}
768768

769-
func TestRequestBadHost(t *testing.T) {
769+
func TestRequestBadHostHeader(t *testing.T) {
770770
got := []string{}
771771
req, err := NewRequest("GET", "http://foo/after", nil)
772772
if err != nil {
773773
t.Fatal(err)
774774
}
775-
req.Host = "foo.com with spaces"
776-
req.URL.Host = "foo.com with spaces"
777-
if err := req.Write(logWrites{t, &got}); err == nil {
778-
t.Errorf("Writing request with invalid Host: succeded, want error")
775+
req.Host = "foo.com\nnewline"
776+
req.URL.Host = "foo.com\nnewline"
777+
req.Write(logWrites{t, &got})
778+
want := []string{
779+
"GET /after HTTP/1.1\r\n",
780+
"Host: \r\n",
781+
"User-Agent: " + DefaultUserAgent + "\r\n",
782+
"\r\n",
783+
}
784+
if !reflect.DeepEqual(got, want) {
785+
t.Errorf("Writes = %q\n Want = %q", got, want)
779786
}
780787
}
781788

0 commit comments

Comments
 (0)