Skip to content

Commit 3b9396c

Browse files
committed
Allow mime types to match based off of class
The old behavior prevented simple file types like `text/plain` from being uploaded since browsers upload them with the charset as well (e.g. `text/plain charset=utf-8`) without specifying all possible charsets. Additionally, this allows for blanket includes like `text/*` or `image/*` by class type.
1 parent a97fe76 commit 3b9396c

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

modules/upload/filetype.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ func VerifyAllowedContentType(buf []byte, allowedTypes []string) error {
3434
for _, t := range allowedTypes {
3535
t := strings.Trim(t, " ")
3636

37-
if t == "*/*" || t == fileType ||
37+
if t == fileType ||
38+
// Allow wildcard */* to match all mime types
39+
t == "*/*" ||
3840
// Allow directives after type, like 'text/plain; charset=utf-8'
39-
strings.HasPrefix(fileType, t+";") {
41+
strings.HasPrefix(fileType, t+";") ||
42+
// Allow a class whitelist, like 'image/*'
43+
(strings.HasSuffix(t, "/*") && strings.HasPrefix(fileType, strings.TrimRight(t, "*"))) {
4044
return nil
4145
}
4246
}

0 commit comments

Comments
 (0)