Skip to content

Commit

Permalink
Resolve the security issue of GoSec (#762)
Browse files Browse the repository at this point in the history
Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
  • Loading branch information
panjf2000 and erikdubbelboer committed Mar 13, 2020
1 parent b71c8c5 commit 0b93308
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,16 +883,21 @@ func (ctx *RequestCtx) FormFile(key string) (*multipart.FileHeader, error) {
var ErrMissingFile = errors.New("there is no uploaded file associated with the given key")

// SaveMultipartFile saves multipart file fh under the given filename path.
func SaveMultipartFile(fh *multipart.FileHeader, path string) error {
f, err := fh.Open()
func SaveMultipartFile(fh *multipart.FileHeader, path string) (err error) {
var (
f multipart.File
ff *os.File
)
f, err = fh.Open()
if err != nil {
return err
return
}

if ff, ok := f.(*os.File); ok {
var ok bool
if ff, ok = f.(*os.File); ok {
// Windows can't rename files that are opened.
if err := f.Close(); err != nil {
return err
if err = f.Close(); err != nil {
return
}

// If renaming fails we try the normal copying method.
Expand All @@ -902,21 +907,29 @@ func SaveMultipartFile(fh *multipart.FileHeader, path string) error {
}

// Reopen f for the code below.
f, err = fh.Open()
if err != nil {
return err
if f, err = fh.Open(); err != nil {
return
}
}

defer f.Close()
defer func() {
e := f.Close()
if err == nil {
err = e
}
}()

ff, err := os.Create(path)
if err != nil {
return err
if ff, err = os.Create(path); err != nil {
return
}
defer ff.Close() // #nosec G307
defer func() {
e := ff.Close()
if err == nil {
err = e
}
}()
_, err = copyZeroAlloc(ff, f)
return err
return
}

// FormValue returns form value associated with the given key.
Expand Down

0 comments on commit 0b93308

Please sign in to comment.