pgreco / rpms / golang

Forked from rpms/golang 5 years ago
Clone

Blame SOURCES/reject-control-chars.patch

a8be19
From eb0f2b3d27a896e4b832f2450490a2bbf72fbb6c Mon Sep 17 00:00:00 2001
a8be19
From: Brad Fitzpatrick <bradfitz@golang.org>
a8be19
Date: Thu, 31 Jan 2019 20:17:12 +0000
a8be19
Subject: [PATCH] [release-branch.go1.11] net/http, net/url: reject control
a8be19
 characters in URLs
a8be19
a8be19
Cherry pick of combined CL 159157 + CL 160178.
a8be19
a8be19
Fixes #29923
a8be19
Updates #27302
a8be19
Updates #22907
a8be19
a8be19
Change-Id: I6de92c14284595a58321a4b4d53229285979b872
a8be19
Reviewed-on: https://go-review.googlesource.com/c/160798
a8be19
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
a8be19
TryBot-Result: Gobot Gobot <gobot@golang.org>
a8be19
Reviewed-by: Ian Lance Taylor <iant@golang.org>
a8be19
---
a8be19
 src/net/http/fs_test.go           | 15 +++++++++++----
a8be19
 src/net/http/http.go              | 11 +++++++++++
a8be19
 src/net/http/request.go           |  7 ++++++-
a8be19
 src/net/http/requestwrite_test.go | 11 +++++++++++
a8be19
 src/net/url/url.go                | 15 +++++++++++++++
a8be19
 src/net/url/url_test.go           | 23 ++++++++++++++++++++++-
a8be19
 6 files changed, 76 insertions(+), 6 deletions(-)
a8be19
a8be19
diff --git a/src/net/http/fs_test.go b/src/net/http/fs_test.go
a8be19
index 255d215f3cf..762e88b05ff 100644
a8be19
--- a/src/net/http/fs_test.go
a8be19
+++ b/src/net/http/fs_test.go
a8be19
@@ -583,16 +583,23 @@ func TestFileServerZeroByte(t *testing.T) {
a8be19
 	ts := httptest.NewServer(FileServer(Dir(".")))
a8be19
 	defer ts.Close()
a8be19
 
a8be19
-	res, err := Get(ts.URL + "/..\x00")
a8be19
+	c, err := net.Dial("tcp", ts.Listener.Addr().String())
a8be19
 	if err != nil {
a8be19
 		t.Fatal(err)
a8be19
 	}
a8be19
-	b, err := ioutil.ReadAll(res.Body)
a8be19
+	defer c.Close()
a8be19
+	_, err = fmt.Fprintf(c, "GET /..\x00 HTTP/1.0\r\n\r\n")
a8be19
+	if err != nil {
a8be19
+		t.Fatal(err)
a8be19
+	}
a8be19
+	var got bytes.Buffer
a8be19
+	bufr := bufio.NewReader(io.TeeReader(c, &got))
a8be19
+	res, err := ReadResponse(bufr, nil)
a8be19
 	if err != nil {
a8be19
-		t.Fatal("reading Body:", err)
a8be19
+		t.Fatal("ReadResponse: ", err)
a8be19
 	}
a8be19
 	if res.StatusCode == 200 {
a8be19
-		t.Errorf("got status 200; want an error. Body is:\n%s", string(b))
a8be19
+		t.Errorf("got status 200; want an error. Body is:\n%s", got.Bytes())
a8be19
 	}
a8be19
 }
a8be19
 
a8be19
diff --git a/src/net/http/http.go b/src/net/http/http.go
a8be19
index ce0eceb1de3..07ca78dbc84 100644
a8be19
--- a/src/net/http/http.go
a8be19
+++ b/src/net/http/http.go
a8be19
@@ -59,6 +59,17 @@ func isASCII(s string) bool {
a8be19
 	return true
a8be19
 }
a8be19
 
