Skip to content

Commit cb04ec0

Browse files
committed
fix lint
1 parent 8c99d03 commit cb04ec0

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

internal/config/types.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -436,19 +436,14 @@ func isAllowedDir(path string, allowedDirs []string) bool {
436436
slog.Debug("File path detected, checking parent directory is allowed", "path", directoryPath)
437437
}
438438

439-
fInfo, err := os.Stat(directoryPath)
440-
if err != nil {
441-
slog.Warn("Path error", "path", path, "error", err)
439+
fInfo, statErr := os.Stat(directoryPath)
440+
if statErr != nil {
441+
slog.Warn("Stat: Path error", "path", path, "error", statErr)
442442
}
443443

444444
if fInfo != nil {
445-
//check if it contains a symlink
446-
lInfo, err := os.Lstat(directoryPath)
447-
if err != nil {
448-
slog.Warn("Lstat error", "path", path, "error", err)
449-
}
450-
if lInfo != nil && lInfo.Mode()&os.ModeSymlink != 0 {
451-
slog.Warn("Path is a symlink, not allowed", "path", path)
445+
if isSymlink(directoryPath) {
446+
slog.Warn("Path is a symlink, skipping allowed directory check", "path", directoryPath)
452447
return false
453448
}
454449
}
@@ -463,3 +458,17 @@ func isAllowedDir(path string, allowedDirs []string) bool {
463458

464459
return false
465460
}
461+
462+
func isSymlink(path string) bool {
463+
// check if it contains a symlink
464+
lInfo, err := os.Lstat(path)
465+
if err != nil {
466+
slog.Warn("Lstat error", "path", path, "error", err)
467+
return false
468+
}
469+
if lInfo != nil && lInfo.Mode()&os.ModeSymlink != 0 {
470+
slog.Warn("Path is a symlink", "path", path)
471+
return true
472+
}
473+
return false
474+
}

internal/config/types_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
package config
77

88
import (
9-
"github.com/stretchr/testify/require"
109
"os"
1110
"testing"
1211

12+
"github.com/stretchr/testify/require"
13+
1314
"github.com/stretchr/testify/assert"
1415
)
1516

@@ -84,25 +85,24 @@ func TestTypes_isAllowedDir(t *testing.T) {
8485

8586
// Create a temp directory for the symlink
8687
tempDir, err := os.MkdirTemp("", "symlink_test")
87-
assert.NoError(t, err)
88+
require.NoError(t, err)
8889
defer os.RemoveAll(tempDir) // Clean up the temp directory after the test
8990

9091
// Ensure the temp directory is in the allowedDirs
9192
allowedDirs = append(allowedDirs, tempDir)
9293

93-
//create filePath
9494
filePath = tempDir + "/" + filePath
9595
defer os.RemoveAll(filePath)
96-
err = os.WriteFile(filePath, []byte("test content"), 0644)
96+
err = os.WriteFile(filePath, []byte("test content"), 0o600)
9797
require.NoError(t, err)
9898

9999
// Create a symlink for testing
100100
symlinkPath = tempDir + "/" + symlinkPath
101101
defer os.Remove(symlinkPath)
102102
err = os.Symlink(filePath, symlinkPath)
103-
assert.NoError(t, err)
103+
require.NoError(t, err)
104104

105105
result := isAllowedDir(symlinkPath, allowedDirs)
106-
assert.False(t, result, "Symlink in allowed directory should return false")
106+
require.False(t, result, "Symlink in allowed directory should return false")
107107
})
108108
}

0 commit comments

Comments
 (0)