From 176a501a64bf52f116945b014fe0e2de6721c003 Mon Sep 17 00:00:00 2001 From: sensslen Date: Wed, 17 Feb 2021 08:37:40 +0100 Subject: [PATCH 01/14] Fix Root path on unix Operating system (cherry picked from commit 14a6292fe21488d655efdc649e100246902e4bce) (cherry picked from commit 93e3464e401c006ebde5db48634475552ea9f1c7) --- src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs b/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs index 7bc770d17..84b8d1795 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs @@ -358,6 +358,11 @@ public string RootPath } // Convert to forward slashes for matching. Trim trailing / for correct final path rootPath = value.Replace('\\', '/').TrimEnd('/'); + // Fix rooted paths on linux + while (rootPath.StartsWith("/", StringComparison.Ordinal)) + { + rootPath = rootPath.Substring(1); + } } } From 4a9fd0d23a94640d287d2ec79eb18ec4441bb0b8 Mon Sep 17 00:00:00 2001 From: Simon Ensslen Date: Wed, 17 Feb 2021 10:12:53 +0100 Subject: [PATCH 02/14] - Use string extension method for path cleanup to make it equal wihtout code duplication - Add unit test that tests the compression and extraction (cherry picked from commit cf007d53c9cddc51bc652fec05b1ad40b69b6a74) (cherry picked from commit 29ac1d3571cd3cf8d522a930f8ca2766afa0b43a) --- src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs | 8 +- src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs | 7 +- .../Tar/TarStringExtension.cs | 16 ++++ .../Tar/TarTests.cs | 77 +++++++++++++++++-- 4 files changed, 90 insertions(+), 18 deletions(-) create mode 100644 src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs b/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs index 84b8d1795..91b513427 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs @@ -356,13 +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('/'); - // Fix rooted paths on linux - while (rootPath.StartsWith("/", StringComparison.Ordinal)) - { - rootPath = rootPath.Substring(1); - } + rootPath = value.ClearTarPath().TrimEnd('/'); } } diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs b/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs index 64a1e5e18..cec6fc322 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs @@ -418,15 +418,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.ClearTarPath(); header.LinkName = String.Empty; header.Name = name; diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs b/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs new file mode 100644 index 000000000..b5506c189 --- /dev/null +++ b/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs @@ -0,0 +1,16 @@ +using System.IO; + +namespace ICSharpCode.SharpZipLib.Tar +{ + internal static class TarStringExtension + { + public static string ClearTarPath(this string s) + { + if (Path.GetPathRoot(s) != null) + { + s = s.Substring(Path.GetPathRoot(s).Length); + } + return s.Replace(Path.DirectorySeparatorChar, '/'); + } + } +} diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs index 5cdd9404e..b53ffdb6a 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs @@ -859,7 +859,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]); } @@ -878,17 +878,17 @@ public void StreamWithJapaneseName(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; tarOutput.PutNextEntry(entry); tarOutput.Write(data, 0, data.Length); } - using(var memInput = new MemoryStream(memoryStream.ToArray())) - using(var inputStream = new TarInputStream(memInput, encoding)) + using (var memInput = new MemoryStream(memoryStream.ToArray())) + using (var inputStream = new TarInputStream(memInput, encoding)) { var buf = new byte[64]; var entry = inputStream.GetNextEntry(); @@ -899,5 +899,72 @@ public void StreamWithJapaneseName(int length, string encodingName) File.WriteAllBytes(Path.Combine(Path.GetTempPath(), $"jpnametest_{length}_{encodingName}.tar"), memoryStream.ToArray()); } } + [Test] + [Category("Tar")] + public void rootPathIsRespected() + { + // create dummy folder structure + var tempDirectory = Path.Combine(Path.GetTempPath(), "sharpziplib_tar_test_folder"); + CreateAndClearDirectory(tempDirectory); + using (var dummyfile = File.Create("dummyfile")) + { + using (var randomStream = new ChaosStream()) + { + randomStream.CopyTo(dummyfile); + } + } + + var tarFileName = Path.Combine(Path.GetTempPath(), "sharpziplib_tar_test_folder_archive.tar"); + + using (var tarFile = File.Open(tarFileName, FileMode.Create)) + { + using (var tarOutputStream = TarArchive.CreateOutputTarArchive(tarFile)) + { + tarOutputStream.RootPath = tempDirectory; + var entry = TarEntry.CreateEntryFromFile(tempDirectory); + tarOutputStream.WriteEntry(entry, true); + } + } + + var extractDirectory = Path.Combine(Path.GetTempPath(), "sharpziplib_tar_extract_folder"); + CreateAndClearDirectory(extractDirectory); + using (var file = File.OpenRead(tarFileName)) + { + using (var archive = TarArchive.CreateInputTarArchive(file, Encoding.UTF8)) + { + archive.ExtractContents(extractDirectory); + } + } + + var expectationDirectory = new DirectoryInfo(tempDirectory); + foreach (var checkFile in expectationDirectory.GetFiles("", SearchOption.AllDirectories)) + { + var relativePath = expectationDirectory.FullName.Substring(expectationDirectory.FullName.Length); + FileAssert.Exists(Path.Combine(extractDirectory, relativePath)); + FileAssert.AreEqual(checkFile.FullName, Path.Combine(extractDirectory, relativePath)); + } + } + + private void CreateAndClearDirectory(string path) + { + if (Directory.Exists(path)) + { + Directory.Delete(path); + } + Directory.CreateDirectory(path); + } + + public class ChaosStream : MemoryStream + { + private readonly int length = new Random().Next() % 5000 + 200; + + // Create constructors as needed to match desired MemoryStream construction + + public override int Read(byte[] buffer, int offset, int count) + { + int readCount = Math.Max(0, Math.Min(length - offset, count)); + return base.Read(buffer, offset, readCount); + } + } } } From 52f3dc63cd99ef57655deb1f5ea58cc612f0930b Mon Sep 17 00:00:00 2001 From: Simon Ensslen Date: Wed, 17 Feb 2021 10:20:15 +0100 Subject: [PATCH 03/14] Add test commend and correct file location for dummy file (cherry picked from commit a90e117a8bddd59e5c85306177ddee84c887b8f2) (cherry picked from commit 216ab62bd95c9330249715b4a62b0a346a4b38c3) --- test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs index b53ffdb6a..80f762bdd 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs @@ -899,6 +899,11 @@ public void StreamWithJapaneseName(int length, string encodingName) File.WriteAllBytes(Path.Combine(Path.GetTempPath(), $"jpnametest_{length}_{encodingName}.tar"), memoryStream.ToArray()); } } + /// + /// 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. + /// [Test] [Category("Tar")] public void rootPathIsRespected() @@ -906,7 +911,7 @@ public void rootPathIsRespected() // create dummy folder structure var tempDirectory = Path.Combine(Path.GetTempPath(), "sharpziplib_tar_test_folder"); CreateAndClearDirectory(tempDirectory); - using (var dummyfile = File.Create("dummyfile")) + using (var dummyfile = File.Create(Path.Combine(tempDirectory, "dummyfile"))) { using (var randomStream = new ChaosStream()) { @@ -956,7 +961,7 @@ private void CreateAndClearDirectory(string path) public class ChaosStream : MemoryStream { - private readonly int length = new Random().Next() % 5000 + 200; + private readonly int length = new Random().Next() % 500000 + 200; // Create constructors as needed to match desired MemoryStream construction From a5c16343cf239b523304e8fecb4282d9f04056c4 Mon Sep 17 00:00:00 2001 From: Simon Ensslen Date: Wed, 17 Feb 2021 10:40:59 +0100 Subject: [PATCH 04/14] Fix Unit Tests (cherry picked from commit 376ca1e7e52a7bd56c48a546450b222f56c69a32) (cherry picked from commit 159dbd4877881ef513eafa01b8b644f7f6787f4b) --- src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs | 5 +++-- test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs b/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs index b5506c189..8244bb6d4 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs @@ -6,9 +6,10 @@ internal static class TarStringExtension { public static string ClearTarPath(this string s) { - if (Path.GetPathRoot(s) != null) + var pathRoot = Path.GetPathRoot(s); + if (!string.IsNullOrEmpty(pathRoot)) { - s = s.Substring(Path.GetPathRoot(s).Length); + s = s.Substring(pathRoot.Length); } return s.Replace(Path.DirectorySeparatorChar, '/'); } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs index 80f762bdd..da4427d02 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs @@ -944,7 +944,7 @@ public void rootPathIsRespected() var expectationDirectory = new DirectoryInfo(tempDirectory); foreach (var checkFile in expectationDirectory.GetFiles("", SearchOption.AllDirectories)) { - var relativePath = expectationDirectory.FullName.Substring(expectationDirectory.FullName.Length); + var relativePath = checkFile.FullName.Substring(expectationDirectory.FullName.Length + 1); FileAssert.Exists(Path.Combine(extractDirectory, relativePath)); FileAssert.AreEqual(checkFile.FullName, Path.Combine(extractDirectory, relativePath)); } @@ -954,7 +954,7 @@ private void CreateAndClearDirectory(string path) { if (Directory.Exists(path)) { - Directory.Delete(path); + Directory.Delete(path, true); } Directory.CreateDirectory(path); } From 0e0a45179e997b875e4463711f9f119e9165c436 Mon Sep 17 00:00:00 2001 From: sensslen Date: Sat, 22 May 2021 12:33:18 +0200 Subject: [PATCH 05/14] Update src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: nils måsén --- src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs b/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs index cec6fc322..0de350168 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarEntry.cs @@ -421,7 +421,7 @@ public void GetFileTarHeader(TarHeader header, string file) // No absolute pathnames // Windows (and Posix?) paths can start with UNC style "\\NetworkDrive\", // so we loop on starting /'s. - name = name.ClearTarPath(); + name = name.ToTarArchivePath(); header.LinkName = String.Empty; header.Name = name; From 8bcddfe1e3d1c3c49ab5ed3f6071080307b88dc4 Mon Sep 17 00:00:00 2001 From: sensslen Date: Sat, 22 May 2021 12:33:47 +0200 Subject: [PATCH 06/14] Update test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: nils måsén --- .../Tar/TarTests.cs | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs index da4427d02..5a6c67f2c 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs @@ -950,26 +950,5 @@ public void rootPathIsRespected() } } - private void CreateAndClearDirectory(string path) - { - if (Directory.Exists(path)) - { - Directory.Delete(path, true); - } - Directory.CreateDirectory(path); - } - - public class ChaosStream : MemoryStream - { - private readonly int length = new Random().Next() % 500000 + 200; - - // Create constructors as needed to match desired MemoryStream construction - - public override int Read(byte[] buffer, int offset, int count) - { - int readCount = Math.Max(0, Math.Min(length - offset, count)); - return base.Read(buffer, offset, readCount); - } - } } } From 32b4c8a2bb4531f83b649766f72d01477855f09a Mon Sep 17 00:00:00 2001 From: sensslen Date: Sat, 22 May 2021 12:34:02 +0200 Subject: [PATCH 07/14] Update test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: nils måsén --- test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs index 5a6c67f2c..b455e7a8f 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs @@ -931,8 +931,7 @@ public void rootPathIsRespected() } } - var extractDirectory = Path.Combine(Path.GetTempPath(), "sharpziplib_tar_extract_folder"); - CreateAndClearDirectory(extractDirectory); + using var extractDirectory = new Utils.TempDir(); using (var file = File.OpenRead(tarFileName)) { using (var archive = TarArchive.CreateInputTarArchive(file, Encoding.UTF8)) From 947a71292ad0ceccf97c1be1a3c2cff329dadf95 Mon Sep 17 00:00:00 2001 From: sensslen Date: Sat, 22 May 2021 12:34:14 +0200 Subject: [PATCH 08/14] Update test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: nils måsén --- test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs index b455e7a8f..45b02e22a 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs @@ -908,18 +908,10 @@ public void StreamWithJapaneseName(int length, string encodingName) [Category("Tar")] public void rootPathIsRespected() { - // create dummy folder structure - var tempDirectory = Path.Combine(Path.GetTempPath(), "sharpziplib_tar_test_folder"); - CreateAndClearDirectory(tempDirectory); - using (var dummyfile = File.Create(Path.Combine(tempDirectory, "dummyfile"))) - { - using (var randomStream = new ChaosStream()) - { - randomStream.CopyTo(dummyfile); - } - } + using var tempDirectory = new Utils.TempDir(); + tempDirectory.CreateDummyFile(); - var tarFileName = Path.Combine(Path.GetTempPath(), "sharpziplib_tar_test_folder_archive.tar"); + using var tarFileName = new Utils.TempFile(); using (var tarFile = File.Open(tarFileName, FileMode.Create)) { From 5ffbddc641c47c22b631004bdc819025ddb54471 Mon Sep 17 00:00:00 2001 From: sensslen Date: Sat, 22 May 2021 12:34:25 +0200 Subject: [PATCH 09/14] Update src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: nils måsén --- src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs b/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs index 8244bb6d4..f55540b35 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs @@ -6,12 +6,7 @@ internal static class TarStringExtension { public static string ClearTarPath(this string s) { - var pathRoot = Path.GetPathRoot(s); - if (!string.IsNullOrEmpty(pathRoot)) - { - s = s.Substring(pathRoot.Length); - } - return s.Replace(Path.DirectorySeparatorChar, '/'); + return PathUtils.DropPathRoot(s).Replace(Path.DirectorySeparatorChar, '/'); } } } From 17e951f75e7229893a950a369994091dfd90b7ff Mon Sep 17 00:00:00 2001 From: sensslen Date: Sat, 22 May 2021 12:34:33 +0200 Subject: [PATCH 10/14] Update src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: nils måsén --- src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs b/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs index f55540b35..e654726c6 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs @@ -4,7 +4,7 @@ namespace ICSharpCode.SharpZipLib.Tar { internal static class TarStringExtension { - public static string ClearTarPath(this string s) + public static string ToTarArchivePath(this string s) { return PathUtils.DropPathRoot(s).Replace(Path.DirectorySeparatorChar, '/'); } From 77191140033ac60e5879cb010464a428cc5f6997 Mon Sep 17 00:00:00 2001 From: sensslen Date: Sat, 22 May 2021 12:34:48 +0200 Subject: [PATCH 11/14] Update src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: nils måsén --- src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs b/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs index e654726c6..433c6a424 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarStringExtension.cs @@ -1,4 +1,5 @@ using System.IO; +using ICSharpCode.SharpZipLib.Core; namespace ICSharpCode.SharpZipLib.Tar { From faa7725671c85b2ac7ceb236fffa2b352273b71b Mon Sep 17 00:00:00 2001 From: sensslen Date: Sat, 22 May 2021 12:35:40 +0200 Subject: [PATCH 12/14] Update src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: nils måsén --- src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs b/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs index 91b513427..e512b82e1 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs @@ -356,7 +356,7 @@ public string RootPath { throw new ObjectDisposedException("TarArchive"); } - rootPath = value.ClearTarPath().TrimEnd('/'); + rootPath = value.ToTarArchivePath().TrimEnd('/'); } } From f2371ce1d769a46a1a1587afb595dfadcf392d33 Mon Sep 17 00:00:00 2001 From: Simon Ensslen Date: Sat, 22 May 2021 12:55:36 +0200 Subject: [PATCH 13/14] Fix test --- .../Tar/TarTests.cs | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs index 45b02e22a..370eb805f 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs @@ -906,40 +906,40 @@ public void StreamWithJapaneseName(int length, string encodingName) /// [Test] [Category("Tar")] - public void rootPathIsRespected() + public void RootPathIsRespected() { - using var tempDirectory = new Utils.TempDir(); - tempDirectory.CreateDummyFile(); - - using var tarFileName = new Utils.TempFile(); - - using (var tarFile = File.Open(tarFileName, FileMode.Create)) + using (var extractDirectory = new Utils.TempDir()) + using (var tarFileName = new Utils.TempFile()) + using (var tempDirectory = new Utils.TempDir()) { - using (var tarOutputStream = TarArchive.CreateOutputTarArchive(tarFile)) + tempDirectory.CreateDummyFile(); + + using (var tarFile = File.Open(tarFileName.Filename, FileMode.Create)) { - tarOutputStream.RootPath = tempDirectory; - var entry = TarEntry.CreateEntryFromFile(tempDirectory); - tarOutputStream.WriteEntry(entry, true); + using (var tarOutputStream = TarArchive.CreateOutputTarArchive(tarFile)) + { + tarOutputStream.RootPath = tempDirectory.Fullpath; + var entry = TarEntry.CreateEntryFromFile(tempDirectory.Fullpath); + tarOutputStream.WriteEntry(entry, true); + } } - } - using var extractDirectory = new Utils.TempDir(); - using (var file = File.OpenRead(tarFileName)) - { - using (var archive = TarArchive.CreateInputTarArchive(file, Encoding.UTF8)) + using (var file = File.OpenRead(tarFileName.Filename)) { - archive.ExtractContents(extractDirectory); + using (var archive = TarArchive.CreateInputTarArchive(file, Encoding.UTF8)) + { + archive.ExtractContents(extractDirectory.Fullpath); + } } - } - var expectationDirectory = new DirectoryInfo(tempDirectory); - foreach (var checkFile in expectationDirectory.GetFiles("", SearchOption.AllDirectories)) - { - var relativePath = checkFile.FullName.Substring(expectationDirectory.FullName.Length + 1); - FileAssert.Exists(Path.Combine(extractDirectory, relativePath)); - FileAssert.AreEqual(checkFile.FullName, Path.Combine(extractDirectory, relativePath)); + var expectationDirectory = new DirectoryInfo(tempDirectory.Fullpath); + foreach (var checkFile in expectationDirectory.GetFiles("", SearchOption.AllDirectories)) + { + var relativePath = checkFile.FullName.Substring(expectationDirectory.FullName.Length + 1); + FileAssert.Exists(Path.Combine(extractDirectory.Fullpath, relativePath)); + FileAssert.AreEqual(checkFile.FullName, Path.Combine(extractDirectory.Fullpath, relativePath)); + } } } - } } From f1b175a5af0dfac455429747e1f3fdf5ca3ec484 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Tue, 16 Aug 2022 10:40:48 +0200 Subject: [PATCH 14/14] fix(tar): except root dir from traversal logic also updates the references to test util helpers --- src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs | 4 +++- .../Tar/TarTests.cs | 22 +++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs b/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs index fa03b264a..878649017 100644 --- a/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs +++ b/src/ICSharpCode.SharpZipLib/Tar/TarArchive.cs @@ -659,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"); } diff --git a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs index 397f6e957..c6a35ff08 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Tar/TarTests.cs @@ -883,36 +883,36 @@ public async Task StreamWithJapaneseNameAsync(int length, string encodingName) [Category("Tar")] public void RootPathIsRespected() { - using (var extractDirectory = new Utils.TempDir()) - using (var tarFileName = new Utils.TempFile()) - using (var tempDirectory = new Utils.TempDir()) + using (var extractDirectory = new TempDir()) + using (var tarFileName = new TempFile()) + using (var tempDirectory = new TempDir()) { tempDirectory.CreateDummyFile(); - using (var tarFile = File.Open(tarFileName.Filename, FileMode.Create)) + using (var tarFile = File.Open(tarFileName.FullName, FileMode.Create)) { using (var tarOutputStream = TarArchive.CreateOutputTarArchive(tarFile)) { - tarOutputStream.RootPath = tempDirectory.Fullpath; - var entry = TarEntry.CreateEntryFromFile(tempDirectory.Fullpath); + tarOutputStream.RootPath = tempDirectory.FullName; + var entry = TarEntry.CreateEntryFromFile(tempDirectory.FullName); tarOutputStream.WriteEntry(entry, true); } } - using (var file = File.OpenRead(tarFileName.Filename)) + using (var file = File.OpenRead(tarFileName.FullName)) { using (var archive = TarArchive.CreateInputTarArchive(file, Encoding.UTF8)) { - archive.ExtractContents(extractDirectory.Fullpath); + archive.ExtractContents(extractDirectory.FullName); } } - var expectationDirectory = new DirectoryInfo(tempDirectory.Fullpath); + 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.Fullpath, relativePath)); - FileAssert.AreEqual(checkFile.FullName, Path.Combine(extractDirectory.Fullpath, relativePath)); + FileAssert.Exists(Path.Combine(extractDirectory.FullName, relativePath)); + FileAssert.AreEqual(checkFile.FullName, Path.Combine(extractDirectory.FullName, relativePath)); } } }