Skip to content

Commit 239ec40

Browse files
committed
Fix path traversal vulnerability, issue #21
1 parent fa2f5a7 commit 239ec40

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

unarr.go

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

@@ -170,7 +171,7 @@ func (a *Archive) Offset() int64 {
170171

171172
// Name returns the name of the current entry as UTF-8 string
172173
func (a *Archive) Name() string {
173-
return unarrc.EntryGetName(a.archive)
174+
return toValidName(unarrc.EntryGetName(a.archive))
174175
}
175176

176177
// RawName returns the name of the current entry as raw string
@@ -263,3 +264,14 @@ func (a *Archive) List() (contents []string, err error) {
263264

264265
return
265266
}
267+
268+
func toValidName(name string) string {
269+
p := filepath.Clean(name)
270+
if strings.HasPrefix(p, "/") {
271+
p = p[len("/"):]
272+
}
273+
for strings.HasPrefix(p, "../") {
274+
p = p[len("../"):]
275+
}
276+
return p
277+
}

0 commit comments

Comments
 (0)