Skip to content

Commit a93d00d

Browse files
authored
Merge pull request #1253 from jinroh/vfs-walk-overflow
VFS: add maximum amount of recursivity when walking the vfs index
2 parents 2c8a5ef + 67e71cf commit a93d00d

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

pkg/vfs/archive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (a *Archive) Serve(fs VFS, w http.ResponseWriter) error {
149149
defer f.Close()
150150
_, err = io.Copy(ze, f)
151151
return err
152-
})
152+
}, 0)
153153
}
154154

155155
return nil

pkg/vfs/directory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func setTrashedForFilesInsideDir(fs VFS, doc *DirDoc, trashed bool) error {
236236
files = append(files, file)
237237
}
238238
return err
239-
})
239+
}, 0)
240240
if err != nil {
241241
return err
242242
}

pkg/vfs/vfs.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,18 @@ const (
4545
conflictFormat = "%s (__cozy__: %s)"
4646
)
4747

48+
// maxWalkRecursive is the maximum amount of recursion allowed for the
49+
// recursive walk process.
50+
const maxWalkRecursive = 512
51+
4852
// ErrSkipDir is used in WalkFn as an error to skip the current
4953
// directory. It is not returned by any function of the package.
5054
var ErrSkipDir = errors.New("skip directories")
5155

56+
// ErrWalkOverflow is used in the walk process when the maximum amount of
57+
// recursivity allowed is reached when browsing the index tree.
58+
var ErrWalkOverflow = errors.New("vfs: walk overflow")
59+
5260
// Fs is an interface providing a set of high-level methods to interact with
5361
// the file-system binaries and metadata.
5462
type Fs interface {
@@ -517,10 +525,13 @@ func Walk(fs VFS, root string, walkFn WalkFn) error {
517525
if err != nil {
518526
return walkFn(root, dir, file, err)
519527
}
520-
return walk(fs, root, dir, file, walkFn)
528+
return walk(fs, root, dir, file, walkFn, 0)
521529
}
522530

523-
func walk(fs VFS, name string, dir *DirDoc, file *FileDoc, walkFn WalkFn) error {
531+
func walk(fs VFS, name string, dir *DirDoc, file *FileDoc, walkFn WalkFn, count int) error {
532+
if count >= maxWalkRecursive {
533+
return ErrWalkOverflow
534+
}
524535
err := walkFn(name, dir, file, nil)
525536
if err != nil {
526537
if dir != nil && err == ErrSkipDir {
@@ -546,7 +557,7 @@ func walk(fs VFS, name string, dir *DirDoc, file *FileDoc, walkFn WalkFn) error
546557
} else {
547558
fullpath = path.Join(name, d.DocName)
548559
}
549-
if err = walk(fs, fullpath, d, f, walkFn); err != nil {
560+
if err = walk(fs, fullpath, d, f, walkFn, count+1); err != nil {
550561
return err
551562
}
552563
}

web/auth/auth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ func TestAuthorizeWithInvalidCSRFToken(t *testing.T) {
806806
defer res.Body.Close()
807807
assert.Equal(t, "403 Forbidden", res.Status)
808808
body, _ := ioutil.ReadAll(res.Body)
809-
assert.Contains(t, string(body), "Invalid csrf token")
809+
assert.Contains(t, string(body), "invalid csrf token")
810810
}
811811

812812
func TestAuthorizeWithNoState(t *testing.T) {

0 commit comments

Comments
 (0)