Skip to content

Commit 6e6f4aa

Browse files
committed
[release-branch.go1.12-security] net/textproto: don't normalize headers with spaces before the colon
RFC 7230 is clear about headers with a space before the colon, like X-Answer : 42 being invalid, but we've been accepting and normalizing them for compatibility purposes since CL 5690059 in 2012. On the client side, this is harmless and indeed most browsers behave the same to this day. On the server side, this becomes a security issue when the behavior doesn't match that of a reverse proxy sitting in front of the server. For example, if a WAF accepts them without normalizing them, it might be possible to bypass its filters, because the Go server would interpret the header differently. Worse, if the reverse proxy coalesces requests onto a single HTTP/1.1 connection to a Go server, the understanding of the request boundaries can get out of sync between them, allowing an attacker to tack an arbitrary method and path onto a request by other clients, including authentication headers unknown to the attacker. This was recently presented at multiple security conferences: https://portswigger.net/blog/http-desync-attacks-request-smuggling-reborn net/http servers already reject header keys with invalid characters. Simply stop normalizing extra spaces in net/textproto, let it return them unchanged like it does for other invalid headers, and let net/http enforce RFC 7230, which is HTTP specific. This loses us normalization on the client side, but there's no right answer on the client side anyway, and hiding the issue sounds worse than letting the application decide. Fixes CVE-2019-16276 Change-Id: I6d272de827e0870da85d93df770d6a0e161bbcf1 Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/549719 Reviewed-by: Brad Fitzpatrick <[email protected]> (cherry picked from commit 1280b868e82bf173ea3e988be3092d160ee66082) Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/558776 Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 06472b9 commit 6e6f4aa

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

src/net/http/serve_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4748,6 +4748,10 @@ func TestServerValidatesHeaders(t *testing.T) {
47484748
{"foo\xffbar: foo\r\n", 400}, // binary in header
47494749
{"foo\x00bar: foo\r\n", 400}, // binary in header
47504750
{"Foo: " + strings.Repeat("x", 1<<21) + "\r\n", 431}, // header too large
4751+
// Spaces between the header key and colon are not allowed.
4752+
// See RFC 7230, Section 3.2.4.
4753+
{"Foo : bar\r\n", 400},
4754+
{"Foo\t: bar\r\n", 400},
47514755

47524756
{"foo: foo foo\r\n", 200}, // LWS space is okay
47534757
{"foo: foo\tfoo\r\n", 200}, // LWS tab is okay

src/net/http/transport_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5133,3 +5133,30 @@ func TestTransportIgnores408(t *testing.T) {
51335133
}
51345134
t.Fatalf("timeout after %v waiting for Transport connections to die off", time.Since(t0))
51355135
}
5136+
5137+
func TestInvalidHeaderResponse(t *testing.T) {
5138+
setParallel(t)
5139+
defer afterTest(t)
5140+
cst := newClientServerTest(t, h1Mode, HandlerFunc(func(w ResponseWriter, r *Request) {
5141+
conn, buf, _ := w.(Hijacker).Hijack()
5142+
buf.Write([]byte("HTTP/1.1 200 OK\r\n" +
5143+
"Date: Wed, 30 Aug 2017 19:09:27 GMT\r\n" +
5144+
"Content-Type: text/html; charset=utf-8\r\n" +
5145+
"Content-Length: 0\r\n" +
5146+
"Foo : bar\r\n\r\n"))
5147+
buf.Flush()
5148+
conn.Close()
5149+
}))
5150+
defer cst.close()
5151+
res, err := cst.c.Get(cst.ts.URL)
5152+
if err != nil {
5153+
t.Fatal(err)
5154+
}
5155+
defer res.Body.Close()
5156+
if v := res.Header.Get("Foo"); v != "" {
5157+
t.Errorf(`unexpected "Foo" header: %q`, v)
5158+
}
5159+
if v := res.Header.Get("Foo "); v != "bar" {
5160+
t.Errorf(`bad "Foo " header value: %q, want %q`, v, "bar")
5161+
}
5162+
}

src/net/textproto/reader.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -493,18 +493,12 @@ func (r *Reader) ReadMIMEHeader() (MIMEHeader, error) {
493493
return m, err
494494
}
495495

496-
// Key ends at first colon; should not have trailing spaces
497-
// but they appear in the wild, violating specs, so we remove
498-
// them if present.
496+
// Key ends at first colon.
499497
i := bytes.IndexByte(kv, ':')
500498
if i < 0 {
501499
return m, ProtocolError("malformed MIME header line: " + string(kv))
502500
}
503-
endKey := i
504-
for endKey > 0 && kv[endKey-1] == ' ' {
505-
endKey--
506-
}
507-
key := canonicalMIMEHeaderKey(kv[:endKey])
501+
key := canonicalMIMEHeaderKey(kv[:i])
508502

509503
// As per RFC 7230 field-name is a token, tokens consist of one or more chars.
510504
// We could return a ProtocolError here, but better to be liberal in what we

src/net/textproto/reader_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,10 @@ func TestLargeReadMIMEHeader(t *testing.T) {
188188
}
189189
}
190190

191-
// Test that we read slightly-bogus MIME headers seen in the wild,
192-
// with spaces before colons, and spaces in keys.
191+
// TestReadMIMEHeaderNonCompliant checks that we don't normalize headers
192+
// with spaces before colons, and accept spaces in keys.
193193
func TestReadMIMEHeaderNonCompliant(t *testing.T) {
194-
// Invalid HTTP response header as sent by an Axis security
195-
// camera: (this is handled by IE, Firefox, Chrome, curl, etc.)
194+
// These invalid headers will be rejected by net/http according to RFC 7230.
196195
r := reader("Foo: bar\r\n" +
197196
"Content-Language: en\r\n" +
198197
"SID : 0\r\n" +
@@ -202,9 +201,9 @@ func TestReadMIMEHeaderNonCompliant(t *testing.T) {
202201
want := MIMEHeader{
203202
"Foo": {"bar"},
204203
"Content-Language": {"en"},
205-
"Sid": {"0"},
206-
"Audio Mode": {"None"},
207-
"Privilege": {"127"},
204+
"SID ": {"0"},
205+
"Audio Mode ": {"None"},
206+
"Privilege ": {"127"},
208207
}
209208
if !reflect.DeepEqual(m, want) || err != nil {
210209
t.Fatalf("ReadMIMEHeader =\n%v, %v; want:\n%v", m, err, want)

0 commit comments

Comments
 (0)