Skip to content

Commit 5f3d250

Browse files
committed
Fix OS-specific path separator
1 parent 53cf1d6 commit 5f3d250

File tree

6 files changed

+59
-52
lines changed

6 files changed

+59
-52
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: 6 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,8 @@ 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+
outputDir := filepath.Clean(x.OutputDir)
88+
if !strings.HasPrefix(wfile, outputDir) {
8889
// The file being written is trying to write outside of our base path. Malicious archive?
8990
return 0, fmt.Errorf("%s: %w: %s (from: %s)", zipFile.FileInfo().Name(), ErrInvalidPath, wfile, zipFile.Name)
9091
}

iso.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ 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+
outputDir := filepath.Clean(x.OutputDir)
73+
//nolint:gocritic // this 1-argument filepath.Clean removes a ./ prefix should there be one.
74+
if !strings.HasPrefix(destFile, outputDir) {
7475
// The file being written is trying to write outside of our base path. Malicious ISO?
7576
return 0, nil, fmt.Errorf("%s: %w: %s != %s (from: %s)",
7677
x.FilePath, ErrInvalidPath, destFile, x.OutputDir, isoFile.Name())

rar.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ 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+
outputDir := filepath.Clean(x.OutputDir)
96+
//nolint:gocritic // this 1-argument filepath.Clean removes a ./ prefix should there be one.
97+
if !strings.HasPrefix(wfile, outputDir) {
9798
// The file being written is trying to write outside of our base path. Malicious archive?
9899
return size, files, fmt.Errorf("%s: %w: %s != %s (from: %s)",
99100
x.FilePath, ErrInvalidPath, wfile, x.OutputDir, header.Name)

tar.go

Lines changed: 3 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,8 @@ 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+
outputDir := filepath.Clean(x.OutputDir)
107+
if !strings.HasPrefix(wfile, outputDir) {
106108
// The file being written is trying to write outside of our base path. Malicious archive?
107109
return size, files, fmt.Errorf("%s: %w: %s (from: %s)", x.FilePath, ErrInvalidPath, wfile, header.Name)
108110
}

zip.go

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,65 @@
11
package xtractr
22

33
import (
4-
"archive/zip"
5-
"fmt"
6-
"os"
7-
"path/filepath"
8-
"strings"
4+
"archive/zip"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
99
)
1010

1111
/* How to extract a ZIP file. */
1212

1313
// ExtractZIP extracts a zip file.. to a destination. Simple enough.
1414
func ExtractZIP(xFile *XFile) (int64, []string, error) {
15-
zipReader, err := zip.OpenReader(xFile.FilePath)
16-
if err != nil {
17-
return 0, nil, fmt.Errorf("zip.OpenReader: %w", err)
18-
}
19-
defer zipReader.Close()
15+
zipReader, err := zip.OpenReader(xFile.FilePath)
16+
if err != nil {
17+
return 0, nil, fmt.Errorf("zip.OpenReader: %w", err)
18+
}
19+
defer zipReader.Close()
2020

21-
files := []string{}
22-
size := int64(0)
21+
files := []string{}
22+
size := int64(0)
2323

24-
for _, zipFile := range zipReader.Reader.File {
25-
fSize, err := xFile.unzip(zipFile)
26-
if err != nil {
27-
return size, files, fmt.Errorf("%s: %w", xFile.FilePath, err)
28-
}
24+
for _, zipFile := range zipReader.Reader.File {
25+
fSize, err := xFile.unzip(zipFile)
26+
if err != nil {
27+
return size, files, fmt.Errorf("%s: %w", xFile.FilePath, err)
28+
}
2929

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

34-
return size, files, nil
34+
return size, files, nil
3535
}
3636

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

44-
if strings.HasSuffix(wfile, "/") || zipFile.FileInfo().IsDir() {
45-
if err := os.MkdirAll(wfile, x.DirMode); err != nil {
46-
return 0, fmt.Errorf("making zipFile dir: %w", err)
47-
}
45+
if strings.HasSuffix(wfile, "/") || zipFile.FileInfo().IsDir() {
46+
if err := os.MkdirAll(wfile, x.DirMode); err != nil {
47+
return 0, fmt.Errorf("making zipFile dir: %w", err)
48+
}
4849

49-
return 0, nil
50-
}
50+
return 0, nil
51+
}
5152

52-
zFile, err := zipFile.Open()
53-
if err != nil {
54-
return 0, fmt.Errorf("zipFile.Open: %w", err)
55-
}
56-
defer zFile.Close()
53+
zFile, err := zipFile.Open()
54+
if err != nil {
55+
return 0, fmt.Errorf("zipFile.Open: %w", err)
56+
}
57+
defer zFile.Close()
5758

58-
s, err := writeFile(wfile, zFile, x.FileMode, x.DirMode)
59-
if err != nil {
60-
return s, fmt.Errorf("%s: %w: %s (from: %s)", zipFile.FileInfo().Name(), err, wfile, zipFile.Name)
61-
}
59+
s, err := writeFile(wfile, zFile, x.FileMode, x.DirMode)
60+
if err != nil {
61+
return s, fmt.Errorf("%s: %w: %s (from: %s)", zipFile.FileInfo().Name(), err, wfile, zipFile.Name)
62+
}
6263

63-
return s, nil
64+
return s, nil
6465
}

0 commit comments

Comments
 (0)