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 wildcard globbing in root of device paths #19442

Merged
merged 6 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ internal static bool PathIsUnc(string path, bool networkOnly = false)
}

// handle special cases like '\\wsl$\ubuntu', '\\?\', and '\\.\pipe\' which aren't a UNC path, but we can say it is so the filesystemprovider can use it
if (!networkOnly && (path.StartsWith(WslRootPath, StringComparison.OrdinalIgnoreCase) || path.StartsWith("\\\\?\\") || path.StartsWith("\\\\.\\")))
if (!networkOnly && (path.StartsWith(WslRootPath, StringComparison.OrdinalIgnoreCase) || PathIsDevicePath(path)))
{
return true;
}
Expand All @@ -1251,6 +1251,15 @@ internal static bool PathIsUnc(string path, bool networkOnly = false)
#endif
}

internal static bool PathIsDevicePath(string path)
{
#if UNIX
return false;
#else
return path.StartsWith(@"\\.\") || path.StartsWith(@"\\?\");
#endif
}

internal static readonly string PowerShellAssemblyStrongNameFormat =
"{0}, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4913,7 +4913,17 @@ protected override string GetParentPath(string path, string root)
// make sure we return two backslashes so it still results in a UNC path
parentPath = "\\\\";
}

MartinGC94 marked this conversation as resolved.
Show resolved Hide resolved
if (!parentPath.EndsWith(StringLiterals.DefaultPathSeparator)
&& Utils.PathIsDevicePath(parentPath)
&& parentPath.Length - parentPath.Replace(StringLiterals.DefaultPathSeparatorString, string.Empty).Length == 3)
{
// Device paths start with either "\\.\" or "\\?\"
// When referring to the root, like: "\\.\CDROM0\" then it needs the trailing separator to be valid.
parentPath += StringLiterals.DefaultPathSeparator;
}
#endif
s_tracer.WriteLine("GetParentPath returning '{0}'", parentPath);
return parentPath;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,43 @@ Describe "Handling of globbing patterns" -Tags "CI" {
Test-Path -LiteralPath $testPath2 | Should -BeTrue
}
}

Context "Device paths" {
# The globber is overly greedy somewhere so you need to escape the escape backtick to preserve the question mark issue https://github.com/PowerShell/PowerShell/issues/19627
It "Handle device paths: <path>" -Skip:(!$IsWindows) -TestCases @(
@{ path = "\\.\${env:SystemDrive}\" }
@{ path = "\\.\${env:SystemDrive}\*" }
@{ path = "\\``?\${env:SystemDrive}\" }
@{ path = "\\``?\${env:SystemDrive}\*" }
) {
param($path)
$expected = Get-ChildItem -Path ${env:SystemDrive}\
$result = Get-ChildItem -Path $path
$result.Count | Should -Be $expected.Count
}

It "Handle folders within a device path: <path>" -Skip:(!$IsWindows) -TestCases @(
@{ path = "\\.\${env:SystemRoot}\" }
@{ path = "\\.\${env:SystemRoot}\*" }
@{ path = "\\``?\${env:SystemRoot}\" }
@{ path = "\\``?\${env:SystemRoot}\*" }
) {
param($path)
$expected = Get-ChildItem -Path ${env:SystemRoot}
$result = Get-ChildItem -Path $path
$result.Count | Should -Be $expected.Count
}

It "Fails for invalid device path: <path>" -Skip:(!$IsWindows) -TestCases @(
@{ path = "\\.\INVALID0\" }
@{ path = "\\``?\INVALID0\" }
# @{ path = "\\.\INVALID0\*" } // problem in globber where this fails but is ignored issue https://github.com/PowerShell/PowerShell/issues/19626
# @{ path = "\\``?\INVALID0\*" }
) {
param($path)
{ Get-ChildItem -Path $path -ErrorAction Stop } | Should -Throw -ErrorId 'PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand'
}
}
}

Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows" {
Expand Down