Repository navigation
feat(remote): 远程连接安全(SSH 选项白名单、安装校验、会话令牌、日志轮转) - #134
Merged
Merged
Conversation
Lithe-IDEA#2231 R0 的 Core 安全部分,全部在 rust/crates/remote 内: - S06/S08/S11:SSH 跳点只接受白名单内的 11 个 -o 选项并校验取值, 强制 ForwardAgent=no、ForwardX11=no、ClearAllForwardings=yes, 且排在调用方选项之前(OpenSSH 首个值生效,命令行优先于配置文件)。 - S19:远端安装目录必须是当前用户私有、非符号链接且不可被他人写入; 已安装的 server 由远端自己计算 SHA-256 后才复用,不信任程序自报的哈希。 - S20:常驻会话签发 128 位随机令牌,重连必须出示;令牌不符时以 INVALID_SESSION_TOKEN 拒绝并关闭该连接,不影响已连接的客户端。 令牌常量时间比较,Debug 输出打码,不进日志。 - 空闲规则:有运行中的进程或打开的终端时会话不因空闲超时结束, 只在既无客户端也无保留进程时才计时。 - S21:守护进程日志 0600、不跟随符号链接,按 1 MiB 轮转为 .1; 启动时和运行中都会检查;异常消息不再回显原始报文内容。 - 重构:十六进制编码统一为 protocol::methods::encode_hex;测试辅助 Daemon 像应用一样记住令牌,新增 try_connect 和环境变量注入。
xiaoyumuxi
marked this pull request as ready for review
November 20, 2026 15:19
This was referenced Nov 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
关联 Issue
1lck/Lithe-IDEA#2231(远程开发完整设计)R0 中 Core 侧的连接安全部分;对应设计里的 S06、S08、S11、S19、S20、S21 和 §9.2 空闲规则。服务构建与 CI 产物校验在桌面仓库 Draft 1lck/Lithe-IDEA#2240。
类型
remotecrate 内部重构改动说明
所有改动都在
rust/crates/remote/内,没有新增 crate、没有挪动模块,client::bootstrap对 workspace 的导出(parse_probe、probe_command、HostDetails)保持不变。S06 / S08 / S11:SSH 选项白名单与强制选项(
client/transport.rs)SshHop.options只接受 11 个白名单选项(Port、User、IdentityFile、IdentitiesOnly、PreferredAuthentications、PubkeyAuthentication、BatchMode、ConnectTimeout、ServerAliveInterval、ServerAliveCountMax、ProxyJump),名称大小写不敏感,取值按类型校验(yes/no、计数、端口、用户名、路径、认证方法、跳点列表);其他选项返回TransportError::DisallowedOption。ForwardAgent=no、ForwardX11=no、ClearAllForwardings=yes,并放在调用方选项之前。OpenSSH 取第一个出现的值、命令行优先于~/.ssh/config,所以用户配置和调用方都改不掉这三项。S19:已安装服务的校验(
client/launch.rs)bin和版本目录:不能是符号链接、必须属于远端用户、组和其他用户不可写;不满足时以LITHE-INSTALL-UNSAFE <原因> <路径>拒绝启动,客户端得到LaunchError::Unsafe和UnsafeReason。sha256sum/shasum算出的 SHA-256 与要发送的产物完全一致时才复用;不再用--identity让程序自报哈希。远端没有哈希工具时不复用,每次连接都重新上传。umask 077下写入临时文件(0700)。有哈希工具时按远端算出的 SHA-256 校验;没有时只能核对字节数和刚上传文件的--identity,这只防传输损坏,因为文件是本次连接刚写入的私有文件。校验失败会删掉临时文件,成功后才改名就位。S20:会话令牌(
protocol/methods.rs、protocol/message.rs、server/session.rs、server/connection.rs、client/session.rs)--connect)从/dev/urandom生成 128 位令牌,hex 编码后在server.hello结果的session.sessionToken返回,整个会话期间不变;--stdio会话没有令牌。server.hello新增可选参数sessionToken。会话的第一个客户端无论出示什么都放行(新会话不可能知道令牌);之后的连接必须出示相同令牌,比较用常量时间。不符时返回新错误码INVALID_SESSION_TOKEN,先把错误送达客户端再关闭连接;已连接的客户端不受影响,也不会被顶掉。只有通过校验的连接才会替换当前客户端、应用它的idleTimeoutSeconds。SessionToken的Debug输出为SessionToken(<redacted>);拒绝时日志只写refused a client without this session's token,不写令牌。PERMISSION_DENIED等已有错误码。协议版本和features不变:字段都是可选的,桌面端目前还没有接入远程会话。§9.2 空闲规则(
server/daemon.rs、server/session.rs、server/process.rs、server/terminal.rs)server.shutdown。S21:守护进程日志(
server/daemon.rs、server/connection.rs)log/<name>.log,用O_NOFOLLOW打开并强制 0600,不跟随符号链接。<name>.log.1(覆盖上一份)。代理启动守护进程时检查一次;运行中在空闲轮询里检查,确认 stderr 仍指向同一个日志文件(比较 dev/ino)后改名,再用dup2把 stderr 指向新文件。轮转失败在每段连续失败里只记录一次。重构
protocol::methods::encode_hex(查表实现),server/mod.rs的identity()、server/upload.rs、client/bootstrap.rs和守护进程名都改用它,删掉三份重复的format!("{:02x}")循环。server/connection.rs的握手结果改为Handshake { Accepted, Failed, Refused }。serve新增on_attach回调,守护进程只在连接通过令牌校验后才替换当前客户端(原来在 accept 时就替换);--stdio传空回调。client/command.rs只在 Unix 测试里编译(#[cfg(all(test, unix))]),修复 Windows 目标上的死代码告警。tests/support::Daemon像应用一样记住令牌,新增with_environment(替代各测试自拼env HOME=…)和原样使用选项的try_connect。已知限制与后续
BatchMode、PubkeyAuthentication在白名单内,供非交互和密钥选择使用。rust/contracts/remote-workspace-protocol-v1.md还没跟上本 PR:缺少sessionToken、INVALID_SESSION_TOKEN和新的空闲规则,第 285 行仍写“按自报哈希复用”。按“改动只在remote包内”的要求,本 PR 没有改它,需要单独更新。验证
cargo test -p lithe-remote --all-features连跑 3 轮全部通过,每轮 126 项:单元 63、persistence 12、processes 8、run_directories 7、server 21、terminals 3、tunnels 6、watch_and_walk 5。新增测试覆盖令牌校验(缺失、错误、截断三种都被拒、原客户端不受影响、日志不含令牌、日志 0600)、进程和终端让空闲会话保持存活、日志轮转与符号链接拒绝、启动脚本拒绝不安全目录。测试结束后没有遗留lithe-remote-server进程。cargo test -p lithe-workspace:21 项通过。cargo clippy --all-targets --all-features -D warnings,以及--target x86_64-pc-windows-msvc的 Windows lint,均无告警。cargo fmt --all -- --check、scripts/rust-core/verify-rust-core-comments.sh remote、node scripts/logging/generate-log-events.mjs --check、node scripts/rust-core/verify-platform-tests.mjs、node --test scripts/rust-core/*.test.mjs(16 项)通过。write-stable-tests的verify-test-stability)通过。范围检查
rust/crates/remote/。