Skip to content

fix(runner): pin SSH grant re-arming to the task directory and stop agent socket recursion - #1390

Merged
zhaozhaozz merged 2 commits into
agent-CI-Infra:mainfrom
zhaozhaozz:feat/issue-1379-remote-ssh
Nov 25, 2026
Merged

zhaozhaozz merged 2 commits into
agent-CI-Infra:mainfrom
zhaozhaozz:feat/issue-1379-remote-ssh

Conversation

@zhaozhaozz

@zhaozhaozz zhaozhaozz commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #1379. Follow-up to #1389.

#1389 merged from the merge queue at 8ebaae45 (merge commit d0ed9f7e). That head was enqueued at 13:09:37, after the review's must-fix finding F1 (#1389 (comment)) and before the fix was pushed (5b0bf4cf, 13:10). main therefore still has F1. This PR carries that fix unchanged; its tree is identical to 5b0bf4cf.

F1: cross-task SSH grant transfer through agent/state.json

The pid of a running session comes from its task's agent/state.json, which every process of a project computer can write (all share UID 10001). SshAgentBroker.rearm() accepted any agent the daemon had registered. A task could therefore point another task's state.json at its own agent and receive that run's grant on the next workspace_agent_start / workspace_agent_continue. The new grant includes that run's role and trusted-checkout targets.

Fix: the daemon records the task directory an agent was started for, i.e. the workspace_id of the starting call as validated by the manager (runner/daemon.mjs takeSsh). SshAgentBroker.register() stores it. rearm(pid, payload, workspace) refuses unless the call bringing the new grant names the same workspace (runner/ssh-agent.mjs). A non-agent pid or a missing workspace is refused as before. docs/design/agent-and-harness-runtime.md names the starting task directory as what pins the transfer.

Tests

  • test/ut/runner/ssh-agent.test.mjs: a rearm for another workspace (and for none) leaves the agent on its old grant; a rearm for its own workspace moves it.
  • test/ut/manager/ssh-grant.test.mjs: real daemon through manager/server.mjs. Task B's state.json names task A's running agent; B's workspace_agent_continue with B's grant leaves A's agent on A's grant, and A's own continue moves it.
  • Both fail on main (d0ed9f7e = 8ebaae45): with the previous runner/ssh-agent.mjs and runner/daemon.mjs, B's grant ends up on A's agent.

Agent socket recursion (second commit, c6018c94)

The third merge-queue run timed out in ssh-access (900 s) with RangeError: Maximum call stack size exceeded at runner/ssh-agent.mjs:266. After every pass, serve() called pump() again. With a partial message buffered (at least the 4-byte length, less than the body), pump() returned without awaiting anything, so the call recursed until the stack overflowed. That happened outside its try block, as an unhandled rejection, which ends the runner daemon. Any process on the computer could send half a message to ssh-agent.sock and take the daemon down. A client whose request arrived in two chunks hung; on CI that client was OpenSSH, so the set timed out. This bug is on main since #1388.

The loop already reads bytes that arrive during its awaits, so the re-entry is gone. A partial message waits for its next chunk, and a failing pump() closes only its own connection. Regression test in test/ut/runner/ssh-agent.test.mjs: a message sent in two pieces is answered, and a client that leaves after half a message does not stop the socket from serving the next one. On the previous code it reproduces the RangeError.

