Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Added IllegalPathError
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraimgm authored and AJ ONeal committed Oct 8, 2020
1 parent be59044 commit fea6146
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 6 deletions.
27 changes: 27 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package archiver

import (
"fmt"
"strings"
)

// IllegalPathError is an error returned when an illegal
// path is detected during the archival process.
//
// By default, only the Filename is showed on error, but you might
// also get the absolute value of the invalid path on the AbsolutePath
// field.
type IllegalPathError struct {
AbsolutePath string
Filename string
}

func (err *IllegalPathError) Error() string {
return fmt.Sprintf("illegal file path: %s", err.Filename)
}

// IsIllegalPathError returns true if the provided error is of
// the type IllegalPathError.
func IsIllegalPathError(err error) bool {
return err != nil && strings.Contains(err.Error(), "illegal file path: ")
}
54 changes: 54 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package archiver_test

import (
"errors"
"fmt"
"os"
"testing"

"github.com/mholt/archiver/v3"
)

func TestIllegalPathErrorString(t *testing.T) {
tests := []struct {
instance *archiver.IllegalPathError
expected string
}{
{instance: &archiver.IllegalPathError{Filename: "foo.txt"}, expected: "illegal file path: foo.txt"},
{instance: &archiver.IllegalPathError{AbsolutePath: "/tmp/bar.txt", Filename: "bar.txt"}, expected: "illegal file path: bar.txt"},
}

for i, test := range tests {
test := test

t.Run(fmt.Sprintf("Case %d", i), func(t *testing.T) {
if test.expected != test.instance.Error() {
t.Fatalf("Excepected '%s', but got '%s'", test.expected, test.instance.Error())
}
})
}
}

func TestIsIllegalPathError(t *testing.T) {
tests := []struct {
instance error
expected bool
}{
{instance: nil, expected: false},
{instance: os.ErrNotExist, expected: false},
{instance: fmt.Errorf("some error"), expected: false},
{instance: errors.New("another error"), expected: false},
{instance: &archiver.IllegalPathError{Filename: "foo.txt"}, expected: true},
}

for i, test := range tests {
test := test

t.Run(fmt.Sprintf("Case %d", i), func(t *testing.T) {
actual := archiver.IsIllegalPathError(test.instance)
if actual != test.expected {
t.Fatalf("Excepected '%v', but got '%v'", test.expected, actual)
}
})
}
}
4 changes: 2 additions & 2 deletions rar.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (*Rar) CheckPath(to, filename string) error {
dest := filepath.Join(to, filename)
//prevent path traversal attacks
if !strings.HasPrefix(dest, to) {
return fmt.Errorf("illegal file path: %s", filename)
return &IllegalPathError{AbsolutePath: dest, Filename: filename}
}
return nil
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func (r *Rar) Unarchive(source, destination string) error {
break
}
if err != nil {
if r.ContinueOnError || strings.Contains(err.Error(), "illegal file path") {
if r.ContinueOnError || IsIllegalPathError(err) {
log.Printf("[ERROR] Reading file in rar archive: %v", err)
continue
}
Expand Down
4 changes: 2 additions & 2 deletions tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (*Tar) CheckPath(to, filename string) error {
dest := filepath.Join(to, filename)
//prevent path traversal attacks
if !strings.HasPrefix(dest, to) {
return fmt.Errorf("illegal file path: %s", filename)
return &IllegalPathError{AbsolutePath: dest, Filename: filename}
}
return nil
}
Expand Down Expand Up @@ -161,7 +161,7 @@ func (t *Tar) Unarchive(source, destination string) error {
break
}
if err != nil {
if t.ContinueOnError || strings.Contains(err.Error(), "illegal file path") {
if t.ContinueOnError || IsIllegalPathError(err) {
log.Printf("[ERROR] Reading file in tar archive: %v", err)
continue
}
Expand Down
4 changes: 2 additions & 2 deletions zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (*Zip) CheckPath(to, filename string) error {
dest := filepath.Join(to, filename)
//prevent path traversal attacks
if !strings.HasPrefix(dest, to) {
return fmt.Errorf("illegal file path: %s", filename)
return &IllegalPathError{AbsolutePath: dest, Filename: filename}
}
return nil
}
Expand Down Expand Up @@ -225,7 +225,7 @@ func (z *Zip) Unarchive(source, destination string) error {
break
}
if err != nil {
if z.ContinueOnError || strings.Contains(err.Error(), "illegal file path") {
if z.ContinueOnError || IsIllegalPathError(err) {
log.Printf("[ERROR] Reading file in zip archive: %v", err)
continue
}
Expand Down

0 comments on commit fea6146

Please sign in to comment.