aboutsummaryrefslogtreecommitdiff
path: root/src/mime/multipart/formdata_test.go
blob: d422729c96ab4f480d5cb24650b3d15e61dd27a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package multipart

import (
	"bytes"
	"fmt"
	"io"
	"math"
	"net/textproto"
	"os"
	"strings"
	"testing"
)

func TestReadForm(t *testing.T) {
	b := strings.NewReader(strings.ReplaceAll(message, "\n", "\r\n"))
	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)
	}
	if g, e := f.Value["textb"][0], textbValue; g != e {
		t.Errorf("texta value = %q, want %q", g, e)
	}
	fd := testFile(t, f.File["filea"][0], "filea.txt", fileaContents)
	if _, ok := fd.(*os.File); ok {
		t.Error("file is *os.File, should not be")
	}
	fd.Close()
	fd = testFile(t, f.File["fileb"][0], "fileb.txt", filebContents)
	if _, ok := fd.(*os.File); !ok {
		t.Errorf("file has unexpected underlying type %T", fd)
	}
	fd.Close()
}

func TestReadFormWithNamelessFile(t *testing.T) {
	b := strings.NewReader(strings.ReplaceAll(messageWithFileWithoutName, "\n", "\r\n"))
	r := NewReader(b, boundary)
	f, err := r.ReadForm(25)
	if err != nil {
		t.Fatal("ReadForm:", err)
	}
	defer f.RemoveAll()

	if g, e := f.Value["hiddenfile"][0], filebContents; g != e {
		t.Errorf("hiddenfile value = %q, want %q", g, e)
	}
}

// Issue 58384: Handle ReadForm(math.MaxInt64)
func TestReadFormWitFileNameMaxMemoryOverflow(t *testing.T) {
	b := strings.NewReader(strings.ReplaceAll(messageWithFileName, "\n", "\r\n"))
	r := NewReader(b, boundary)
	f, err := r.ReadForm(math.MaxInt64)
	if err != nil {
		t.Fatalf("ReadForm(MaxInt64): %v", err)
	}
	defer f.RemoveAll()

	fd := testFile(t, f.File["filea"][0], "filea.txt", fileaContents)
	if _, ok := fd.(*os.File); ok {
		t.Error("file is *os.File, should not be")
	}
	fd.Close()
}

// Issue 40430: Handle ReadForm(math.MaxInt64)
func TestReadFormMaxMemoryOverflow(t *testing.T) {
	b := strings.NewReader(strings.ReplaceAll(messageWithTextContentType, "\n", "\r\n"))
	r := NewReader(b, boundary)
	f, err := r.ReadForm(math.MaxInt64)
	if err != nil {
		t.Fatalf("ReadForm(MaxInt64): %v", err)
	}
	if f == nil {
		t.Fatal("ReadForm(MaxInt64): missing form")
	}
	defer f.RemoveAll()

	if g, e := f.Value["texta"][0], textaValue; g != e {
		t.Errorf("texta value = %q, want %q", g, e)
	}
}

