Skip to content

Commit f369788

Browse files
sapktechknowlogick
authored andcommitted
Refactor filetype is not allowed errors (#7309)
1 parent 75d4414 commit f369788

File tree

5 files changed

+61
-46
lines changed

5 files changed

+61
-46
lines changed

modules/upload/filetype.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package upload
6+
7+
import (
8+
"fmt"
9+
"net/http"
10+
"strings"
11+
12+
"code.gitea.io/gitea/modules/log"
13+
)
14+
15+
// ErrFileTypeForbidden not allowed file type error
16+
type ErrFileTypeForbidden struct {
17+
Type string
18+
}
19+
20+
// IsErrFileTypeForbidden checks if an error is a ErrFileTypeForbidden.
21+
func IsErrFileTypeForbidden(err error) bool {
22+
_, ok := err.(ErrFileTypeForbidden)
23+
return ok
24+
}
25+
26+
func (err ErrFileTypeForbidden) Error() string {
27+
return fmt.Sprintf("File type is not allowed: %s", err.Type)
28+
}
29+
30+
// VerifyAllowedContentType validates a file is allowed to be uploaded.
31+
func VerifyAllowedContentType(buf []byte, allowedTypes []string) error {
32+
fileType := http.DetectContentType(buf)
33+
34+
allowed := false
35+
for _, t := range allowedTypes {
36+
t := strings.Trim(t, " ")
37+
if t == "*/*" || t == fileType {
38+
allowed = true
39+
break
40+
}
41+
}
42+
43+
if !allowed {
44+
log.Info("Attachment with type %s blocked from upload", fileType)
45+
return ErrFileTypeForbidden{Type: fileType}
46+
}
47+
48+
return nil
49+
}

routers/api/v1/repo/release_attachment.go

+4-16
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
package repo
66

77
import (
8-
"errors"
9-
"net/http"
108
"strings"
119

1210
"code.gitea.io/gitea/models"
1311
"code.gitea.io/gitea/modules/context"
1412
"code.gitea.io/gitea/modules/setting"
13+
"code.gitea.io/gitea/modules/upload"
1514

1615
api "code.gitea.io/gitea/modules/structs"
1716
)
@@ -177,20 +176,9 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
177176
}
178177

179178
// Check if the filetype is allowed by the settings
180-
fileType := http.DetectContentType(buf)
181-
182-
allowedTypes := strings.Split(setting.AttachmentAllowedTypes, ",")
183-
allowed := false
184-
for _, t := range allowedTypes {
185-
t := strings.Trim(t, " ")
186-
if t == "*/*" || t == fileType {
187-
allowed = true
188-
break
189-
}
190-
}
191-
192-
if !allowed {
193-
ctx.Error(400, "DetectContentType", errors.New("File type is not allowed"))
179+
err = upload.VerifyAllowedContentType(buf, strings.Split(setting.AttachmentAllowedTypes, ","))
180+
if err != nil {
181+
ctx.Error(400, "DetectContentType", err)
194182
return
195183
}
196184

routers/repo/attachment.go

+4-15
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ package repo
66

77
import (
88
"fmt"
9-
"net/http"
109
"strings"
1110

1211
"code.gitea.io/gitea/models"
1312
"code.gitea.io/gitea/modules/context"
1413
"code.gitea.io/gitea/modules/log"
1514
"code.gitea.io/gitea/modules/setting"
15+
"code.gitea.io/gitea/modules/upload"
1616
)
1717

1818
func renderAttachmentSettings(ctx *context.Context) {
@@ -42,21 +42,10 @@ func UploadAttachment(ctx *context.Context) {
4242
if n > 0 {
4343
buf = buf[:n]
4444
}
45-
fileType := http.DetectContentType(buf)
4645

47-
allowedTypes := strings.Split(setting.AttachmentAllowedTypes, ",")
48-
allowed := false
49-
for _, t := range allowedTypes {
50-
t := strings.Trim(t, " ")
51-
if t == "*/*" || t == fileType {
52-
allowed = true
53-
break
54-
}
55-
}
56-
57-
if !allowed {
58-
log.Info("Attachment with type %s blocked from upload", fileType)
59-
ctx.Error(400, ErrFileTypeForbidden.Error())
46+
err = upload.VerifyAllowedContentType(buf, strings.Split(setting.AttachmentAllowedTypes, ","))
47+
if err != nil {
48+
ctx.Error(400, err.Error())
6049
return
6150
}
6251

routers/repo/editor.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package repo
77
import (
88
"fmt"
99
"io/ioutil"
10-
"net/http"
1110
"path"
1211
"strings"
1312

@@ -20,6 +19,7 @@ import (
2019
"code.gitea.io/gitea/modules/repofiles"
2120
"code.gitea.io/gitea/modules/setting"
2221
"code.gitea.io/gitea/modules/templates"
22+
"code.gitea.io/gitea/modules/upload"
2323
"code.gitea.io/gitea/modules/util"
2424
)
2525

@@ -594,20 +594,11 @@ func UploadFileToServer(ctx *context.Context) {
594594
if n > 0 {
595595
buf = buf[:n]
596596
}
597-
fileType := http.DetectContentType(buf)
598597

599598
if len(setting.Repository.Upload.AllowedTypes) > 0 {
600-
allowed := false
601-
for _, t := range setting.Repository.Upload.AllowedTypes {
602-
t := strings.Trim(t, " ")
603-
if t == "*/*" || t == fileType {
604-
allowed = true
605-
break
606-
}
607-
}
608-
609-
if !allowed {
610-
ctx.Error(400, ErrFileTypeForbidden.Error())
599+
err = upload.VerifyAllowedContentType(buf, setting.Repository.Upload.AllowedTypes)
600+
if err != nil {
601+
ctx.Error(400, err.Error())
611602
return
612603
}
613604
}

routers/repo/issue.go

-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ const (
4141
)
4242

4343
var (
44-
// ErrFileTypeForbidden not allowed file type error
45-
ErrFileTypeForbidden = errors.New("File type is not allowed")
4644
// ErrTooManyFiles upload too many files
4745
ErrTooManyFiles = errors.New("Maximum number of files to upload exceeded")
4846
// IssueTemplateCandidates issue templates

0 commit comments

Comments
 (0)