-
Notifications
You must be signed in to change notification settings - Fork 5k
Fix bug in Tar preventing extraction of hardlinks or entries starting with .\
#70853
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f60243d
Add PlatformDetection.SupportsHardLinkCreation property.
carlossanlop ad5a951
Fix how paths are combined/joined and sanitized on extraction, to ens…
carlossanlop 1f0c436
Add tests that verify archives with entries whose paths start with .\…
carlossanlop f9d3986
Re-enable the hardlink test, condition it to not run if platform does…
carlossanlop 5454349
Remove unnecessary test - This same code is already being tested by T…
carlossanlop eb77add
Reuse test code that retrieves memory stream.
carlossanlop 52ea9bc
Bump test data package version
carlossanlop f60153d
Add missing typeof(PlatformDetection) in ConditionalFact
carlossanlop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you know you are unelevated here? Don't we run CI tests as
sudo
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's my understanding that we only run as sudo when we explicitly wrap the code with RemoteExecutor, and that has been the behavior I've seen in the few tests I've protected with RemoteExecutor.
Is that assumption incorrect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. I've seen other tests be conditional on whether the process is elevated or not. For example:
runtime/src/libraries/System.Net.Sockets/tests/FunctionalTests/CreateSocketTests.cs
Lines 43 to 44 in ab0de53
runtime/src/libraries/System.Net.Sockets/tests/FunctionalTests/CreateSocketTests.cs
Lines 98 to 121 in ab0de53
I'm not sure of all the ways our tests are run. So if you are expecting this test to only work in an unelevated process, it might be good to stick that in a condition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW this test already existed, I merely renamed the file and the test class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok - if it is already passing in CI, then we can just keep it.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool.
BTW the last argument of the
RemoteExecutor.Invoke
method is where I indicate that I want the code to be elevated:Anything not explicitly elevated, runs as a normal user. Or at least that's my understanding.