Error uploading file with square brackets in the file name
I used Posh-SSH 2.2 in Powershell 4 on Windows Server 2012R2 to upload files from a local file share to a remote system file import hotfolder.
The command used to upload the file is:
$Result = Set-SFTPItem -SessionId ($ThisSession).SessionId -Path $_.FullName -Destination $DestFolder
When I upload a file called "ASO_149178 Con_DK-da Sortimentsoversigt – Geberit Mepla kompatibilitet [1] og [2].pdf" I get the following error:
Set-SFTPFile : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
At E:\Scripts\Overview-PDF-to-STEP\Upload-PDF-files-Test.ps1:37 char:12
+ $Result = Set-SFTPFile -SessionId ($ThisSession).SessionId -LocalFile $_.FullNa ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Set-SFTPFile], ArgumentOutOfRangeException
+ FullyQualifiedErrorId : System.ArgumentOutOfRangeException,SSH.SetSftpFile
When I rename the file so that the square brackets are changed to rounded brackets, the file uploads normally: "ASO_149178 Con_DK-da Sortimentsoversigt – Geberit Mepla kompatibilitet (1) og (2).pdf"
The error indicates that somehow the filename is being evaluated as code? This seems like there is a code injection issue. I did not test the 3.0-alpha yet as I would need to have Powershell upgraded on the machine.
I can upload the file manually with an SFTP client without errors. The filename is important in this case as the receiving system will use the filename to update an asset.
BR Robert
I now rename the file before uploading and it gets renamed back afterwards. Rename-Item only works for this file if I use -LiteralPath rather than -Path.
Supporting LiteralPath in PoshSSH would it seems also fix the issue.
Same issue, when using brackets in a file name the following error is generated:
Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
Test code
#region Create SFTP credential
Write-Verbose 'Create SFTP credential'
$params = @{
String = 'xxx'
AsPlainText = $true
Force = $true
ErrorAction = 'Stop'
}
$sftpPassword = ConvertTo-SecureString @params
$params = @{
TypeName = 'System.Management.Automation.PSCredential'
ArgumentList = 'sftpUser', $sftpPassword
}
$sftpCredential = New-Object @params
#endregion
$params = @{
ComputerName = 'localhost'
Credential = $sftpCredential
Port = 22
ConnectionTimeout = 60
AcceptKey = $true
Force = $true
Verbose = $false
}
$sessionParams = New-SFTPSession @params
$params = @{
Path = 'T:\Test file [with brackets].txt'
Destination = '\'
Force = $true
}
Set-SFTPItem @sessionParams @params
@darkoperator thank you for having another look at this.
The problem is here SetSftpItem/GetResolvedProviderPathFromPSPath
Seems we shoud use another one The drive or provider qualified path to be converted. This path may contain wildcard characters which will not get resolved
Or better make a special logic for Path/LiteralPath
Indeed. The funny thing is that for downloads this not an issue, so the logic there is already correct. But not for uploads it seems.
nope 👎
PS > Get-SFTPItem -Path "/home/xxx" -Destination "D:\Work" -SFTPSession $sftp -Verbose
VERBOSE: Downloading /home/xxx
PS > Get-SFTPItem -Path "/home/xxx" -Destination "D:\Test folder [with brackets]" -SFTPSession $sftp -Verbose
Get-SFTPItem: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
Oh sorry, I'm incorrect. We use another script for transferring files too, and I mixed that up. Sorry about that.
And apparently still a problem. Running in Windows 11, powershell 7, and uploading files to a Linux distro