func TestReadFormWithTextContentType(t *testing.T) {
	// From https://github.com/golang/go/issues/24041
	b := strings.NewReader(strings.ReplaceAll(messageWithTextContentType, "\n", "\r\n"))
	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 {
	if fh.Filename != efn {
		t.Errorf("filename = %q, want %q", fh.Filename, efn)
	}
	if fh.Size != int64(len(econtent)) {
		t.Errorf("size = %d, want %d", fh.Size, len(econtent))
	}
	f, err := fh.Open()
	if err != nil {
		t.Fatal("opening file:", err)
	}
	b := new(strings.Builder)
	_, err = io.Copy(b, f)
	if err != nil {
		t.Fatal("copying contents:", err)
	}
	if g := b.String(); g != econtent {
		t.Errorf("contents = %q, want %q", g, econtent)
	}
	return f
}

const (
	fileaContents = "This is a test file."
	filebContents = "Another test file."
	textaValue    = "foo"
	textbValue    = "bar"
	boundary      = `MyBoundary`
)

const messageWithFileWithoutName = `
--MyBoundary
Content-Disposition: form-data; name="hiddenfile"; filename=""
Content-Type: text/plain

` + filebContents + `
--MyBoundary--
`

const messageWithFileName = `
--MyBoundary
Content-Disposition: form-data; name="filea"; filename="filea.txt"
Content-Type: text/plain

` + fileaContents + `
--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"
Content-Type: text/plain

` + fileaContents + `
--MyBoundary
Content-Disposition: form-data; name="fileb"; filename="fileb.txt"
Content-Type: text/plain

` + filebContents + `
--MyBoundary
Content-Disposition: form-data; name="texta"

` + textaValue + `
--MyBoundary
Content-Disposition: form-data; name="textb"

` + textbValue + `
--MyBoundary--
`

func TestReadForm_NoReadAfterEOF(t *testing.T) {
	maxMemory := int64(32) << 20
	boundary := `---------------------------8d345eef0d38dc9`
	body := `
-----------------------------8d345eef0d38dc9
Content-Disposition: form-data; name="version"

171
-----------------------------8d345eef0d38dc9--`

	mr := NewReader(&failOnReadAfterErrorReader{t: t, r: strings.NewReader(body)}, boundary)

	f, err := mr.ReadForm(maxMemory)
	if err != nil {
		t.Fatal(err)
	}
	t.Logf("Got: %#v", f)
}

// failOnReadAfterErrorReader is an io.Reader wrapping r.
// It fails t if any Read is called after a failing Read.
type failOnReadAfterErrorReader struct {
	t      *testing.T
	r      io.Reader
	sawErr error
}

func (r *failOnReadAfterErrorReader) Read(p []byte) (n int, err error) {
	if r.sawErr != nil {
		r.t.Fatalf("unexpected Read on Reader after previous read saw error %v", r.sawErr)
	}
	n, err = r.r.Read(p)
	r.sawErr = err
	return
}

// TestReadForm_NonFileMaxMemory asserts that the ReadForm maxMemory limit is applied
// while processing non-file form data as well as file form data.
func TestReadForm_NonFileMaxMemory(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping in -short mode")
	}
	n := 10 << 20
	largeTextValue := strings.Repeat("1", n)
	message := `--MyBoundary
Content-Disposition: form-data; name="largetext"

` + largeTextValue + `
--MyBoundary--
`
	testBody := strings.ReplaceAll(message, "\n", "\r\n")
	// Try parsing the form with increasing maxMemory values.
	// Changes in how we account for non-file form data may cause the exact point
	// where we change from rejecting the form as too large to accepting it to vary,
	// but we should see both successes and failures.
	const failWhenMaxMemoryLessThan = 128
	for maxMemory := int64(0); maxMemory < failWhenMaxMemoryLessThan*2; maxMemory += 16 {
		b := strings.NewReader(testBody)
		r := NewReader(b, boundary)
		f, err := r.ReadForm(maxMemory)
		if err != nil {
			continue
		}
		if g := f.Value["largetext"][0]; g != largeTextValue {
			t.Errorf("largetext mismatch: got size: %v, expected size: %v", len(g), len(largeTextValue))
		}
		f.RemoveAll()
		if maxMemory < failWhenMaxMemoryLessThan {
			t.Errorf("ReadForm(%v): no error, expect to hit memory limit when maxMemory < %v", maxMemory, failWhenMaxMemoryLessThan)
		}
		return
	}
	t.Errorf("ReadForm(x) failed for x < 1024, expect success")
}

