File tree Expand file tree Collapse file tree 1 file changed +17
-0
lines changed
Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change 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 }
You can’t perform that action at this time.
0 commit comments