Skip to content
Myr

Fix #1693: file.Filename should not be trusted (#1699)

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

authored by Ganlv · committed by thinkerou · parent f67d7a9 · Browse files · 3 files+10 added−2 removed
This commit

State

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

Diff stats

3 files+10 added−2 removed

  • other:+10 added−2 removed

3 files+10 added−2 removed

modified:examples/upload-file/multiple/main.go+3 added-1 removed
1 unmodified line
23import (4 "fmt"5 "net/http"6 "path/filepath"78 "github.com/gin-gonic/gin"9)10
14 unmodified lines
25 }26 files := form.File["files"]2728 for _, file := range files {29 filename := filepath.Base(file.Filename)28 if err := c.SaveUploadedFile(file, file.Filename); err != nil {30 if err := c.SaveUploadedFile(file, filename); err != nil {31 c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))32 return33 }34 }
5 unmodified lines
modified:examples/upload-file/single/main.go+3 added-1 removed
1 unmodified line
23import (4 "fmt"5 "net/http"6 "path/filepath"78 "github.com/gin-gonic/gin"9)10
12 unmodified lines
23 c.String(http.StatusBadRequest, fmt.Sprintf("get form err: %s", err.Error()))24 return25 }2627 filename := filepath.Base(file.Filename)26 if err := c.SaveUploadedFile(file, file.Filename); err != nil {28 if err := c.SaveUploadedFile(file, filename); err != nil {29 c.String(http.StatusBadRequest, fmt.Sprintf("upload file err: %s", err.Error()))30 return31 }32
4 unmodified lines
modified:README.md+4 added
362 unmodified lines
363#### Single file364365References issue [#774](https://github.com/gin-gonic/gin/issues/774) and detail [example code](examples/upload-file/single).366367`file.Filename` **SHOULD NOT** be trusted. See [`Content-Disposition` on MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Directives) and [#1693](https://github.com/gin-gonic/gin/issues/1693)368369> The filename is always optional and must not be used blindly by the application: path information should be stripped, and conversion to the server file system rules should be done.370371```go372func main() {373 router := gin.Default()374 // Set a lower memory limit for multipart forms (default is 32 MiB)
1672 unmodified lines