Skip to content

Commit 0d02ef1

Browse files
committed
Fix OS-specific path separator
1 parent 53cf1d6 commit 0d02ef1

File tree

6 files changed

+14
-12
lines changed

6 files changed

+14
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*~
2+
.idea
23
*.DS*
34
*.zip
45
*.rar

7z.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ func extract7z(xFile *XFile) (int64, []string, []string, error) {
6868
if err != nil {
6969
lastFile := xFile.FilePath
7070
/* // https://github.com/bodgit/sevenzip/issues/54
71-
// We can probably never get the file with the error.
72-
if volumes := sevenZip.Volumes(); len(volumes) > 0 {
73-
lastFile = volumes[len(volumes)-1]
74-
} */
71+
// We can probably never get the file with the error.
72+
if volumes := sevenZip.Volumes(); len(volumes) > 0 {
73+
lastFile = volumes[len(volumes)-1]
74+
} */
7575
return size, files, sevenZip.Volumes(), fmt.Errorf("%s: %w", lastFile, err)
7676
}
7777

@@ -84,7 +84,7 @@ func extract7z(xFile *XFile) (int64, []string, []string, error) {
8484

8585
func (x *XFile) un7zip(zipFile *sevenzip.File) (int64, error) { //nolint:dupl
8686
wfile := x.clean(zipFile.Name)
87-
if !strings.HasPrefix(wfile, x.OutputDir) {
87+
if !strings.HasPrefix(wfile, filepath.Clean(x.OutputDir)) {
8888
// The file being written is trying to write outside of our base path. Malicious archive?
8989
return 0, fmt.Errorf("%s: %w: %s (from: %s)", zipFile.FileInfo().Name(), ErrInvalidPath, wfile, zipFile.Name)
9090
}

iso.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ func (x *XFile) uniso(isoFile *iso9660.File, parent string) (int64, []string, er
6969

7070
func (x *XFile) unisofile(isoFile *iso9660.File, fileName string) (int64, []string, error) {
7171
destFile := x.clean(fileName)
72-
//nolint:gocritic // this 1-argument filepath.Join removes a ./ prefix should there be one.
73-
if !strings.HasPrefix(destFile, filepath.Join(x.OutputDir)) {
72+
//nolint:gocritic // this 1-argument filepath.Clean removes a ./ prefix should there be one.
73+
if !strings.HasPrefix(destFile, filepath.Clean(x.OutputDir)) {
7474
// The file being written is trying to write outside of our base path. Malicious ISO?
7575
return 0, nil, fmt.Errorf("%s: %w: %s != %s (from: %s)",
7676
x.FilePath, ErrInvalidPath, destFile, x.OutputDir, isoFile.Name())

rar.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ func (x *XFile) unrar(rarReader *rardecode.ReadCloser) (int64, []string, error)
9292
}
9393

9494
wfile := x.clean(header.Name)
95-
//nolint:gocritic // this 1-argument filepath.Join removes a ./ prefix should there be one.
96-
if !strings.HasPrefix(wfile, filepath.Join(x.OutputDir)) {
95+
//nolint:gocritic // this 1-argument filepath.Clean removes a ./ prefix should there be one.
96+
if !strings.HasPrefix(wfile, filepath.Clean(x.OutputDir)) {
9797
// The file being written is trying to write outside of our base path. Malicious archive?
9898
return size, files, fmt.Errorf("%s: %w: %s != %s (from: %s)",
9999
x.FilePath, ErrInvalidPath, wfile, x.OutputDir, header.Name)

tar.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"os"
11+
"path/filepath"
1112
"strings"
1213

1314
lzw "github.com/sshaman1101/dcompress"
@@ -102,7 +103,7 @@ func (x *XFile) untar(tarReader *tar.Reader) (int64, []string, error) {
102103
}
103104

104105
wfile := x.clean(header.Name)
105-
if !strings.HasPrefix(wfile, x.OutputDir) {
106+
if !strings.HasPrefix(wfile, filepath.Clean(x.OutputDir)) {
106107
// The file being written is trying to write outside of our base path. Malicious archive?
107108
return size, files, fmt.Errorf("%s: %w: %s (from: %s)", x.FilePath, ErrInvalidPath, wfile, header.Name)
108109
}

zip.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func ExtractZIP(xFile *XFile) (int64, []string, error) {
2727
return size, files, fmt.Errorf("%s: %w", xFile.FilePath, err)
2828
}
2929

30-
files = append(files, filepath.Join(xFile.OutputDir, zipFile.Name)) //nolint: gosec
30+
files = append(files, filepath.Join(xFile.OutputDir, zipFile.Name)) //nolint:gosec
3131
size += fSize
3232
}
3333

@@ -36,7 +36,7 @@ func ExtractZIP(xFile *XFile) (int64, []string, error) {
3636

3737
func (x *XFile) unzip(zipFile *zip.File) (int64, error) { //nolint:dupl
3838
wfile := x.clean(zipFile.Name)
39-
if !strings.HasPrefix(wfile, x.OutputDir) {
39+
if !strings.HasPrefix(wfile, filepath.Clean(x.OutputDir)) {
4040
// The file being written is trying to write outside of our base path. Malicious archive?
4141
return 0, fmt.Errorf("%s: %w: %s (from: %s)", zipFile.FileInfo().Name(), ErrInvalidPath, wfile, zipFile.Name)
4242
}

0 commit comments

Comments
 (0)