Browse Source

fix: Ensure HTTP response body is closed on non-200 responses

In ViewFile, the response body was only closed on 200 OK paths,
which could leak connections on non-200 responses. Now the body
is always closed via defer immediately after checking err == nil,
before checking the status code.
pull/7633/head
chrislu 4 weeks ago
parent
commit
c9068f7433
  1. 23
      weed/admin/handlers/file_browser_handlers.go

23
weed/admin/handlers/file_browser_handlers.go

@ -657,19 +657,24 @@ func (h *FileBrowserHandlers) ViewFile(c *gin.Context) {
Timeout: 30 * time.Second,
}
resp, err := clientWithTimeout.Get(fileURL)
if err == nil && resp.StatusCode == http.StatusOK {
if err != nil {
viewable = false
reason = "Failed to fetch file from filer"
} else {
defer resp.Body.Close()
contentBytes, err := io.ReadAll(resp.Body)
if err == nil {
content = string(contentBytes)
viewable = true
if resp.StatusCode == http.StatusOK {
contentBytes, err := io.ReadAll(resp.Body)
if err == nil {
content = string(contentBytes)
viewable = true
} else {
viewable = false
reason = "Failed to read file content"
}
} else {
viewable = false
reason = "Failed to read file content"
reason = "Failed to fetch file from filer"
}
} else {
viewable = false
reason = "Failed to fetch file from filer"
}
}
} else {

Loading…
Cancel
Save