From: Ian Lance Taylor Date: Tue, 26 Jun 2018 23:21:56 +0000 (-0700) Subject: [release-branch.go1.10] mime/multipart: restore 1.9 handling of missing/empty form... X-Git-Tag: go1.10.4~18 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=b5375d70d1d626595ec68568b68cc28dddc859d1;p=gostls13.git [release-branch.go1.10] mime/multipart: restore 1.9 handling of missing/empty form-data file name Revert the code change of CL 70931, but keep the test, appropriately modified for the code changes. Also add a new test. This restores the 1.9 handling of form-data entries with missing or empty file names. Changing the handling of this simply confused existing programs for no useful benefit. Go back to the old behavior. Updates #19183 Fixes #24041 Change-Id: Ie7a0309a061218ceda3bbc2a7da85e6fb3dd016d Reviewed-on: https://go-review.googlesource.com/121075 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- diff --git a/src/mime/multipart/formdata.go b/src/mime/multipart/formdata.go index 2a4ebdd4a0..832d0ad693 100644 --- a/src/mime/multipart/formdata.go +++ b/src/mime/multipart/formdata.go @@ -58,8 +58,7 @@ func (r *Reader) readForm(maxMemory int64) (_ *Form, err error) { var b bytes.Buffer - _, hasContentTypeHeader := p.Header["Content-Type"] - if !hasContentTypeHeader && filename == "" { + if filename == "" { // value, store as string in memory n, err := io.CopyN(&b, p, maxValueBytes+1) if err != nil && err != io.EOF { diff --git a/src/mime/multipart/formdata_test.go b/src/mime/multipart/formdata_test.go index 69333d3d0d..2d6a830cb6 100644 --- a/src/mime/multipart/formdata_test.go +++ b/src/mime/multipart/formdata_test.go @@ -47,12 +47,24 @@ func TestReadFormWithNamelessFile(t *testing.T) { } defer f.RemoveAll() - fd := testFile(t, f.File["hiddenfile"][0], "", filebContents) - if _, ok := fd.(sectionReadCloser); !ok { - t.Errorf("file has unexpected underlying type %T", fd) + if g, e := f.Value["hiddenfile"][0], filebContents; g != e { + t.Errorf("hiddenfile value = %q, want %q", g, e) } - fd.Close() +} +func TestReadFormWithTextContentType(t *testing.T) { + // From https://github.com/golang/go/issues/24041 + b := strings.NewReader(strings.Replace(messageWithTextContentType, "\n", "\r\n", -1)) + r := NewReader(b, boundary) + f, err := r.ReadForm(25) + if err != nil { + t.Fatal("ReadForm:", err) + } + defer f.RemoveAll() + + if g, e := f.Value["texta"][0], textaValue; g != e { + t.Errorf("texta value = %q, want %q", g, e) + } } func testFile(t *testing.T, fh *FileHeader, efn, econtent string) File { @@ -94,6 +106,15 @@ Content-Type: text/plain --MyBoundary-- ` +const messageWithTextContentType = ` +--MyBoundary +Content-Disposition: form-data; name="texta" +Content-Type: text/plain + +` + textaValue + ` +--MyBoundary +` + const message = ` --MyBoundary Content-Disposition: form-data; name="filea"; filename="filea.txt"