Skip to content

Commit 41b1f88

Browse files
FiloSottiledmitshur
authored andcommitted
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 Fixes #34540 Change-Id: I6d272de827e0870da85d93df770d6a0e161bbcf1 Reviewed-on: https://team-review.git.corp.google.com/c/golang/go-private/+/549719 Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/197503 Run-TryBot: Filippo Valsorda <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 0ad3686 commit 41b1f88

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

src/net/http/serve_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -4755,6 +4755,10 @@ func TestServerValidatesHeaders(t *testing.T) {
47554755
{"foo\xffbar: foo\r\n", 400}, // binary in header
47564756
{"foo\x00bar: foo\r\n", 400}, // binary in header
47574757
{"Foo: " + strings.Repeat("x", 1<<21) + "\r\n", 431}, // header too large
4758+
// Spaces between the header key and colon are not allowed.
4759+
// See RFC 7230, Section 3.2.4.
4760+
{"Foo : bar\r\n", 400},
4761+
{"Foo\t: bar\r\n", 400},
47584762

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

src/net/http/transport_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -5692,3 +5692,30 @@ func TestTransportIgnores408(t *testing.T) {
56925692
}
56935693
t.Fatalf("timeout after %v waiting for Transport connections to die off", time.Since(t0))
56945694
}
5695+
5696+
func TestInvalidHeaderResponse(t *testing.T) {
5697+
setParallel(t)
5698+
defer afterTest(t)
5699+
cst := newClientServerTest(t, h1Mode, HandlerFunc(func(w ResponseWriter, r *Request) {
5700+
conn, buf, _ := w.(Hijacker).Hijack()
5701+
buf.Write([]byte("HTTP/1.1 200 OK\r\n" +
5702+
"Date: Wed, 30 Aug 2017 19:09:27 GMT\r\n" +
5703+
"Content-Type: text/html; charset=utf-8\r\n" +
5704+
"Content-Length: 0\r\n" +
5705+
"Foo : bar\r\n\r\n"))
5706+
buf.Flush()
5707+
conn.Close()
5708+
}))
5709+
defer cst.close()
5710+
res, err := cst.c.Get(cst.ts.URL)
5711+
if err != nil {
5712+
t.Fatal(err)
5713+
}
5714+
defer res.Body.Close()
5715+
if v := res.Header.Get("Foo"); v != "" {
5716+
t.Errorf(`unexpected "Foo" header: %q`, v)
5717+
}
5718+
if v := res.Header.Get("Foo "); v != "bar" {
5719+
t.Errorf(`bad "Foo " header value: %q, want %q`, v, "bar")
5720+
}
5721+
}

src/net/textproto/reader.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -495,18 +495,12 @@ func (r *Reader) ReadMIMEHeader() (MIMEHeader, error) {
495495
return m, err
496496
}
497497

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

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

src/net/textproto/reader_test.go

+6-7
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)