mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Don't allow Move-Item with FileSystemProvider to move a directory into itself (#16198)
This commit is contained in:
@@ -5851,6 +5851,15 @@ namespace Microsoft.PowerShell.Commands
|
||||
destination = MakePath(destination, dir.Name);
|
||||
}
|
||||
|
||||
// Don't allow moving a directory into itself
|
||||
if (destination.StartsWith(Path.TrimEndingDirectorySeparator(path) + Path.DirectorySeparatorChar))
|
||||
{
|
||||
string error = StringUtil.Format(FileSystemProviderStrings.TargetCannotBeSubdirectoryOfSource, destination);
|
||||
var e = new IOException(error);
|
||||
WriteError(new ErrorRecord(e, "MoveItemArgumentError", ErrorCategory.InvalidArgument, destination));
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the confirmation text
|
||||
string action = FileSystemProviderStrings.MoveItemActionDirectory;
|
||||
|
||||
|
||||
@@ -345,4 +345,7 @@
|
||||
<data name="AlreadyListedDirectory" xml:space="preserve">
|
||||
<value>Skip already-visited directory {0}.</value>
|
||||
</data>
|
||||
<data name="TargetCannotBeSubdirectoryOfSource" xml:space="preserve">
|
||||
<value>Destination path cannot be a subdirectory of the source: {0}.</value>
|
||||
</data>
|
||||
</root>
|
||||
|
||||
@@ -187,6 +187,20 @@ Describe "Basic FileSystem Provider Tests" -Tags "CI" {
|
||||
$e.Exception | Should -BeOfType System.IO.IOException
|
||||
}
|
||||
|
||||
It 'Verify Move-Item fails for destination that is subdir of source with trailing: <trailingChar>' -TestCases @(
|
||||
@{ trailingChar = [System.IO.Path]::DirectorySeparatorChar }
|
||||
@{ trailingChar = [System.IO.Path]::AltDirectorySeparatorChar }
|
||||
@{ trailingChar = '' }
|
||||
) {
|
||||
param($trailingChar)
|
||||
|
||||
$dest = Join-Path -Path $TestDrive -ChildPath dest
|
||||
$null = New-item -ItemType Directory -Path $dest -Force -ErrorAction Stop
|
||||
$src = "$TestDrive$trailingChar"
|
||||
|
||||
{ Move-Item -Path $src -Destination $dest -ErrorAction Stop } | Should -Throw -ErrorId 'MoveItemArgumentError,Microsoft.PowerShell.Commands.MoveItemCommand'
|
||||
}
|
||||
|
||||
It "Verify Move-Item throws correct error for non-existent source" {
|
||||
{ Move-Item -Path /does/not/exist -Destination $testFile -ErrorAction Stop } | Should -Throw -ErrorId 'PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user