Commit 626188dd authored by Jeff R. Allen's avatar Jeff R. Allen Committed by Brad Fitzpatrick

net/textproto: skip zero-length keys

A header of ": value" results in an empty key. Do not add
it to the headers, because RFC7230 (section 3.2) says that
field-names are tokens, which are one or more characters.

Fixes #11205.

Change-Id: I883be89da1489dc84f98523786b019d1d0169d46
Reviewed-on: https://go-review.googlesource.com/11242Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 3cab4760
......@@ -485,6 +485,13 @@ func (r *Reader) ReadMIMEHeader() (MIMEHeader, error) {
}
key := canonicalMIMEHeaderKey(kv[:endKey])
// As per RFC 7230 field-name is a token, tokens consist of one or more chars.
// We could return a ProtocolError here, but better to be liberal in what we
// accept, so if we get an empty key, skip it.
if key == "" {
continue
}
// Skip initial spaces in value.
i++ // skip colon
for i < len(kv) && (kv[i] == ' ' || kv[i] == '\t') {
......
......@@ -153,6 +153,15 @@ func TestReadMIMEHeaderSingle(t *testing.T) {
}
}
func TestReadMIMEHeaderNoKey(t *testing.T) {
r := reader(": bar\ntest-1: 1\n\n")
m, err := r.ReadMIMEHeader()
want := MIMEHeader{"Test-1": {"1"}}
if !reflect.DeepEqual(m, want) || err != nil {
t.Fatalf("ReadMIMEHeader: %v, %v; want %v", m, err, want)
}
}
func TestLargeReadMIMEHeader(t *testing.T) {
data := make([]byte, 16*1024)
for i := 0; i < len(data); i++ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment