Skip to content

Commit ccec64e

Browse files
committed
Fix file and directory permission security issues
Security fixes for gosec G301 and G306: - Change directory permissions from 0755 to 0750 (remove world-readable) - Change file permissions from 0644 to 0600 (owner-only access) Affected files: - pkg/storage/local/local.go: backup directory creation - pkg/metadata/metadata.go: metadata file and directory - pkg/backup/backup.go: log and backup directories - cmd/metadata-recovery/main_test.go: test directory These changes ensure sensitive backup data and metadata are not accessible to unauthorized users, following security best practices.
1 parent b90e992 commit ccec64e

4 files changed

Lines changed: 7 additions & 7 deletions

File tree

cmd/metadata-recovery/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func TestScanLocalStorage(t *testing.T) {
136136

137137
for _, tf := range testFiles {
138138
fullPath := filepath.Join(tempDir, tf.path)
139-
err := os.MkdirAll(filepath.Dir(fullPath), 0755)
139+
err := os.MkdirAll(filepath.Dir(fullPath), 0750)
140140
require.NoError(t, err)
141141

142142
err = os.WriteFile(fullPath, []byte(tf.content), 0644)

pkg/backup/backup.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ func (m *Manager) createLogFile(id string) (string, *os.File, error) {
369369
}
370370

371371
// Ensure log directory exists
372-
if err := os.MkdirAll(logDir, 0755); err != nil {
372+
if err := os.MkdirAll(logDir, 0750); err != nil {
373373
return "", nil, fmt.Errorf("failed to create log directory: %w", err)
374374
}
375375

@@ -431,7 +431,7 @@ func (m *Manager) backupDatabase(serverName, serverType, database, backupType st
431431
primaryBackupPath = localPaths["by-server"]
432432

433433
// Ensure the directory exists
434-
err := os.MkdirAll(filepath.Dir(primaryBackupPath), 0755)
434+
err := os.MkdirAll(filepath.Dir(primaryBackupPath), 0750)
435435
if err != nil {
436436
errMsg := fmt.Sprintf("failed to create backup directory: %v", err)
437437
if logFile != nil {
@@ -677,7 +677,7 @@ func (m *Manager) backupDatabase(serverName, serverType, database, backupType st
677677
// We already created the primary file (by-server), now copy to by-type
678678
if byTypePath, ok := localPaths["by-type"]; ok {
679679
// Create parent directory
680-
if err := os.MkdirAll(filepath.Dir(byTypePath), 0755); err != nil {
680+
if err := os.MkdirAll(filepath.Dir(byTypePath), 0750); err != nil {
681681
log.Printf("Warning: Failed to create directory for by-type backup: %v", err)
682682
} else {
683683
// Copy the file

pkg/metadata/metadata.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,12 @@ func (s *Store) save() error {
174174

175175
// Ensure directory exists
176176
dir := filepath.Dir(s.filepath)
177-
if err := os.MkdirAll(dir, 0755); err != nil {
177+
if err := os.MkdirAll(dir, 0750); err != nil {
178178
return fmt.Errorf("failed to create directory for metadata: %w", err)
179179
}
180180

181181
// Write to file
182-
if err := os.WriteFile(s.filepath, data, 0644); err != nil {
182+
if err := os.WriteFile(s.filepath, data, 0600); err != nil {
183183
return fmt.Errorf("failed to write metadata file: %w", err)
184184
}
185185

pkg/storage/local/local.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (c *Client) EnsureBackupPath(backupType string) (string, error) {
3434
backupDir := filepath.Join(c.cfg.Local.BackupDirectory, backupType)
3535

3636
// Ensure the directory exists
37-
if err := os.MkdirAll(backupDir, 0755); err != nil {
37+
if err := os.MkdirAll(backupDir, 0750); err != nil {
3838
return "", fmt.Errorf("failed to create backup directory %s: %w", backupDir, err)
3939
}
4040

0 commit comments

Comments
 (0)