Skip to content

Commit c7e4cdb

Browse files
authored
fix: Crash occurs when doing scan against a directory that is denied [#125] (#129)
1 parent 9590118 commit c7e4cdb

2 files changed

Lines changed: 48 additions & 8 deletions

File tree

‎utils/file_utils.go‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package utils
1515

1616
import (
17+
"errors"
18+
"fmt"
1719
"io/fs"
1820
"os"
1921
"path/filepath"
@@ -30,14 +32,20 @@ func NewFileDirReader(fileDir string, recursive bool, maxDepth int) (*StringArra
3032
var filePaths []string
3133
rootDepth := pathDepth(fileDir)
3234

35+
var errs []error
36+
3337
// filePaths is safely appended within WalkDir because WalkDir executes the callback sequentially.
3438
// No race conditions occur in this implementation, even with slice reallocation.
3539
err := filepath.WalkDir(fileDir, func(path string, d fs.DirEntry, err error) error {
3640
if err != nil {
37-
return err
41+
errs = append(errs, fmt.Errorf("%s: %w", path, err))
42+
if d != nil && d.IsDir() {
43+
return fs.SkipDir
44+
}
45+
return nil
3846
}
3947

40-
if !d.IsDir() {
48+
if d != nil && !d.IsDir() {
4149
filePaths = append(filePaths, path)
4250
return nil
4351
}
@@ -54,9 +62,10 @@ func NewFileDirReader(fileDir string, recursive bool, maxDepth int) (*StringArra
5462
})
5563

5664
if err != nil {
57-
return nil, err
65+
errs = append(errs, err)
5866
}
59-
return &StringArrayReader{strings: filePaths}, nil
67+
68+
return &StringArrayReader{strings: filePaths}, errors.Join(errs...)
6069
}
6170

6271
// pathDepth returns the depth of a given path by counting its components.

‎utils/file_utils_test.go‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,22 +169,53 @@ func Test_NewFileDirReader_Error(t *testing.T) {
169169
t.Parallel()
170170

171171
rootDir := t.TempDir()
172-
noPerm := os.FileMode(0000)
173-
if err := os.WriteFile(filepath.Join(rootDir, "a.txt"), []byte("hello world!"), noPerm); err != nil {
172+
rwxPerm := os.FileMode(0755)
173+
if err := os.WriteFile(filepath.Join(rootDir, "a.txt"), []byte("hello world!"), rwxPerm); err != nil {
174+
t.Fatalf("unexpected error while WriteFile %v", err)
175+
}
176+
if err := os.WriteFile(filepath.Join(rootDir, "z.txt"), []byte("hello world!"), rwxPerm); err != nil {
174177
t.Fatalf("unexpected error while WriteFile %v", err)
175178
}
176179
path := filepath.Join(rootDir, "sub")
180+
noPerm := os.FileMode(0000)
177181
if err := os.Mkdir(path, noPerm); err != nil {
178182
t.Fatalf("unexpected error while Mkdir %v", err)
179183
}
180-
_, err := NewFileDirReader(rootDir, false, 10)
184+
185+
path = filepath.Join(rootDir, "sub_2")
186+
if err := os.Mkdir(path, rwxPerm); err != nil {
187+
t.Fatalf("unexpected error while Mkdir %v", err)
188+
}
189+
190+
if err := os.WriteFile(filepath.Join(path, "www.txt"), []byte("hello world!"), rwxPerm); err != nil {
191+
t.Fatalf("unexpected error while WriteFile %v", err)
192+
}
193+
194+
sr, err := NewFileDirReader(rootDir, false, 10)
181195
if err != nil {
182196
t.Errorf("unexpected error while NewFileDirReader err:%v", err)
183197
}
184-
_, err = NewFileDirReader(rootDir, true, 10)
198+
199+
sr, err = NewFileDirReader(rootDir, true, 10)
185200
if !strings.Contains(err.Error(), "permission denied") {
186201
t.Errorf("unexpected error permissions denied message got:%v", err.Error())
187202
}
203+
204+
if diff := cmp.Diff(
205+
&StringArrayReader{
206+
strings: []string{
207+
filepath.Join(rootDir, "a.txt"),
208+
// note we are ignoring, and not getting back sub permission denied directory
209+
filepath.Join(rootDir, "sub_2/www.txt"),
210+
filepath.Join(rootDir, "z.txt"),
211+
},
212+
},
213+
sr,
214+
cmp.AllowUnexported(StringArrayReader{}),
215+
); diff != "" {
216+
t.Errorf("unexpected StringArrayReader mismatch (-want +got):\n%s", diff)
217+
}
218+
188219
}
189220

190221
func Test_pathDepth(t *testing.T) {

0 commit comments

Comments
 (0)