Skip to content

Commit f69ad10

Browse files
hcliffandybons
authored andcommitted
mime/multipart: test for presence of filename instead of content-type
Fixes #24041 Preserving the intended fix in https://go.googlesource.com/go/+/81ec7256072ed5e20b8827c583193258769aebc0 Change-Id: I600d3d7edc74ca072a066739e2ef3235877d808f GitHub-Last-Rev: 14973d7 GitHub-Pull-Request: #24104 Reviewed-on: https://go-review.googlesource.com/96975 Reviewed-by: Andrew Bonventre <[email protected]> Run-TryBot: Andrew Bonventre <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 0eacf8c commit f69ad10

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/mime/multipart/formdata.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ func (r *Reader) readForm(maxMemory int64) (_ *Form, err error) {
5858

5959
var b bytes.Buffer
6060

61-
_, hasContentTypeHeader := p.Header["Content-Type"]
62-
if !hasContentTypeHeader && filename == "" {
61+
if !p.hasFileName() {
6362
// value, store as string in memory
6463
n, err := io.CopyN(&b, p, maxValueBytes+1)
6564
if err != nil && err != io.EOF {

src/mime/multipart/formdata_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ func TestReadFormWithNamelessFile(t *testing.T) {
5555

5656
}
5757

58+
func TestReadFormWithTextContentType(t *testing.T) {
59+
// From https://github.com/golang/go/issues/24041
60+
b := strings.NewReader(strings.Replace(messageWithTextContentType, "\n", "\r\n", -1))
61+
r := NewReader(b, boundary)
62+
f, err := r.ReadForm(25)
63+
if err != nil {
64+
t.Fatal("ReadForm:", err)
65+
}
66+
defer f.RemoveAll()
67+
68+
if g, e := f.Value["texta"][0], textaValue; g != e {
69+
t.Errorf("texta value = %q, want %q", g, e)
70+
}
71+
}
72+
5873
func testFile(t *testing.T, fh *FileHeader, efn, econtent string) File {
5974
if fh.Filename != efn {
6075
t.Errorf("filename = %q, want %q", fh.Filename, efn)
@@ -94,6 +109,15 @@ Content-Type: text/plain
94109
--MyBoundary--
95110
`
96111

112+
const messageWithTextContentType = `
113+
--MyBoundary
114+
Content-Disposition: form-data; name="texta"
115+
Content-Type: text/plain
116+
117+
` + textaValue + `
118+
--MyBoundary
119+
`
120+
97121
const message = `
98122
--MyBoundary
99123
Content-Disposition: form-data; name="filea"; filename="filea.txt"

src/mime/multipart/multipart.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ func (p *Part) FileName() string {
8181
return p.dispositionParams["filename"]
8282
}
8383

84+
// hasFileName determines if a (empty or otherwise)
85+
// filename parameter was included in the Content-Disposition header
86+
func (p *Part) hasFileName() bool {
87+
if p.dispositionParams == nil {
88+
p.parseContentDisposition()
89+
}
90+
_, ok := p.dispositionParams["filename"]
91+
return ok
92+
}
93+
8494
func (p *Part) parseContentDisposition() {
8595
v := p.Header.Get("Content-Disposition")
8696
var err error

0 commit comments

Comments
 (0)