Repository navigation
feat(remote): ssh 子进程只保留它自己需要的环境变量(S26,#2231) - #148
Merged
Merged
Conversation
OpenSSH 会把 SendEnv 模式匹配到的本地变量发给远程主机,很多系统自带 SendEnv LANG LC_*。ssh 会合并命令行和所有配置文件里的 SendEnv 模式,命令行没有办法撤回,所以唯一可靠的做法是让 ssh 子进程只继承白名单里的变量。 TransportChain::restrict_environment:第一跳是 SSH 的链清空继承环境,只保留 ssh 本身需要的变量(HOME、USER、LOGNAME、SHELL、PATH、TMPDIR、SSH_AUTH_SOCK、SSH_SK_PROVIDER、KRB5CCNAME、KRB5_CONFIG、XDG_RUNTIME_DIR 和 Windows 系统变量,Windows 上不区分大小写);本地链和第一跳是容器的链不变,容器引擎只传 -e 列出的变量。远程服务连接(client::session::spawn)、连接探测(workspace probe)和会话连接(workspace sessions)三处在设置 AskPass 变量之前调用,AskPass 变量照常传给 ssh。 测试:白名单与大小写规则、只有第一跳是 SSH 的链才收紧、探测和会话连接两条路径用假 ssh 记录实际收到的环境(不含 CARGO_PKG_NAME 和 LANG,PATH 不变,SSH_ASKPASS_REQUIRE=force)。去掉探测处的调用后该测试失败。
1 task done
xiaoyumuxi
marked this pull request as ready for review
November 21, 2026 03:51
xiaoyumuxi
added a commit
that referenced
this pull request
Nov 21, 2026
七个文件冲突,都是两边各自在同一列表末尾追加条目,按并集保留: - protocol::ErrorCode 与 application-boundary.md:保留 forward_target_not_confirmed、forward_limit_reached,再加 main 的 remote_server_invalid;connection_closed 用本分支补全的说明。 - RemoteCommand、命令名映射、分发器与 rust-core-api.md 方法表:remote.startForward、remote.stopForward、remote.forwards 之后接 remote.connections、remote.editConnections。 - workspace lib.rs:connections 与 forwards 两个模块都保留。 - sessions.rs 的 import:同时引入 ArtifactError 与 ForwardSet。 端口转发经已有连接的服务端协议完成,不另起 ssh 进程,与 #148 的环境变量白名单不交叉。
This was referenced Nov 21, 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(远程开发完整设计),安全项 S26:Lithe 启动的
ssh只继承它自己需要的本地环境变量,不把本地语言、代理和各种工具的凭据变量带到远程主机。类型
ssh时清空继承环境,只保留白名单变量改动说明
问题
OpenSSH 会把
SendEnv模式匹配到的本地变量发给远程主机,很多系统的/etc/ssh/ssh_config自带SendEnv LANG LC_*,用户配置里也可能写了更宽的模式。ssh 会把命令行和所有配置文件里的SendEnv模式合并起来,-o SendEnv=-*之类的写法撤不回配置文件里已有的模式。所以唯一可靠的做法是:ssh 子进程从一开始就只拿到白名单里的变量。TransportChain::restrict_environment(crates/remote/src/client/transport.rs)env_clear(),再只放回KEPT_SSH_VARIABLES里的变量:HOME、USER、LOGNAME、SHELL、PATH、TMPDIR、SSH_AUTH_SOCK、SSH_SK_PROVIDER、KRB5CCNAME、KRB5_CONFIG、XDG_RUNTIME_DIR。SystemRoot、SystemDrive、windir、ComSpec、PATHEXT、TEMP、TMP、USERPROFILE、HOMEDRIVE、HOMEPATH、USERNAME、USERDOMAIN、APPDATA、LOCALAPPDATA、ProgramData、ProgramFiles、ProgramFiles(x86)、ProgramW6432、CommonProgramFiles。Windows 的变量名不区分大小写,匹配时也不区分(cfg!(windows)决定,函数本身接受参数,两种模式在所有主机上都有测试)。LANG、LC_*、代理变量、GIT_*、云服务和其他工具的凭据变量。-e写明的变量传进容器,本地环境本来就不会过去。Command上设置的变量,所以调用方要在它之后再设置自己的变量(AskPass)。三处启动 ssh 的地方
都在设置 AskPass 变量之前调用
restrict_environment,AskPass 变量照常传给 ssh:crates/remote/src/client/session.rs的spawn:远程服务连接。crates/workspace/src/probe.rs:连接探测。crates/workspace/src/sessions.rs:会话连接(remote.connect)。仓库里另一处
Command::new在client/command.rs,只给端到端测试运行传输链用,不在生产路径上。协议文档(
rust/contracts/remote-workspace-protocol-v1.md)“Starting the server” 一节加一段:参考客户端启动
ssh时只保留 OpenSSH 需要的变量,再加上自己的 AskPass 变量;其他客户端也应这样做。不在
crates/remote里的改动crates/workspace/src/probe.rs、crates/workspace/src/sessions.rs:这两个命令入口自己启动 ssh,只加一行调用。crates/workspace/src/probe/tests.rs:命令级的端到端测试。rust/contracts/remote-workspace-protocol-v1.md:上面那段文档。已知限制
SendEnv *时,AskPass 变量(含一次性令牌)仍会发给主机;主机多数情况下会因AcceptEnv拒收。令牌只能用来连接本机上的 AskPass socket,而每一跳都强制ClearAllForwardings=yes,远程主机连不到这个 socket。LANG/LC_*后,ssh 输出的横幅里的非 ASCII 字符会被转义显示;远端服务自己用远程用户的登录环境,不受影响。ProxyCommand如果依赖白名单之外的本地变量,将拿不到这些变量。与其他 PR 的关系
从 origin/main(7c1cd55)开分支,不叠在 #145/#146/#147 上。用
git merge-tree检查过,与三者的当前 head 都能干净合并;三个 PR 都没有新增启动 ssh 的地方(#145 只用TransportChain校验命令行,#147 的spawn是线程)。兼容性
桌面端目前还没有调用
remote.connect;探测和连接的请求、返回值、错误码都没有变化。本 PR 不改插件契约、能力版本或插件函数协议,不需要更新协议更新记录,也不需要运行sdk-e2e.mjs。测试
crates/remote/src/client/transport.rs新增 3 项单元测试:ssh_keeps_only_the_variables_it_needs:保留HOME、PATH、SSH_AUTH_SOCK;丢弃LANG、LC_ALL、HTTPS_PROXY、AWS_SECRET_ACCESS_KEY、GIT_ASKPASS。windows_variable_names_match_without_regard_to_case:不区分大小写时保留Path、SYSTEMROOT;区分大小写时都不保留。only_a_chain_that_starts_with_ssh_restricts_the_environment:[ssh]、[ssh, container]丢掉调用方之前设置的变量;本地链、[container]、[container, ssh]保留。crates/workspace/src/probe/tests.rs新增the_ssh_client_starts_with_only_the_variables_it_needs(Unix):替身 ssh 把自己收到的环境写进文件,探测和会话连接两条路径都断言没有CARGO_PKG_NAME、LANG,PATH与测试进程相同,SSH_ASKPASS_REQUIRE=force。断言失败时只打印变量名,不打印值。probe.rs里的调用后,这项测试在探测那一半失败;恢复后通过。本地验证(macOS arm64,Rust 1.100.0),包列表取自
affected-crates.mjs --base origin/main:cargo fmt --all -- --checknode scripts/logging/generate-log-events.mjs --checkscripts/rust-core/verify-rust-core-comments.sh(13 个受影响 crate):通过;第一次运行时公开文档链接了私有常量,改成文字说明后对 remote、workspace 复跑,无警告node scripts/rust-core/verify-platform-tests.mjscargo clippy --locked <受影响的 13 个包> --all-targets --all-features -- -D warnings--target x86_64-pc-windows-msvc(默认特性)cargo test --locked <受影响的包,lithe-git 除外> --features lithe-remote/server:39 组 1475 项全部通过。lithe-git 没有改动,clippy 已编译它的测试,测试交给 CI 运行。