Skip to content

Add a phpseclib v4 SFTP adapter (league/flysystem-sftp-v4) - #1943

Open
onlime wants to merge 3 commits into
thephpleague:3.xfrom
onlime:phpseclib-v4
Open

onlime wants to merge 3 commits into
thephpleague:3.xfrom
onlime:phpseclib-v4

Conversation

@onlime

@onlime onlime commented Nov 22, 2026 •

Copy link
Copy Markdown

Fixes #1921.

This adds src/PhpseclibV4, published as league/flysystem-sftp-v4, as suggested in #1921: a new directory plus a new sub-split, like PhpseclibV3 was added next to PhpseclibV2. league/flysystem-sftp-v3 keeps working unchanged with phpseclib 3.

It builds on #1929 by @rikvdh, who is credited as co-author of the port. I opened a new PR because #1929's CI fails, and its author mentioned they could not run the SFTP tests locally. Running it against the repo's Docker SFTP servers shows three problems: the V4 adapter lets raw phpseclib exceptions through, readStream() and deleteDirectory() fail, and the V3 suite cannot run, because the root composer.json only allows phpseclib 4.

Commits

The three commits are meant to be reviewed one by one:

  1. Copy src/PhpseclibV3 to src/PhpseclibV4. A verbatim copy, with only the namespaces (League\Flysystem\PhpseclibV4, phpseclib4) and the package metadata (league/flysystem-sftp-v4, phpseclib/phpseclib: ^4.0, php: ^8.1) changed, plus the config.subsplit-publish.json and .gitattributes entries.
  2. Adapt to the phpseclib 4 API. This commit's diff is the actual port:
    • put(), mkdir(), get(), chmod(), stat() and rename() throw instead of returning false. Each call now translates the exception into the matching Flysystem exception and keeps the original as $previous.
    • get() into a stream returns null on success, so it is no longer checked for truthiness.
    • rawlist() throws for a missing directory, which still yields an empty listing.
    • The recursive delete() removes the directory itself, so the rmdir() that follows in deleteDirectory() throws. That failure is ignored, as phpseclib 3's false was.
    • chmod() takes the filename first, and NET_SFTP_TYPE_DIRECTORY becomes FileType::DIRECTORY.
    • login() only accepts a PrivateKey, so keys are loaded with PublicKeyLoader::loadPrivateKey().
    • getServerPublicHostKey() returns null instead of false.
    • SftpStub follows the new typed signatures and throws on a tripwire, as phpseclib 4 does.
  3. Test both majors in CI. Only one phpseclib major can be installed at a time:
    • The root composer.json allows ^3.0.36 || ^4.0, so the existing matrix resolves phpseclib 4, and one new matrix entry pins phpseclib 3 with --with phpseclib/phpseclib:^3.0.36.
    • The SFTP tests of the missing major are skipped. Until now they called markTestIncomplete() in setUpBeforeClass(), which PHPUnit 10 reports as an error.
    • wait_for_sftp.php picks the V4 or V3 provider.
    • phpstan.neon excludes src/PhpseclibV4, like V2 and V3.

Testing

The CI run is waiting for approval, so I replayed both jobs locally with the workflow's own composer update commands. The tests ran against docker compose up sftp sftp_eddsa_only toxiproxy, with COMPOSER_OPTS='' and the test key in an ssh-agent, so the agent test runs too:

Job PHP phpseclib src/PhpseclibV4 src/PhpseclibV3
existing (composer update) 8.2, 8.5, 8.6 4.0.4 96 tests green skipped
new (--with phpseclib/phpseclib:^3.0.36) 8.2, 8.5 3.0.59 skipped 96 tests green
  • Both suites skip the same 5 tests: no public or temporary URLs, no checksums.
  • The V4 suite also passes on phpseclib 4.0.0 and 4.0.3, so ^4.0 is an honest lower bound. These were installed with --no-blocking, because Composer blocks releases before 4.0.4 over security advisories.
  • PHPStan reports no errors with either major.
  • Line coverage of the V4 adapter and connection provider leaves the same paths uncovered as V3's.

Behavior differences from the V3 adapter

  • phpseclib 4 removed SSH2::getLastError(), so UnableToAuthenticate::connectionError() is always null here.
  • The UnableTo* exceptions now carry phpseclib's message (for example Error from server (SSH_FX_NO_SUCH_FILE): No such file) as their reason, and the phpseclib exception as $previous.

Left for a maintainer

  • Create thephpleague/flysystem-sftp-v4 and register it on Packagist, so the sub-split can publish.
  • The sub-split README links to docs/adapter/sftp-v4/. I'm happy to send a gh-pages PR that mirrors sftp-v3.md once this direction is fine with you.

onlime and others added 3 commits November 22, 2026 16:50
A verbatim copy of src/PhpseclibV3 with only the namespaces (League\Flysystem\PhpseclibV4, phpseclib4) and the package metadata (league/flysystem-sftp-v4, phpseclib/phpseclib ^4.0, php ^8.1) changed, published to a new thephpleague/flysystem-sftp-v4 sub-split, the same way PhpseclibV3 was added next to PhpseclibV2. The phpseclib 4 API changes follow in the next commit, so that they can be reviewed on their own.
phpseclib 4 throws exceptions where phpseclib 3 returned false, so every SFTP call that used to be checked for false now translates the exception into the matching Flysystem exception, keeping the original as the previous exception:

- put(), mkdir(), get(), chmod(), stat() and rename() throw instead of returning false. get() into a stream returns null on success, so it can no longer be checked for truthiness.
- rawlist() throws for a missing directory, which still yields an empty listing.
- The recursive delete() removes the directory itself, so the rmdir() in deleteDirectory() now throws. Its failure is ignored, as phpseclib 3's false was.
- chmod() takes the filename first, and NET_SFTP_TYPE_DIRECTORY is replaced by FileType::DIRECTORY.
- getLastError() is gone, so UnableToAuthenticate::connectionError() stays null.
- login() only accepts a PrivateKey, so keys are loaded with PublicKeyLoader::loadPrivateKey(), which also rejects a public key with NoKeyLoadedException.
- getServerPublicHostKey() returns null instead of false.

SftpStub follows the new typed signatures and throws a FileSystemException on a tripwire, like phpseclib 4 does.

Co-authored-by: Rik van der Heijden <mail@rikvanderheijden.com>
Both phpseclib majors share the phpseclib/phpseclib package name, so only one of them can be installed per run:

- The root composer.json allows ^3.0.36 || ^4.0, so the existing matrix resolves phpseclib 4 and tests src/PhpseclibV4, and a new matrix entry pins phpseclib 3 with --with to keep src/PhpseclibV3 covered.
- The SFTP tests skip instead of marking themselves incomplete when their phpseclib major is missing, as PHPUnit 10 reports markTestIncomplete() in setUpBeforeClass() as an error.
- wait_for_sftp.php uses the V4 or V3 connection provider, depending on the installed major. phpseclib 2 cannot be installed next to them, so the V2 fallback is gone.
- phpstan.neon excludes src/PhpseclibV4, like src/PhpseclibV2 and src/PhpseclibV3.
This was referenced Nov 22, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

phpseclib v4 support

1 participant