Skip to content

fix(ftp): fall back to PASV and keep data connections on the control host - #614

Merged
linroid merged 2 commits into
mainfrom
fix/ftp-passive-fallback
Oct 23, 2026
Merged

linroid merged 2 commits into
mainfrom
fix/ftp-passive-fallback

Conversation

@linroid

@linroid linroid commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Found while downloading from public FTP servers with the CLI: ftp://ftp.gnu.org/... and ftp://demo:[email protected]/readme.txt failed with Connection refused on every data connection, while curl downloads both from the same machine.

Why

Here, the network path (a NAT/proxy) drops connections to EPSV data ports. curl copes in two ways that Ketch did not:

  1. When the EPSV data connection fails, curl disables EPSV and retries with PASV. Ketch only fell back to PASV when the server refused the EPSV command.
  2. curl ignores the address a PASV reply names and connects to the control host (its default since 7.74). GNU's server answers 227 Entering Passive Mode (0,0,0,0,…), and many servers behind NAT name a private address. Ketch dialed whatever address the reply named, so a server could also send it to another host.

What changes

  • RealFtpClient.openPassiveDataConnection: if the EPSV port cannot be reached (refused or timed out), it sends PASV instead, and the connection skips EPSV for later transfers.
  • PASV data connections always go to the control host; the reply's address is only logged as ignored (without the address itself).
  • ScriptedFtpServer (jvmTest) can answer PASV, name an unreachable EPSV port, and no longer needs a TLS context for plain sessions.

Verification

  • ketch built from this branch: GNU hello-2.12.tar.gz (SHA-256 matches the HTTPS copy), GNU gcc-14.2.0.tar.xz (88 MB, segmented, ~3 MB/s) and Rebex readme.txt (matches curl) all complete. Before: all failed after retries.
  • New RealFtpClientPassiveTest: an unreachable EPSV port falls back to PASV (and later transfers skip EPSV); a PASV reply naming TEST-NET-1 still downloads from the control host. With the old host handling, the second test fails.
  • ./gradlew :library:ftp:jvmTest passes.

…host

FTP downloads from ftp.gnu.org and test.rebex.net failed with "Connection
refused" on every data connection behind a NAT/proxy that drops EPSV data
ports, while curl downloads them: curl falls back to PASV when the EPSV
connection fails, and ignores the address a PASV reply names.

RealFtpClient now does both. A data connection that cannot reach the EPSV
port retries with PASV, and the connection skips EPSV from then on. PASV
data connections go to the control host: servers behind NAT name a private
address or 0.0.0.0, and a server should not send the client elsewhere.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T13:49:51.356046Z e3c11d5 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@claude

claude Bot commented Oct 23, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e3c11d5f32

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Test Results

 2 194 files  + 1   2 222 suites  +1   57m 58s ⏱️ + 1m 17s
15 257 tests +10  14 989 ✅ +10  268 💤 ±0  0 ❌ ±0 
18 608 runs  +10  18 340 ✅ +10  268 💤 ±0  0 ❌ ±0 

Results for commit 4adc24c. ± Comparison against base commit 94bd6b5.

This pull request removes 1 and adds 11 tests. Note that renamed tests count towards both.
com.linroid.ketch.engine.HttpDownloadIntegrationTest ‑ download_ignoredRange_failsInsteadOfCorruptingFile[jvm]
com.linroid.ketch.engine.HttpDownloadIntegrationTest ‑ download_ignoredRange_restartsOverOneConnection[jvm]
com.linroid.ketch.engine.HttpDownloadSourceTest ‑ download_serverIgnoringAdvertisedRanges_restartsOverOneConnection
com.linroid.ketch.engine.HttpDownloadSourceTest ‑ download_serverIgnoringAdvertisedRanges_restartsOverOneConnection[jvm]
com.linroid.ketch.ftp.RealFtpClientPassiveTest ‑ retrieve_pasvNamingAnotherAddress_connectsToControlHost[jvm]
com.linroid.ketch.ftp.RealFtpClientPassiveTest ‑ retrieve_unreachableEpsvPort_fallsBackToPasv[jvm]
com.linroid.ketch.ftp.RealFtpClientTest ‑ numericHost_ipv4[jvm]
com.linroid.ketch.ftp.RealFtpClientTest ‑ numericHost_ipv6[jvm]
iosSimulatorArm64Test.com.linroid.ketch.engine.HttpDownloadSourceTest ‑ download_serverIgnoringAdvertisedRanges_restartsOverOneConnection[iosSimulatorArm64]
iosSimulatorArm64Test.com.linroid.ketch.ftp.RealFtpClientTest ‑ numericHost_ipv4[iosSimulatorArm64]
iosSimulatorArm64Test.com.linroid.ketch.ftp.RealFtpClientTest ‑ numericHost_ipv6[iosSimulatorArm64]
…

♻️ This comment has been updated with latest results.

Resolving the host again for a data connection can reach another server of
a round-robin name, which is not listening on the passive port. Data
connections now go to the numeric address the control connection reached,
for EPSV and PASV alike.
@linroid
linroid merged commit 9fb402d into main Oct 23, 2026
14 checks passed
@linroid
linroid deleted the fix/ftp-passive-fallback branch October 23, 2026 14:49
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.

1 participant