Posh-SSH icon indicating copy to clipboard operation
Posh-SSH copied to clipboard

Get-SCPFile not clearing old file on overwrite

Open RockSlice opened this issue 5 years ago • 5 comments

When overwriting a local file, and the downloaded file is shorter, Get-SCPFile does not remove the old file first.

As a result, the new file file contains the end of the old file. Eg if the old file is "abcdefghijk" and you download "www", you end up with "wwwdefghijk"

The command I was using was: Get-SCPFile -ComputerName $sddcMgr -Credential $cred -LocalFile '.\Cache\markerfile' -RemoteFile '/home/vcf/markerFile' -force

This was on the SDDC manager markerFile on VCF 4.2.

RockSlice avatar Apr 26 '21 10:04 RockSlice

Thanks for reporting it. This is a big one I will have to figure out how to do it safely .

darkoperator avatar Apr 26 '21 12:04 darkoperator

I think this is underlying library bug This is how it is done on .Net (see Remarks) https://docs.microsoft.com/en-us/dotnet/api/system.io.file.openwrite so it can be fixed at

  1. script level
  2. posh-ssh level
  3. renci.ssh level

Since the solution might introduce security / access issues, I vote for 1 :) but you can try to open issue to fix renci - it just need output.SetLength(0) after output = fileInfo.OpenWrite()

MVKozlov avatar Apr 26 '21 18:04 MVKozlov

It is, thinking if I should post it to the library and wait for a fix which could be months to years or just download to a temp file, if successful, delete and copy.

darkoperator avatar Apr 26 '21 19:04 darkoperator

the temporary file is a potential security issue that I mentioned above. the temporary path can have other owner/ACL, it can fill the available space, and so on... May be truncating be better, it mimic original scp

MVKozlov avatar Apr 26 '21 20:04 MVKozlov

Beta 3 for 3.0 pushed to the gallery, if the file exists with get-scpitem it will error, if force is given it will delete and then download if it exists

darkoperator avatar Jun 01 '21 00:06 darkoperator