-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug in Tar preventing extraction of hardlinks or entries starting…
… with `.\` (#70853) * Add PlatformDetection.SupportsHardLinkCreation property. * Fix how paths are combined/joined and sanitized on extraction, to ensure paths with redundant segments get properly handled. * Add tests that verify archives with entries whose paths start with .\, including the root folder itself. * Re-enable the hardlink test, condition it to not run if platform does not support extraction of hardlinks. * Remove unnecessary test - This same code is already being tested by TarReader_ExtractToFile_Tests.ExtractEntriesWithSlashDotPrefix * Reuse test code that retrieves memory stream. * Bump test data package version * Add missing typeof(PlatformDetection) in ConditionalFact Co-authored-by: carlossanlop <[email protected]>
- Loading branch information
1 parent
2627ea3
commit 051b482
Showing
10 changed files
with
126 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/libraries/System.Formats.Tar/tests/TarReader/TarReader.ExtractToFile.Tests.Unix.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Xunit; | ||
|
||
namespace System.Formats.Tar.Tests | ||
{ | ||
public partial class TarReader_ExtractToFile_Tests : TarTestsBase | ||
{ | ||
[Fact] | ||
public void ExtractToFile_SpecialFile_Unelevated_Throws() | ||
{ | ||
using TempDirectory root = new TempDirectory(); | ||
using MemoryStream ms = GetTarMemoryStream(CompressionMethod.Uncompressed, TestTarFormat.ustar, "specialfiles"); | ||
|
||
using TarReader reader = new TarReader(ms); | ||
|
||
string path = Path.Join(root.Path, "output"); | ||
|
||
// Block device requires elevation for writing | ||
PosixTarEntry blockDevice = reader.GetNextEntry() as PosixTarEntry; | ||
Assert.NotNull(blockDevice); | ||
Assert.Throws<UnauthorizedAccessException>(() => blockDevice.ExtractToFile(path, overwrite: false)); | ||
Assert.False(File.Exists(path)); | ||
|
||
// Character device requires elevation for writing | ||
PosixTarEntry characterDevice = reader.GetNextEntry() as PosixTarEntry; | ||
Assert.NotNull(characterDevice); | ||
Assert.Throws<UnauthorizedAccessException>(() => characterDevice.ExtractToFile(path, overwrite: false)); | ||
Assert.False(File.Exists(path)); | ||
|
||
// Fifo does not require elevation, should succeed | ||
PosixTarEntry fifo = reader.GetNextEntry() as PosixTarEntry; | ||
Assert.NotNull(fifo); | ||
fifo.ExtractToFile(path, overwrite: false); | ||
Assert.True(File.Exists(path)); | ||
|
||
Assert.Null(reader.GetNextEntry()); | ||
} | ||
} | ||
} |
50 changes: 22 additions & 28 deletions
50
src/libraries/System.Formats.Tar/tests/TarReader/TarReader.ExtractToFile.Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,38 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace System.Formats.Tar.Tests | ||
{ | ||
public class TarReader_ExtractToFile_Tests : TarTestsBase | ||
public partial class TarReader_ExtractToFile_Tests : TarTestsBase | ||
{ | ||
[Fact] | ||
public void ExtractToFile_SpecialFile_Unelevated_Throws() | ||
public void ExtractEntriesWithSlashDotPrefix() | ||
{ | ||
using TempDirectory root = new TempDirectory(); | ||
using MemoryStream ms = GetTarMemoryStream(CompressionMethod.Uncompressed, TestTarFormat.ustar, "specialfiles"); | ||
|
||
using TarReader reader = new TarReader(ms); | ||
|
||
string path = Path.Join(root.Path, "output"); | ||
|
||
// Block device requires elevation for writing | ||
PosixTarEntry blockDevice = reader.GetNextEntry() as PosixTarEntry; | ||
Assert.NotNull(blockDevice); | ||
Assert.Throws<UnauthorizedAccessException>(() => blockDevice.ExtractToFile(path, overwrite: false)); | ||
Assert.False(File.Exists(path)); | ||
|
||
// Character device requires elevation for writing | ||
PosixTarEntry characterDevice = reader.GetNextEntry() as PosixTarEntry; | ||
Assert.NotNull(characterDevice); | ||
Assert.Throws<UnauthorizedAccessException>(() => characterDevice.ExtractToFile(path, overwrite: false)); | ||
Assert.False(File.Exists(path)); | ||
|
||
// Fifo does not require elevation, should succeed | ||
PosixTarEntry fifo = reader.GetNextEntry() as PosixTarEntry; | ||
Assert.NotNull(fifo); | ||
fifo.ExtractToFile(path, overwrite: false); | ||
Assert.True(File.Exists(path)); | ||
|
||
Assert.Null(reader.GetNextEntry()); | ||
using MemoryStream archiveStream = GetStrangeTarMemoryStream("prefixDotSlashAndCurrentFolderEntry"); | ||
using (TarReader reader = new TarReader(archiveStream, leaveOpen: false)) | ||
{ | ||
string rootPath = Path.TrimEndingDirectorySeparator(root.Path); | ||
TarEntry entry; | ||
while ((entry = reader.GetNextEntry()) != null) | ||
{ | ||
Assert.NotNull(entry); | ||
Assert.StartsWith("./", entry.Name); | ||
// Normalize the path (remove redundant segments), remove trailing separators | ||
// this is so the first entry can be skipped if it's the same as the root directory | ||
string entryPath = Path.TrimEndingDirectorySeparator(Path.GetFullPath(Path.Join(rootPath, entry.Name))); | ||
if (entryPath != rootPath) | ||
{ | ||
entry.ExtractToFile(entryPath, overwrite: true); | ||
Assert.True(Path.Exists(entryPath), $"Entry was not extracted: {entryPath}"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Destination directory must have a final path separator here, or you can still write outside the path.