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 relative symlink support in TarFile #77338

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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));
am11 marked this conversation as resolved.
Show resolved Hide resolved

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.
Show resolved Hide resolved
}

return (fileDestinationPath, linkTargetPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Show resolved Hide resolved
<EnableLibraryImportGenerator>true</EnableLibraryImportGenerator>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
</PropertyGroup>
Expand All @@ -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" />
Expand Down
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.
Show resolved Hide resolved
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 = Record.Exception(() => TarFile.ExtractToDirectory(archiveStream, destinationDirectoryName, overwriteFiles: true));
am11 marked this conversation as resolved.
Show resolved Hide resolved

Assert.Equal(SR.Format(SR.TarExtractingResultsLinkOutside, symlinkTargetPath, destinationDirectoryName), exception.Message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Show resolved Hide resolved
{
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)]
Expand Down