Skip to content

Commit 41cba31

Browse files
neildgopherbot
authored andcommitted
mime/multipart: percent-encode CR and LF in header values to avoid CRLF injection
When provided with a field or file name containing newlines, multipart.FileContentDisposition and other header-producing functions could create an invalid header value. In some scenarios, this could permit a malicious input to perform a CRLF injection attack: field := "field" evilFile := "name\"\r\nEvil-Header: \"evil" fmt.Printf("Content-Disposition: %v\r\n", multipart.FileContentDisposition(field, evilFile)) // Prints: // Content-Disposition: form-data; name="field"; filename="name" // Evil-Header: "evil" Percent-endode \r and \n characters in headers, as recommended by https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#multipart/form-data-encoding-algorithm The above algorithm also recommends using percent-encoding for quotes, but preserve the existing backslash-escape behavior for now. Empirically, browsers understand backslash-escape in attribute values. Fixes golang#75557 Change-Id: Ia203df6ef45a098070f3ebb17f9b6cf80c520ed4 Reviewed-on: https://go-review.googlesource.com/c/go/+/706677 Auto-Submit: Damien Neil <[email protected]> Reviewed-by: Nicholas Husin <[email protected]> Reviewed-by: Nicholas Husin <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent dd1d597 commit 41cba31

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/mime/multipart/writer.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,20 @@ func (w *Writer) CreatePart(header textproto.MIMEHeader) (io.Writer, error) {
125125
return p, nil
126126
}
127127

128-
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
128+
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"", "\r", "%0D", "\n", "%0A")
129129

130+
// escapeQuotes escapes special characters in field parameter values.
131+
//
132+
// For historical reasons, this uses \ escaping for " and \ characters,
133+
// and percent encoding for CR and LF.
134+
//
135+
// The WhatWG specification for form data encoding suggests that we should
136+
// use percent encoding for " (%22), and should not escape \.
137+
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#multipart/form-data-encoding-algorithm
138+
//
139+
// Empirically, as of the time this comment was written, it is necessary
140+
// to escape \ characters or else Chrome (and possibly other browsers) will
141+
// interpet the unescaped \ as an escape.
130142
func escapeQuotes(s string) string {
131143
return quoteEscaper.Replace(s)
132144
}

src/mime/multipart/writer_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ func TestFileContentDisposition(t *testing.T) {
184184
{`somefield`, `somefile"withquotes".txt`, `form-data; name="somefield"; filename="somefile\"withquotes\".txt"`},
185185
{`somefield\withbackslash`, "somefile.txt", `form-data; name="somefield\\withbackslash"; filename="somefile.txt"`},
186186
{"somefield", `somefile\withbackslash.txt`, `form-data; name="somefield"; filename="somefile\\withbackslash.txt"`},
187+
{"a\rb\nc", "e\rf\ng", `form-data; name="a%0Db%0Ac"; filename="e%0Df%0Ag"`},
187188
}
188189
for i, tt := range tests {
189190
if found := FileContentDisposition(tt.fieldname, tt.filename); found != tt.want {

0 commit comments

Comments
 (0)