Skip to content

Commit 5fa6923

Browse files
neildjoedian
authored andcommitted
[release-branch.go1.19] 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. Updates #60374 Fixes #61075 For CVE-2023-29406 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]> (cherry picked from commit 499458f) Reviewed-on: https://go-review.googlesource.com/c/go/+/507358 Run-TryBot: Tatiana Bradley <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]>
1 parent c4590af commit 5fa6923

File tree

4 files changed

+30
-75
lines changed

4 files changed

+30
-75
lines changed

src/net/http/http_test.go

-29
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

+10-37
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

@@ -571,12 +571,19 @@ func (r *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, waitF
571571
// is not given, use the host from the request URL.
572572
//
573573
// Clean the host, in case it arrives with unexpected stuff in it.
574-
host := cleanHost(r.Host)
574+
host := r.Host
575575
if host == "" {
576576
if r.URL == nil {
577577
return errMissingHost
578578
}
579-
host = cleanHost(r.URL.Host)
579+
host = r.URL.Host
580+
}
581+
host, err = httpguts.PunycodeHostPort(host)
582+
if err != nil {
583+
return err
584+
}
585+
if !httpguts.ValidHostHeader(host) {
586+
return errors.New("http: invalid Host header")
580587
}
581588

582589
// According to RFC 6874, an HTTP client, proxy, or other
@@ -733,40 +740,6 @@ func idnaASCII(v string) (string, error) {
733740
return idna.Lookup.ToASCII(v)
734741
}
735742

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

src/net/http/request_test.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -778,15 +778,8 @@ func TestRequestBadHost(t *testing.T) {
778778
}
779779
req.Host = "foo.com with spaces"
780780
req.URL.Host = "foo.com with spaces"
781-
req.Write(logWrites{t, &got})
782-
want := []string{
783-
"GET /after HTTP/1.1\r\n",
784-
"Host: foo.com\r\n",
785-
"User-Agent: " + DefaultUserAgent + "\r\n",
786-
"\r\n",
787-
}
788-
if !reflect.DeepEqual(got, want) {
789-
t.Errorf("Writes = %q\n Want = %q", got, want)
781+
if err := req.Write(logWrites{t, &got}); err == nil {
782+
t.Errorf("Writing request with invalid Host: succeded, want error")
790783
}
791784
}
792785

src/net/http/transport_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -6566,3 +6566,21 @@ func TestHandlerAbortRacesBodyRead(t *testing.T) {
65666566
}
65676567
wg.Wait()
65686568
}
6569+
6570+
func TestRequestSanitization(t *testing.T) {
6571+
setParallel(t)
6572+
defer afterTest(t)
6573+
6574+
ts := newClientServerTest(t, h1Mode, HandlerFunc(func(rw ResponseWriter, req *Request) {
6575+
if h, ok := req.Header["X-Evil"]; ok {
6576+
t.Errorf("request has X-Evil header: %q", h)
6577+
}
6578+
})).ts
6579+
defer ts.Close()
6580+
req, _ := NewRequest("GET", ts.URL, nil)
6581+
req.Host = "go.dev\r\nX-Evil:evil"
6582+
resp, _ := ts.Client().Do(req)
6583+
if resp != nil {
6584+
resp.Body.Close()
6585+
}
6586+
}

0 commit comments

Comments
 (0)