Skip to content

Commit 1e564e8

Browse files
committed
Replace r.Form.Get with r.PostForm.Get to only parse Post values
1 parent 81311b2 commit 1e564e8

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

internal/storage/chunking/Chunking.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func ParseChunkInfo(r *http.Request, isApiCall bool) (ChunkInfo, error) {
4949
formUuid = "uuid"
5050
}
5151

52-
buf := r.Form.Get(formTotalSize)
52+
buf := r.PostForm.Get(formTotalSize)
5353
info.TotalFilesizeBytes, err = strconv.ParseInt(buf, 10, 64)
5454
if err != nil {
5555
return ChunkInfo{}, err
@@ -58,7 +58,7 @@ func ParseChunkInfo(r *http.Request, isApiCall bool) (ChunkInfo, error) {
5858
return ChunkInfo{}, errors.New("value cannot be negative")
5959
}
6060

61-
buf = r.Form.Get(formOffset)
61+
buf = r.PostForm.Get(formOffset)
6262
info.Offset, err = strconv.ParseInt(buf, 10, 64)
6363
if err != nil {
6464
return ChunkInfo{}, err
@@ -67,7 +67,7 @@ func ParseChunkInfo(r *http.Request, isApiCall bool) (ChunkInfo, error) {
6767
return ChunkInfo{}, errors.New("value cannot be negative")
6868
}
6969

70-
info.UUID = r.Form.Get(formUuid)
70+
info.UUID = r.PostForm.Get(formUuid)
7171
if len(info.UUID) < 10 {
7272
return ChunkInfo{}, errors.New("invalid uuid submitted, needs to be at least 10 characters long")
7373
}
@@ -87,12 +87,12 @@ func ParseFileHeader(r *http.Request) (FileHeader, error) {
8787
if err != nil {
8888
return FileHeader{}, err
8989
}
90-
name := r.Form.Get("filename")
90+
name := r.PostForm.Get("filename")
9191
if name == "" {
9292
return FileHeader{}, errors.New("empty filename provided")
9393
}
9494
contentType := parseContentType(r)
95-
size := r.Form.Get("filesize")
95+
size := r.PostForm.Get("filesize")
9696
if size == "" {
9797
return FileHeader{}, errors.New("empty size provided")
9898
}
@@ -111,11 +111,11 @@ func ParseFileHeader(r *http.Request) (FileHeader, error) {
111111
}
112112

113113
func parseContentType(r *http.Request) string {
114-
contentType := r.Form.Get("filecontenttype")
114+
contentType := r.PostForm.Get("filecontenttype")
115115
if contentType != "" {
116116
return contentType
117117
}
118-
fileExt := strings.ToLower(filepath.Ext(r.Form.Get("filename")))
118+
fileExt := strings.ToLower(filepath.Ext(r.PostForm.Get("filename")))
119119
switch fileExt {
120120
case ".jpg", ".jpeg":
121121
contentType = "image/jpeg"

internal/test/TestHelper_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ package test
44

55
import (
66
"errors"
7-
"github.com/forceu/gokapi/internal/helper"
87
"io"
98
"log"
109
"net/http"
1110
"net/http/httptest"
1211
"os"
1312
"testing"
1413
"time"
14+
15+
"github.com/forceu/gokapi/internal/helper"
1516
)
1617

1718
var (
@@ -300,13 +301,13 @@ func TestResponseBodyContains(t *testing.T) {
300301

301302
func startTestServer() {
302303
http.HandleFunc("/test", func(writer http.ResponseWriter, request *http.Request) {
303-
io.WriteString(writer, "TestContent\n")
304+
_, _ = io.WriteString(writer, "TestContent\n")
304305
for _, cookie := range request.Cookies() {
305-
io.WriteString(writer, "cookie name: "+cookie.Name+" cookie value: "+cookie.Value+"\n")
306+
_, _ = io.WriteString(writer, "cookie name: "+cookie.Name+" cookie value: "+cookie.Value+"\n")
306307
}
307-
request.ParseForm()
308-
if request.Form.Get("testPostKey") != "" {
309-
io.WriteString(writer, "testPostKey: "+request.Form.Get("testPostKey")+"\n")
308+
_ = request.ParseForm()
309+
if request.PostForm.Get("testPostKey") != "" {
310+
_, _ = io.WriteString(writer, "testPostKey: "+request.PostForm.Get("testPostKey")+"\n")
310311
}
311312
})
312313
go func() { log.Fatal(http.ListenAndServe("127.0.0.1:9999", nil)) }()

internal/webserver/Webserver.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ func changePassword(w http.ResponseWriter, r *http.Request) {
335335
var ok bool
336336
var pwHash string
337337

338-
pw := r.Form.Get("newpw")
338+
pw := r.PostForm.Get("newpw")
339339
errMessage, pwHash, ok = validateNewPassword(pw, user)
340340
if ok {
341341
user.Password = pwHash
@@ -507,8 +507,8 @@ func showLogin(w http.ResponseWriter, r *http.Request) {
507507
fmt.Println(err)
508508
return
509509
}
510-
user := r.Form.Get("username")
511-
pw := r.Form.Get("password")
510+
user := r.PostForm.Get("username")
511+
pw := r.PostForm.Get("password")
512512
failedLogin := false
513513
if pw != "" && user != "" {
514514
ip := logging.GetIpAddress(r)
@@ -581,7 +581,7 @@ func showDownload(w http.ResponseWriter, r *http.Request) {
581581

582582
if file.PasswordHash != "" {
583583
_ = r.ParseForm()
584-
enteredPassword := r.Form.Get("password")
584+
enteredPassword := r.PostForm.Get("password")
585585
if configuration.HashPassword(enteredPassword, true) != file.PasswordHash && !isValidPwCookie(r, file) {
586586
if enteredPassword != "" {
587587
view.IsFailedLogin = true

internal/webserver/fileupload/FileUpload.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func ParseFileHeader(r *http.Request) (string, chunking.FileHeader, models.Uploa
108108
if err != nil {
109109
return "", chunking.FileHeader{}, models.UploadParameters{}, err
110110
}
111-
chunkId := r.Form.Get("chunkid")
111+
chunkId := r.PostForm.Get("chunkid")
112112
config, err := parseConfig(r.Form)
113113
if err != nil {
114114
return "", chunking.FileHeader{}, models.UploadParameters{}, err

0 commit comments

Comments
 (0)