Skip to content

fix(server): keep paired devices out of sensitive folders and slow down repeated pairing requests - #545

Merged
linroid merged 3 commits into
mainfrom
fix/sharing-hardening
Oct 14, 2026
Merged

linroid merged 3 commits into
mainfrom
fix/sharing-hardening

Conversation

@linroid

@linroid linroid commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

Why

A review of device sharing found three ways a shared device could be misused:

  • Arbitrary file writes. With an access token and no allowedDirectories (always the case in the apps), DestinationGuard was off. A paired device, or anyone holding the token, could save into ~/Library/LaunchAgents, ~/.ssh, the Windows Startup folder or the app's own folders, which means code execution as the user.
  • Pairing prompt fatigue. After a denial, the same address could ask again immediately, so a device on the LAN could keep prompting the owner until they tapped Allow by mistake.
  • Lock-screen Allow. On Android, the Allow notification button worked on a locked phone.

What changed

Folders (DestinationPathPolicy, DestinationGuard, systemProtectedDirectories())

  • With a token and no allowed folders, callers may still choose any folder, since the apps let owners type folders on a remote device. Protected places are refused with 403 path_rejected:
    • system folders, ~/Library and AppData
    • folders the hosts add: the config or data directory (desktop, Android, ketch server) and the desktop app's install folder, for portable copies
    • hidden entries such as ~/.ssh and ~/.zshrc
  • How the check works:
    • New excluded and refuseHidden options on DestinationPathPolicy are checked only below the root that holds a path. The download folder stays usable even inside a protected or hidden folder.
    • Exclusions are compared ignoring case, and symbolic links are followed.
  • New downloads are now confined in this mode too, so they never replace an existing file (file (1).zip instead).
  • content:// folder URIs pass unchanged, since the app can only write to folders it was granted.
  • New KetchServer(protectedDirectories = ...) parameter.

Pairing (PairingBackoff)

  • A request that is denied or left unanswered is a strike against its address. The address waits 30 s before asking again, doubling per strike up to 1 h; strikes are forgotten after an hour without one. An IPv6 address counts as its /64.
  • Five strikes from any addresses within 10 minutes pause all pairing for 10 minutes.
  • Withdrawing a request is not a strike.
  • The server answers 429 pairing_paused with Retry-After. RemotePairing returns a new PairingResult.Paused, and the connect form shows a new message, translated into all ten languages.

Where pairing requests are answered

  • No more dialog popping up over whatever the owner is doing. Requests show as cards at the top of Settings › Sharing (PairingRequestCards), the only place a device can be allowed in.
  • Elsewhere in the app, a sticky toast ("Pixel 9 wants to control this Mac") with Review opens Sharing. It goes away once Sharing shows the request or the request ends.
  • While the app is not in front, the hosts notify (PairingNotices.follow), on arrival or once the app leaves the front. Tapping the notification opens Settings › Sharing: the desktop tray via NotificationClick.Pairing, Android via AndroidNotifier.opensSharing.
  • The Android notification keeps only Don't allow, so nobody holding a locked phone can let a device in.

Docs updated: AGENTS.md, cli/README.md, docs/docker.md, docs/architecture.md.

Reviewer notes

  • Behavior change for clients with a token: relative destinations are now resolved against the download folder, and an existing target file gets a free name instead of being replaced.
  • Not addressed here: the server still speaks plain HTTP, so tokens and pairing answers travel unencrypted on the LAN. TLS is a separate, larger change. A time-limited pairing window was also left out, because it changes the pairing UX.

Testing

  • :library:core:jvmTest (new DestinationPathPolicyTest cases: exclusions, case, links, hidden entries)
  • :library:server:test (new DestinationGuardTest cases, PairingBackoffTest, pairing route test)
  • :library:remote:jvmTest (new PairingResult.Paused test)
  • :app:shared:jvmTest
  • Compiled :app:desktop, :cli and :app:android (compileDirectDebugKotlin)
  • AppStatePairingTest (toast) and PairingNoticesTest (host notifications)
  • Snapshot of the new card: ConnectSnapshots.pairingRequest_asksTheOwnerWithTheCode
  • Not run on a device or desktop: the Android notification and the tray notification click

