From d0e823797f1bbac8c256a4f607bc97812ca0fdb6 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 12 Oct 2021 14:16:32 -0700 Subject: [PATCH] Don't allow `Move-Item` with FileSystemProvider to move a directory into itself (#16198) --- .../namespaces/FileSystemProvider.cs | 9 +++++++++ .../resources/FileSystemProviderStrings.resx | 3 +++ .../FileSystem.Tests.ps1 | 14 ++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 7e6d302b22..37229a18db 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -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; diff --git a/src/System.Management.Automation/resources/FileSystemProviderStrings.resx b/src/System.Management.Automation/resources/FileSystemProviderStrings.resx index 1b4d1159e5..9c232ae836 100644 --- a/src/System.Management.Automation/resources/FileSystemProviderStrings.resx +++ b/src/System.Management.Automation/resources/FileSystemProviderStrings.resx @@ -345,4 +345,7 @@ Skip already-visited directory {0}. + + Destination path cannot be a subdirectory of the source: {0}. + diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 index 10ec92b6be..bec4ad83b8 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 @@ -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: ' -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' }