-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor filetype is not allowed errors (#7309)
- Loading branch information
1 parent
75d4414
commit f369788
Showing
5 changed files
with
61 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package upload | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
) | ||
|
||
// ErrFileTypeForbidden not allowed file type error | ||
type ErrFileTypeForbidden struct { | ||
Type string | ||
} | ||
|
||
// IsErrFileTypeForbidden checks if an error is a ErrFileTypeForbidden. | ||
func IsErrFileTypeForbidden(err error) bool { | ||
_, ok := err.(ErrFileTypeForbidden) | ||
return ok | ||
} | ||
|
||
func (err ErrFileTypeForbidden) Error() string { | ||
return fmt.Sprintf("File type is not allowed: %s", err.Type) | ||
} | ||
|
||
// VerifyAllowedContentType validates a file is allowed to be uploaded. | ||
func VerifyAllowedContentType(buf []byte, allowedTypes []string) error { | ||
fileType := http.DetectContentType(buf) | ||
|
||
allowed := false | ||
for _, t := range allowedTypes { | ||
t := strings.Trim(t, " ") | ||
if t == "*/*" || t == fileType { | ||
allowed = true | ||
break | ||
} | ||
} | ||
|
||
if !allowed { | ||
log.Info("Attachment with type %s blocked from upload", fileType) | ||
return ErrFileTypeForbidden{Type: fileType} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters