Skip to content

refactor(platform): 远程服务端与 AskPass 共用同一份 Unix socket 对端账户检查(#2231) - #143

Merged
xiaoyumuxi merged 1 commit into
mainfrom
refactor/unix-peer-uid-shared
Nov 20, 2026
Merged

xiaoyumuxi merged 1 commit into
mainfrom
refactor/unix-peer-uid-shared

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Contributor

关联 Issue

1lck/Lithe-IDEA#2231(远程开发完整设计)。#137 合入时留下的重复代码:AskPass 通道(S13)和远程服务端的常驻会话(S20)各有一份 Unix socket 对端账户检查。

类型

  • refactor:合并重复实现,行为不变

改动说明

原来的问题

git-host/src/authentication/unix.rs 和 remote/src/server/platform/{linux,macos}.rs 里各有一份逐字相同的 peer_uid:Linux 和 Android 用 getsockopt(SO_PEERCRED),其他 Unix 用 getpeereid。两处都是安全检查,以后只改一处、漏掉另一处,就会出现两种判定。

lithe-platform(新增 socket 模块,只在 Unix 上编译)

  • peer_uid(&UnixStream) -> io::Result<u32>:返回 socket 另一端进程的有效用户 id。对从监听 socket 接受的连接,是发起连接的账户;对客户端的连接,是创建监听 socket 的账户。参数从裸 RawFd 改为借用 UnixStream,调用方不能再传一个已经关闭或不是 Unix socket 的描述符。
  • effective_uid():本进程的有效用户 id,从 git-host 移过来。
  • lithe-platform 本身的定位就是“少数需要系统 API 的调用放在本 crate 的 cfg 后面,真实主机上的测试放在 lithe-platform-tests”,所以没有新建 crate。为此新增唯一的依赖 [target.'cfg(unix)'.dependencies] libc。

git-host

  • 删除自己的 peer_uid 和 effective_uid,改用 lithe_platform::socket。监听端读不到对端账户时,仍当作其他账户拒绝;助手端读不到时,仍返回错误。

remote(服务端)

  • 删除 linux.rs、macos.rs 里的两份 peer_uid。platform/mod.rs 改为转出 lithe_platform::socket::peer_uid,daemon.rs 的对端检查只改成借用 stream。
  • server 特性新增可选依赖 lithe-platform。这是一个只依赖 libc 的叶子 crate,不会把 Core 的其他 crate 带进服务端;Cargo.toml 里“服务端和客户端不依赖 Core crate”的注释已改成写明这个例外。客户端(client 特性)的依赖不变。
  • remote 自己的 current_uid(getuid,也是 server.hello 里的 userId 和信任键的一部分)不变,常驻会话仍拿对端账户与它比较,行为不变。

改动超出 rust/crates/remote 的原因

要去掉重复,至少要动 remote 之外的两个 crate:git-host(删除副本)和 lithe-platform(放共享实现)。测试按 CONTRIBUTING 的平台测试规则第 2 条放进 lithe-platform-tests,因为结果取决于真实操作系统的系统调用。

体积(D9)

产物 main(94408ca) 本 PR 变化
x86_64-unknown-linux-musl release 1,587,600 B 1,587,824 B +224 B
aarch64-unknown-linux-musl release 1,438,400 B 1,438,600 B +200 B

两者都用 --release --no-default-features --features server、rust-lld 链接,在同一台机器上构建。

本 PR 不改插件契约、能力版本或插件函数协议,不需要更新协议更新记录。

测试

  • 新增 lithe-platform-tests 的 socket::unix::both_ends_of_a_socket_see_the_account_that_opened_the_other:在临时目录里建一个真实的监听 socket,连接排进队列后用非阻塞 accept 取出,不做任何等待;在服务端和客户端两端分别核对 peer_uid 等于 effective_uid()。Linux 上测 SO_PEERCRED 分支,macOS 上测 getpeereid 分支。
  • 已有的 AskPass 真实助手测试(lithe-platform-tests askpass)和 remote 常驻会话测试(tests/persistence.rs)覆盖两个调用方。

验证

命令或操作 平台 结果
cargo fmt --all -- --check macOS arm64 通过
cargo clippy --workspace --all-targets --features lithe-remote/server -- -D warnings macOS arm64 通过
cargo clippy --target x86_64-unknown-linux-musl -p lithe-platform -p lithe-git-host -p lithe-platform-tests -p lithe-remote --features lithe-remote/server --all-targets -- -D warnings macOS arm64 交叉检查 通过(SO_PEERCRED 分支编译通过)
cargo clippy --target x86_64-pc-windows-msvc -p lithe-platform -p lithe-git-host -p lithe-platform-tests --all-targets -- -D warnings macOS arm64 交叉检查 通过(socket 模块不编译)
cargo test -p lithe-platform -p lithe-platform-tests -p lithe-git-host macOS arm64 通过,含新 socket 测试和 AskPass 真实助手测试
cargo test -p lithe-remote --features server macOS arm64 82 + 71 项通过(含 persistence 12 项)
node scripts/rust-core/verify-platform-tests.mjs macOS arm64 通过(52 个 crate)
node --test scripts/rust-core/*.test.mjs macOS arm64 16 项通过
node scripts/rust-core/affected-crates.mjs --base origin/main macOS arm64 Linux 跑整个 workspace;macOS 跑 platform、remote、platform-tests;Windows 跑 platform、platform-tests

未能执行的检查: Linux 上没有在本地实际运行 SO_PEERCRED 分支的测试,依靠 CI 的 Linux 车道;Windows 上只做了交叉 clippy。

git-host 的 AskPass 通道和 remote 服务端的常驻会话各有一份逐字相同的 peer_uid(Linux 用 SO_PEERCRED,其他 Unix 用 getpeereid)。现在合并到 lithe_platform::socket,两处都改用它;git-host 的 effective_uid 也一并移入。

remote 服务端多依赖一个只依赖 libc 的叶子 crate lithe-platform;remote 自己的 current_uid(getuid,也是 server.hello 的 userId)不变,行为不变。新增 lithe-platform-tests 的真实 socket 测试,两端都核对对端账户。
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review November 20, 2026 23:20
@xiaoyumuxi
xiaoyumuxi merged commit 9aa95a7 into main Nov 20, 2026
10 checks passed
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