Skip to content

Commit

Permalink
Fixes #14: Prevents directory listing in subdirectories
Browse files Browse the repository at this point in the history
This commit prevents directory listings in every directory level,
not just on root level ("upload/").

In earlier versions, an attacker could get a list of past uploads
if he ever received a valid file download path, removed the file name and
descendet in parent directories.
  • Loading branch information
ThomasLeister committed Jan 8, 2020
1 parent 65ca295 commit 7dff020
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func addCORSheaders(w http.ResponseWriter) {
w.Header().Set("Access-Control-Max-Age", "7200")
}


/*
* Request handler
* Is activated when a clients requests the file, file information or an upload
Expand All @@ -67,7 +66,7 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
log.Println("Failed to parse URL query params:", err)
}

fileStorePath := strings.TrimPrefix(u.Path, "/" + conf.UploadSubDir)
fileStorePath := strings.TrimPrefix(u.Path, "/"+conf.UploadSubDir)

// Add CORS headers
addCORSheaders(w)
Expand Down Expand Up @@ -137,7 +136,8 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", contentType)
} else if r.Method == "GET" {
contentType := mime.TypeByExtension(filepath.Ext(fileStorePath))
if fileStorePath == "" {
if f, err := os.Stat(conf.Storedir + fileStorePath); err != nil || f.IsDir() {
log.Println("Directory listing forbidden!")
http.Error(w, "403 Forbidden", 403)
return
}
Expand Down

0 comments on commit 7dff020

Please sign in to comment.