Verification

  • b965712f (F1 only, same tree as 5b0bf4cf): npm run check exit 0; BOT_TEST_JOBS=4 npm test exit 0, 351 test sets, 2841 tests, 2841 passed.
  • c6018c94: npm run check exit 0; BOT_TEST_JOBS=4 npm test exit 1, 351 test sets, 2842 tests, 2841 passed. All SSH sets pass. The one failure is distributed-nodes (test/st/src/cluster/distributed-nodes.test.mjs:146, from [分布式部署] 支持总控与多 Linux Docker 计算节点分离:ClusterManager、node-agent 与节点管理 #1380, unchanged here): node_agent_connect_timeout under 4-way parallel load, as in earlier local runs; it passes in CI and passes alone on this head (node test/framework/test-sets.mjs --jobs 1 test/st/src/cluster/distributed-nodes.test.mjs).

The pid of a running session comes from its task's agent/state.json,
which every process of the computer can write. rearm() accepted any
agent the daemon had registered, so a task could point another task's
state.json at its own agent and receive that run's grant on the next
start or continue. The daemon now records the workspace an agent was
started for and moves an agent only for a call from the same workspace.

@agent-ci-infra-bot agent-ci-infra-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit: b965712
Coverage: PR #1390; 5 changed file(s)

Comment thread runner/daemon.mjs
onAttach: pid => { if (taken.kind === 'agent') sshBroker.rearm(pid, taken.grant); },
onSpawn: pid => { onSpawn?.(pid); if (sshBroker.register(pid, taken.grant, taken.kind, taken.workspace)) taken.spawned.push(pid); },
// The pid of a running session comes from a task-writable file: only an agent started for this workspace moves.
onAttach: pid => { if (taken.kind === 'agent') sshBroker.rearm(pid, taken.grant, taken.workspace); },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

F2(suggestion,沿用 #1389 round-2 的建议,仍成立):rearm() 的布尔结果在 runner/daemon.mjs:202 被丢弃,runner/agent.mjs:275 与 :420 也只用 try/catch 包住调用。因此一次被拒绝的 re-arm 不会留下任何记录:workspace_agent_continue 仍返回 {written:true}、workspace_agent_start 仍返回 {started:true},该 run 继续用旧的 grant 运行。

本 PR 之后,“re-arm 被拒绝”正是跨任务 agent/state.json 这一唯一攻击/异常路径的预期结果(正常续接同任务目录一定匹配),也仍可能在别的边界情形(例如 pid 已失效)发生,但控制面只能从之后某次 SSH 检查的 ssh_target_denied 间接推广,daemon 侧无法区分“没发生”与“被拒绝”。安全上没有影响(签名/检查每次都由控制面按 run 令牌、角色与目标重新授权)。

建议:把结果带出来,至少记一次,例如

onAttach: pid => {
  if (taken.kind === 'agent' && !sshBroker.rearm(pid, taken.grant, taken.workspace)) {
    console.error(`[ssh-agent] rearm refused: pid=${pid} workspace=${taken.workspace || 'none'}`);
  }
},

ssh-gate/daemon 的日志本来就会进容器日志,不需要新增持久化字段;不记录 grant secret。非阻断。

@agent-ci-infra-bot

agent-ci-infra-bot Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

第 2 次检视,更新于 2026-11-25 15:14 UTC,检视提交 c6018c9

round 2 检视 c6018c94(base d0ed9f7e,2 个提交 / 5 个文件)。上一个 head b965712f 的结论不变:没有 must-fix;唯一未结项仍是 F2(非阻断,作者已说明另开跟进)。

状态对照上一轮

  • F1(跨任务 SSH grant 转移)fixed:本 head 未改动 runner/daemon.mjs / runner/ssh-agent.mjs 的 workspace pin 部分,我复核后仍与 round 1 的结论一致(pin 来自 manager 校验过的 workspace_id,任务侧只能写 agent/state.json,不能伪造 workspace)。
  • F2(拒绝 re-arm 无任何记录)open:仍在 runner/daemon.mjs:202,作者在 评论 7164346885 明确留到后续 PR;我保留原线程为 open,不阻断合并。

第二个提交 c6018c94(ssh-agent.sock 半包递归)——修复正确

  • 机制判断正确:旧 serve() 在每轮 pump() 之后又 if (!raw && buffer.length >= 4) void pump()。当缓冲里只有长度字段、正文未到齐时,while 里的 buffer.length < 4 + length 直接 break,整趟没有 await,于是同步递归到底 → RangeError;而这一句在 try/finally 之外,busy 已经复位,异常以未处理拒绝的形式冒出,daemon 进程随之退出(我在 d0ed9f7e 的 runner/ssh-agent.mjs 上复现到同一行::266)。
  • 去掉重入不会丢消息:pump() 的“检查 buffer 是否还有整包 → finally { busy = false }”之间没有 await,数据回调不可能插进来;等待期间到达的字节由同一个 while 循环在恢复后继续消费;剩下的只会是“不足 4 字节”或“正文未到齐”,本来就只能等下一块数据,而 socket.on('data') 再次调用 pump()。因此新写法既保持活性,也不会漏掉一次数据事件。
  • 失败隔离:pump().catch(() => socket.destroy()) 让异常只关闭自己的连接,符合注释里“a failing pump only closes its own connection”。
  • 回归测试真实有效:新用例先发 9 字节再补完(覆盖“分片到达被挂住”),再让一个客户端发半包后离开,最后要求 socket 还能服务下一个连接。我把 runner/ssh-agent.mjs 换回 b965712f 的版本(两 head 之间只差这个提交)后运行同一套件,确实得到 RangeError: Maximum call stack size exceeded,且进程 120 s 内不退出——说明这不是镜像实现的断言。

我实际跑过的验证(本 head c6018c94)

  • npm run check → 通过(Runtime JavaScript syntax checks passed (850 files))。
  • node --test test/ut/runner/*.test.mjs test/ut/manager/ssh-grant.test.mjs → 112/112 通过(runner 套件含 11 个 ssh-agent 用例)。
  • node --test test/st/src/projects/ssh-access.test.mjs → 2/2 通过,5.8 s;这正是合并队列里 900 s 超时的那个集合,现在不再挂住。
  • 反向验证:如上,旧 ssh-agent.mjs 上新用例报 RangeError 并挂住。
  • 文档:docs/design/agent-and-harness-runtime.md 没有描述分帧/重入循环(只描述套接字、SO_PEERCRED 与授权),第二个提交不需要文档同步;docs/features/ 亦不受影响。第二个提交已写进 PR 标题与正文,属于同一 PR 内解释清楚的扩展,不是无说明的范围扩张。
  • CI(我检视时):本 head 上 CodeQL、Analyze(3 个)与四个镜像 job 成功;两个 unit shard 与 e2e 仍在运行,我没有等待结束。

一点措辞提醒(不是代码缺陷,不要求修改)

PR 正文说“任何进程发半包就能让 daemon 退出”属实,但项目电脑上的任务与 daemon 同为 UID 10001,本来就能直接 kill 它,所以这里不是权限边界,真正的损害是可用性——而且触发方是合法的 OpenSSH 分片写入(也就是 CI 里 ssh-access 超时的原因)。按这个定位理解修复即可。

Reviewed commit: c6018c9

之前的行内意见仍未解决 1 条:runner/daemon.mjs:202。

Reviewed commit: c6018c9
Coverage: PR #1390; 5 changed file(s)

@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

Thanks. The only failing check, e2e, is test/e2e/console/admin-area.spec.mjs:16: the phone account menu showed no links. This PR does not touch the console (it changes runner/daemon.mjs, runner/ssh-agent.mjs, two tests and one doc sentence), and the same spec already has a retry on main (6c0c361a); I re-ran the failed job. I am leaving F2, a log line when a re-arm is refused, out of this PR so that the F1 fix reaches main without another round, and will follow it up separately.

@zhaozhaozz
zhaozhaozz added this pull request to the merge queue Nov 25, 2026
@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

Reviewed head b965712f2724a36366781de4a65fd96461304dd2 (base d0ed9f7e). No blocking findings; enqueued into the merge queue with expectedHeadOid=b965712f2724a36366781de4a65fd96461304dd2.

  • Independently re-checked the diff: the daemon takes the task directory from the manager-validated workspace_id, register() records it, and rearm() moves an agent only for the same workspace (a missing workspace is refused too). The value cannot be forged by a task: Workspace.call/deliver always set workspace_id: task.workspace_id, and the runner resolves tasks/<workspace_id> as the same directory whose agent/state.json the attach reads.
  • docs/design/agent-and-harness-runtime.md matches the implementation.
  • Regression coverage: both touched suites pass on this head (test/ut/runner/ssh-agent.test.mjs, test/ut/manager/ssh-grant.test.mjs); applying only these test files to d0ed9f7e makes both fail, so they catch the old behaviour.
  • The unresolved inline thread is the non-blocking F2 diagnostic suggestion from fix(projects): address SSH follow-ups from the #1388 review #1389, which the author explicitly deferred; it has no security impact because every signature is re-authorized by the control plane.
  • The earlier e2e failure was an unrelated console spec; the rerun passed, and all six required checks are green on this head. No open code-scanning alerts on the PR ref.

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 25, 2026
@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

The merge queue removed this PR at 14:12 (failed_checks on merge group 98ff94d8): unit shard 2/2 failed in acp-harness (test/st/src/harness/acp-harness.test.mjs:394, "ACP negotiates, streams, …", acp_process_exited). This PR does not touch the ACP harness; it changes the daemon's SSH agent registration only. The same set passed in this PR's own checks on b965712f and in the local full run of this tree (2841/2841). main has not moved since (d0ed9f7e), so the head is unchanged and still ready to be re-queued; it carries the F1 security fix that main is missing.

@agent-ci-infra-bot

Copy link
Copy Markdown
Contributor

I read the merge-group failure from the run itself, and it matches your reading:

  • Checks on merge group 98ff94d8 (run 45630176336): unit shard 1 / 2 and e2e passed; unit shard 2 / 2 ended with # fail 1 out of 1461 tests and # failed sets: acp-harness, so the unit gate job then failed on that artifact. The failure is confined to the ACP harness set.
  • On b965712f the same unit shard 2 / 2 check (139251558297) is success (13:41:51Z), and the 5 changed files are runner/daemon.mjs, runner/ssh-agent.mjs, the two SSH grant tests and one sentence in docs/design/agent-and-harness-runtime.md — nothing under test/st/src/harness/ or src/harness/.
  • Base is still d0ed9f7e, head b965712f unchanged, mergeable: clean; the review's only open item remains the non-blocking F2.

On re-queueing: this project's auto-merge policy lets me merge or re-queue only on an explicit request from someone with write access to the repository, so I do not re-add it on my own even though it is reviewed and green. Either re-add it with expectedHeadOid=b965712f2724a36366781de4a65fd96461304dd2 yourself, or say here that I should re-queue it — I will do it and cite that comment as the request.

@zhaozhaozz
zhaozhaozz added this pull request to the merge queue Nov 25, 2026
@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

Re-enqueued the same reviewed head b965712f2724a36366781de4a65fd96461304dd2 (expectedHeadOid set to it; new entry MQE_lQDO… is QUEUED, not UNMERGEABLE).

The previous queue attempt was removed as failed_checks: the merge-group commit 98ff94d8 failed only unit shard 2 / 2 on test/st/src/harness/acp-harness.test.mjs:394 (acp_process_exited), while the PR head's own unit, e2e, four image builds and CodeQL all pass and its diff (runner SSH files, two SSH tests, one design-doc sentence) touches no ACP code. Treated as an unrelated flaky failure; no code change requested. F2 remains non-blocking.

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 25, 2026
@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

Removed from the queue again at 14:29 (failed_checks, merge group c25603f7): this time e2e failed in test/e2e/console/admin-area.spec.mjs:45. The phone account menu showed both links, then closed before the click (element is not visible until the 90 s timeout). This is the async shell-sync race on the account menu. The same spec also failed in the merge queue run for #1375 (run 45549096151, 04:46 UTC), before any of the #1379 changes existed, and unit (both shards) passed this time. This PR touches neither the console nor its tests. Head b965712f is unchanged and main is still d0ed9f7e.

@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

Re-enqueued the same reviewed head b965712f2724a36366781de4a65fd96461304dd2 again (expectedHeadOid set to it; the new entry is not UNMERGEABLE).

The second queue removal was failed_checks on merge group c25603f7 (e2e only; unit both shards and all four image builds passed). The single failure was test/e2e/console/admin-area.spec.mjs:45: the phone account menu resolved #console-account-settings, then became invisible before the click and timed out. This is the pre-existing account-menu shell-sync race, tracked in #1391; it is unrelated to this PR's SSH-grant diff, and the same spec had already failed in an earlier unrelated merge group. main is still d0ed9f7e, so replaying the same head is safe.

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 25, 2026
serve() called pump() again after every pass. With a partial message
buffered (at least the length field, less than the body) pump() returns
without awaiting anything and the call recursed until the stack
overflowed, outside the try block: an unhandled rejection that ends the
runner daemon. Any process of the computer could send half a message to
ssh-agent.sock and take the daemon down; a client whose request arrived
in two chunks hung (seen in CI as the ssh-access set timing out).

The loop already reads bytes that arrive during its awaits, so the
re-entry is removed; a partial message waits for its next chunk, and a
failing pump only closes its own connection.
@zhaozhaozz zhaozhaozz changed the title fix(runner): re-arm an SSH grant only for the agent's own task directory fix(runner): pin SSH grant re-arming to the task directory and stop agent socket recursion Nov 25, 2026
@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

The third queue run (14:36, merge group bb7ff057) found a real bug, not a flake: ssh-access timed out after 900 s with RangeError: Maximum call stack size exceeded at runner/ssh-agent.mjs:266. The agent socket's pump() re-entered itself on a partial message without awaiting, so half a message to ssh-agent.sock crashes the daemon. This has been on main since #1388. The fix and a regression test are in c6018c94, pushed on this PR; details in the description. npm run check passes; the full npm test is running.

@zhaozhaozz
zhaozhaozz added this pull request to the merge queue Nov 25, 2026
@zhaozhaozz

Copy link
Copy Markdown
Collaborator Author

Reviewed head c6018c942e23d310af45b5479b3945391e8af351 (on top of the previously reviewed b965712f, base d0ed9f7e). No blocking findings; all six required checks are green, and the PR is enqueued with expectedHeadOid=c6018c942e23d310af45b5479b3945391e8af351 (entry MQE_lQDO…RQb-o, state QUEUED, not UNMERGEABLE; one unrelated PR is ahead in the queue).

  • The incremental commit reverts nothing of F1: a re-arm still moves an agent only within its starting task directory, and the manager-supplied workspace_id remains the only source for that pin.
  • The new commit fixes a real daemon crash: serve() re-entered pump() after each pass, so a buffered partial message (length field present, body missing) recursed synchronously until RangeError: Maximum call stack size exceeded outside the try block, becoming an unhandled rejection that ends the runner daemon; the third merge-queue run's ssh-access timeout came from this. Removing the re-entry is correct: the loop consumes bytes appended during its awaits, and a partial message is resumed by the next data event; pump() rejections now destroy only the affected connection. Pipelined frames, the tunnel handoff and the message-size cap are unchanged.
  • Verification on this head: node --test test/ut/runner/ssh-agent.test.mjs test/ut/manager/ssh-grant.test.mjs passes 13/13, and node --check runner/ssh-agent.mjs passes. Applying only the updated test file to b965712f reproduces the RangeError (test-level failure), so the regression test catches the old code.
  • CI on this head: unit (both shards), e2e, all four image builds and CodeQL pass; no open code-scanning alerts. The only unresolved inline thread is the non-blocking F2 diagnostic suggestion, which the author already deferred.

Merged via the queue into agent-CI-Infra:main with commit 5298075 Nov 25, 2026
12 checks passed
@zhaozhaozz
zhaozhaozz deleted the feat/issue-1379-remote-ssh branch November 25, 2026 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant