File tree Expand file tree Collapse file tree 1 file changed +12
-0
lines changed
Expand file tree Collapse file tree 1 file changed +12
-0
lines changed Original file line number Diff line number Diff line change @@ -500,6 +500,18 @@ func ExtractTgzWithStatus(tgzFile, destDir string) error {
500500 // Increment the file count
501501 totalFilesExtracted ++
502502 case tar .TypeSymlink :
503+ // Sanitize symlink target to prevent directory traversal
504+ sanitizedLinkname := filepath .Clean (header .Linkname )
505+ if strings .HasPrefix (sanitizedLinkname , "/" ) || strings .Contains (sanitizedLinkname , ".." ) {
506+ return fmt .Errorf ("invalid symlink target in archive: %s -> %s" , header .Name , header .Linkname )
507+ }
508+
509+ // Ensure the resolved symlink target stays within the destination directory
510+ fullTarget := filepath .Join (filepath .Dir (target ), sanitizedLinkname )
511+ if ! strings .HasPrefix (filepath .Clean (fullTarget ), filepath .Clean (destDir )+ string (os .PathSeparator )) {
512+ return fmt .Errorf ("attempted directory traversal in symlink: %s -> %s" , header .Name , header .Linkname )
513+ }
514+
503515 // Create symlink
504516 if err := os .Symlink (header .Linkname , target ); err != nil {
505517 return fmt .Errorf ("failed to create symlink: %v" , err )
You can’t perform that action at this time.
0 commit comments