Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tar): enable unix paths in tar RootPath #582

Merged
merged 15 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,7 @@ public string RootPath
{
throw new ObjectDisposedException("TarArchive");
}
// Convert to forward slashes for matching. Trim trailing / for correct final path
rootPath = value.Replace('\\', '/').TrimEnd('/');
rootPath = value.ToTarArchivePath().TrimEnd('/');
}
}

Expand Down Expand Up @@ -660,7 +659,9 @@ private void ExtractEntry(string destDir, TarEntry entry, bool allowParentTraver
string destFile = Path.Combine(destDir, name);
var destFileDir = Path.GetDirectoryName(Path.GetFullPath(destFile)) ?? "";

if (!allowParentTraversal && !destFileDir.StartsWith(destDir, StringComparison.InvariantCultureIgnoreCase))
var isRootDir = entry.IsDirectory && entry.Name == "";

if (!allowParentTraversal && !isRootDir && !destFileDir.StartsWith(destDir, StringComparison.InvariantCultureIgnoreCase))
{
throw new InvalidNameException("Parent traversal in paths is not allowed");
}
Expand Down
7 changes: 1 addition & 6 deletions src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,10 @@ public void GetFileTarHeader(TarHeader header, string file)
}
*/

name = name.Replace(Path.DirectorySeparatorChar, '/');

// No absolute pathnames
// Windows (and Posix?) paths can start with UNC style "\\NetworkDrive\",
// so we loop on starting /'s.
while (name.StartsWith("/", StringComparison.Ordinal))
{
name = name.Substring(1);
}
name = name.ToTarArchivePath();

header.LinkName = String.Empty;
header.Name = name;
Expand Down
13 changes: 13 additions & 0 deletions src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.IO;
using ICSharpCode.SharpZipLib.Core;

sensslen marked this conversation as resolved.
Show resolved Hide resolved
namespace ICSharpCode.SharpZipLib.Tar
{
internal static class TarStringExtension
{
public static string ToTarArchivePath(this string s)
{
return PathUtils.DropPathRoot(s).Replace(Path.DirectorySeparatorChar, '/');
}
}
}
48 changes: 45 additions & 3 deletions test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ public void ParseHeaderWithEncoding(int length, string encodingName)
reparseHeader.ParseBuffer(headerbytes, enc);
Assert.AreEqual(name, reparseHeader.Name);
// top 100 bytes are name field in tar header
for (int i = 0;i < encodedName.Length;i++)
for (int i = 0; i < encodedName.Length; i++)
{
Assert.AreEqual(encodedName[i], headerbytes[i]);
}
Expand All @@ -852,9 +852,9 @@ public async Task StreamWithJapaneseNameAsync(int length, string encodingName)
var entryName = new string((char)0x3042, length);
var data = new byte[32];
var encoding = Encoding.GetEncoding(encodingName);
using(var memoryStream = new MemoryStream())
using (var memoryStream = new MemoryStream())
{
using(var tarOutput = new TarOutputStream(memoryStream, encoding))
using (var tarOutput = new TarOutputStream(memoryStream, encoding))
{
var entry = TarEntry.CreateTarEntry(entryName);
entry.Size = 32;
Expand All @@ -874,5 +874,47 @@ public async Task StreamWithJapaneseNameAsync(int length, string encodingName)
File.WriteAllBytes(Path.Combine(Path.GetTempPath(), $"jpnametest_{length}_{encodingName}.tar"), memoryStream.ToArray());
}
}
/// <summary>
/// This test could be considered integration test. it creates a tar archive with the root directory specified
/// Then extracts it and compares the two folders. This used to fail on unix due to issues with root folder handling
/// in the tar archive.
/// </summary>
[Test]
[Category("Tar")]
public void RootPathIsRespected()
{
using (var extractDirectory = new TempDir())
using (var tarFileName = new TempFile())
using (var tempDirectory = new TempDir())
{
tempDirectory.CreateDummyFile();

using (var tarFile = File.Open(tarFileName.FullName, FileMode.Create))
{
using (var tarOutputStream = TarArchive.CreateOutputTarArchive(tarFile))
{
tarOutputStream.RootPath = tempDirectory.FullName;
var entry = TarEntry.CreateEntryFromFile(tempDirectory.FullName);
tarOutputStream.WriteEntry(entry, true);
}
}

using (var file = File.OpenRead(tarFileName.FullName))
{
using (var archive = TarArchive.CreateInputTarArchive(file, Encoding.UTF8))
{
archive.ExtractContents(extractDirectory.FullName);
}
}

var expectationDirectory = new DirectoryInfo(tempDirectory.FullName);
foreach (var checkFile in expectationDirectory.GetFiles("", SearchOption.AllDirectories))
{
var relativePath = checkFile.FullName.Substring(expectationDirectory.FullName.Length + 1);
FileAssert.Exists(Path.Combine(extractDirectory.FullName, relativePath));
FileAssert.AreEqual(checkFile.FullName, Path.Combine(extractDirectory.FullName, relativePath));
}
}
}
}
}