Skip to content

Commit b9153f6

Browse files
committed
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 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]>
1 parent 26e0660 commit b9153f6

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/net/http/request.go

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

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

src/net/http/request_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,16 +767,23 @@ func TestRequestWriteBufferedWriter(t *testing.T) {
767767
}
768768
}
769769

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

0 commit comments

Comments
 (0)