From abcfb59d2d7b226ee7798d8965c6e8ac87e6eeac Mon Sep 17 00:00:00 2001 From: Jacobe Kerr Date: Thu, 27 Aug 2020 10:53:59 -0600 Subject: [PATCH 01/11] Remove an unnecessary dbg.assert() --- src/System.Management.Automation/engine/CommandSearcher.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/System.Management.Automation/engine/CommandSearcher.cs b/src/System.Management.Automation/engine/CommandSearcher.cs index d3a1033c932..329e4fb7ddd 100644 --- a/src/System.Management.Automation/engine/CommandSearcher.cs +++ b/src/System.Management.Automation/engine/CommandSearcher.cs @@ -1334,10 +1334,6 @@ private bool ShouldSkipCommandResolutionForConstrainedLanguage(CommandInfo? resu /// internal LookupPathCollection ConstructSearchPatternsFromName(string name, bool commandDiscovery = false) { - Dbg.Assert( - !string.IsNullOrEmpty(name), - "Caller should verify name"); - var result = new LookupPathCollection(); // First check to see if the commandName has an extension, if so From ed7328b541d561751090d889407fc9872c3d9fbc Mon Sep 17 00:00:00 2001 From: Jacobe Kerr Date: Wed, 2 Sep 2020 12:50:54 -0600 Subject: [PATCH 02/11] Add IsNullOrEmpty check to relative path lookup. --- src/System.Management.Automation/engine/CommandSearcher.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/CommandSearcher.cs b/src/System.Management.Automation/engine/CommandSearcher.cs index 329e4fb7ddd..37f27bf4dc1 100644 --- a/src/System.Management.Automation/engine/CommandSearcher.cs +++ b/src/System.Management.Automation/engine/CommandSearcher.cs @@ -1099,7 +1099,7 @@ private bool ShouldSkipCommandResolutionForConstrainedLanguage(CommandInfo? resu // Relative Path: ".\command.exe" // Home Path: "~\command.exe" // Drive Relative Path: "\Users\User\AppData\Local\Temp\command.exe" - if (_commandName[0] == '.' || _commandName[0] == '~' || _commandName[0] == '\\') + if (!string.IsNullOrEmpty(_commandName) && (_commandName[0] == '.' || _commandName[0] == '~' || _commandName[0] == '\\')) { using (CommandDiscovery.discoveryTracer.TraceScope( "{0} appears to be a relative path. Trying to resolve relative path", From e451c532488a289243c0247ad38e0f4c8da6b6af Mon Sep 17 00:00:00 2001 From: Jacobe Kerr Date: Thu, 3 Sep 2020 22:14:54 -0600 Subject: [PATCH 03/11] Add test case for single whitespace name parameter --- .../Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 index 2dbc01c38db..7f47348563c 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 @@ -101,6 +101,10 @@ Describe "Get-Command Feature tests" -Tag Feature { It "No results if wildcard is used" { Get-Command i-psd* -UseAbbreviationExpansion | Should -BeNullOrEmpty } + + It "Returns CommandNotFoundException if a single space is used" { + {Get-Command ' ' -ErrorAction Stop} | Should -Throw -ErrorId "CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand" + } } } From 934e21472a39512fe393c4e8e99911c702aad684 Mon Sep 17 00:00:00 2001 From: Jacobe Kerr Date: Sun, 6 Sep 2020 13:12:47 -0600 Subject: [PATCH 04/11] Check for _commandName length in setupPathSearcher --- .../engine/CommandSearcher.cs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/engine/CommandSearcher.cs b/src/System.Management.Automation/engine/CommandSearcher.cs index 37f27bf4dc1..13facabccf2 100644 --- a/src/System.Management.Automation/engine/CommandSearcher.cs +++ b/src/System.Management.Automation/engine/CommandSearcher.cs @@ -1099,13 +1099,19 @@ private bool ShouldSkipCommandResolutionForConstrainedLanguage(CommandInfo? resu // Relative Path: ".\command.exe" // Home Path: "~\command.exe" // Drive Relative Path: "\Users\User\AppData\Local\Temp\command.exe" - if (!string.IsNullOrEmpty(_commandName) && (_commandName[0] == '.' || _commandName[0] == '~' || _commandName[0] == '\\')) + + //char firstChar = _commandName[0]; + if (_commandName.Length != 0) { - using (CommandDiscovery.discoveryTracer.TraceScope( - "{0} appears to be a relative path. Trying to resolve relative path", - _commandName)) + char firstChar = _commandName[0]; + if (firstChar == '.' || firstChar == '~' || firstChar == '\\') { - result = ResolvePSPath(_commandName); + using (CommandDiscovery.discoveryTracer.TraceScope( + "{0} appears to be a relative path. Trying to resolve relative path", + _commandName)) + { + result = ResolvePSPath(_commandName); + } } } } From 1d5c83cddd5ab250c3ba7e02eda1d7619a7905fc Mon Sep 17 00:00:00 2001 From: Jacobe Kerr Date: Sun, 6 Sep 2020 13:52:11 -0600 Subject: [PATCH 05/11] Added tests for whitespace, empty and null names --- .../Get-Command.Tests.ps1 | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 index 7f47348563c..d2e2b079baa 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 @@ -102,9 +102,6 @@ Describe "Get-Command Feature tests" -Tag Feature { Get-Command i-psd* -UseAbbreviationExpansion | Should -BeNullOrEmpty } - It "Returns CommandNotFoundException if a single space is used" { - {Get-Command ' ' -ErrorAction Stop} | Should -Throw -ErrorId "CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand" - } } } @@ -182,5 +179,15 @@ Describe "Get-Command" -Tag CI { $Result | Should -BeOfType [System.Management.Automation.CommandInfo] } + + It "Throws exception if name is used" -TestCases @( + @{ Name = ' '; expected = "CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand" } + @{ Name = ''; expected = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCommandCommand" } + @{ Name = $null; expected = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCommandCommand" } + ) { + param($name, $expected) + $shortExpected = $expected -split "," + { Get-Command $name -ErrorAction Stop } | Should -Throw -ErrorId $expected + } } } From 033e766d9066b888e17d07027d64a829430c4c24 Mon Sep 17 00:00:00 2001 From: Jacobe Kerr Date: Sun, 6 Sep 2020 13:58:15 -0600 Subject: [PATCH 06/11] Remove commented out code. --- src/System.Management.Automation/engine/CommandSearcher.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/System.Management.Automation/engine/CommandSearcher.cs b/src/System.Management.Automation/engine/CommandSearcher.cs index 13facabccf2..6d68b8f46e0 100644 --- a/src/System.Management.Automation/engine/CommandSearcher.cs +++ b/src/System.Management.Automation/engine/CommandSearcher.cs @@ -1100,7 +1100,6 @@ private bool ShouldSkipCommandResolutionForConstrainedLanguage(CommandInfo? resu // Home Path: "~\command.exe" // Drive Relative Path: "\Users\User\AppData\Local\Temp\command.exe" - //char firstChar = _commandName[0]; if (_commandName.Length != 0) { char firstChar = _commandName[0]; From 58f63e0b2cb20bbe1042ffa10671d2dfb5fc3549 Mon Sep 17 00:00:00 2001 From: Jacobe Kerr Date: Sun, 6 Sep 2020 21:21:56 -0600 Subject: [PATCH 07/11] Moved _commandName length conditional check. --- .../engine/CommandSearcher.cs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/System.Management.Automation/engine/CommandSearcher.cs b/src/System.Management.Automation/engine/CommandSearcher.cs index 6d68b8f46e0..845cba10138 100644 --- a/src/System.Management.Automation/engine/CommandSearcher.cs +++ b/src/System.Management.Automation/engine/CommandSearcher.cs @@ -1087,7 +1087,7 @@ private bool ShouldSkipCommandResolutionForConstrainedLanguage(CommandInfo? resu string? result = null; if (_context.EngineSessionState != null && - _context.EngineSessionState.ProviderCount > 0) + _context.EngineSessionState.ProviderCount > 0 && _commandName.Length != 0) { // NTRAID#Windows OS Bugs-1009294-2004/02/04-JeffJon // This is really slow. Maybe since we are only allowing FS paths right @@ -1100,17 +1100,14 @@ private bool ShouldSkipCommandResolutionForConstrainedLanguage(CommandInfo? resu // Home Path: "~\command.exe" // Drive Relative Path: "\Users\User\AppData\Local\Temp\command.exe" - if (_commandName.Length != 0) + char firstChar = _commandName[0]; + if (firstChar == '.' || firstChar == '~' || firstChar == '\\') { - char firstChar = _commandName[0]; - if (firstChar == '.' || firstChar == '~' || firstChar == '\\') + using (CommandDiscovery.discoveryTracer.TraceScope( + "{0} appears to be a relative path. Trying to resolve relative path", + _commandName)) { - using (CommandDiscovery.discoveryTracer.TraceScope( - "{0} appears to be a relative path. Trying to resolve relative path", - _commandName)) - { - result = ResolvePSPath(_commandName); - } + result = ResolvePSPath(_commandName); } } } From a8cbd6e99e2070851853a2d2757fc69d2e05e981 Mon Sep 17 00:00:00 2001 From: Jacobe Kerr Date: Mon, 7 Sep 2020 14:09:20 -0600 Subject: [PATCH 08/11] Remove unused variable. --- .../Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 index d2e2b079baa..4e47e2a031b 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 @@ -186,7 +186,6 @@ Describe "Get-Command" -Tag CI { @{ Name = $null; expected = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCommandCommand" } ) { param($name, $expected) - $shortExpected = $expected -split "," { Get-Command $name -ErrorAction Stop } | Should -Throw -ErrorId $expected } } From 2014b426e92b0b2ff834543e90d4449609929028 Mon Sep 17 00:00:00 2001 From: jackerr3 Date: Mon, 7 Sep 2020 21:32:44 -0600 Subject: [PATCH 09/11] Remove extra empty line. Co-authored-by: Ilya --- .../Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 index 4e47e2a031b..aea29f00216 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 @@ -101,7 +101,6 @@ Describe "Get-Command Feature tests" -Tag Feature { It "No results if wildcard is used" { Get-Command i-psd* -UseAbbreviationExpansion | Should -BeNullOrEmpty } - } } From d262767106ebce606942f2d821ec41c7d0463885 Mon Sep 17 00:00:00 2001 From: Jacobe Kerr Date: Mon, 7 Sep 2020 22:23:34 -0600 Subject: [PATCH 10/11] use Name variable for headers and value for tests. --- .../Get-Command.Tests.ps1 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 index 4e47e2a031b..0faaddb8e12 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 @@ -180,13 +180,13 @@ Describe "Get-Command" -Tag CI { $Result | Should -BeOfType [System.Management.Automation.CommandInfo] } - It "Throws exception if name is used" -TestCases @( - @{ Name = ' '; expected = "CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand" } - @{ Name = ''; expected = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCommandCommand" } - @{ Name = $null; expected = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCommandCommand" } + It "Throws '' exception if name is used" -TestCases @( + @{ Name = 'space'; Value = ' '; expected = "CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand" } + @{ Name = 'empty'; Value = ''; expected = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCommandCommand" } + @{ Name = 'null'; Value = $null; expected = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCommandCommand" } ) { - param($name, $expected) - { Get-Command $name -ErrorAction Stop } | Should -Throw -ErrorId $expected + param($value, $expected) + { Get-Command $value -ErrorAction Stop } | Should -Throw -ErrorId $expected } } } From df6bda7c61852a8077ba4a65074817185124e003 Mon Sep 17 00:00:00 2001 From: Jacobe Kerr Date: Mon, 7 Sep 2020 22:26:17 -0600 Subject: [PATCH 11/11] Capitalized Name header and put it in single quote --- .../Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 index 0faaddb8e12..0f5dabd02b5 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Command.Tests.ps1 @@ -180,7 +180,7 @@ Describe "Get-Command" -Tag CI { $Result | Should -BeOfType [System.Management.Automation.CommandInfo] } - It "Throws '' exception if name is used" -TestCases @( + It "Throws '' exception if '' name is used" -TestCases @( @{ Name = 'space'; Value = ' '; expected = "CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand" } @{ Name = 'empty'; Value = ''; expected = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCommandCommand" } @{ Name = 'null'; Value = $null; expected = "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetCommandCommand" }