Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -1419,18 +1419,25 @@ private void AddRemoteCommandMetadata(
return;
}

if (resolvedCommandName != null && !IsSafeNameOrIdentifier(commandMetadata.Name))
if (resolvedCommandName is not null && !IsSafeNameOrIdentifier(resolvedCommandName))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the change IsSafeNameOrIdentifier was used only once and it threw if null or empty.
Now we always check for not null so we could do the check in IsSafeNameOrIdentifier.

Also here we don't check for empty but do the check below.

Should we move both checks to IsSafeNameOrIdentifier?

{
this.WriteError(this.GetErrorSkippedUnsafeCommandName(resolvedCommandName));
return;
}

if (IsCommandSkippedByServerDeclaration(commandMetadata.Name))
// - We do the skip-list check on 'commandMetadata.Name' regardless to keep the existing behavior.
// - We do the skip-list check on the resolved command if no prefix is specified, to avoid a server skipped command
// from being exported and shadow the same-name command on client side without the user explicitly asking for it.
if (IsCommandSkippedByServerDeclaration(commandMetadata.Name)
|| (resolvedCommandName is not null
&& string.IsNullOrEmpty(Prefix)
&& IsCommandSkippedByServerDeclaration(resolvedCommandName)))
{
return;
}

if (!IsCommandNameAllowedForImport(commandMetadata.Name))
if (!IsCommandNameAllowedForImport(commandMetadata.Name)
|| (resolvedCommandName is not null && !IsCommandNameAllowedForImport(resolvedCommandName)))
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1823,7 +1823,11 @@ try

It "Bad verb alias causes no error or warning" {
try {
$ps = [powershell]::Create().AddCommand("Import-PSSession", $true).AddParameter("Session", $session).AddParameter("CommandName", "BadVerb-Variable")
$ps = [powershell]::Create().
AddCommand("Import-PSSession", $true).
AddParameter("Session", $session).
AddParameter("CommandName", "BadVerb-Variable").
AddParameter("AllowClobber")
$module = $ps.Invoke() | Select-Object -First 1

$ps.Streams.Error.Count | Should -Be 0
Expand Down Expand Up @@ -2077,6 +2081,61 @@ try
$errorVariable | Should -BeNullOrEmpty
}
}

Describe "Import-PSSession resolved command of an alias" -tags "Feature","RequireAdminOnWindows" {

BeforeAll {
if ($skipTest) { return }
$session = New-RemoteSession

# Define a remote function
Invoke-Command -Session $session { function MyFunction { "Remote!" } Set-Alias SafeAlias -Value MyFunction }
# Define an alias to 'Get-Help', which is in the skip-list of the remote session configuration.
Invoke-Command -Session $session { Set-Alias -Name myhelp -Value Get-Help }
}

AfterAll {
if ($skipTest) { return }
if ($null -ne $session) { Remove-PSSession $session -ErrorAction SilentlyContinue }
}

It "When '-AllowClobber' is false, shadowing command cannot be bypassed using an alias to it" -Skip:$skipTest {
function MyFunction { "Local!" }

{ Import-PSSession -Session $session -CommandName SafeAlias -AllowClobber:$false -DisableNameChecking } | Should -Throw -ErrorId "ErrorNoCommandsImportedBecauseOfSkipping,Microsoft.PowerShell.Commands.ImportPSSessionCommand"
MyFunction | Should -BeExactly "Local!"
}

It "Command in server skip-list cannot be bypassed using an alias to it" -Skip:$skipTest {
try {
$m = Import-PSSession -Session $session -CommandName myhelp -AllowClobber
$m.ExportedCommands.Count | Should -Be 0
{ myhelp } | Should -Throw -ErrorId "CommandNotFoundException"
}
finally {
if ($null -ne $m) {
Remove-Module $m -Force -ErrorAction SilentlyContinue
Remove-Item $m.ModuleBase -Recurse -Force -ErrorAction SilentlyContinue
}
}
}

It "Server skip-list check should be skipped for the resolved command of an alias when '-Prefix' is specified" -Skip:$skipTest {
try {
$m = Import-PSSession -Session $session -CommandName myhelp -AllowClobber -Prefix "Remote"
$m.ExportedCommands.Count | Should -Be 2
$exportedCommands = Get-Command -Module $m -All | ForEach-Object Name
$exportedCommands | Should -Contain "Remotemyhelp"
$exportedCommands | Should -Contain "Get-RemoteHelp"
}
finally {
if ($null -ne $m) {
Remove-Module $m -Force -ErrorAction SilentlyContinue
Remove-Item $m.ModuleBase -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
}
}
finally
{
Expand Down
Loading