Skip to content

fix(remote-ssh): distinguish SSH servers by port - #3670

Merged
bobleer merged 6 commits into
openBitFun:mainfrom
bobleer:bob/ssh-port-identity
Nov 2, 2026
Merged

bobleer merged 6 commits into
openBitFun:mainfrom
bobleer:bob/ssh-port-identity

Conversation

@bobleer

@bobleer bobleer commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

SSH profiles on the same host that differ only by port now get distinct connection ids, saved profiles, workspace ids, runtime roots, and session stores. Default-port (22) users see no change.

Fixes #3451

Type and Areas

Type: bug fix

Areas: Rust core (services-core workspace identity, services-integrations remote SSH, assembly workspace/session/snapshot paths), desktop/Tauri commands, web UI (SSH connection dialog)

Motivation / Impact

On clusters behind one address with port-mapped nodes, every node collapsed into one identity:

  • the dialog ignored the port when building the id (ssh-user@host);
  • save_connection deduped by host+user, so saving port 2202 overwrote the 2201 profile;
  • workspace stable id, runtime key, and session mirror were keyed by host+root only.

Design:

  • Connection id: port 22 keeps ssh-user@host. Other ports use ssh-user@host#port. The issue suggested host:port, but git history (feat(ssh): stable connection IDs and session hydration after port changes #710) shows released builds wrote ssh-user@host:port for every port, and canonical_ssh_connection_id (also in older peers) treats that as an alias of ssh-user@host. Reusing that spelling would make an older peer fold a 2222 node into the port-22 profile. No build ever wrote #, so older builds treat the new ids as opaque.
  • Workspace identity: ssh_workspace_identity_host(ssh_host, connection_id) returns ssh_host unchanged for every id without a port scope, and host#scope for port-scoped ids. This is applied at the stable id, runtime root/key, session dir, snapshot context, permission project id, and session-recovery runtime key call sites. # is a valid path component on all client OSes and never appears in a real SSH host, so a new identity can't pick up or migrate another host's legacy mirror.
  • sshHost metadata is unchanged (still the plain host). Port scope comes from the opaque connection id, so no new persisted field is needed.
  • Save dedup: replaces the same id, or a legacy :port alias of the exact same endpoint (host+port+user+container+wsl). A profile for another port is never replaced. A different, non-alias id for the same endpoint is kept, because records may still point to it.
  • Dialog: editing keeps the saved id, so a port edit never moves sessions. A new profile reuses the id of an existing profile for the same endpoint (including legacy port-less ids on non-22 ports). It never takes an id owned by another endpoint; it falls back to #22 or #port~n instead.

Verification

  • cargo test --locked -p openbitfun-services-core --no-default-features --features workspace-persistence --lib workspace_identity: 9 passed. New tests cover: default-port ids, scope parsing (every legacy id shape, plus synthetic workspace-host- container wrappers), byte-for-byte legacy identity, distinct port identities, per-root legacy pinning, and pinning one root leaving other roots of the same connection alone.
  • cargo test --locked -p openbitfun-services-integrations --no-default-features --features remote-ssh-concrete --lib remote_ssh::manager: 72 passed. New tests cover: two ports keep both profiles, a port edit keeps the id, and an old ssh_connections.json payload round-trips.
  • TMPDIR=/private/tmp/ cargo test --locked -p openbitfun-services-core --no-default-features --features session-recovery --lib session_recovery: 13 passed. The default macOS /var temp dir trips the existing linked-path guard, hence the override.
  • cargo test --locked -p openbitfun-product-domains --lib remote_surface: 20 passed (capability map).
  • cargo test --locked -p openbitfun-core --no-default-features --features agent-runtime,git,remote-workspace --lib -- service::workspace:: service::remote_ssh service::snapshot service::workspace_runtime agentic::workspace: 99 passed. New tests cover: an old session payload with no connection id ignores #port records; an old-host record (port-scoped id, legacy stable id) imported and round-tripped keeps its identity; port-scoped session paths; separate workspaces with no rebind conflict.
  • cargo check --locked -p openbitfun-desktop: ok
  • pnpm --dir src/web-ui run test:run src/features/ssh-remote src/infrastructure/api/service-api/legacyWorkspaceCompatibility src/infrastructure/peer-device src/infrastructure/api/adapters: 395 passed. New tests cover: id rules (:port alias taken, orphan reuse, legacy ids for older peers plus the conflict they would cause), the resolver, ~/.ssh/config presets resolved against saved profiles (and routed to the dialog when the list fails), the peer adapter guard, and a connection-less legacy session reference.
  • pnpm --dir src/web-ui run type-check, eslint on touched files, pnpm run i18n:audit, pnpm run capabilities:check: ok
  • pnpm --dir src/mobile-web run test:workspace-identity (25 passed, one new) and pnpm --dir src/mobile-web run type-check: ok
  • pnpm run fmt:rs
  • Not run: the Kotlin LegacyWorkspaceCompatibilityTest (no Java runtime on this machine). pnpm run check:core-boundaries fails here because this worktree is nested inside the main checkout; that is an environment issue, not this change.