a8be19
+// stringContainsCTLByte reports whether s contains any ASCII control character.
a8be19
+func stringContainsCTLByte(s string) bool {
a8be19
+	for i := 0; i < len(s); i++ {
a8be19
+		b := s[i]
a8be19
+		if b < ' ' || b == 0x7f {
a8be19
+			return true
a8be19
+		}
a8be19
+	}
a8be19
+	return false
a8be19
+}
a8be19
+
a8be19
 func hexEscapeNonASCII(s string) string {
a8be19
 	newLen := 0
a8be19
 	for i := 0; i < len(s); i++ {
a8be19
diff --git a/src/net/http/request.go b/src/net/http/request.go
a8be19
index a40b0a3cb83..e352386b083 100644
a8be19
--- a/src/net/http/request.go
a8be19
+++ b/src/net/http/request.go
a8be19
@@ -545,7 +545,12 @@ func (r *Request) write(w io.Writer, usingProxy bool, extraHeaders Header, waitF
a8be19
 		// CONNECT requests normally give just the host and port, not a full URL.
a8be19
 		ruri = host
a8be19
 	}
a8be19
-	// TODO(bradfitz): escape at least newlines in ruri?
a8be19
+	if stringContainsCTLByte(ruri) {
a8be19
+		return errors.New("net/http: can't write control character in Request.URL")
a8be19
+	}
a8be19
+	// TODO: validate r.Method too? At least it's less likely to
a8be19
+	// come from an attacker (more likely to be a constant in
a8be19
+	// code).
a8be19
 
a8be19
 	// Wrap the writer in a bufio Writer if it's not already buffered.
a8be19
 	// Don't always call NewWriter, as that forces a bytes.Buffer
a8be19
diff --git a/src/net/http/requestwrite_test.go b/src/net/http/requestwrite_test.go
a8be19
index eb65b9f736f..3daab4b8b7b 100644
a8be19
--- a/src/net/http/requestwrite_test.go
a8be19
+++ b/src/net/http/requestwrite_test.go
a8be19
@@ -512,6 +512,17 @@ var reqWriteTests = []reqWriteTest{
a8be19
 			"User-Agent: Go-http-client/1.1\r\n" +
a8be19
 			"\r\n",
a8be19
 	},
a8be19
+
a8be19
+	21: {
a8be19
+		Req: Request{
a8be19
+			Method: "GET",
a8be19
+			URL: &url.URL{
a8be19
+				Host:     "www.example.com",
a8be19
+				RawQuery: "new\nline", // or any CTL
a8be19
+			},
a8be19
+		},
a8be19
+		WantError: errors.New("net/http: can't write control character in Request.URL"),
a8be19
+	},
a8be19
 }
a8be19
 
a8be19
 func TestRequestWrite(t *testing.T) {
a8be19
diff --git a/src/net/url/url.go b/src/net/url/url.go
a8be19
index 80eb7a86c8d..8d2a8566998 100644
a8be19
--- a/src/net/url/url.go
a8be19
+++ b/src/net/url/url.go
a8be19
@@ -494,6 +494,10 @@ func parse(rawurl string, viaRequest bool) (*URL, error) {
a8be19
 	var rest string
a8be19
 	var err error
a8be19
 
a8be19
+	if stringContainsCTLByte(rawurl) {
a8be19
+		return nil, errors.New("net/url: invalid control character in URL")
a8be19
+	}
a8be19
+
a8be19
 	if rawurl == "" && viaRequest {
a8be19
 		return nil, errors.New("empty url")
a8be19
 	}
a8be19
@@ -1114,3 +1118,14 @@ func validUserinfo(s string) bool {
a8be19
 	}
a8be19
 	return true
a8be19
 }
a8be19
+
a8be19
+// stringContainsCTLByte reports whether s contains any ASCII control character.
a8be19
+func stringContainsCTLByte(s string) bool {
a8be19
+	for i := 0; i < len(s); i++ {
a8be19
+		b := s[i]
a8be19
+		if b < ' ' || b == 0x7f {
a8be19
+			return true
a8be19
+		}
a8be19
+	}
a8be19
+	return false
a8be19
+}
a8be19
diff --git a/src/net/url/url_test.go b/src/net/url/url_test.go
a8be19
index 9043a844e88..369ea6cbd25 100644
a8be19
--- a/src/net/url/url_test.go
a8be19
+++ b/src/net/url/url_test.go
a8be19
@@ -1738,8 +1738,29 @@ func TestNilUser(t *testing.T) {
a8be19
 }
a8be19
 
a8be19
 func TestInvalidUserPassword(t *testing.T) {
a8be19
-	_, err := Parse("http://us\ner:pass\nword@foo.com/")
a8be19
+	_, err := Parse("http://user^:passwo^rd@foo.com/")
a8be19
 	if got, wantsub := fmt.Sprint(err), "net/url: invalid userinfo"; !strings.Contains(got, wantsub) {
a8be19
 		t.Errorf("error = %q; want substring %q", got, wantsub)
a8be19
 	}
a8be19
 }
a8be19
+
a8be19
+func TestRejectControlCharacters(t *testing.T) {
a8be19
+	tests := []string{
a8be19
+		"http://foo.com/?foo\nbar",
a8be19
+		"http\r://foo.com/",
a8be19
+		"http://foo\x7f.com/",
a8be19
+	}
a8be19
+	for _, s := range tests {
a8be19
+		_, err := Parse(s)
a8be19
+		const wantSub = "net/url: invalid control character in URL"
a8be19
+		if got := fmt.Sprint(err); !strings.Contains(got, wantSub) {
a8be19
+			t.Errorf("Parse(%q) error = %q; want substring %q", s, got, wantSub)
a8be19
+		}
a8be19
+	}
a8be19
+
a8be19
+	// But don't reject non-ASCII CTLs, at least for now:
a8be19
+	if _, err := Parse("http://foo.com/ctl\x80"); err != nil {
a8be19
+		t.Errorf("error parsing URL with non-ASCII control byte: %v", err)
a8be19
+	}
a8be19
+
a8be19
+}