// TestReadForm_MetadataTooLarge verifies that we account for the size of field names,
// MIME headers, and map entry overhead while limiting the memory consumption of parsed forms.
func TestReadForm_MetadataTooLarge(t *testing.T) {
	for _, test := range []struct {
		name string
		f    func(*Writer)
	}{{
		name: "large name",
		f: func(fw *Writer) {
			name := strings.Repeat("a", 10<<20)
			w, _ := fw.CreateFormField(name)
			w.Write([]byte("value"))
		},
	}, {
		name: "large MIME header",
		f: func(fw *Writer) {
			h := make(textproto.MIMEHeader)
			h.Set("Content-Disposition", `form-data; name="a"`)
			h.Set("X-Foo", strings.Repeat("a", 10<<20))
			w, _ := fw.CreatePart(h)
			w.Write([]byte("value"))
		},
	}, {
		name: "many parts",
		f: func(fw *Writer) {
			for i := 0; i < 110000; i++ {
				w, _ := fw.CreateFormField("f")
				w.Write([]byte("v"))
			}
		},
	}} {
		t.Run(test.name, func(t *testing.T) {
			var buf bytes.Buffer
			fw := NewWriter(&buf)
			test.f(fw)
			if err := fw.Close(); err != nil {
				t.Fatal(err)
			}
			fr := NewReader(&buf, fw.Boundary())
			_, err := fr.ReadForm(0)
			if err != ErrMessageTooLarge {
				t.Errorf("fr.ReadForm() = %v, want ErrMessageTooLarge", err)
			}
		})
	}
}

// TestReadForm_ManyFiles_Combined tests that a multipart form containing many files only
// results in a single on-disk file.
func TestReadForm_ManyFiles_Combined(t *testing.T) {
	const distinct = false
	testReadFormManyFiles(t, distinct)
}

// TestReadForm_ManyFiles_Distinct tests that setting GODEBUG=multipartfiles=distinct
// results in every file in a multipart form being placed in a distinct on-disk file.
func TestReadForm_ManyFiles_Distinct(t *testing.T) {
	t.Setenv("GODEBUG", "multipartfiles=distinct")
	const distinct = true
	testReadFormManyFiles(t, distinct)
}

func testReadFormManyFiles(t *testing.T, distinct bool) {
	var buf bytes.Buffer
	fw := NewWriter(&buf)
	const numFiles = 10
	for i := 0; i < numFiles; i++ {
		name := fmt.Sprint(i)
		w, err := fw.CreateFormFile(name, name)
		if err != nil {
			t.Fatal(err)
		}
		w.Write([]byte(name))
	}
	if err := fw.Close(); err != nil {
		t.Fatal(err)
	}
	fr := NewReader(&buf, fw.Boundary())
	fr.tempDir = t.TempDir()
	form, err := fr.ReadForm(0)
	if err != nil {
		t.Fatal(err)
	}
	for i := 0; i < numFiles; i++ {
		name := fmt.Sprint(i)
		if got := len(form.File[name]); got != 1 {
			t.Fatalf("form.File[%q] has %v entries, want 1", name, got)
		}
		fh := form.File[name][0]
		file, err := fh.Open()
		if err != nil {
			t.Fatalf("form.File[%q].Open() = %v", name, err)
		}
		if distinct {
			if _, ok := file.(*os.File); !ok {
				t.Fatalf("form.File[%q].Open: %T, want *os.File", name, file)
			}
		}
		got, err := io.ReadAll(file)
		file.Close()
		if string(got) != name || err != nil {
			t.Fatalf("read form.File[%q]: %q, %v; want %q, nil", name, string(got), err, name)
		}
	}
	dir, err := os.Open(fr.tempDir)
	if err != nil {
		t.Fatal(err)
	}
	defer dir.Close()
	names, err := dir.Readdirnames(0)
	if err != nil {
		t.Fatal(err)
	}
	wantNames := 1
	if distinct {
		wantNames = numFiles
	}
	if len(names) != wantNames {
		t.Fatalf("temp dir contains %v files; want 1", len(names))
	}
	if err := form.RemoveAll(); err != nil {
		t.Fatalf("form.RemoveAll() = %v", err)
	}
	names, err = dir.Readdirnames(0)
	if err != nil {
		t.Fatal(err)
	}
	if len(names) != 0 {
		t.Fatalf("temp dir contains %v files; want 0", len(names))
	}
}

