Skip to content

Commit

Permalink
Merge branch 'go1.20.6' into merged-main
Browse files Browse the repository at this point in the history
Conflicts:
	http_test.go
	request.go
  • Loading branch information
bassosimone committed Jul 18, 2023
2 parents bde8728 + e5b8f0b commit a2e27a9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 75 deletions.
29 changes: 0 additions & 29 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,6 @@ func TestForeachHeaderElement(t *testing.T) {
}
}

func TestCleanHost(t *testing.T) {
tests := []struct {
in, want string
}{
{"www.google.com", "www.google.com"},
{"www.google.com foo", "www.google.com"},
{"www.google.com/foo", "www.google.com"},
{" first character is a space", ""},
{"[1::6]:8080", "[1::6]:8080"},

// Punycode:
{"гофер.рф/foo", "xn--c1ae0ajs.xn--p1ai"},
{"bücher.de", "xn--bcher-kva.de"},
{"bücher.de:8080", "xn--bcher-kva.de:8080"},
// Verify we convert to lowercase before punycode:
{"BÜCHER.de", "xn--bcher-kva.de"},
{"BÜCHER.de:8080", "xn--bcher-kva.de:8080"},
// Verify we normalize to NFC before punycode:
{"gophér.nfc", "xn--gophr-esa.nfc"}, // NFC input; no work needed
{"goph\u0065\u0301r.nfd", "xn--gophr-esa.nfd"}, // NFD input
}
for _, tt := range tests {
got := cleanHost(tt.in)
if tt.want != got {
t.Errorf("cleanHost(%q) = %q, want %q", tt.in, got, tt.want)
}
}
}

var valuesCount int

func BenchmarkCopyValues(b *testing.B) {
Expand Down
47 changes: 10 additions & 37 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"io"
"mime"
"mime/multipart"
"net"
"net/textproto"
"net/url"
urlpkg "net/url"
Expand All @@ -27,6 +26,7 @@ import (

"github.com/ooni/oohttp/httptrace"
"github.com/ooni/oohttp/internal/ascii"
"golang.org/x/net/http/httpguts"
"golang.org/x/net/idna"
)

Expand Down Expand Up @@ -575,12 +575,19 @@ func (r *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, waitF
// is not given, use the host from the request URL.
//
// Clean the host, in case it arrives with unexpected stuff in it.
host := cleanHost(r.Host)
host := r.Host
if host == "" {
if r.URL == nil {
return errMissingHost
}
host = cleanHost(r.URL.Host)
host = r.URL.Host
}
host, err = httpguts.PunycodeHostPort(host)
if err != nil {
return err
}
if !httpguts.ValidHostHeader(host) {
return errors.New("http: invalid Host header")
}

// According to RFC 6874, an HTTP client, proxy, or other
Expand Down Expand Up @@ -737,40 +744,6 @@ func idnaASCII(v string) (string, error) {
return idna.Lookup.ToASCII(v)
}

// cleanHost cleans up the host sent in request's Host header.
//
// It both strips anything after '/' or ' ', and puts the value
// into Punycode form, if necessary.
//
// Ideally we'd clean the Host header according to the spec:
//
// https://tools.ietf.org/html/rfc7230#section-5.4 (Host = uri-host [ ":" port ]")
// https://tools.ietf.org/html/rfc7230#section-2.7 (uri-host -> rfc3986's host)
// https://tools.ietf.org/html/rfc3986#section-3.2.2 (definition of host)
//
// But practically, what we are trying to avoid is the situation in
// issue 11206, where a malformed Host header used in the proxy context
// would create a bad request. So it is enough to just truncate at the
// first offending character.
func cleanHost(in string) string {
if i := strings.IndexAny(in, " /"); i != -1 {
in = in[:i]
}
host, port, err := net.SplitHostPort(in)
if err != nil { // input was just a host
a, err := idnaASCII(in)
if err != nil {
return in // garbage in, garbage out
}
return a
}
a, err := idnaASCII(host)
if err != nil {
return in // garbage in, garbage out
}
return net.JoinHostPort(a, port)
}

// removeZone removes IPv6 zone identifier from host.
// E.g., "[fe80::1%en0]:8080" to "[fe80::1]:8080"
func removeZone(host string) string {
Expand Down
11 changes: 2 additions & 9 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,15 +775,8 @@ func TestRequestBadHost(t *testing.T) {
}
req.Host = "foo.com with spaces"
req.URL.Host = "foo.com with spaces"
req.Write(logWrites{t, &got})
want := []string{
"GET /after HTTP/1.1\r\n",
"Host: foo.com\r\n",
"User-Agent: " + DefaultUserAgent + "\r\n",
"\r\n",
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Writes = %q\n Want = %q", got, want)
if err := req.Write(logWrites{t, &got}); err == nil {
t.Errorf("Writing request with invalid Host: succeded, want error")
}
}

Expand Down
19 changes: 19 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6282,3 +6282,22 @@ func testHandlerAbortRacesBodyRead(t *testing.T, mode testMode) {
}
wg.Wait()
}

func TestRequestSanitization(t *testing.T) { run(t, testRequestSanitization) }
func testRequestSanitization(t *testing.T, mode testMode) {
if mode == http2Mode {
// Remove this after updating x/net.
t.Skip("https://go.dev/issue/60374 test fails when run with HTTP/2")
}
ts := newClientServerTest(t, mode, HandlerFunc(func(rw ResponseWriter, req *Request) {
if h, ok := req.Header["X-Evil"]; ok {
t.Errorf("request has X-Evil header: %q", h)
}
})).ts
req, _ := NewRequest("GET", ts.URL, nil)
req.Host = "go.dev\r\nX-Evil:evil"
resp, _ := ts.Client().Do(req)
if resp != nil {
resp.Body.Close()
}
}

0 comments on commit a2e27a9

Please sign in to comment.