Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OS-specific path separator #71

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*~
.idea
*.DS*
*.zip
*.rar
Expand Down
10 changes: 5 additions & 5 deletions 7z.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ func extract7z(xFile *XFile) (int64, []string, []string, error) {
if err != nil {
lastFile := xFile.FilePath
/* // https://github.com/bodgit/sevenzip/issues/54
// We can probably never get the file with the error.
if volumes := sevenZip.Volumes(); len(volumes) > 0 {
lastFile = volumes[len(volumes)-1]
} */
// We can probably never get the file with the error.
if volumes := sevenZip.Volumes(); len(volumes) > 0 {
lastFile = volumes[len(volumes)-1]
} */
return size, files, sevenZip.Volumes(), fmt.Errorf("%s: %w", lastFile, err)
}

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

func (x *XFile) un7zip(zipFile *sevenzip.File) (int64, error) { //nolint:dupl
wfile := x.clean(zipFile.Name)
if !strings.HasPrefix(wfile, x.OutputDir) {
if !strings.HasPrefix(wfile, filepath.Clean(x.OutputDir)) {
// The file being written is trying to write outside of our base path. Malicious archive?
return 0, fmt.Errorf("%s: %w: %s (from: %s)", zipFile.FileInfo().Name(), ErrInvalidPath, wfile, zipFile.Name)
}
Expand Down
4 changes: 2 additions & 2 deletions iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func (x *XFile) uniso(isoFile *iso9660.File, parent string) (int64, []string, er

func (x *XFile) unisofile(isoFile *iso9660.File, fileName string) (int64, []string, error) {
destFile := x.clean(fileName)
//nolint:gocritic // this 1-argument filepath.Join removes a ./ prefix should there be one.
if !strings.HasPrefix(destFile, filepath.Join(x.OutputDir)) {
//nolint:gocritic // this 1-argument filepath.Clean removes a ./ prefix should there be one.
if !strings.HasPrefix(destFile, filepath.Clean(x.OutputDir)) {
// The file being written is trying to write outside of our base path. Malicious ISO?
return 0, nil, fmt.Errorf("%s: %w: %s != %s (from: %s)",
x.FilePath, ErrInvalidPath, destFile, x.OutputDir, isoFile.Name())
Expand Down
4 changes: 2 additions & 2 deletions rar.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func (x *XFile) unrar(rarReader *rardecode.ReadCloser) (int64, []string, error)
}

wfile := x.clean(header.Name)
//nolint:gocritic // this 1-argument filepath.Join removes a ./ prefix should there be one.
if !strings.HasPrefix(wfile, filepath.Join(x.OutputDir)) {
//nolint:gocritic // this 1-argument filepath.Clean removes a ./ prefix should there be one.
if !strings.HasPrefix(wfile, filepath.Clean(x.OutputDir)) {
// The file being written is trying to write outside of our base path. Malicious archive?
return size, files, fmt.Errorf("%s: %w: %s != %s (from: %s)",
x.FilePath, ErrInvalidPath, wfile, x.OutputDir, header.Name)
Expand Down
3 changes: 2 additions & 1 deletion tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

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

wfile := x.clean(header.Name)
if !strings.HasPrefix(wfile, x.OutputDir) {
if !strings.HasPrefix(wfile, filepath.Clean(x.OutputDir)) {
// The file being written is trying to write outside of our base path. Malicious archive?
return size, files, fmt.Errorf("%s: %w: %s (from: %s)", x.FilePath, ErrInvalidPath, wfile, header.Name)
}
Expand Down
4 changes: 2 additions & 2 deletions zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func ExtractZIP(xFile *XFile) (int64, []string, error) {
return size, files, fmt.Errorf("%s: %w", xFile.FilePath, err)
}

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

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

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