Skip to content

Commit c052222

Browse files
committed
net/http: don't accept invalid bytes in server request headers
Fixes #11207 Change-Id: I7f00b638e749fbc7907dc1597347ea426367d13e Reviewed-on: https://go-review.googlesource.com/17980 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
1 parent 18227bb commit c052222

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

src/net/http/request.go

+21
Original file line numberDiff line numberDiff line change
@@ -1121,3 +1121,24 @@ var validHostByte = [256]bool{
11211121
'_': true, // unreserved
11221122
'~': true, // unreserved
11231123
}
1124+
1125+
func validHeaderName(v string) bool {
1126+
if len(v) == 0 {
1127+
return false
1128+
}
1129+
return strings.IndexFunc(v, isNotToken) == -1
1130+
}
1131+
1132+
func validHeaderValue(v string) bool {
1133+
for i := 0; i < len(v); i++ {
1134+
b := v[i]
1135+
if b == '\t' {
1136+
continue
1137+
}
1138+
if ' ' <= b && b <= '~' {
1139+
continue
1140+
}
1141+
return false
1142+
}
1143+
return true
1144+
}

src/net/http/serve_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -3629,6 +3629,7 @@ func testHandlerSetsBodyNil(t *testing.T, h2 bool) {
36293629
}
36303630

36313631
// Test that we validate the Host header.
3632+
// Issue 11206 (invalid bytes in Host) and 13624 (Host present in HTTP/1.1)
36323633
func TestServerValidatesHostHeader(t *testing.T) {
36333634
tests := []struct {
36343635
proto string
@@ -3676,6 +3677,43 @@ func TestServerValidatesHostHeader(t *testing.T) {
36763677
}
36773678
}
36783679

3680+
// Test that we validate the valid bytes in HTTP/1 headers.
3681+
// Issue 11207.
3682+
func TestServerValidatesHeaders(t *testing.T) {
3683+
tests := []struct {
3684+
header string
3685+
want int
3686+
}{
3687+
{"", 200},
3688+
{"Foo: bar\r\n", 200},
3689+
{"X-Foo: bar\r\n", 200},
3690+
{"Foo: a space\r\n", 200},
3691+
3692+
{"A space: foo\r\n", 400}, // space in header
3693+
{"foo\xffbar: foo\r\n", 400}, // binary in header
3694+
{"foo\x00bar: foo\r\n", 400}, // binary in header
3695+
3696+
{"foo: foo\x00foo\r\n", 400}, // binary in value
3697+
{"foo: foo\xfffoo\r\n", 400}, // binary in value
3698+
}
3699+
for _, tt := range tests {
3700+
conn := &testConn{closec: make(chan bool)}
3701+
io.WriteString(&conn.readBuf, "GET / HTTP/1.1\r\nHost: foo\r\n"+tt.header+"\r\n")
3702+
3703+
ln := &oneConnListener{conn}
3704+
go Serve(ln, HandlerFunc(func(ResponseWriter, *Request) {}))
3705+
<-conn.closec
3706+
res, err := ReadResponse(bufio.NewReader(&conn.writeBuf), nil)
3707+
if err != nil {
3708+
t.Errorf("For %q, ReadResponse: %v", tt.header, res)
3709+
continue
3710+
}
3711+
if res.StatusCode != tt.want {
3712+
t.Errorf("For %q, Status = %d; want %d", tt.header, res.StatusCode, tt.want)
3713+
}
3714+
}
3715+
}
3716+
36793717
func BenchmarkClientServer(b *testing.B) {
36803718
b.ReportAllocs()
36813719
b.StopTimer()

src/net/http/server.go

+10
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,16 @@ func (c *conn) readRequest() (w *response, err error) {
707707
if len(hosts) == 1 && !validHostHeader(hosts[0]) {
708708
return nil, badRequestError("malformed Host header")
709709
}
710+
for k, vv := range req.Header {
711+
if !validHeaderName(k) {
712+
return nil, badRequestError("invalid header name")
713+
}
714+
for _, v := range vv {
715+
if !validHeaderValue(v) {
716+
return nil, badRequestError("invalid header value")
717+
}
718+
}
719+
}
710720
delete(req.Header, "Host")
711721

712722
req.RemoteAddr = c.remoteAddr

0 commit comments

Comments
 (0)