Skip to content

Commit c9a5573

Browse files
committed
fix code warnning
1 parent b87c88b commit c9a5573

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

tools/trond/utils/http.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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)

0 commit comments

Comments
 (0)