Harden path traversal check with filepath.IsLocal before opening files
This commit is contained in:
@@ -212,6 +212,12 @@ func handleDownload(w http.ResponseWriter, r *http.Request, start time.Time) {
|
|||||||
id := parts[0]
|
id := parts[0]
|
||||||
filename := parts[1]
|
filename := parts[1]
|
||||||
|
|
||||||
|
if !filepath.IsLocal(id) || !filepath.IsLocal(filename) {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
logRequest(r, 404, time.Since(start))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
filePath := filepath.Join(uploadDir, id, filename)
|
filePath := filepath.Join(uploadDir, id, filename)
|
||||||
file, err := os.Open(filePath)
|
file, err := os.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -219,12 +225,6 @@ func handleDownload(w http.ResponseWriter, r *http.Request, start time.Time) {
|
|||||||
logRequest(r, 404, time.Since(start))
|
logRequest(r, 404, time.Since(start))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !strings.HasPrefix(filePath, uploadDir) {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
logRequest(r, 404, time.Since(start))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
stat, err := file.Stat()
|
stat, err := file.Stat()
|
||||||
|
|||||||
Reference in New Issue
Block a user