Skip to content

Commit 2ba2043

Browse files
committed
fix: security issues by gosec G304, G104
1 parent dfcfe44 commit 2ba2043

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

pkg/cmd/edit/edit.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ func runEdit(cmd *cobra.Command, args []string) {
6767

6868
// Check if reading from file
6969
if fromFile != "" {
70-
fmt.Printf("Reading secret value from file: %s\n", fromFile)
71-
content, err := os.ReadFile(fromFile)
70+
// Validate that file path is absolute or clean it
71+
cleanPath := filepath.Clean(fromFile)
72+
fmt.Printf("Reading secret value from file: %s\n", cleanPath)
73+
content, err := os.ReadFile(cleanPath) // #nosec G304 - User-specified file path for reading secret
7274
if err != nil {
7375
root.ExitWithError(fmt.Errorf("failed to read file: %w", err))
7476
}
@@ -98,7 +100,7 @@ func runEdit(cmd *cobra.Command, args []string) {
98100
}
99101

100102
// Read the edited content
101-
newValue, err := os.ReadFile(tempFile)
103+
newValue, err := os.ReadFile(tempFile) // #nosec G304 - Reading from controlled temp file we created
102104
if err != nil {
103105
root.ExitWithError(fmt.Errorf("failed to read edited file: %w", err))
104106
}
@@ -164,14 +166,20 @@ func createSecureTempFile(content string) (string, error) {
164166
tempDir := os.TempDir()
165167
tempPath := filepath.Join(tempDir, filename)
166168

167-
file, err := os.OpenFile(tempPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
169+
file, err := os.OpenFile(tempPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) // #nosec G304 - Creating temp file in system temp dir with random name
168170
if err != nil {
169171
return "", err
170172
}
171-
defer file.Close()
173+
defer func() {
174+
if err := file.Close(); err != nil {
175+
fmt.Fprintf(os.Stderr, "Warning: failed to close temp file: %v\n", err)
176+
}
177+
}()
172178

173179
if _, err := file.WriteString(content); err != nil {
174-
os.Remove(tempPath)
180+
if removeErr := os.Remove(tempPath); removeErr != nil {
181+
fmt.Fprintf(os.Stderr, "Warning: failed to remove temp file: %v\n", removeErr)
182+
}
175183
return "", err
176184
}
177185

@@ -195,29 +203,37 @@ func secureDelete(filePath string) error {
195203
}
196204

197205
// Overwrite file with random data
198-
file, err := os.OpenFile(filePath, os.O_WRONLY, 0600)
206+
file, err := os.OpenFile(filePath, os.O_WRONLY, 0600) // #nosec G304 - Securely deleting temp file we created
199207
if err != nil {
200208
return err
201209
}
202210

203211
size := info.Size()
204212
randomData := make([]byte, size)
205213
if _, err := rand.Read(randomData); err != nil {
206-
file.Close()
214+
if closeErr := file.Close(); closeErr != nil {
215+
fmt.Fprintf(os.Stderr, "Warning: failed to close file during cleanup: %v\n", closeErr)
216+
}
207217
return err
208218
}
209219

210220
if _, err := file.Write(randomData); err != nil {
211-
file.Close()
221+
if closeErr := file.Close(); closeErr != nil {
222+
fmt.Fprintf(os.Stderr, "Warning: failed to close file during cleanup: %v\n", closeErr)
223+
}
212224
return err
213225
}
214226

215227
if err := file.Sync(); err != nil {
216-
file.Close()
228+
if closeErr := file.Close(); closeErr != nil {
229+
fmt.Fprintf(os.Stderr, "Warning: failed to close file during cleanup: %v\n", closeErr)
230+
}
217231
return err
218232
}
219233

220-
file.Close()
234+
if err := file.Close(); err != nil {
235+
return err
236+
}
221237

222238
// Delete the file
223239
return os.Remove(filePath)

0 commit comments

Comments
 (0)