Skip to content

Commit 25416fa

Browse files
committed
Limit file upload size to 2GB
1 parent 8cd547f commit 25416fa

1 file changed

Lines changed: 15 additions & 28 deletions

File tree

handler.go

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,13 @@ import (
2222
"google.golang.org/grpc/credentials/insecure"
2323
)
2424

25+
const MAX_UPLOAD_SIZE = 2 * 1024 * 1024 * 1024 // 2GB
26+
2527
type uploadAttachmentResponse struct {
2628
Sha string `json:"sha"`
2729
Size int64 `json:"size"`
2830
}
2931

30-
type forkRepositoryPostBody struct {
31-
SourceRepositoryID uint64 `json:"source_repository_id"`
32-
TargetRepositoryID uint64 `json:"target_repository_id"`
33-
}
34-
35-
type forkRepositoryResponseData struct {
36-
Forked bool `json:"forked"`
37-
}
38-
39-
type forkRepositoryResponse struct {
40-
Data forkRepositoryResponseData `json:"data"`
41-
Error string `json:"error"`
42-
}
43-
4432
type pullRequestCheckResponseData struct {
4533
IsMergeable bool `json:"is_mergeable"`
4634
}
@@ -50,17 +38,12 @@ type pullRequestCheckResponse struct {
5038
Error string `json:"error"`
5139
}
5240

53-
type pullRequestMergeResponseData struct {
54-
Merged bool `json:"merged"`
55-
MergeCommitSha string `json:"merge_commit_sha"`
56-
}
57-
58-
type pullRequestMergeResponse struct {
59-
Data pullRequestMergeResponseData `json:"data"`
60-
Error string `json:"error"`
61-
}
62-
6341
func uploadAttachmentHandler(w http.ResponseWriter, r *http.Request) {
42+
r.Body = http.MaxBytesReader(w, r.Body, MAX_UPLOAD_SIZE)
43+
if err := r.ParseMultipartForm(MAX_UPLOAD_SIZE); err != nil {
44+
http.Error(w, "The uploaded file is too big. Please choose an file that's less than 2GB in size", http.StatusBadRequest)
45+
return
46+
}
6447

6548
err := r.ParseMultipartForm(32 << 20)
6649
if err != nil {
@@ -85,11 +68,19 @@ func uploadAttachmentHandler(w http.ResponseWriter, r *http.Request) {
8568

8669
sha := sha256.New()
8770
_, err = io.Copy(io.MultiWriter(sha, tmpFile), file)
71+
if err != nil {
72+
http.Error(w, err.Error(), http.StatusInternalServerError)
73+
return
74+
}
8875

8976
attachmentDir := viper.GetString("attachment_dir")
9077
shaString := hex.EncodeToString(sha.Sum(nil))
9178
filePath := fmt.Sprintf("%s/%s", attachmentDir, shaString)
9279
localFile, err := os.Create(filePath)
80+
if err != nil {
81+
http.Error(w, err.Error(), http.StatusInternalServerError)
82+
return
83+
}
9384
defer localFile.Close()
9485

9586
tmpFile.Seek(0, io.SeekStart)
@@ -108,8 +99,6 @@ func uploadAttachmentHandler(w http.ResponseWriter, r *http.Request) {
10899
}
109100

110101
json.NewEncoder(w).Encode(resp)
111-
112-
return
113102
}
114103

115104
func getAttachmentHandler(w http.ResponseWriter, r *http.Request) {
@@ -170,8 +159,6 @@ func getAttachmentHandler(w http.ResponseWriter, r *http.Request) {
170159
if err != nil {
171160
http.Error(w, err.Error(), http.StatusInternalServerError)
172161
}
173-
174-
return
175162
}
176163

177164
func (s *Server) pullRequestCommitsHandler(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)