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 failing symlink tests#130342
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
alinpahontu2912
merged 4 commits into
dotnet:release/8.0-staging
from
alinpahontu2912:tar_fix_8Jul 15, 2026
Uh oh!
There was an error while loading. Please reload this page.
Merged
Fix failing symlink tests #130342
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b7e8c7b
fix tests
alinpahontu2912 afd4329
Guard destination-root path in FilePathEscapesDirectory and re-enable…
alinpahontu2912 ac6974c
Potential fix for pull request finding
alinpahontu2912 198927c
Merge branch 'release/8.0-staging' into tar_fix_8
alinpahontu2912 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
62 changes: 35 additions & 27 deletions
62 src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarEntry.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 |
|---|---|---|
| @@ -336,7 +336,6 @@ internal Task ExtractRelativeToDirectoryAsync(string destinationDirectoryPath, b | ||
| string? fileDestinationPath = GetFullDestinationPath( | ||
| destinationDirectoryPath, | ||
| Path.IsPathFullyQualified(name) ? name : Path.Join(destinationDirectoryPath, name)); | ||
| if (fileDestinationPath is null || FilePathEscapesDirectory(destinationDirectoryPath, fileDestinationPath)) | ||
| { | ||
| throw new IOException(SR.Format(SR.TarExtractingResultsFileOutside, name, destinationDirectoryPath)); | ||
| @@ -348,9 +347,9 @@ internal Task ExtractRelativeToDirectoryAsync(string destinationDirectoryPath, b | ||
| // LinkName is an absolute path, or path relative to the fileDestinationPath directory. | ||
| // We don't check if the LinkName is empty. In that case, creation of the link will fail because link targets can't be empty. | ||
| string linkName = ArchivingUtils.SanitizeEntryFilePath(LinkName, preserveDriveRoot: true); | ||
| // On Windows, reject rootedbutnotfullyqualified paths ("\Windows\win.ini", "C:foo"). | ||
| // They resolve ambiguously based on the current drive or working directory. | ||
| // Symlinks are resolved at access time, so Path.GetFullPath cannot reliably determine which drive will be used. | ||
| // On Windows, reject rooted-but-not-fully-qualified symlink targets (e.g., "\Windows\win.ini"). | ||
| // Unlike files, symlink targets are resolved at access time, not extraction time, | ||
| // so Path.GetFullPath here cannot reliably predict what drive the OS will resolve them against. | ||
| if (OperatingSystem.IsWindows() && Path.IsPathRooted(linkName) && !Path.IsPathFullyQualified(linkName)) | ||
| { | ||
| throw new IOException(SR.Format(SR.TarExtractingResultsLinkOutside, linkName, destinationDirectoryPath)); | ||
| @@ -384,7 +383,9 @@ internal Task ExtractRelativeToDirectoryAsync(string destinationDirectoryPath, b | ||
| return (fileDestinationPath, linkTargetPath); | ||
| } | ||
| // Check if the file destination path or the link target path escapes the destination directory, by walking through the relative path components and resolving symlinks at each step. | ||
| // Prevent an archive from escaping the extraction root through symlinks that were created by earlier entries in the same archive. | ||
| // This protection applies only to links introduced by the archive itself. It is not intended to defend against preexisting symlinks | ||
| // already present on disk before extraction. | ||
alinpahontu2912 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| private static bool FilePathEscapesDirectory(string destinationDirectoryPath, string fileDestinationPath) | ||
| { | ||
| // Windows is case insensitive while Linux is case sensitive | ||
| @@ -394,16 +395,34 @@ private static bool FilePathEscapesDirectory(string destinationDirectoryPath, st | ||
| : StringComparison.Ordinal; | ||
| string resolvedDest = ResolvePhysicalPath(destinationDirectoryPath); | ||
| // Use the logical destination path for computing the relative path | ||
| string logicalDest = Path.GetFullPath(destinationDirectoryPath); | ||
| string logicalPrefix = logicalDest.EndsWith(Path.DirectorySeparatorChar) | ||
| ? logicalDest | ||
| : logicalDest + Path.DirectorySeparatorChar; | ||
| string destPrefix = resolvedDest.EndsWith(Path.DirectorySeparatorChar) | ||
| ? resolvedDest | ||
| : resolvedDest + Path.DirectorySeparatorChar; | ||
| // Normalize file path (resolves .. and . but not symlinks) | ||
| string normalizedFile = Path.GetFullPath(fileDestinationPath); | ||
| // Walk relative components, resolving symlinks at each step | ||
| string relative = normalizedFile.Substring(resolvedDest.Length) | ||
| .TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); | ||
| // Guard with StartsWith before computing relative path | ||
| if (!normalizedFile.StartsWith(logicalPrefix, pathComparison) && | ||
| !normalizedFile.Equals(logicalDest, pathComparison)) | ||
| { | ||
| return true; | ||
| } | ||
| // Walk relative components, resolving symlinks at each step. | ||
| // When the file resolves to the destination directory itself, there are no components to walk, | ||
| // so the relative path is empty. Guarding here avoids an out-of-range Substring on the prefix length. | ||
| string relative = normalizedFile.Equals(logicalDest, pathComparison) | ||
| ? string.Empty | ||
| : normalizedFile.Substring(logicalPrefix.Length) | ||
| .TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); | ||
| string[] components = relative.Split(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, | ||
| StringSplitOptions.RemoveEmptyEntries); | ||
| @@ -413,16 +432,7 @@ private static bool FilePathEscapesDirectory(string destinationDirectoryPath, st | ||
| foreach (string component in components) | ||
| { | ||
| current = Path.Combine(current, component); | ||
| if (Path.Exists(current)) | ||
| { | ||
| string? resolved = ResolveSymlink(current); | ||
| if (resolved is null) | ||
| { | ||
| return true; | ||
| } | ||
| current = resolved; | ||
| } | ||
| current = ResolveSymlink(current); | ||
| string normalizedCurrent = Path.GetFullPath(current); | ||
| if (!normalizedCurrent.StartsWith(destPrefix, pathComparison) && | ||
| @@ -435,15 +445,18 @@ private static bool FilePathEscapesDirectory(string destinationDirectoryPath, st | ||
| return false; | ||
| } | ||
| private static string? ResolveSymlink(string path) | ||
| private static string ResolveSymlink(string path) | ||
| { | ||
| FileSystemInfo? target = new FileInfo(path).ResolveLinkTarget(returnFinalTarget: true); | ||
| var info = new FileInfo(path); | ||
| if (target is null) | ||
| // Check LinkTarget first so dangling symlinks/junctions (whose final target doesn't exist yet) | ||
| // are still resolved to their raw target, rather than being treated as a non-link. | ||
| if (info.LinkTarget is null) | ||
| { | ||
| return Path.GetFullPath(path); | ||
| } | ||
| FileSystemInfo target = info.ResolveLinkTarget(returnFinalTarget: true) ?? info; | ||
| return target.FullName; | ||
| } | ||
alinpahontu2912 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| @@ -467,12 +480,7 @@ private static string ResolvePhysicalPath(string path) | ||
| current = Path.Combine(current, component); | ||
| if (Path.Exists(current)) | ||
| { | ||
| string? resolved = ResolveSymlink(current); | ||
| if (resolved is null) | ||
| { | ||
| return current; | ||
| } | ||
| current = resolved; | ||
| current = ResolveSymlink(current); | ||
| } | ||
| } | ||
8 changes: 0 additions & 8 deletions
8 src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.File.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
7 changes: 0 additions & 7 deletions
7 src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectory.Stream.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
6 changes: 0 additions & 6 deletions
6 src/libraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectoryAsync.File.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
7 changes: 0 additions & 7 deletions
7 ...ibraries/System.Formats.Tar/tests/TarFile/TarFile.ExtractToDirectoryAsync.Stream.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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.