1
1
using System ;
2
2
using System . IO ;
3
+ using System . Numerics ;
3
4
using System . Text ;
5
+ using ICSharpCode . SharpZipLib . Core ;
4
6
5
7
namespace ICSharpCode . SharpZipLib . Tar
6
8
{
@@ -594,7 +596,17 @@ public void ListContents()
594
596
/// <param name="destinationDirectory">
595
597
/// The destination directory into which to extract.
596
598
/// </param>
597
- public void ExtractContents ( string destinationDirectory )
599
+ public void ExtractContents ( string destinationDirectory )
600
+ => ExtractContents ( destinationDirectory , false ) ;
601
+
602
+ /// <summary>
603
+ /// Perform the "extract" command and extract the contents of the archive.
604
+ /// </summary>
605
+ /// <param name="destinationDirectory">
606
+ /// The destination directory into which to extract.
607
+ /// </param>
608
+ /// <param name="allowParentTraversal">Allow parent directory traversal in file paths (e.g. ../file)</param>
609
+ public void ExtractContents ( string destinationDirectory , bool allowParentTraversal )
598
610
{
599
611
if ( isDisposed )
600
612
{
@@ -613,7 +625,7 @@ public void ExtractContents(string destinationDirectory)
613
625
if ( entry . TarHeader . TypeFlag == TarHeader . LF_LINK || entry . TarHeader . TypeFlag == TarHeader . LF_SYMLINK )
614
626
continue ;
615
627
616
- ExtractEntry ( destinationDirectory , entry ) ;
628
+ ExtractEntry ( destinationDirectory , entry , allowParentTraversal ) ;
617
629
}
618
630
}
619
631
@@ -627,7 +639,8 @@ public void ExtractContents(string destinationDirectory)
627
639
/// <param name="entry">
628
640
/// The TarEntry returned by tarIn.GetNextEntry().
629
641
/// </param>
630
- private void ExtractEntry ( string destDir , TarEntry entry )
642
+ /// <param name="allowParentTraversal">Allow parent directory traversal in file paths (e.g. ../file)</param>
643
+ private void ExtractEntry ( string destDir , TarEntry entry , bool allowParentTraversal )
631
644
{
632
645
OnProgressMessageEvent ( entry , null ) ;
633
646
@@ -644,6 +657,11 @@ private void ExtractEntry(string destDir, TarEntry entry)
644
657
645
658
string destFile = Path . Combine ( destDir , name ) ;
646
659
660
+ if ( ! allowParentTraversal && ! Path . GetFullPath ( destFile ) . StartsWith ( destDir , StringComparison . InvariantCultureIgnoreCase ) )
661
+ {
662
+ throw new InvalidNameException ( "Parent traversal in paths is not allowed" ) ;
663
+ }
664
+
647
665
if ( entry . IsDirectory )
648
666
{
649
667
EnsureDirectoryExists ( destFile ) ;
0 commit comments