Skip to content

Commit cd8c686

Browse files
[misc] add path traversal and file size protections (#5755)
1 parent f5c41e3 commit cd8c686

2 files changed

Lines changed: 126 additions & 8 deletions

File tree

upload-server/server/local.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/url"
88
"os"
99
"path/filepath"
10+
"strings"
1011

1112
log "github.com/sirupsen/logrus"
1213

@@ -82,15 +83,18 @@ func (l *local) getUploadURL(objectKey string) (string, error) {
8283
return newURL.String(), nil
8384
}
8485

86+
const maxUploadSize = 150 << 20
87+
8588
func (l *local) handlePutRequest(w http.ResponseWriter, r *http.Request) {
8689
if r.Method != http.MethodPut {
8790
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
8891
return
8992
}
9093

94+
r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize)
9195
body, err := io.ReadAll(r.Body)
9296
if err != nil {
93-
http.Error(w, fmt.Sprintf("failed to read body: %v", err), http.StatusInternalServerError)
97+
http.Error(w, "request body too large or failed to read", http.StatusRequestEntityTooLarge)
9498
return
9599
}
96100

@@ -105,20 +109,47 @@ func (l *local) handlePutRequest(w http.ResponseWriter, r *http.Request) {
105109
return
106110
}
107111

108-
dirPath := filepath.Join(l.dir, uploadDir)
109-
err = os.MkdirAll(dirPath, 0750)
110-
if err != nil {
112+
cleanBase := filepath.Clean(l.dir) + string(filepath.Separator)
113+
114+
dirPath := filepath.Clean(filepath.Join(l.dir, uploadDir))
115+
if !strings.HasPrefix(dirPath, cleanBase) {
116+
http.Error(w, "invalid path", http.StatusBadRequest)
117+
log.Warnf("Path traversal attempt blocked (dir): %s", dirPath)
118+
return
119+
}
120+
121+
filePath := filepath.Clean(filepath.Join(dirPath, uploadFile))
122+
if !strings.HasPrefix(filePath, cleanBase) {
123+
http.Error(w, "invalid path", http.StatusBadRequest)
124+
log.Warnf("Path traversal attempt blocked (file): %s", filePath)
125+
return
126+
}
127+
128+
if err = os.MkdirAll(dirPath, 0750); err != nil {
111129
http.Error(w, "failed to create upload dir", http.StatusInternalServerError)
112130
log.Errorf("Failed to create upload dir: %v", err)
113131
return
114132
}
115133

116-
file := filepath.Join(dirPath, uploadFile)
117-
if err := os.WriteFile(file, body, 0600); err != nil {
134+
flags := os.O_WRONLY | os.O_CREATE | os.O_EXCL
135+
f, err := os.OpenFile(filePath, flags, 0600)
136+
if err != nil {
137+
if os.IsExist(err) {
138+
http.Error(w, "file already exists", http.StatusConflict)
139+
return
140+
}
141+
http.Error(w, "failed to create file", http.StatusInternalServerError)
142+
log.Errorf("Failed to create file %s: %v", filePath, err)
143+
return
144+
}
145+
defer func() { _ = f.Close() }()
146+
147+
if _, err = f.Write(body); err != nil {
118148
http.Error(w, "failed to write file", http.StatusInternalServerError)
119-
log.Errorf("Failed to write file %s: %v", file, err)
149+
log.Errorf("Failed to write file %s: %v", filePath, err)
120150
return
121151
}
122-
log.Infof("Uploading file %s", file)
152+
153+
log.Infof("Uploaded file %s", filePath)
123154
w.WriteHeader(http.StatusOK)
124155
}

upload-server/server/local_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,90 @@ func Test_LocalHandlePutRequest(t *testing.T) {
6363
require.NoError(t, err)
6464
require.Equal(t, fileContent, createdFileContent)
6565
}
66+
67+
func Test_LocalHandlePutRequest_PathTraversal(t *testing.T) {
68+
mockDir := t.TempDir()
69+
mockURL := "http://localhost:8080"
70+
t.Setenv("SERVER_URL", mockURL)
71+
t.Setenv("STORE_DIR", mockDir)
72+
73+
mux := http.NewServeMux()
74+
err := configureLocalHandlers(mux)
75+
require.NoError(t, err)
76+
77+
fileContent := []byte("malicious content")
78+
req := httptest.NewRequest(http.MethodPut, putURLPath+"/uploads/%2e%2e%2f%2e%2e%2fetc%2fpasswd", bytes.NewReader(fileContent))
79+
80+
rec := httptest.NewRecorder()
81+
mux.ServeHTTP(rec, req)
82+
83+
require.Equal(t, http.StatusBadRequest, rec.Code)
84+
85+
_, err = os.Stat(filepath.Join(mockDir, "..", "..", "etc", "passwd"))
86+
require.True(t, os.IsNotExist(err), "traversal file should not exist")
87+
}
88+
89+
func Test_LocalHandlePutRequest_DirTraversal(t *testing.T) {
90+
mockDir := t.TempDir()
91+
t.Setenv("SERVER_URL", "http://localhost:8080")
92+
t.Setenv("STORE_DIR", mockDir)
93+
94+
l := &local{url: "http://localhost:8080", dir: mockDir}
95+
96+
body := bytes.NewReader([]byte("bad"))
97+
req := httptest.NewRequest(http.MethodPut, putURLPath+"/x/evil.txt", body)
98+
req.SetPathValue("dir", "../../../tmp")
99+
req.SetPathValue("file", "evil.txt")
100+
101+
rec := httptest.NewRecorder()
102+
l.handlePutRequest(rec, req)
103+
104+
require.Equal(t, http.StatusBadRequest, rec.Code)
105+
106+
_, err := os.Stat(filepath.Join("/tmp", "evil.txt"))
107+
require.True(t, os.IsNotExist(err), "traversal file should not exist outside store dir")
108+
}
109+
110+
func Test_LocalHandlePutRequest_DuplicateFile(t *testing.T) {
111+
mockDir := t.TempDir()
112+
t.Setenv("SERVER_URL", "http://localhost:8080")
113+
t.Setenv("STORE_DIR", mockDir)
114+
115+
mux := http.NewServeMux()
116+
err := configureLocalHandlers(mux)
117+
require.NoError(t, err)
118+
119+
req := httptest.NewRequest(http.MethodPut, putURLPath+"/dir/dup.txt", bytes.NewReader([]byte("first")))
120+
rec := httptest.NewRecorder()
121+
mux.ServeHTTP(rec, req)
122+
require.Equal(t, http.StatusOK, rec.Code)
123+
124+
req = httptest.NewRequest(http.MethodPut, putURLPath+"/dir/dup.txt", bytes.NewReader([]byte("second")))
125+
rec = httptest.NewRecorder()
126+
mux.ServeHTTP(rec, req)
127+
require.Equal(t, http.StatusConflict, rec.Code)
128+
129+
content, err := os.ReadFile(filepath.Join(mockDir, "dir", "dup.txt"))
130+
require.NoError(t, err)
131+
require.Equal(t, []byte("first"), content)
132+
}
133+
134+
func Test_LocalHandlePutRequest_BodyTooLarge(t *testing.T) {
135+
mockDir := t.TempDir()
136+
t.Setenv("SERVER_URL", "http://localhost:8080")
137+
t.Setenv("STORE_DIR", mockDir)
138+
139+
mux := http.NewServeMux()
140+
err := configureLocalHandlers(mux)
141+
require.NoError(t, err)
142+
143+
largeBody := make([]byte, maxUploadSize+1)
144+
req := httptest.NewRequest(http.MethodPut, putURLPath+"/dir/big.txt", bytes.NewReader(largeBody))
145+
rec := httptest.NewRecorder()
146+
mux.ServeHTTP(rec, req)
147+
148+
require.Equal(t, http.StatusRequestEntityTooLarge, rec.Code)
149+
150+
_, err = os.Stat(filepath.Join(mockDir, "dir", "big.txt"))
151+
require.True(t, os.IsNotExist(err))
152+
}

0 commit comments

Comments
 (0)