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 (10.0)#130344
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Fix failing symlink tests (10.0) #130344
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6b458c2
fix tests
alinpahontu2912 52c7257
Guard destination-root path in FilePathEscapesDirectory and re-enable…
alinpahontu2912 8582be2
apply copilot comment
alinpahontu2912 f0fd529
Merge branch 'release/10.0' into tar_fix_10
alinpahontu2912 0b79631
fix windows junction failure
alinpahontu2912 fb5711c
Potential fix for pull request finding
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
55 changes: 32 additions & 23 deletions
55 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 |
|---|---|---|
| @@ -398,7 +398,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. | ||
| private static bool FilePathEscapesDirectory(string destinationDirectoryPath, string fileDestinationPath) | ||
| { | ||
| // Windows is case insensitive while Linux is case sensitive | ||
| @@ -408,16 +410,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); | ||
| @@ -427,16 +447,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) && | ||
| @@ -449,15 +460,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. alinpahontu2912 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| @@ -481,12 +495,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); | ||
| } | ||
| } | ||
21 changes: 11 additions & 10 deletions
21 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.