Skip to content

Commit

Permalink
Merge pull request #1253 from jinroh/vfs-walk-overflow
Browse files Browse the repository at this point in the history
VFS: add maximum amount of recursivity when walking the vfs index
  • Loading branch information
jinroh authored Feb 23, 2018
2 parents 2c8a5ef + 67e71cf commit a93d00d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/vfs/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (a *Archive) Serve(fs VFS, w http.ResponseWriter) error {
defer f.Close()
_, err = io.Copy(ze, f)
return err
})
}, 0)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/vfs/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func setTrashedForFilesInsideDir(fs VFS, doc *DirDoc, trashed bool) error {
files = append(files, file)
}
return err
})
}, 0)
if err != nil {
return err
}
Expand Down
17 changes: 14 additions & 3 deletions pkg/vfs/vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@ const (
conflictFormat = "%s (__cozy__: %s)"
)

// maxWalkRecursive is the maximum amount of recursion allowed for the
// recursive walk process.
const maxWalkRecursive = 512

// ErrSkipDir is used in WalkFn as an error to skip the current
// directory. It is not returned by any function of the package.
var ErrSkipDir = errors.New("skip directories")

// ErrWalkOverflow is used in the walk process when the maximum amount of
// recursivity allowed is reached when browsing the index tree.
var ErrWalkOverflow = errors.New("vfs: walk overflow")

// Fs is an interface providing a set of high-level methods to interact with
// the file-system binaries and metadata.
type Fs interface {
Expand Down Expand Up @@ -517,10 +525,13 @@ func Walk(fs VFS, root string, walkFn WalkFn) error {
if err != nil {
return walkFn(root, dir, file, err)
}
return walk(fs, root, dir, file, walkFn)
return walk(fs, root, dir, file, walkFn, 0)
}

func walk(fs VFS, name string, dir *DirDoc, file *FileDoc, walkFn WalkFn) error {
func walk(fs VFS, name string, dir *DirDoc, file *FileDoc, walkFn WalkFn, count int) error {
if count >= maxWalkRecursive {
return ErrWalkOverflow
}
err := walkFn(name, dir, file, nil)
if err != nil {
if dir != nil && err == ErrSkipDir {
Expand All @@ -546,7 +557,7 @@ func walk(fs VFS, name string, dir *DirDoc, file *FileDoc, walkFn WalkFn) error
} else {
fullpath = path.Join(name, d.DocName)
}
if err = walk(fs, fullpath, d, f, walkFn); err != nil {
if err = walk(fs, fullpath, d, f, walkFn, count+1); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion web/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ func TestAuthorizeWithInvalidCSRFToken(t *testing.T) {
defer res.Body.Close()
assert.Equal(t, "403 Forbidden", res.Status)
body, _ := ioutil.ReadAll(res.Body)
assert.Contains(t, string(body), "Invalid csrf token")
assert.Contains(t, string(body), "invalid csrf token")
}

func TestAuthorizeWithNoState(t *testing.T) {
Expand Down

0 comments on commit a93d00d

Please sign in to comment.