@@ -28,12 +28,13 @@ func IsErrFileTypeForbidden(err error) bool {
2828}
2929
3030func (err ErrFileTypeForbidden ) Error () string {
31- return "This file extension or type is not allowed to be uploaded ."
31+ return "This file cannot be uploaded or modified due to a forbidden file extension or type ."
3232}
3333
3434var wildcardTypeRe = regexp .MustCompile (`^[a-z]+/\*$` )
3535
36- // Verify validates whether a file is allowed to be uploaded.
36+ // Verify validates whether a file is allowed to be uploaded. If buf is empty, it will just check if the file
37+ // has an allowed file extension.
3738func Verify (buf []byte , fileName , allowedTypesStr string ) error {
3839 allowedTypesStr = strings .ReplaceAll (allowedTypesStr , "|" , "," ) // compat for old config format
3940
@@ -56,21 +57,31 @@ func Verify(buf []byte, fileName, allowedTypesStr string) error {
5657 return ErrFileTypeForbidden {Type : fullMimeType }
5758 }
5859 extension := strings .ToLower (path .Ext (fileName ))
60+ isBufEmpty := len (buf ) <= 1
5961
6062 // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers
6163 for _ , allowEntry := range allowedTypes {
6264 if allowEntry == "*/*" {
6365 return nil // everything allowed
64- } else if strings .HasPrefix (allowEntry , "." ) && allowEntry == extension {
66+ }
67+ if strings .HasPrefix (allowEntry , "." ) && allowEntry == extension {
6568 return nil // extension is allowed
66- } else if mimeType == allowEntry {
69+ }
70+ if isBufEmpty {
71+ continue // skip mime type checks if buffer is empty
72+ }
73+ if mimeType == allowEntry {
6774 return nil // mime type is allowed
68- } else if wildcardTypeRe .MatchString (allowEntry ) && strings .HasPrefix (mimeType , allowEntry [:len (allowEntry )- 1 ]) {
75+ }
76+ if wildcardTypeRe .MatchString (allowEntry ) && strings .HasPrefix (mimeType , allowEntry [:len (allowEntry )- 1 ]) {
6977 return nil // wildcard match, e.g. image/*
7078 }
7179 }
7280
73- log .Info ("Attachment with type %s blocked from upload" , fullMimeType )
81+ if ! isBufEmpty {
82+ log .Info ("Attachment with type %s blocked from upload" , fullMimeType )
83+ }
84+
7485 return ErrFileTypeForbidden {Type : fullMimeType }
7586}
7687
0 commit comments