The pairing window (pairing off until the owner turns it on, for 3 minutes) and the Find on local network changes came after this was merged; they are in #547.

…wn repeated pairing requests

With an access token and no allowed folders, callers could save anywhere,
so a paired device or a leaked token could drop files into ~/Library/
LaunchAgents, ~/.ssh or the app's own folders. Such callers are now kept
out of system folders, the user's application data, the app's data and
install folders, and hidden entries, unless inside the download folder,
and a new download never replaces an existing file.

Pairing requests that are denied or left unanswered now make their
address wait before asking again, doubling each time, and many of them
pause pairing for everyone, so a device cannot prompt the owner until
they tap Allow by mistake. Android's Allow notification button needs the
phone unlocked.
@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-14T01:08:43.694858Z 9bff0ea 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: 9bff0eaf47

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

Pairing requests no longer pop up a dialog over whatever the owner is
doing. They show as cards at the top of Settings › Sharing, the only
place a device can be allowed in. Elsewhere in the app a sticky toast
with Review opens Sharing, and while the app is not in front the desktop
tray or an Android notification tells about them, on arrival or once the
app leaves the front; tapping it opens Sharing. The Android notification
keeps only Don't allow, so nobody holding a locked phone lets a device
in.
…Retry-After up

A file:// destination skipped the protected and hidden folder checks, and
Android opens any absolute URI for writing, so a caller with the token
could still overwrite the app's own files. Only content:// URIs, which
reach folders the owner granted, pass now.

Retry-After truncated the remaining backoff, so a device that waited as
told was turned away again; it is rounded up.
@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 1 898 files  +  483   1 925 suites  +483   22m 30s ⏱️ + 5m 39s
12 851 tests +4 312  12 606 ✅ +4 312  245 💤 ±0  0 ❌ ±0 
15 392 runs  +4 330  15 147 ✅ +4 330  245 💤 ±0  0 ❌ ±0 

Results for commit 29ba1e5. ± Comparison against base commit bafc86e.

This pull request removes 2 and adds 4314 tests. Note that renamed tests count towards both.
com.linroid.ketch.app.instance.PairingRequestsTest ‑ watch_reportsRequestsAsTheyArriveAndLeave[jvm]
com.linroid.ketch.config.FileConfigStoreTest ‑ load_unreadableFile_movesItAsideAndReturnsDefaults[jvm]
com.linroid.ketch.app.feedback.PairingNoticesTest ‑ answeredRequest_isWithdrawn[jvm]
com.linroid.ketch.app.feedback.PairingNoticesTest ‑ requestAnsweredInFront_isNeverPosted[jvm]
com.linroid.ketch.app.feedback.PairingNoticesTest ‑ requestWhileAway_isPostedOnce[jvm]
com.linroid.ketch.app.feedback.PairingNoticesTest ‑ requestWhileInFront_isPostedOnceTheAppLeaves[jvm]
com.linroid.ketch.app.instance.UploadNetworkTest ‑ followUploadNetwork_networkFails_pausesAndKeepsFollowingTheSettings[jvm]
com.linroid.ketch.app.state.AppStatePairingTest ‑ answeredRequest_withdrawsTheToast[jvm]
com.linroid.ketch.app.state.AppStatePairingTest ‑ requestWhileSharingIsNotShown_postsAToastThatOpensSharing[jvm]
com.linroid.ketch.app.state.AppStatePairingTest ‑ requestWhileSharingIsShown_postsNoToast[jvm]
com.linroid.ketch.app.state.AppStatePairingTest ‑ sharingComesOnScreen_withdrawsTheToast[jvm]
com.linroid.ketch.config.FileConfigStoreTest ‑ load_unreadableFile_movesItAsideAndReturnsDefaultsWithoutUploads[jvm]
…

@linroid
linroid merged commit 5aa3f45 into main Oct 14, 2026
23 of 25 checks passed
@linroid
linroid deleted the fix/sharing-hardening branch October 14, 2026 01:38
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