Skip to content

Commit 9c3fbfa

Browse files
committed
fix(tar): permit slashed output dir
fixes an issue where TarArchive.ExtractContents would cause an InvalidNameException when the destination dir ended with / adds additional tests for combinations of tar file names and extraction destination paths
1 parent 1b1ab01 commit 9c3fbfa

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ public void ExtractContents(string destinationDirectory, bool allowParentTravers
613613
throw new ObjectDisposedException("TarArchive");
614614
}
615615

616-
var fullDistDir = Path.GetFullPath(destinationDirectory);
616+
var fullDistDir = Path.GetFullPath(destinationDirectory).TrimEnd('/', '\\');
617617

618618
while (true)
619619
{

test/ICSharpCode.SharpZipLib.Tests/Tar/TarArchiveTests.cs

+19-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ public class TarArchiveTests
1313
[Test]
1414
[Category("Tar")]
1515
[Category("CreatesTempFile")]
16-
public void ExtractingContentsWithNonTraversalPathSucceeds()
16+
[TestCase("output")]
17+
[TestCase("output/")]
18+
[TestCase(@"output\")]
19+
public void ExtractingContentsWithNonTraversalPathSucceeds(string outputDir)
1720
{
18-
Assert.DoesNotThrow(() => ExtractTarOK("output", "test-good", allowTraverse: false));
21+
Assert.DoesNotThrow(() => ExtractTarOK(outputDir, "test-good", allowTraverse: false));
1922
}
2023

2124
[Test]
@@ -30,12 +33,26 @@ public void ExtractingContentsWithExplicitlyAllowedTraversalPathSucceeds()
3033
[Category("Tar")]
3134
[Category("CreatesTempFile")]
3235
[TestCase("output", "../file")]
36+
[TestCase("output/", "../file")]
3337
[TestCase("output", "../output.txt")]
3438
public void ExtractingContentsWithDisallowedPathsFails(string outputDir, string fileName)
3539
{
3640
Assert.Throws<InvalidNameException>(() => ExtractTarOK(outputDir, fileName, allowTraverse: false));
3741
}
3842

43+
[Test]
44+
[Category("Tar")]
45+
[Category("CreatesTempFile")]
46+
[Platform(Include = "Win", Reason = "Backslashes are only treated as path separators on windows")]
47+
[TestCase(@"output\", @"..\file")]
48+
[TestCase(@"output/", @"..\file")]
49+
[TestCase("output", @"..\output.txt")]
50+
[TestCase(@"output\", @"..\output.txt")]
51+
public void ExtractingContentsOnWindowsWithDisallowedPathsFails(string outputDir, string fileName)
52+
{
53+
Assert.Throws<InvalidNameException>(() => ExtractTarOK(outputDir, fileName, allowTraverse: false));
54+
}
55+
3956
public void ExtractTarOK(string outputDir, string fileName, bool allowTraverse)
4057
{
4158
var fileContent = Encoding.UTF8.GetBytes("file content");

0 commit comments

Comments
 (0)