func TestReadFormLimits(t *testing.T) {
	for _, test := range []struct {
		values           int
		files            int
		extraKeysPerFile int
		wantErr          error
		godebug          string
	}{
		{values: 1000},
		{values: 1001, wantErr: ErrMessageTooLarge},
		{values: 500, files: 500},
		{values: 501, files: 500, wantErr: ErrMessageTooLarge},
		{files: 1000},
		{files: 1001, wantErr: ErrMessageTooLarge},
		{files: 1, extraKeysPerFile: 9998}, // plus Content-Disposition and Content-Type
		{files: 1, extraKeysPerFile: 10000, wantErr: ErrMessageTooLarge},
		{godebug: "multipartmaxparts=100", values: 100},
		{godebug: "multipartmaxparts=100", values: 101, wantErr: ErrMessageTooLarge},
		{godebug: "multipartmaxheaders=100", files: 2, extraKeysPerFile: 48},
		{godebug: "multipartmaxheaders=100", files: 2, extraKeysPerFile: 50, wantErr: ErrMessageTooLarge},
	} {
		name := fmt.Sprintf("values=%v/files=%v/extraKeysPerFile=%v", test.values, test.files, test.extraKeysPerFile)
		if test.godebug != "" {
			name += fmt.Sprintf("/godebug=%v", test.godebug)
		}
		t.Run(name, func(t *testing.T) {
			if test.godebug != "" {
				t.Setenv("GODEBUG", test.godebug)
			}
			var buf bytes.Buffer
			fw := NewWriter(&buf)
			for i := 0; i < test.values; i++ {
				w, _ := fw.CreateFormField(fmt.Sprintf("field%v", i))
				fmt.Fprintf(w, "value %v", i)
			}
			for i := 0; i < test.files; i++ {
				h := make(textproto.MIMEHeader)
				h.Set("Content-Disposition",
					fmt.Sprintf(`form-data; name="file%v"; filename="file%v"`, i, i))
				h.Set("Content-Type", "application/octet-stream")
				for j := 0; j < test.extraKeysPerFile; j++ {
					h.Set(fmt.Sprintf("k%v", j), "v")
				}
				w, _ := fw.CreatePart(h)
				fmt.Fprintf(w, "value %v", i)
			}
			if err := fw.Close(); err != nil {
				t.Fatal(err)
			}
			fr := NewReader(bytes.NewReader(buf.Bytes()), fw.Boundary())
			form, err := fr.ReadForm(1 << 10)
			if err == nil {
				defer form.RemoveAll()
			}
			if err != test.wantErr {
				t.Errorf("ReadForm = %v, want %v", err, test.wantErr)
			}
		})
	}
}

func BenchmarkReadForm(b *testing.B) {
	for _, test := range []struct {
		name string
		form func(fw *Writer, count int)
	}{{
		name: "fields",
		form: func(fw *Writer, count int) {
			for i := 0; i < count; i++ {
				w, _ := fw.CreateFormField(fmt.Sprintf("field%v", i))
				fmt.Fprintf(w, "value %v", i)
			}
		},
	}, {
		name: "files",
		form: func(fw *Writer, count int) {
			for i := 0; i < count; i++ {
				w, _ := fw.CreateFormFile(fmt.Sprintf("field%v", i), fmt.Sprintf("file%v", i))
				fmt.Fprintf(w, "value %v", i)
			}
		},
	}} {
		b.Run(test.name, func(b *testing.B) {
			for _, maxMemory := range []int64{
				0,
				1 << 20,
			} {
				var buf bytes.Buffer
				fw := NewWriter(&buf)
				test.form(fw, 10)
				if err := fw.Close(); err != nil {
					b.Fatal(err)
				}
				b.Run(fmt.Sprintf("maxMemory=%v", maxMemory), func(b *testing.B) {
					b.ReportAllocs()
					for i := 0; i < b.N; i++ {
						fr := NewReader(bytes.NewReader(buf.Bytes()), fw.Boundary())
						form, err := fr.ReadForm(maxMemory)
						if err != nil {
							b.Fatal(err)
						}
						form.RemoveAll()
					}

				})
			}
		})
	}
}