Skip to content

Commit 499458f

Browse files
committed
net/http: validate Host header before sending
Verify that the Host header we send is valid. Avoids surprising behavior such as a Host of "go.dev\r\nX-Evil:oops" adding an X-Evil header to HTTP/1 requests. Add a test, skip the test for HTTP/2. HTTP/2 is not vulnerable to header injection in the way HTTP/1 is, but x/net/http2 doesn't validate the header and will go into a retry loop when the server rejects it. CL 506995 adds the necessary validation to x/net/http2. For #60374 Change-Id: I05cb6866a9bead043101954dfded199258c6dd04 Reviewed-on: https://go-review.googlesource.com/c/go/+/506996 Reviewed-by: Tatiana Bradley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Damien Neil <[email protected]>
1 parent fe73c18 commit 499458f

File tree

4 files changed

+31
-75
lines changed

4 files changed

+31
-75
lines changed

src/net/http/http_test.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,35 +48,6 @@ func TestForeachHeaderElement(t *testing.T) {
4848
}
4949
}
5050

51-
func TestCleanHost(t *testing.T) {
52-
tests := []struct {
53-
in, want string
54-
}{
55-
{"www.google.com", "www.google.com"},
56-
{"www.google.com foo", "www.google.com"},
57-
{"www.google.com/foo", "www.google.com"},
58-
{" first character is a space", ""},
59-
{"[1::6]:8080", "[1::6]:8080"},
60-
61-
// Punycode:
62-
{"гофер.рф/foo", "xn--c1ae0ajs.xn--p1ai"},
63-
{"bücher.de", "xn--bcher-kva.de"},
64-
{"bücher.de:8080", "xn--bcher-kva.de:8080"},
65-
// Verify we convert to lowercase before punycode:
66-
{"BÜCHER.de", "xn--bcher-kva.de"},
67-
{"BÜCHER.de:8080", "xn--bcher-kva.de:8080"},
68-
// Verify we normalize to NFC before punycode:
69-
{"gophér.nfc", "xn--gophr-esa.nfc"}, // NFC input; no work needed
70-
{"goph\u0065\u0301r.nfd", "xn--gophr-esa.nfd"}, // NFD input
71-
}
72-
for _, tt := range tests {
73-
got := cleanHost(tt.in)
74-
if tt.want != got {
75-
t.Errorf("cleanHost(%q) = %q, want %q", tt.in, got, tt.want)
76-
}
77-
}
78-
}
79-
8051
// Test that cmd/go doesn't link in the HTTP server.
8152
//
8253
// This catches accidental dependencies between the HTTP transport and

src/net/http/request.go

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"io"
1818
"mime"
1919
"mime/multipart"
20-
"net"
2120
"net/http/httptrace"
2221
"net/http/internal/ascii"
2322
"net/textproto"
@@ -27,6 +26,7 @@ import (
2726
"strings"
2827
"sync"
2928

29+
"golang.org/x/net/http/httpguts"
3030
"golang.org/x/net/idna"
3131
)
3232

@@ -580,12 +580,19 @@ func (r *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, waitF
580580
// is not given, use the host from the request URL.
581581
//
582582
// Clean the host, in case it arrives with unexpected stuff in it.
583-
host := cleanHost(r.Host)
583+
host := r.Host
584584
if host == "" {
585585
if r.URL == nil {
586586
return errMissingHost
587587
}
588-
host = cleanHost(r.URL.Host)
588+
host = r.URL.Host
589+
}
590+
host, err = httpguts.PunycodeHostPort(host)
591+
if err != nil {
592+
return err
593+
}
594+
if !httpguts.ValidHostHeader(host) {
595+
return errors.New("http: invalid Host header")
589596
}
590597

591598
// According to RFC 6874, an HTTP client, proxy, or other
@@ -742,40 +749,6 @@ func idnaASCII(v string) (string, error) {
742749
return idna.Lookup.ToASCII(v)
743750
}
744751

745-
// cleanHost cleans up the host sent in request's Host header.
746-
//
747-
// It both strips anything after '/' or ' ', and puts the value
748-
// into Punycode form, if necessary.
749-
//
750-
// Ideally we'd clean the Host header according to the spec:
751-
//
752-
// https://tools.ietf.org/html/rfc7230#section-5.4 (Host = uri-host [ ":" port ]")
753-
// https://tools.ietf.org/html/rfc7230#section-2.7 (uri-host -> rfc3986's host)
754-
// https://tools.ietf.org/html/rfc3986#section-3.2.2 (definition of host)
755-
//
756-
// But practically, what we are trying to avoid is the situation in
757-
// issue 11206, where a malformed Host header used in the proxy context
758-
// would create a bad request. So it is enough to just truncate at the
759-
// first offending character.
760-
func cleanHost(in string) string {
761-
if i := strings.IndexAny(in, " /"); i != -1 {
762-
in = in[:i]
763-
}
764-
host, port, err := net.SplitHostPort(in)
765-
if err != nil { // input was just a host
766-
a, err := idnaASCII(in)
767-
if err != nil {
768-
return in // garbage in, garbage out
769-
}
770-
return a
771-
}
772-
a, err := idnaASCII(host)
773-
if err != nil {
774-
return in // garbage in, garbage out
775-
}
776-
return net.JoinHostPort(a, port)
777-
}
778-
779752
// removeZone removes IPv6 zone identifier from host.
780753
// E.g., "[fe80::1%en0]:8080" to "[fe80::1]:8080"
781754
func removeZone(host string) string {

src/net/http/request_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -775,15 +775,8 @@ func TestRequestBadHost(t *testing.T) {
775775
}
776776
req.Host = "foo.com with spaces"
777777
req.URL.Host = "foo.com with spaces"
778-
req.Write(logWrites{t, &got})
779-
want := []string{
780-
"GET /after HTTP/1.1\r\n",
781-
"Host: foo.com\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)
778+
if err := req.Write(logWrites{t, &got}); err == nil {
779+
t.Errorf("Writing request with invalid Host: succeded, want error")
787780
}
788781
}
789782

src/net/http/transport_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6731,3 +6731,22 @@ func testHandlerAbortRacesBodyRead(t *testing.T, mode testMode) {
67316731
}
67326732
wg.Wait()
67336733
}
6734+
6735+
func TestRequestSanitization(t *testing.T) { run(t, testRequestSanitization) }
6736+
func testRequestSanitization(t *testing.T, mode testMode) {
6737+
if mode == http2Mode {
6738+
// Remove this after updating x/net.
6739+
t.Skip("https://go.dev/issue/60374 test fails when run with HTTP/2")
6740+
}
6741+
ts := newClientServerTest(t, mode, HandlerFunc(func(rw ResponseWriter, req *Request) {
6742+
if h, ok := req.Header["X-Evil"]; ok {
6743+
t.Errorf("request has X-Evil header: %q", h)
6744+
}
6745+
})).ts
6746+
req, _ := NewRequest("GET", ts.URL, nil)
6747+
req.Host = "go.dev\r\nX-Evil:evil"
6748+
resp, _ := ts.Client().Do(req)
6749+
if resp != nil {
6750+
resp.Body.Close()
6751+
}
6752+
}

0 commit comments

Comments
 (0)