34 Commits

Author SHA1 Message Date
Rob Hague c66b9f8fb0 Require an explicit IRemotePathTransformation for ScpClient
SCP performs a transfer by running scp on the server with the remote path
embedded in a command. On a shell-based server that command is interpreted
by a shell, so a path that is not quoted to suit that shell can be executed
as a command on the server (GHSA-mggc-4xg6-vcxf); on a non-shell-based
server the path is used literally and must not be quoted at all. The right
encoding therefore depends on the server, and no single transformation is
safe for every server.

Rather than default this choice, obsolete the ScpClient constructors that
implicitly used DoubleQuote and add constructors that take an
IRemotePathTransformation explicitly, so callers must choose one suited to
their server and trust environment. DoubleQuote remains the default for the
obsolete constructors, so existing behaviour is unchanged. Document the
consideration on ScpClient and IRemotePathTransformation, and recommend
using SFTP.
2026-08-09 18:31:07 +02:00
Rob Hague 89f378a0ab Keep APM and sync UploadFile/DownloadFile callbacks on the threadpool (#1805)
* Tweak internal IProgress usage for APM and sync UploadFile/DownloadFile

Changes to support IProgress<> callback on UploadAsync/DownloadAsync meant wrapping
the Action<> callback on existing methods in a Progress<>, which posts the callback
onto the current synchronisation context rather than the threadpool. For the legacy
APM methods (Begin[..]), let's just preserve their old behaviour.

For the synchronous methods, posting to the synchronisation context is probably the
worst choice (because if there is one, the method itself is running there). We can
either revert to the threadpool as well, or take the opportunity to invoke the
callback synchronously, which is a behavioural change but probably the least
surprising behaviour for a synchronous method.

* keep callbacks on the threadpool

Actually, we could call the Download callback synchronously easily enough, but the Upload progress
reports are being made on the message listener thread upon request ack. A more involved
scheme could drain callbacks to fire during the read loop. For now just make it all the
same behaviour as in 2025.1.0.
2026-06-26 19:02:09 +02:00
dependabot[bot] f5e6ce710c Bump the dependencies group with 11 updates (#1808)
* Bump the dependencies group with 11 updates

Bumps coverlet.collector from 6.0.4 to 10.0.1
Bumps coverlet.msbuild from 6.0.4 to 10.0.1
Bumps GitHubActionsTestLogger from 3.0.1 to 3.0.4
Bumps Meziantou.Analyzer from 3.0.18 to 3.0.114
Bumps Microsoft.Bcl.Cryptography from 10.0.3 to 10.0.9
Bumps Microsoft.Extensions.Logging.Console from 10.0.3 to 10.0.9
Bumps MSTest from 4.1.0 to 4.2.3
Bumps PolySharp from 1.15.0 to 1.16.0
Bumps SonarAnalyzer.CSharp from 10.20.0.135146 to 10.27.0.140913
Bumps System.Formats.Asn1 from 10.0.3 to 10.0.9
Bumps Testcontainers from 4.10.0 to 4.12.0

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Robert Hague <rh@johnstreetcapital.com>
2026-06-26 17:20:18 +02:00
mus65 0ff2c50765 SftpClient: add IProgress to DownloadFileAsync and UploadFileAsync (#1771)
* SftpClient: add DownloadFileAsync overload with downloadCallback

fixes #1765

* Change to IProgress and also add to UploadFileAsync
2026-03-16 19:47:59 +01:00
mus65 adbb68e6e8 Add UploadFileAsync override with canOverride (#1750)
fixes #1734
2025-12-23 11:04:27 +00:00
mus65 933613e31c Update to MSTest 4 (#1721)
* Update to MSTest 4

* fix TestMethodForPlatformAttribute

replace Execute with ExecuteAsync and fix MSTEST0057

https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0057 (link currently dead)

* fix compilation error

* fix MSTEST0037

https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0037

* fix MSTEST0052

https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0052

* fix MSTEST0045

Fixing this properly would require the tests to respect
testContext.CancellationToken. I'm not sure this is worth fixing
or how to even do it for the sync methods.

https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0045

* fix MSTEST0001

I assume that parallelization would break a lot of stuff.

https://learn.microsoft.com/en-us/dotnet/core/testing/mstest-analyzers/mstest0001

* Workaround for new Sonar warnings because of MSTest4

* revert analyzer fixes in OrderedDictionaryTest

* use custom sync console logger for MSTest

to work around https://github.com/microsoft/testfx/issues/6457

* remove redundant args

---------

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2025-11-14 22:09:13 +01:00
Rob Hague cd9ec8f395 Add SftpException and SftpPathNotFoundException.Path (#1716)
This adds an SftpException which sits between the existing SftpPathNotFoundException/
SftpPermissionDeniedException and SshException, and which contains the response code
from the SSH_FXP_STATUS packet, along with a default message if one was not provided.

SftpPathNotFoundException also gains a Path property which is populated in cases where
it makes sense.
2025-10-23 07:34:26 +02:00
Rob Hague dccdedc36e Build the read-ahead mechanism into SftpFileStream (#1705)
* Build the read-ahead mechanism into SftpFileStream

This change unifies the SFTP download implementations that exist via DownloadFile and
via SftpFileStream, by rewriting SftpFileStream to perform the same "read-aheads" as
DownloadFile. This brings the performance of downloads via SftpFileStream in line with
DownloadFile, such that the latter is now effectively SftpFileStream.CopyTo. It also
brings the recently added DownloadFileAsync up to speed since that was implemented via
SftpFileStream.CopyToAsync.

The methodology is a mix of the previous one and that within OpenSSH: the first call to
SftpFileStream.Read sends one read request to the server. The second sends two and when
not interrupted by Write or similar, the number of in-flight read requests continues to
scale up in this fashion.

I have measured CopyTo to be 3-20x faster than before, depending on file size and server
round-trip time.

* Check CanSeek in ReadAllBytes

* Squeeze out some performance
2025-10-22 19:55:13 +02:00
dependabot[bot] 03ae6bfa0f Bump dependencies (#1682)
* Bump the dependencies group with 4 updates

Bumps BouncyCastle.Cryptography from 2.6.1 to 2.6.2
Bumps Meziantou.Analyzer from 2.0.205 to 2.0.210
Bumps MSTest from 3.9.3 to 3.10.0
Bumps SonarAnalyzer.CSharp from 10.13.0.120203 to 10.15.0.120848

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2025-08-02 17:57:09 +02:00
William Decker 9cf282731a Added GetAttributesAsync to SftpClient (#1648)
* Added GetAttributesAsync to SftpClient

* Adding integration tests + unit test

* Address warnings in test classes.

---------

Co-authored-by: William Decker <william.decker@syndigo.com>
2025-06-05 23:20:13 +02:00
mus65 f24a5d6de6 fix IDE0350 warnings with .NET SDK 9.0.300 (#1645)
test\Renci.SshNet.IntegrationTests\OldIntegrationTests\SshCommandTest.cs(460,21,460,22): warning IDE0350: Lambda expression can be simplified (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0350)
2025-05-25 17:21:30 +02:00
Noah Dela Rosa d08c4aaa8a Add UploadFileAsync and DownloadFileAsync methods (#1634)
* add interface methods

* add internal file methods

* impl interface methods

* tweak buffer size usage

* swap tests with async upload

* swap more upload file references

* add async download tests

* add upload/download integration async test

* check if net48

* silence not await warning

* try tweaking test init

* remove request close from upload

* dispose already closes, remove dup call

* remove extra upload overload

* inherit doc

* configure await

* remove excess util functions

* add cancel throws

* add cancellation tests

* missed one configure await

* use default buffer size

* private ctor

* docs

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2025-05-05 23:03:10 +02:00
mus65 c318e39d2c lock SendData to fix random connection failures (#1623) 2025-03-29 17:16:42 +01:00
mus65 071a8d56a6 CI: run .NET Framework Integration Tests on Windows (#1615)
* CI: run .NET Framework Integration Tests on Windows

* use apt-get

* use vampire/setup-wsl

* Run Windows Integration Tests in separate job

so publish doesn't depend on it

* Ignore flakey Test from #1253 in CI
2025-03-25 09:13:28 +01:00
mus65 86dc811458 Drop net6.0 target (#1580)
* Drop net6.0 target

* Update src/Renci.SshNet/Common/TaskToAsyncResult.cs

Co-authored-by: Rob Hague <rob.hague00@gmail.com>

* remove redundant #if

for some reason this made the compiler suddenly
realize that the plain text variables are unused.

* use TargetFrameworkIdentifier

this doesn't work in Directory.Build.props, moved it to Directory.Build.targets.

* fix null reference warnings in Benchmarks

seems like the warnings were (somehow) disabled here
before and were fixed by the previous TargetFrameworkIdentifier
change.

* fix unused plainTextOffset in AesGcmCipher.BclImpl

* CI retry

* more cosmetics

* more

* update README

* Revert "use TargetFrameworkIdentifier"

This reverts commit 076ede161d.

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
Co-authored-by: Robert Hague <rh@johnstreetcapital.com>
2025-02-19 13:37:38 +01:00
dependabot[bot] 16d84d0395 Bump test dependencies (#1583)
* Bump coverlet.collector from 6.0.2 to 6.0.4

Bumps [coverlet.collector](https://github.com/coverlet-coverage/coverlet) from 6.0.2 to 6.0.4.
- [Release notes](https://github.com/coverlet-coverage/coverlet/releases)
- [Commits](https://github.com/coverlet-coverage/coverlet/compare/v6.0.2...v6.0.4)

---
updated-dependencies:
- dependency-name: coverlet.collector
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* update test deps

* update test deps

* some fixes

* group dependencies

* analyzer fixes

* just group them all together

* more cleanup

* silent analyzers -> suggestion

* restore constant

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Robert Hague <rh@johnstreetcapital.com>
Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2025-02-04 21:58:59 +01:00
Rob Hague 6d94e97ac0 Fix hang/unhandled exception in SshCommand upon disconnect (#1565) 2025-01-03 11:13:45 +01:00
Ryan Esteves 1a8839ec3e Added support for deleting directories asynchronously (#1503)
* Added support for deleting directories asynchronously

* Clarify that the task represents the asynchronous delete operation

Co-authored-by: Rob Hague <rob.hague00@gmail.com>

* Added DeleteAsync and DeleteDirectoryAsync to ISftpClient

* Inherit docs from interface

* Added additional tests for new async delete functions

* Update list directory test to use async delete methods

* x

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2024-09-26 08:55:51 +02:00
Ryan Esteves ce867d6640 Added CreateDirectoryAsync to SftpClient (#1505)
* Added CreateDirectoryAsync to SftpClient

* Use CreateDirectoryAsync for async test

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2024-09-25 21:50:27 +02:00
Ryan Esteves 737c3e5f9d Added ChangeDirectoryAsync to SftpClient (#1504)
* Add ChangeDirectoryAsync

* Added async tests for ChangeDirectoryAsync
2024-09-24 21:19:26 +02:00
Ryan Esteves 4c5d0c075e Added ExistsAsync and GetAsync to SftpClient (#1501)
* Added ExistsAsync to SftpClient

* Added GetAsync to SftpClient

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
2024-09-21 18:07:37 +02:00
mus65 3b55ba3a0a Update SonarAnalyzer.CSharp (#1494)
* Update SonarAnalyzer.CSharp

* fix S3993: Custom attributes should be marked with "System.AttributeUsageAttribute"

https://rules.sonarsource.com/csharp/RSPEC-3993/

* fix S6966: Awaitable method should be used

Introduced abstractions for CancellationTokenSource.CancelAsync()
and Stream.DisposeAsync() to avoid #ifdef.

Supressed pipeStream.WriteAsync because it deadlocks the test.
I assume because PipeStream doesn't override WriteAsync.

https://rules.sonarsource.com/csharp/RSPEC-6966/

temp

* fix S3431: "[ExpectedException]" should not be used

Removed the Connect() from Multifactor_PublicKeyWithEmptyPassPhrase
because the Exception is already thrown in the factory.

https://rules.sonarsource.com/csharp/RSPEC-3431/

* fix S2325: Methods and properties that don't access instance data should be static

This one is pretty redundant with CA1822 (which is also disabled in the
tests).

It caught a few more cases in the library itself, most of which can't
be changed because they are public API.

https://rules.sonarsource.com/csharp/RSPEC-2325/

* fix S127: "for" loop stop conditions should be invariant

not sure if this one is worth having. The only cases it found
are imho legitimate or not worth fixing, so I supressed them.

https://rules.sonarsource.com/csharp/RSPEC-127/

* fix S1964: An abstract class should have both abstract and concrete methods

Suppressed for public APIs, changed ExtendedReplyInfo to interface.

https://rules.sonarsource.com/csharp/RSPEC-1694/

* Remove redundant test

this is already covered by Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPassphraseIsNull

* Revert "fix S2325: Methods and properties that don't access instance data should be static"

suppress it instead

This reverts commit 2020604958.

* Revert "fix S127: "for" loop stop conditions should be invariant"

suppress it instead

This reverts commit 1d8b4ac335.
2024-09-19 18:56:32 +02:00
Rob Hague aac10fba3d Miscellaneous cleanup/throw helpers (#1491)
* PrivateKeyFile stuff

* Extract from net7 branch

* more ThrowHelper stuff
2024-09-19 07:53:05 +02:00
Rob Hague 3bc568471e Add SshCommand.ExecuteAsync (#1426)
* Add SshCommand.ExecuteAsync

After the previous change (#1423), this basically entails swapping out the
IAsyncResult for a TaskCompletionSource and hooking up the cancellation/timeout
logic.

As with the prior Begin/End implementation, the initiation of the command
(SendExecRequest) happens synchronously, so there's a bit of room for improvement
there, but otherwise it is the Task-based async that we know and like.

I chose to make it void (Task)- returning instead of string like in the existing
overloads, so that OutputStream is not automatically consumed (and encoded as a
string) when that may not be desired. As in #650, I was initially considering
changing the other overloads to be void-returning as well, but decided that it was
not worth the break since most people will probably want to change over to
ExecuteAsync anyway.

* Update examples

---------

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
2024-06-19 13:02:19 +02:00
Rob Hague ac395dd64c A couple of changes/fixes in SshCommand (#1423)
* Send "signal" and set the wait handle on completion always

* Make ExitStatus nullable
2024-06-09 21:44:44 +02:00
mus65 c0a353a4de Cleanup formatting and style and enforce it in CI (#1380)
* Preparation to enforce formatting and style in CI

- enabled IDE0055 to enforce formatting on build
- disabled SA1137 and SA1025 because they are already
  covered by IDE0055
- disabled SA1021 because it conflicts with csharp_space_after_cast

* Cleanup formatting and style on codebase

This commit has no manual changes, it is the result
of running "dotnet format whitespace" and "dotnet format style"

* new formatting fixes after merge

* appveyor: set git autocrlf

as suggested by sharwell to hopefully fix Windows CI.

* use autocrlf input

to hopefully fix Linux tests

* fix formatting

* use autocrlf input for Linux only

* set csharp_space_after_cast to false

* Revert SA1021 suppression

---------

Co-authored-by: Robert Hague <rh@johnstreetcapital.com>
2024-05-17 22:34:27 +02:00
Rob Hague 0cfeb6bb94 Tweak AsyncSocketListener in tests (#1382)
* Tweak AsyncSocketListener in tests

Some CI runs have been crashing lately from within AsyncSocketListener.
This moves a bit of disconnection & exception handling around. It probably won't
fix the underlying tests which would have otherwise failed, but it might stop the
process from outright crashing.

Also reference the same code for the integration tests.

* Simplify the diff

* Stabilise a couple of tests
2024-05-10 11:37:01 +02:00
zeotuan db3d7e8d03 Fix CancelAsync Cause Deadlock (#1345)
* Fix CancelAsync Cause Deadlock

* Fix CancelAsync Cause Deadlock

* Support manual cancelling if exit-signal does not cancel

* Fix switch with duplicate case

* Revert wait exit response, use existing OperationCancelledException

* Not executing callback when command is cancelled
2024-03-24 14:55:26 +01:00
Rob Hague bfe6718fce Allow the integration tests to run on .NET Framework (#1286)
* Allow the integration tests to run on .NET Framework

* Update appveyor.yml

* Update appveyor.yml

---------

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
2024-02-06 13:30:01 +01:00
Rob Hague 47eabe7574 Tweak semaphore usage in Session (#1304)
- Change _connectAndLazySemaphoreInitLock to a SemaphoreSlim and use it in
  ConnectAsync.
- Rename it to _connectLock and only use it for connecting. Replace its
  other usages (on SessionSemaphore and NextChannelNumber) with Interlocked
  operations.
- Remove AuthenticationConnection semaphore. This static member placed a
  process-wide limit on the number of connections an application can make.
  I agree with the argument in
  https://github.com/sshnet/SSH.NET/issues/409#issuecomment-457415542
  (and in several other issues/PRs) that this should not be something
  that the library attempts to control.

The last change broke a few tests which do things like making 100 connections.
I was tempted to delete these tests as I don't think they have much value, but
instead I just limited their concurrency.

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
2024-02-06 13:16:11 +01:00
Rob Hague b0d01df91f Try to stabilise a few tests (#1278)
* Try to stabilise a few tests

* Fix random 0 buffer sizes

---------

Co-authored-by: Wojciech Nagórski <wojtpl2@gmail.com>
2023-12-22 08:37:30 +01:00
Rob Hague 6d9d03205b Rebased AsyncResult fix (#1184)
* 🐛 AsyncResult contains invalid value

- AsyncResult should contain invalid value immediately after async operation is marked as completed
- there was race condition problems with callback method which is invoked on different thread so updating of value is done without any synchronization. So in some cases async operation is marked as completed but async result value is not yet updated and contains invalid value

* Revert test

---------

Co-authored-by: Miroslav Pokorný <m.pokorny@quadient.com>
2023-12-04 09:48:56 +01:00
Wojciech Nagórski d3641a0676 Test integration tests (#1250)
* Test integration tests

* Update appveyor.yml

* Update appveyor.yml

* Update Dockerfile

* Update appveyor.yml

* test?

* Test

* Enable docker

* Update appveyor.yml

* Update appveyor.yml

* Fix & Show additional information

* Try to fix connection problems

* Fix build

* remove artifacts

* Enable logging

* Log Information only

* Update appveyor.yml

Co-authored-by: Rob Hague <rob.hague00@gmail.com>

* Update appveyor.yml

* Update appveyor.yml

Co-authored-by: Rob Hague <rob.hague00@gmail.com>

* Update appveyor.yml

Co-authored-by: Rob Hague <rob.hague00@gmail.com>

* Update appveyor.yml

* Update appveyor.yml

* Update appveyor.yml

* Update appveyor.yml

* Update appveyor.yml

* Update appveyor.yml

* Update appveyor.yml

* Update appveyor.yml

* Update appveyor.yml

* sleep after restarting

* Update RemoteSshd.cs

* Fix tests

* Dispose ports

* Small improvements

* Fix build

* Small fixes

* Revert not related changes

* Test linux and windows

* fix

* test_script

* Use real commands

* Fixes

* fix?

* Add Appveyor TestLogger

* Fix linux tests

* Fix tests

* Try to fix tests

* Revert

* Give time before

* fix

* revert

* Give some time to process all messages after connect

* ForwardedPortDynamicTest_Stop_PortStarted_ChannelNotBound

* fix netsh

* trace

* Update appveyor.yml

* Update appveyor.yml

* etl2pcapng

* Update appveyor.yml

???

* Update appveyor.yml

* Update appveyor.yml

* Update appveyor.yml

* Update appveyor.yml

* come on !!

* Update appveyor.yml

* Fixes tests for linux

* Reverts

* Fix build

* Update TestMethodForPlatformAttribute.cs

* Update TestMethodForPlatformAttribute.cs

* Update appveyor.yml

* Issue #1253

* Install .NET SDK

* next try

* fix?

* try

* Finishing

* Fixes

* apt-get install dotnet-sdk-7.0

* Finish?

* Add environment APPVEYOR_BAKE_IMAGE

* Update test/Renci.SshNet.Tests/Classes/Connection/DirectConnectorTest_Connect_TimeoutConnectingToServer.cs

Co-authored-by: Rob Hague <rob.hague00@gmail.com>

* Fix review

* Fix

* Update appveyor.yml

* Update appveyor.yml

* Delete .runsettings

---------

Co-authored-by: Rob Hague <rob.hague00@gmail.com>
Co-authored-by: Robert Hague <rh@johnstreetcapital.com>
Co-authored-by: Scott Xu <scott-xu@msn.com>
2023-11-30 19:19:00 +01:00
Wojciech Nagórski f1297dec75 Move test projects (#1212)
* Move test projects to test folder.
Move global.json to root of repo.
Update solution items in solution.

* Move test projects to test folder.
Move global.json to root of repo.
Update solution items in solution.
Update appveyor configuration/

* Attempt to have appveyor use the correct .NET SDK.

* Update .NET SDK to version 7.0.402.

* Move Data folder below Renci.SshNet.Tests.

* Make csinst less chatty.

* Move Data folder directly below test folder as it's used by multiple test projects.

* Remove CS1591 nowarn from concrete test projects as this is already defined in the Directory.Build.props that is in the test folder.

* Fix integration test after moving test projects

---------

Co-authored-by: drieseng <gert.driesen@telenet.be>
2023-10-14 22:16:11 +02:00