Skip to content

Commit ae457e8

Browse files
panjf2000neild
authored andcommitted
net/textproto: reject HTTP requests with empty header keys
According to RFC 7230, empty field names in HTTP header are invalid. However, there are no specific instructions for developers to deal with that kind of case in the specification. CL 11242 chose to skip it and do nothing about it, which now seems like a bad idea because it has led `net/http` to behave inconsistently with the most widely-used HTTP implementations: Apache, Nginx, Node with llhttp, H2O, Lighttpd, etc. in the case of empty header keys. There is a very small chance that this CL will break a few existing HTTP clients. Fixes #65244 Change-Id: Ie01e9a6693d27caea4d81d1539345cf42b225535 Reviewed-on: https://go-review.googlesource.com/c/go/+/558095 Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Damien Neil <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 41d6687 commit ae457e8

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/net/http/serve_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -4799,6 +4799,10 @@ func TestServerValidatesHeaders(t *testing.T) {
47994799
{"Foo : bar\r\n", 400},
48004800
{"Foo\t: bar\r\n", 400},
48014801

4802+
// Empty header keys are invalid.
4803+
// See RFC 7230, Section 3.2.
4804+
{": empty key\r\n", 400},
4805+
48024806
{"foo: foo foo\r\n", 200}, // LWS space is okay
48034807
{"foo: foo\tfoo\r\n", 200}, // LWS tab is okay
48044808
{"foo: foo\x00foo\r\n", 400}, // CTL 0x00 in value is bad

src/net/textproto/reader.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,6 @@ func readMIMEHeader(r *Reader, maxMemory, maxHeaders int64) (MIMEHeader, error)
535535
}
536536
}
537537

538-
// As per RFC 7230 field-name is a token, tokens consist of one or more chars.
539-
// We could return a ProtocolError here, but better to be liberal in what we
540-
// accept, so if we get an empty key, skip it.
541-
if key == "" {
542-
continue
543-
}
544-
545538
maxHeaders--
546539
if maxHeaders < 0 {
547540
return nil, errors.New("message too large")
@@ -725,6 +718,10 @@ func validHeaderValueByte(c byte) bool {
725718
// ReadMIMEHeader accepts header keys containing spaces, but does not
726719
// canonicalize them.
727720
func canonicalMIMEHeaderKey(a []byte) (_ string, ok bool) {
721+
if len(a) == 0 {
722+
return "", false
723+
}
724+
728725
// See if a looks like a header key. If not, return it unchanged.
729726
noCanon := false
730727
for _, c := range a {

src/net/textproto/reader_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ func TestReaderUpcomingHeaderKeys(t *testing.T) {
169169
func TestReadMIMEHeaderNoKey(t *testing.T) {
170170
r := reader(": bar\ntest-1: 1\n\n")
171171
m, err := r.ReadMIMEHeader()
172-
want := MIMEHeader{"Test-1": {"1"}}
173-
if !reflect.DeepEqual(m, want) || err != nil {
172+
want := MIMEHeader{}
173+
if !reflect.DeepEqual(m, want) || err == nil {
174174
t.Fatalf("ReadMIMEHeader: %v, %v; want %v", m, err, want)
175175
}
176176
}
@@ -227,6 +227,7 @@ func TestReadMIMEHeaderMalformed(t *testing.T) {
227227
"Foo\r\n\t: foo\r\n\r\n",
228228
"Foo-\n\tBar",
229229
"Foo \tBar: foo\r\n\r\n",
230+
": empty key\r\n\r\n",
230231
}
231232
for _, input := range inputs {
232233
r := reader(input)

0 commit comments

Comments
 (0)