Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.6k
Fix relative symlink support in TarFile#77338
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -349,11 +349,17 @@ internal Task ExtractRelativeToDirectoryAsync(string destinationDirectoryPath, b | ||
| throw new InvalidDataException(SR.TarEntryHardLinkOrSymlinkLinkNameEmpty); | ||
| } | ||
| linkTargetPath = GetSanitizedFullPath(destinationDirectoryPath, LinkName); | ||
| linkTargetPath = GetSanitizedFullPath(destinationDirectoryPath, | ||
| Path.IsPathFullyQualified(LinkName) ? LinkName : Path.Join(Path.GetDirectoryName(fileDestinationPath), LinkName)); | ||
| if (linkTargetPath == null) | ||
| { | ||
| throw new IOException(SR.Format(SR.TarExtractingResultsLinkOutside, LinkName, destinationDirectoryPath)); | ||
| } | ||
| // after TarExtractingResultsLinkOutside validation, preserve the original | ||
| // symlink target path (to match behavior of other utilities). | ||
| linkTargetPath = LinkName; | ||
am11 marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| return (fileDestinationPath, linkTargetPath); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,7 +2,7 @@ | ||
| <PropertyGroup> | ||
| <TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-Unix</TargetFrameworks> | ||
| <AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
| <StringResourcesPath>$(LibrariesProjectRoot)/Common/tests/Resources/Strings.resx</StringResourcesPath> | ||
| <StringResourcesPath>$(MSBuildProjectDirectory)\..\src\Resources\Strings.resx</StringResourcesPath> | ||
am11 marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| <EnableLibraryImportGenerator>true</EnableLibraryImportGenerator> | ||
| <IncludeRemoteExecutor>true</IncludeRemoteExecutor> | ||
| </PropertyGroup> | ||
| @@ -17,6 +17,7 @@ | ||
| <Compile Include="TarFile\TarFile.ExtractToDirectoryAsync.File.Tests.cs" /> | ||
| <Compile Include="TarFile\TarFile.CreateFromDirectoryAsync.Stream.Tests.cs" /> | ||
| <Compile Include="TarFile\TarFile.CreateFromDirectoryAsync.File.Tests.cs" /> | ||
| <Compile Include="TarFile\TarFile.CreateFromDirectory.File.Roundtrip.cs" /> | ||
| <Compile Include="TarEntry\TarEntry.Conversion.Tests.Base.cs" /> | ||
| <Compile Include="TarEntry\GnuTarEntry.Conversion.Tests.cs" /> | ||
| <Compile Include="TarEntry\PaxTarEntry.Conversion.Tests.cs" /> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| using System.IO; | ||
| using Xunit; | ||
| namespace System.Formats.Tar.Tests | ||
| { | ||
| public class TarFile_CreateFromDirectory_Roundtrip_Tests : TarTestsBase | ||
| { | ||
| [ConditionalTheory(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] | ||
| [InlineData("./file.txt", "subDirectory")] | ||
| [InlineData("../file.txt", "subDirectory")] | ||
| [InlineData("../file.txt", "subDirectory1/subDirectory1.1")] | ||
| [InlineData("./file.txt", "subDirectory1/subDirectory1.1")] | ||
| [InlineData("./file.txt", null)] | ||
| public void SymlinkRelativeTargets_InsideTheArchive_RoundtripsSuccessfully(string symlinkTargetPath, string subDirectory) | ||
| { | ||
| using TempDirectory root = new TempDirectory(); | ||
| string destinationArchive = Path.Join(root.Path, "destination.tar"); | ||
| string sourceDirectoryName = Path.Join(root.Path, "baseDirectory"); | ||
| Directory.CreateDirectory(sourceDirectoryName); | ||
| string destinationDirectoryName = Path.Join(root.Path, "destinationDirectory"); | ||
| Directory.CreateDirectory(destinationDirectoryName); | ||
| string sourceSubDirectory = Path.Join(sourceDirectoryName, subDirectory); | ||
| if(subDirectory != null) Directory.CreateDirectory(sourceSubDirectory); | ||
| File.Create(Path.Join(sourceDirectoryName, subDirectory, symlinkTargetPath)).Dispose(); | ||
| File.CreateSymbolicLink(Path.Join(sourceSubDirectory, "linkToFile"), symlinkTargetPath); | ||
| TarFile.CreateFromDirectory(sourceDirectoryName, destinationArchive, includeBaseDirectory: false); | ||
| using FileStream archiveStream = File.OpenRead(destinationArchive); | ||
| TarFile.ExtractToDirectory(archiveStream, destinationDirectoryName, overwriteFiles: true); | ||
| string destinationSubDirectory = Path.Join(destinationDirectoryName, subDirectory); | ||
| string symlinkPath = Path.Join(destinationSubDirectory, "linkToFile"); | ||
| Assert.True(File.Exists(symlinkPath)); | ||
| FileInfo? fileInfo = new(symlinkPath); | ||
| Assert.Equal(symlinkTargetPath, fileInfo.LinkTarget); | ||
| FileSystemInfo? symlinkTarget = File.ResolveLinkTarget(symlinkPath, returnFinalTarget: true); | ||
| Assert.True(File.Exists(symlinkTarget.FullName)); | ||
| } | ||
| [ConditionalTheory(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] | ||
| [InlineData("../file.txt", null)] | ||
| [InlineData("../../file.txt", "subDirectory")] | ||
am11 marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| public void SymlinkRelativeTargets_OutsideTheArchive_Fails(string symlinkTargetPath, string subDirectory) | ||
| { | ||
| using TempDirectory root = new TempDirectory(); | ||
| string destinationArchive = Path.Join(root.Path, "destination.tar"); | ||
| string sourceDirectoryName = Path.Join(root.Path, "baseDirectory"); | ||
| Directory.CreateDirectory(sourceDirectoryName); | ||
| string destinationDirectoryName = Path.Join(root.Path, "destinationDirectory"); | ||
| Directory.CreateDirectory(destinationDirectoryName); | ||
| string sourceSubDirectory = Path.Join(sourceDirectoryName, subDirectory); | ||
| if(subDirectory != null) Directory.CreateDirectory(sourceSubDirectory); | ||
| File.CreateSymbolicLink(Path.Join(sourceSubDirectory, "linkToFile"), symlinkTargetPath); | ||
| TarFile.CreateFromDirectory(sourceDirectoryName, destinationArchive, includeBaseDirectory: false); | ||
| using FileStream archiveStream = File.OpenRead(destinationArchive); | ||
| Exception exception = Assert.Throws<IOException>(() => TarFile.ExtractToDirectory(archiveStream, destinationDirectoryName, overwriteFiles: true)); | ||
| Assert.Equal(SR.Format(SR.TarExtractingResultsLinkOutside, symlinkTargetPath, destinationDirectoryName), exception.Message); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -110,6 +110,29 @@ public async Task ExtractEntry_DockerImageTarWithFileTypeInDirectoriesInMode_Suc | ||
| } | ||
| } | ||
| [ConditionalFact(typeof(MountHelper), nameof(MountHelper.CanCreateSymbolicLinks))] | ||
| public async Task ExtractEntry_PodmanImageTarWithRelativeSymlinksPointingInExtractDirectory_SuccessfullyExtracts_Async() | ||
am11 marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| { | ||
| using (TempDirectory root = new TempDirectory()) | ||
| { | ||
| await using MemoryStream archiveStream = GetTarMemoryStream(CompressionMethod.Uncompressed, "misc", "podman-hello-world"); | ||
| await TarFile.ExtractToDirectoryAsync(archiveStream, root.Path, overwriteFiles: true); | ||
| Assert.True(File.Exists(Path.Join(root.Path, "manifest.json"))); | ||
| Assert.True(File.Exists(Path.Join(root.Path, "repositories"))); | ||
| Assert.True(File.Exists(Path.Join(root.Path, "efb53921da3394806160641b72a2cbd34ca1a9a8345ac670a85a04ad3d0e3507.tar"))); | ||
| string symlinkPath = Path.Join(root.Path, "e7fc2b397c1ab5af9938f18cc9a80d526cccd1910e4678390157d8cc6c94410d/layer.tar"); | ||
| Assert.True(File.Exists(symlinkPath)); | ||
| FileInfo? fileInfo = new(symlinkPath); | ||
| Assert.Equal("../efb53921da3394806160641b72a2cbd34ca1a9a8345ac670a85a04ad3d0e3507.tar", fileInfo.LinkTarget); | ||
| FileSystemInfo? symlinkTarget = File.ResolveLinkTarget(symlinkPath, returnFinalTarget: true); | ||
| Assert.True(File.Exists(symlinkTarget.FullName)); | ||
| } | ||
| } | ||
| [Theory] | ||
| [InlineData(TarEntryType.SymbolicLink)] | ||
| [InlineData(TarEntryType.HardLink)] | ||
Uh oh!
There was an error while loading. Please reload this page.