Skip to content

Commit

Permalink
fix(tar): permit slashed output dir
Browse files Browse the repository at this point in the history
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
  • Loading branch information
piksel committed Sep 19, 2021
1 parent 1b1ab01 commit b089197
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ public void ExtractContents(string destinationDirectory, bool allowParentTravers
throw new ObjectDisposedException("TarArchive");
}

var fullDistDir = Path.GetFullPath(destinationDirectory);
var fullDistDir = Path.GetFullPath(destinationDirectory).TrimEnd('/', '\\');

while (true)
{
Expand Down
11 changes: 9 additions & 2 deletions test/ICSharpCode.SharpZipLib.Tests/Tar/TarArchiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ public class TarArchiveTests
[Test]
[Category("Tar")]
[Category("CreatesTempFile")]
public void ExtractingContentsWithNonTraversalPathSucceeds()
[TestCase("output")]
[TestCase("output/")]
[TestCase(@"output\")]
public void ExtractingContentsWithNonTraversalPathSucceeds(string outputDir)
{
Assert.DoesNotThrow(() => ExtractTarOK("output", "test-good", allowTraverse: false));
Assert.DoesNotThrow(() => ExtractTarOK(outputDir, "test-good", allowTraverse: false));
}

[Test]
Expand All @@ -30,7 +33,11 @@ public void ExtractingContentsWithExplicitlyAllowedTraversalPathSucceeds()
[Category("Tar")]
[Category("CreatesTempFile")]
[TestCase("output", "../file")]
[TestCase(@"output\", @"..\file")]
[TestCase(@"output/", @"..\file")]
[TestCase("output", "../output.txt")]
[TestCase("output", @"..\output.txt")]
[TestCase(@"output\", @"..\output.txt")]
public void ExtractingContentsWithDisallowedPathsFails(string outputDir, string fileName)
{
Assert.Throws<InvalidNameException>(() => ExtractTarOK(outputDir, fileName, allowTraverse: false));
Expand Down

0 comments on commit b089197

Please sign in to comment.