Skip to content

Commit f9043d1

Browse files
committed
fix: fix potential connection leaks on non-ok http status codes
Even the response of a non-ok http status code can contain a body. So we should close the body of a (on transport layer) sucessful request unconditionally. Otherwise we can leak a http connection.
1 parent b63690a commit f9043d1

File tree

5 files changed

+7
-3
lines changed

5 files changed

+7
-3
lines changed

cmd/csaf_downloader/downloader.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ func (d *Downloader) loadOpenPGPKeys(
361361
continue
362362
}
363363
if res.StatusCode != http.StatusOK {
364+
res.Body.Close()
364365
slog.Warn(
365366
"Fetching public OpenPGP key failed",
366367
"url", u,

cmd/csaf_downloader/forwarder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ func (f *Forwarder) forward(
259259
f.storeFailed(filename, doc, sha256, sha512)
260260
return
261261
}
262+
defer res.Body.Close()
262263
if res.StatusCode != http.StatusCreated {
263-
defer res.Body.Close()
264264
if msg, err := limitedString(res.Body, 512); err != nil {
265265
slog.Error("reading forward result failed",
266266
"error", err)

csaf/advisories.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ func (afp *AdvisoryFileProcessor) loadChanges(
230230
if err != nil {
231231
return nil, errs.ErrNetwork{Message: fmt.Sprintf("failed get request for url %s: %v", changesURL, err)}
232232
}
233+
defer resp.Body.Close()
233234

234235
if resp.StatusCode != http.StatusOK {
235236
switch { // we don't expect 401 and 403, as directory based feeds are supposed to be public, but just to be on the safe side
@@ -247,7 +248,6 @@ func (afp *AdvisoryFileProcessor) loadChanges(
247248
}
248249
}
249250

250-
defer resp.Body.Close()
251251
var files []AdvisoryFile
252252
c := csv.NewReader(resp.Body)
253253
// format specification:
@@ -331,6 +331,7 @@ func (afp *AdvisoryFileProcessor) processROLIE(
331331
continue
332332
}
333333
if res.StatusCode != http.StatusOK {
334+
res.Body.Close()
334335
slog.Error("Fetching failed",
335336
"url", feedURL, "status_code", res.StatusCode, "status", res.Status)
336337
switch {

csaf/providermetaloader.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ func (pmdl *ProviderMetadataLoader) loadFromSecurity(domain string) []*LoadedPro
248248
continue
249249
}
250250
if res.StatusCode != http.StatusOK {
251+
res.Body.Close()
251252
pmdl.messages.Add(
252253
HTTPFailed,
253254
fmt.Sprintf("Fetching %q failed: %s (%d)", path, res.Status, res.StatusCode))
@@ -305,6 +306,7 @@ func (pmdl *ProviderMetadataLoader) loadFromURL(path string) *LoadedProviderMeta
305306
return &result
306307
}
307308
if res.StatusCode != http.StatusOK {
309+
defer res.Body.Close()
308310
result.Messages.Add(
309311
HTTPFailed,
310312
fmt.Sprintf("fetching %q failed: %s (%d)", path, res.Status, res.StatusCode))

csaf/remotevalidation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ func (v *remoteValidator) Validate(doc any) (*RemoteValidationResult, error) {
300300
if err != nil {
301301
return nil, err
302302
}
303+
defer resp.Body.Close()
303304

304305
if resp.StatusCode != http.StatusOK {
305306
return nil, fmt.Errorf(
@@ -312,7 +313,6 @@ func (v *remoteValidator) Validate(doc any) (*RemoteValidationResult, error) {
312313
)
313314

314315
if err := func() error {
315-
defer resp.Body.Close()
316316
var in io.Reader
317317
// If we are caching record the incoming data and compress it.
318318
if key != nil {

0 commit comments

Comments
 (0)