Skip to content

Commit dd910e6

Browse files
authored
Merge pull request #10 from johnwmail/alert-autofix-8
Potential fix for code scanning alert no. 8: Uncontrolled data used in path expression
2 parents fc53ad4 + ea997d2 commit dd910e6

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,20 @@ var localMusicDir = os.Getenv("MUSIC_DIR") // e.g. "/mp3"
607607
func localList(prefix string) ([]string, []string, error) {
608608
var dirs, files []string
609609
base := filepath.Join(localMusicDir, prefix)
610-
entries, err := os.ReadDir(base)
610+
// Validate that base is inside localMusicDir (avoid path traversal)
611+
rootAbs, err := filepath.Abs(localMusicDir)
612+
if err != nil {
613+
return nil, nil, fmt.Errorf("failed to resolve music dir: %w", err)
614+
}
615+
baseAbs, err := filepath.Abs(base)
616+
if err != nil {
617+
return nil, nil, fmt.Errorf("failed to resolve target dir: %w", err)
618+
}
619+
// Ensure the requested baseAbs is within rootAbs
620+
if !strings.HasPrefix(baseAbs, rootAbs) {
621+
return nil, nil, fmt.Errorf("invalid directory path: %s", prefix)
622+
}
623+
entries, err := os.ReadDir(baseAbs)
611624
if err != nil {
612625
return nil, nil, err
613626
}

0 commit comments

Comments
 (0)