Skip to content

fix(torrent): keep silent and forged peers from blocking downloads, and fail closed on upload settings - #544

Merged
linroid merged 2 commits into
mainfrom
fix/torrent-upload-hardening
Oct 14, 2026
Merged

linroid merged 2 commits into
mainfrom
fix/torrent-upload-hardening

Conversation

@linroid

@linroid linroid commented Oct 14, 2026

Copy link
Copy Markdown
Owner

Fixes four findings from a review of torrent uploading, which is now on by default and reachable from the internet through port mapping.

Silent incoming connections (high)

Incoming connections and our own dials share one budget of 200. An incoming socket held its slot for up to 15 s before naming a torrent, so one IP opening about 200 silent connections stalled every download.

TorrentHandshakeGate now admits incoming handshakes in dispatchIncoming only up to a quarter of the budget (8 to 64), and 4 per host. An IPv6 host is counted by its /64. Loopback counts only against the total. Connections past either limit are closed at once.

Forged uTP SYNs (high)

One uTP SYN created a connected socket and took a budget slot, and its source can be forged. An accepted socket now stays half open until a packet acknowledges the random sequence number our SYN answer carried (UtpSocket.confirmed).

  • Until then it takes no data, is not handed to the engine and holds no budget slot.
  • At most 64 sockets wait at once, each for 10 s, and are forgotten without a RESET. Past that, SYNs go unanswered.
  • Our dialers acknowledge the answer at once. Other clients' first packet (the handshake) acknowledges it anyway.

Wi-Fi-only uploads failed open (medium)

When the network flow threw, followUploadNetwork stopped following it and left uploads as they were, so a phone that started on Wi-Fi kept uploading on mobile data. A failed network flow now counts as metered from then on. The settings are still followed.

Unreadable config turned uploading back on (medium)

The apps, ketch <url> and ketch mcp --standalone fell back to the defaults (upload = while-downloading) when config.toml could not be read, so a user who chose off started uploading. They now fall back to KetchConfig.Unreadable: the defaults, with uploading off.

Tests

  • TorrentHandshakeGateTest: per-host limit, total limit, IPv6 /64 grouping, loopback exemption.
  • UtpSocketTest: data that doesn't acknowledge our answer is dropped, and the dialer confirms at once.
  • UtpEndpointTest: forged SYNs over real UDP never reach the engine and stop being answered past the limit.
  • UploadNetworkTest: a failing network flow pauses uploads and settings are still followed.
  • FileConfigStoreTest: an unreadable file loads with uploads off.

:library:torrent:jvmTest (all 199 suites), the app's upload network tests and :config:jvmTest pass. :cli:compileKotlin and the iOS test compile of the torrent module succeed.

…nd fail closed on upload settings

- Incoming handshakes take at most a quarter of the connection budget and 4 per host
  (TorrentHandshakeGate), so peers that connect and send nothing never stall our dials
- Accepted uTP sockets stay half open until a packet acknowledges our SYN answer, so
  forged SYN sources never reach the budget; at most 64 wait, then SYNs go unanswered
- Phones pause uploads when the network can no longer be followed
- An unreadable config falls back to the defaults with uploading off
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 14, 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-14T00:50:32.744638Z 258d9b3 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 14, 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: 258d9b3069

ℹ️ 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

Copy link
Copy Markdown
Contributor

Test Results

 1 893 files  +  478   1 920 suites  +478   23m 8s ⏱️ + 6m 17s
12 813 tests +4 274  12 568 ✅ +4 274  245 💤 ±0  0 ❌ ±0 
15 354 runs  +4 292  15 109 ✅ +4 292  245 💤 ±0  0 ❌ ±0 

Results for commit 79b9d30. ± Comparison against base commit bafc86e.

This pull request removes 1 and adds 4275 tests. Note that renamed tests count towards both.
com.linroid.ketch.config.FileConfigStoreTest ‑ load_unreadableFile_movesItAsideAndReturnsDefaults[jvm]
com.linroid.ketch.app.instance.UploadNetworkTest ‑ followUploadNetwork_networkFails_pausesAndKeepsFollowingTheSettings[jvm]
com.linroid.ketch.config.FileConfigStoreTest ‑ load_unreadableFile_movesItAsideAndReturnsDefaultsWithoutUploads[jvm]
com.linroid.ketch.torrent.engine.TorrentHandshakeGateTest ‑ enter_ipv6_countsAWhole64AsOneHost
com.linroid.ketch.torrent.engine.TorrentHandshakeGateTest ‑ enter_ipv6_countsAWhole64AsOneHost[jvm]
com.linroid.ketch.torrent.engine.TorrentHandshakeGateTest ‑ enter_loopback_onlyCountsAgainstTheLimit
com.linroid.ketch.torrent.engine.TorrentHandshakeGateTest ‑ enter_loopback_onlyCountsAgainstTheLimit[jvm]
com.linroid.ketch.torrent.engine.TorrentHandshakeGateTest ‑ enter_manyPeers_isRefusedPastTheLimit
com.linroid.ketch.torrent.engine.TorrentHandshakeGateTest ‑ enter_manyPeers_isRefusedPastTheLimit[jvm]
com.linroid.ketch.torrent.engine.TorrentHandshakeGateTest ‑ enter_onePeerOpeningMany_isRefusedPastItsShare
com.linroid.ketch.torrent.engine.TorrentHandshakeGateTest ‑ enter_onePeerOpeningMany_isRefusedPastItsShare[jvm]
…

@linroid
linroid merged commit 8e4a777 into main Oct 14, 2026
14 checks passed
@linroid
linroid deleted the fix/torrent-upload-hardening branch October 14, 2026 01:11
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