Skip to content

Commit 6c84bd5

Browse files
committed
fix: optimized archive extraction path traversal checks
1 parent 7f79609 commit 6c84bd5

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

opensca/walk/path.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package walk
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
"strings"
7+
)
8+
9+
func resolveExtractPath(output, entry string) (string, error) {
10+
if filepath.IsAbs(entry) {
11+
return "", fmt.Errorf("invalid file path: %s", entry)
12+
}
13+
14+
base := filepath.Clean(output)
15+
target := filepath.Join(base, entry)
16+
17+
rel, err := filepath.Rel(base, target)
18+
if err != nil {
19+
return "", err
20+
}
21+
22+
if rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
23+
return "", fmt.Errorf("invalid file path: %s", entry)
24+
}
25+
26+
return target, nil
27+
}

0 commit comments

Comments
 (0)