Remote scenarios exercised (unit and integration tests only): remote workspace identity and persistence; Peer Device Mode capability gating (adapter tests); legacy references from Remote Control / mobile web. No live SSH host, peer pairing, or Detached Dispatch run.

Reviewer Notes

Upgrade compatibility

  • Default port: no change. Ids, workspace stable ids, runtime keys, runtime roots, and session mirror paths are byte-for-byte identical. Every id shape released builds wrote (ssh-user@host, ssh-user@host:port, container, WSL, and workspace-host- wrappers around them) has no port scope.
  • Existing non-22 profiles keep their id and their host-only identity, so their workspaces, sessions, snapshots, and permissions are not moved or deleted. Editing the port keeps the id. Adding another port creates a new #port profile beside them.
  • Old :port aliases: canonical_ssh_connection_id and the BitFun migration adapter are unchanged. A new profile never takes a bare id that a saved :port alias on another port already owns (it gets #22), so an old workspace is never silently rebound.
  • Delete and re-add: when workspace records still reference a legacy port-less id that no saved profile owns, re-adding that endpoint reuses the id, so its sessions reappear.
  • Old references without a connection id (session configs, cron targets, editor scopes, mobile caches) can only be legacy data. They never match a #port record in the Rust resolver, Web UI, mobile web, or Kotlin mobile, so old (sshHost, root) references don't become ambiguous.
  • Cross-version peers: Desktop hosts advertise ssh_port_scoped_identity_v1. Against a peer without it, the controller keeps legacy ids. If saving would replace another port's profile for the same host and user, the dialog blocks with a localized message instead. The peer adapter also refuses to send a port-scoped id to such a peer. The CLI peer host has no SSH profile commands and does not advertise the capability.
  • Records written by an older host: a port-scoped record stored under the host-only stable id (for example, created through an old peer) is pinned to the legacy identity per (connection, root) when records are loaded or imported, and in session recovery. Its sessions stay where they are, and other roots of the same connection keep the port-scoped identity.
  • ~/.ssh/config shortcut: preset ids are resolved against the host's saved profiles. If those can't be listed, the entry opens the connection dialog instead of connecting in one click.
  • Remote Docker on a synthetic host: workspace-host-<encoded id>-container-<name> ids keep the wrapped id's port scope, so containers on different ports stay separate.
  • Residual: a legacy non-22 profile saved under a pre-feat(ssh): stable connection IDs and session hydration after port changes #710 :port id keeps host-only identity. A new port-22 profile then gets #22 and a separate identity. Existing data is not migrated.

Rollback

Reverting leaves #port profiles readable as opaque ids. Their workspaces would fall back to host-only identity, so their sessions would be hidden until re-upgrade, not deleted.

Servers behind one address that differ only by SSH port (cluster nodes
mapped to different ports) shared a connection id, saved profile, workspace
stable id, runtime root, and session mirror.

- New non-22 profiles use ssh-user@host#port ids; port 22 keeps
  ssh-user@host. '#' was never generated by released builds, so legacy
  ssh-user@host:port aliases still canonicalize as before and older peers
  treat the new ids as opaque.
- Workspace identity (stable id, runtime key, session/runtime paths,
  permission project id, session recovery runtime key) derives from
  host#scope only for port-scoped ids; every existing id keeps its
  identity and on-disk layout byte-for-byte.
- Saving a profile no longer replaces a profile for another port; only
  the same id or a legacy :port alias for the exact endpoint is replaced.
- The dialog reuses an existing profile id for the same endpoint and never
  takes an id owned by another port.

Fixes openBitFun#3451
@bobleer

bobleer commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator Author

接手审查状态:当前已发布 head 的绿色 CI 还不能作为合入依据,后续兼容补丁尚未验证完毕。

已在未提交的后续补丁中复现一个阻断问题:如果为了兼容旧 workspace record,按 connection ID 在进程内全局固定 host-only identity,加载 /srv/legacy 会同时改变同一连接 /srv/scoped 已有 workspace 的 stable ID,进而改变 runtime / session 存储定位。最小回归 loading_one_legacy_root_does_not_reidentify_another_root_on_the_same_connection 已实际失败;这版全局 pin 不会提交合入。

正确修复需要由具体持久化 workspace record 决定旧/新身份,不能影响同连接的其他 root,也不能改写或删除用户会话。ssh_config 快捷入口、旧 alias 与删除后重建、容器派生 ID、旧 Peer capability gate 的补测也仍在收尾。此 PR 暂保留开放,不宣称 #3451 已解决。

- Resolve ~/.ssh/config preset ids against saved profiles; when they cannot
  be listed, route to the connection dialog instead of a one-click connect.
- Count pre-openBitFun#710 ssh-user@host:port aliases as taken, and reuse an orphaned
  legacy id that workspace records still reference.
- Advertise ssh_port_scoped_identity_v1 from Desktop hosts. Against older
  peers the controller keeps legacy ids and blocks adding a second port for
  the same host and user; the peer adapter rejects port-scoped ids.
- Pin the host-only identity per (connection, root) for records an older
  host stored under the legacy stable id, including session recovery.
- Connection-less legacy workspace references never select port-scoped
  records (Rust, Web UI, mobile web, Kotlin mobile).
- Parse port scopes through synthetic workspace-host container wrappers.
- Reuse an orphaned legacy id only through an exact ssh-user@host:port
  alias; a port-less orphan never moves to a non-default port.
- List workspace records once per ~/.ssh/config discovery pass.
- Reject malformed percent escapes (sign, short, invalid UTF-8) in the Rust
  and Kotlin workspace-host decoders, matching decodeURIComponent.
# Conflicts:
#	src/crates/contracts/product-domains/src/generated/remote-surface-registry.json
#	src/mobile-web/tests/workspace-identity.test.mjs
#	src/web-ui/src/infrastructure/api/generated/remoteSurface.ts
# Conflicts:
#	src/crates/contracts/product-domains/src/generated/remote-surface-registry.json
#	src/crates/contracts/product-domains/src/remote_surface/capabilities.rs
#	src/web-ui/src/infrastructure/api/generated/remoteSurface.ts
@bobleer
bobleer merged commit 9822a64 into openBitFun:main Nov 2, 2026
13 checks passed
bobleer added a commit to Raing5Days/OpenBitFun that referenced this pull request Nov 2, 2026
Regenerate the remote surface registry artifacts after openBitFun#3670.
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.

[Bug]: 无法通过端口号区分服务器

1 participant