Skip to content

Commit

Permalink
Ditch pkg/errors, use native error (un)wrapping
Browse files Browse the repository at this point in the history
Compatibility notes:

1. ErrSymlinkLoop is removed in favor of wrapping syscall.ELOOP into
   &os.PathError{}; users should use e.g. errors.Is(err, syscall.ELOOP)
   to check for this particular error.

2. IsNotExist now does not use pkg/errors.Cause but native go error
   unwrapping; it could breaks a use case when it is used for some
   third-party errors wrapped using pkg/errors.Wrap[f].

I was not able to find any users of either feature.

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Jun 14, 2021
1 parent 90b5819 commit 322b993
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 535 deletions.
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
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
github.com/pkg/errors v0.8.0
23 changes: 0 additions & 23 deletions vendor/github.com/pkg/errors/LICENSE

This file was deleted.

310 changes: 0 additions & 310 deletions vendor/github.com/pkg/errors/errors.go

This file was deleted.

Loading

0 comments on commit 322b993

Please sign in to comment.