Skip to content

Commit 54db8b2

Browse files
committed
Add error handling for json.Encode and metadata update calls
Fix gosec G104 warnings by adding proper error handling: - Handle json.NewEncoder().Encode() errors in API responses - Log errors using fmt.Printf to avoid double-writing HTTP response - Handle UpdateBackupStatus and UpdateS3UploadStatus errors in metadata recovery - Continue processing on errors to maintain resilience This reduces security scanner warnings while maintaining functionality.
1 parent 96bc2a0 commit 54db8b2

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

cmd/metadata-recovery/main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,22 +299,26 @@ func processRecoveredBackups(backups []RecoveredBackup) {
299299
}
300300

301301
// Update status
302-
metadata.DefaultStore.UpdateBackupStatus(
302+
if err := metadata.DefaultStore.UpdateBackupStatus(
303303
backupMeta.ID,
304304
types.StatusSuccess,
305305
backupMeta.LocalPaths,
306306
backup.Size,
307307
"",
308-
)
308+
); err != nil {
309+
log.Printf("Error updating backup status for %s: %v", backupMeta.ID, err)
310+
}
309311

310312
// Update S3 status if applicable
311313
if backup.IsS3 {
312-
metadata.DefaultStore.UpdateS3UploadStatus(
314+
if err := metadata.DefaultStore.UpdateS3UploadStatus(
313315
backupMeta.ID,
314316
types.StatusSuccess,
315317
backupMeta.S3Keys,
316318
"",
317-
)
319+
); err != nil {
320+
log.Printf("Error updating S3 upload status for %s: %v", backupMeta.ID, err)
321+
}
318322
}
319323

320324
if *verbose {

main

17.3 MB
Binary file not shown.

pkg/api/backups.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ func (h *BackupsHandler) handleBackups(w http.ResponseWriter, r *http.Request) {
8585

8686
// Return paginated response
8787
w.Header().Set("Content-Type", "application/json")
88-
json.NewEncoder(w).Encode(result)
88+
if err := json.NewEncoder(w).Encode(result); err != nil {
89+
// Log the error but don't try to write another response
90+
// as headers are already sent
91+
fmt.Printf("Error encoding response: %v\n", err)
92+
}
8993
}
9094

9195
// handleBackupsLegacy handles non-paginated backup queries for file-based store
@@ -168,7 +172,11 @@ func (h *BackupsHandler) handleBackupsLegacy(w http.ResponseWriter, r *http.Requ
168172
}
169173

170174
w.Header().Set("Content-Type", "application/json")
171-
json.NewEncoder(w).Encode(response)
175+
if err := json.NewEncoder(w).Encode(response); err != nil {
176+
// Log the error but don't try to write another response
177+
// as headers are already sent
178+
fmt.Printf("Error encoding response: %v\n", err)
179+
}
172180
}
173181

174182
// handleBackupStats handles optimized stats queries
@@ -184,7 +192,11 @@ func (h *BackupsHandler) handleBackupStats(w http.ResponseWriter, r *http.Reques
184192
// Fall back to regular stats for file-based store
185193
stats := metadata.DefaultStore.GetStats()
186194
w.Header().Set("Content-Type", "application/json")
187-
json.NewEncoder(w).Encode(stats)
195+
if err := json.NewEncoder(w).Encode(stats); err != nil {
196+
// Log the error but don't try to write another response
197+
// as headers are already sent
198+
fmt.Printf("Error encoding response: %v\n", err)
199+
}
188200
return
189201
}
190202

@@ -196,7 +208,11 @@ func (h *BackupsHandler) handleBackupStats(w http.ResponseWriter, r *http.Reques
196208
}
197209

198210
w.Header().Set("Content-Type", "application/json")
199-
json.NewEncoder(w).Encode(stats)
211+
if err := json.NewEncoder(w).Encode(stats); err != nil {
212+
// Log the error but don't try to write another response
213+
// as headers are already sent
214+
fmt.Printf("Error encoding response: %v\n", err)
215+
}
200216
}
201217

202218
// Helper functions

0 commit comments

Comments
 (0)