Skip to content

Commit

Permalink
merge branch 'pr-7'
Browse files Browse the repository at this point in the history
Kir Kolyshkin (3):
  Ditch pkg/errors, use native error (un)wrapping
  go.mod: add
  travis-ci: update Go versions

LGTM: cyphar
Closes #7 #4 #3
  • Loading branch information
cyphar committed Jun 15, 2021
2 parents 90b5819 + a5ef4b9 commit 09a7b25
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 537 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

language: go
go:
- 1.7.x
- 1.8.x
- 1.13.x
- 1.16.x
- tip
arch:
- AMD64
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/cyphar/filepath-securejoin

go 1.13
25 changes: 3 additions & 22 deletions join.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,20 @@ package securejoin

import (
"bytes"
"errors"
"os"
"path/filepath"
"strings"
"syscall"

"github.com/pkg/errors"
)

// ErrSymlinkLoop is returned by SecureJoinVFS when too many symlinks have been
// evaluated in attempting to securely join the two given paths.
var ErrSymlinkLoop = errors.Wrap(syscall.ELOOP, "secure join")

// IsNotExist tells you if err is an error that implies that either the path
// accessed does not exist (or path components don't exist). This is
// effectively a more broad version of os.IsNotExist.
func IsNotExist(err error) bool {
// If it's a bone-fide ENOENT just bail.
if os.IsNotExist(errors.Cause(err)) {
return true
}

// Check that it's not actually an ENOTDIR, which in some cases is a more
// convoluted case of ENOENT (usually involving weird paths).
var errno error
switch err := errors.Cause(err).(type) {
case *os.PathError:
errno = err.Err
case *os.LinkError:
errno = err.Err
case *os.SyscallError:
errno = err.Err
}
return errno == syscall.ENOTDIR || errno == syscall.ENOENT
return errors.Is(err, os.ErrNotExist) || errors.Is(err, syscall.ENOTDIR) || errors.Is(err, syscall.ENOENT)
}

// SecureJoinVFS joins the two given path components (similar to Join) except
Expand All @@ -68,7 +49,7 @@ func SecureJoinVFS(root, unsafePath string, vfs VFS) (string, error) {
n := 0
for unsafePath != "" {
if n > 255 {
return "", ErrSymlinkLoop
return "", &os.PathError{Op: "SecureJoin", Path: root + "/" + unsafePath, Err: syscall.ELOOP}
}

// Next path component, p.
Expand Down
2 changes: 1 addition & 1 deletion join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func TestSymlinkLoop(t *testing.T) {
{dir, "/self/././.."},
} {
got, err := SecureJoin(test.root, test.unsafe)
if err != ErrSymlinkLoop {
if !errors.Is(err, syscall.ELOOP) {
t.Errorf("securejoin(%q, %q): expected ELOOP, got %v & %q", test.root, test.unsafe, err, got)
continue
}
Expand Down
1 change: 0 additions & 1 deletion vendor.conf

This file was deleted.

23 changes: 0 additions & 23 deletions vendor/github.com/pkg/errors/LICENSE

This file was deleted.

Loading

0 comments on commit 09a7b25

Please sign in to comment.