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 4db5a0c
Show file tree
Hide file tree
Showing 2 changed files with 20 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
21 changes: 19 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\", IncludePlatform = "Win")]
public void ExtractingContentsWithNonTraversalPathSucceeds(string outputDir)
{
Assert.DoesNotThrow(() => ExtractTarOK("output", "test-good", allowTraverse: false));
Assert.DoesNotThrow(() => ExtractTarOK(outputDir, "file", allowTraverse: false));
}

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

[Test]
[Category("Tar")]
[Category("CreatesTempFile")]
[Platform(Include = "Win", Reason = "Backslashes are only treated as path separators on windows")]
[TestCase(@"output\", @"..\file")]
[TestCase(@"output/", @"..\file")]
[TestCase("output", @"..\output.txt")]
[TestCase(@"output\", @"..\output.txt")]
public void ExtractingContentsOnWindowsWithDisallowedPathsFails(string outputDir, string fileName)
{
Assert.Throws<InvalidNameException>(() => ExtractTarOK(outputDir, fileName, allowTraverse: false));
}

public void ExtractTarOK(string outputDir, string fileName, bool allowTraverse)
{
var fileContent = Encoding.UTF8.GetBytes("file content");
Expand Down

0 comments on commit 4db5a0c

Please sign in to comment.