Skip to content
Myr

fix lack of escaping of filename in Content-Disposition (#3556)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix lack of escaping of filename in Content-Disposition * add test for Content-Disposition filename escaping process * fix filename escape bypass problem fix backslashes before backquotes were not properly escaped problem.

authored by Motoyasu Saburi · committed by GitHub · parent 9f5ecd4 · Browse files · 2 files+21 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+21 added−1 removed

  • other:+21 added−1 removed

2 files+21 added−1 removed

modified:context_test.go+14 added
1030 unmodified lines
1031 assert.Contains(t, w.Body.String(), "func New() *Engine {")1032 assert.Equal(t, fmt.Sprintf("attachment; filename=\"%s\"", newFilename), w.Header().Get("Content-Disposition"))1033}10341035func TestContextRenderAndEscapeAttachment(t *testing.T) {1036 w := httptest.NewRecorder()1037 c, _ := CreateTestContext(w)1038 maliciousFilename := "tampering_field.sh\"; \\\"; dummy=.go"1039 actualEscapedResponseFilename := "tampering_field.sh\\\"; \\\\\\\"; dummy=.go"10401041 c.Request, _ = http.NewRequest("GET", "/", nil)1042 c.FileAttachment("./gin.go", maliciousFilename)10431044 assert.Equal(t, 200, w.Code)1045 assert.Contains(t, w.Body.String(), "func New() *Engine {")1046 assert.Equal(t, fmt.Sprintf("attachment; filename=\"%s\"", actualEscapedResponseFilename), w.Header().Get("Content-Disposition"))1047}10481049func TestContextRenderUTF8Attachment(t *testing.T) {1050 w := httptest.NewRecorder()1051 c, _ := CreateTestContext(w)1052 newFilename := "new🧡_filename.go"
1377 unmodified lines
modified:context.go+7 added-1 removed
1050 unmodified lines
10511052 http.FileServer(fs).ServeHTTP(c.Writer, c.Request)1053}10541055var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")10561057func escapeQuotes(s string) string {1058 return quoteEscaper.Replace(s)1059}10601061// FileAttachment writes the specified file into the body stream in an efficient way1062// On the client side, the file will typically be downloaded with the given filename1063func (c *Context) FileAttachment(filepath, filename string) {1064 if isASCII(filename) {1059 c.Writer.Header().Set("Content-Disposition", `attachment; filename="`+filename+`"`)1065 c.Writer.Header().Set("Content-Disposition", `attachment; filename="`+escapeQuotes(filename)+`"`)1066 } else {1067 c.Writer.Header().Set("Content-Disposition", `attachment; filename*=UTF-8''`+url.QueryEscape(filename))1068 }1069 http.ServeFile(c.Writer, c.Request, filepath)
157 unmodified lines