Skip to content

Commit 453c669

Browse files
committed
fix: optimized archive extraction path traversal checks
1 parent 710a4ed commit 453c669

File tree

4 files changed

+14
-19
lines changed

4 files changed

+14
-19
lines changed

example/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module opensca-example
22

3-
go 1.20
3+
go 1.25
44

55
require github.com/xmirrorsecurity/opensca-cli/v3 v3.0.2
66

opensca/walk/rar.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"io"
66
"os"
77
"path/filepath"
8-
"strings"
98

109
"github.com/xmirrorsecurity/opensca-cli/v3/opensca/logs"
1110

@@ -42,15 +41,14 @@ func xrar(ctx context.Context, filter ExtractFileFilter, input, output string) b
4241
break
4342
}
4443

45-
fp := filepath.Join(output, fh.Name)
46-
if fh.IsDir {
47-
os.MkdirAll(fp, 0755)
44+
fp, err := resolveExtractPath(output, fh.Name)
45+
if err != nil {
46+
logs.Warn(err)
4847
continue
4948
}
5049

51-
// avoid path traversal
52-
if !strings.HasPrefix(fp, filepath.Clean(output)+string(os.PathSeparator)) {
53-
logs.Warn("Invalid file path: %s", fp)
50+
if fh.IsDir {
51+
os.MkdirAll(fp, 0755)
5452
continue
5553
}
5654

opensca/walk/tar.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,9 @@ func xtar(ctx context.Context, filter ExtractFileFilter, input, output string) b
4444
break
4545
}
4646

47-
fp := filepath.Join(output, fh.Name)
48-
49-
// avoid zip slip
50-
if !strings.HasPrefix(fp, filepath.Clean(output)+string(os.PathSeparator)) {
51-
logs.Warn("Invalid file path: %s", fp)
47+
fp, err := resolveExtractPath(output, fh.Name)
48+
if err != nil {
49+
logs.Warn(err)
5250
continue
5351
}
5452

opensca/walk/zip.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"io"
88
"os"
99
"path/filepath"
10-
"strings"
1110

1211
"github.com/xmirrorsecurity/opensca-cli/v3/opensca/common"
1312
"github.com/xmirrorsecurity/opensca-cli/v3/opensca/logs"
@@ -40,17 +39,17 @@ func xzip(ctx context.Context, filter ExtractFileFilter, input, output string) b
4039
continue
4140
}
4241

43-
fp := filepath.Join(output, f.Name)
42+
entryName := f.Name
4443

4544
if f.Flags == 0 {
4645
gbk := mahonia.NewDecoder("gbk").ConvertString(f.Name)
4746
_, cdata, _ := mahonia.NewDecoder("utf-8").Translate([]byte(gbk), true)
48-
fp = filepath.Join(output, string(cdata))
47+
entryName = string(cdata)
4948
}
5049

51-
// avoid zip slip
52-
if !strings.HasPrefix(fp, filepath.Clean(output)+string(os.PathSeparator)) {
53-
logs.Warn("Invalid file path: %s", fp)
50+
fp, err := resolveExtractPath(output, entryName)
51+
if err != nil {
52+
logs.Warn(err)
5453
continue
5554
}
5655

0 commit comments

Comments
 (0)