Skip to content

Commit 84ce672

Browse files
authored
Merge pull request #9420 from okatu-loli/fix/archive-decomposs
feat(iso9660): Refactor path handling for secure file extraction
2 parents 875b27f + a0389e2 commit 84ce672

2 files changed

Lines changed: 28 additions & 9 deletions

File tree

internal/archive/iso9660/iso9660.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package iso9660
22

33
import (
4+
"io"
5+
"os"
6+
47
"github.com/alist-org/alist/v3/internal/archive/tool"
58
"github.com/alist-org/alist/v3/internal/errs"
69
"github.com/alist-org/alist/v3/internal/model"
710
"github.com/alist-org/alist/v3/internal/stream"
811
"github.com/kdomanski/iso9660"
9-
"io"
10-
"os"
11-
stdpath "path"
1212
)
1313

1414
type ISO9660 struct {
@@ -78,7 +78,10 @@ func (ISO9660) Decompress(ss []*stream.SeekableStream, outputPath string, args m
7878
}
7979
if obj.IsDir() {
8080
if args.InnerPath != "/" {
81-
outputPath = stdpath.Join(outputPath, obj.Name())
81+
outputPath, err = tool.SecureJoin(outputPath, obj.Name())
82+
if err != nil {
83+
return err
84+
}
8285
if err = os.MkdirAll(outputPath, 0700); err != nil {
8386
return err
8487
}

internal/archive/iso9660/utils.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package iso9660
22

33
import (
4+
"io"
45
"os"
5-
stdpath "path"
6+
"path/filepath"
67
"strings"
78

9+
"github.com/alist-org/alist/v3/internal/archive/tool"
810
"github.com/alist-org/alist/v3/internal/errs"
911
"github.com/alist-org/alist/v3/internal/model"
1012
"github.com/alist-org/alist/v3/internal/stream"
@@ -62,15 +64,26 @@ func toModelObj(file *iso9660.File) model.Obj {
6264
}
6365

6466
func decompress(f *iso9660.File, path string, up model.UpdateProgress) error {
65-
file, err := os.OpenFile(stdpath.Join(path, f.Name()), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
67+
return decompressEntry(f.Reader(), f.Size(), path, f.Name(), up)
68+
}
69+
70+
func decompressEntry(reader io.Reader, size int64, path, entryName string, up model.UpdateProgress) error {
71+
dstPath, err := tool.SecureJoin(path, entryName)
72+
if err != nil {
73+
return err
74+
}
75+
if err = os.MkdirAll(filepath.Dir(dstPath), 0700); err != nil {
76+
return err
77+
}
78+
file, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
6679
if err != nil {
6780
return err
6881
}
6982
defer file.Close()
7083
_, err = utils.CopyWithBuffer(file, &stream.ReaderUpdatingProgress{
7184
Reader: &stream.SimpleReaderWithSize{
72-
Reader: f.Reader(),
73-
Size: f.Size(),
85+
Reader: reader,
86+
Size: size,
7487
},
7588
UpdateProgress: up,
7689
})
@@ -84,7 +97,10 @@ func decompressAll(children []*iso9660.File, path string) error {
8497
if err != nil {
8598
return err
8699
}
87-
nextPath := stdpath.Join(path, child.Name())
100+
nextPath, err := tool.SecureJoin(path, child.Name())
101+
if err != nil {
102+
return err
103+
}
88104
if err = os.MkdirAll(nextPath, 0700); err != nil {
89105
return err
90106
}

0 commit comments

Comments
 (0)