Skip to content

Commit ab2b71a

Browse files
authored
Merge pull request #572 from oasisprotocol/kostko/fix/rofl-artifacts-tar-symlinks
2 parents 29838c6 + 217beb4 commit ab2b71a

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

cmd/rofl/build/artifacts.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11+
"io/fs"
1112
"net/http"
1213
"net/url"
1314
"os"
@@ -130,6 +131,22 @@ func extractArchive(fn, outputDir string) error {
130131
cleanupPath := func(path string) (string, error) {
131132
// Sanitize path to ensure it doesn't escape to any parent directories.
132133
path = filepath.Clean(filepath.Join(outputDir, path))
134+
135+
var pathErr *fs.PathError
136+
resolvedPath, err := filepath.EvalSymlinks(path)
137+
switch {
138+
case err == nil:
139+
// Path resolved successfully, use it.
140+
path = resolvedPath
141+
case errors.As(err, &pathErr) && errors.Is(pathErr.Err, fs.ErrNotExist):
142+
// There was an error while resolving the path. This is fine as the destination path will
143+
// usually not exist. Check that the non-existent path doesn't escape.
144+
if !strings.HasPrefix(pathErr.Path, outputDir) {
145+
return "", fmt.Errorf("malformed path in archive")
146+
}
147+
default:
148+
return "", fmt.Errorf("unable to sanitize path: %w", err)
149+
}
133150
if !strings.HasPrefix(path, outputDir) {
134151
return "", fmt.Errorf("malformed path in archive")
135152
}

0 commit comments

Comments
 (0)