Repository navigation
fix(ssh): deter agent logins to the owner's account - #42
Merged
Merged
Conversation
An opt-in Match block, keyed on two per-host chezmoi data values, kills the ssh process while it reads the config when the target user and host match and the session is not the owner's terminal profile. Running at config-read time, it cannot be skipped with -o, -i, -J or agent forwarding. Generated-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-opus-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-24T22:54:44Z;subagent=sha-256-64:2b672fff16e1792d;surface=cli Assisted-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-sonnet-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-15T17:46:19Z;surface=cli
Review found that Hostname and User lines after the guard were not seen, which could skip the guard or refuse another user. Match final evaluates after every line. Tests cover later lines, a trailing dot and a local user named like the other user, and the docs state the deliberate-evasion gaps. Generated-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-opus-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-24T22:54:44Z;subagent=sha-256-64:2b672fff16e1792d;surface=cli Assisted-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-sonnet-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-15T17:46:19Z;surface=cli
The delta review found a User line that first matches on the final pass, after the guard, still escaped it, and an inherited SIGTERM ignore let ssh continue. The block now sits last and sends SIGKILL. tmux drops CODEX_THREAD_ID with the other harness variables, so an owner pane from a Codex-started server keeps the terminal profile. The docs no longer suggest a trailing wildcard for every pattern. Generated-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-opus-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-24T22:54:44Z;subagent=sha-256-64:2b672fff16e1792d;surface=cli Assisted-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-sonnet-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-15T17:46:19Z;surface=cli
sshd drops a :style suffix from the user name, which let user:x skip the guard while still logging in as the guarded user. The Match also takes user:*. The docs state the OpenSSH 8.0 floor and the second config pass. Generated-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-opus-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-24T22:54:44Z;subagent=sha-256-64:2b672fff16e1792d;surface=cli Assisted-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-sonnet-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-15T17:46:19Z;surface=cli
Coordination ruled that refusing the owner's own non-terminal ssh is a block, while a missed agent is security debt. The guard now refuses only when CLAUDECODE, CODEX_HOME, CODEX_THREAD_ID or a variable named TBHB_AGENT* is set. Editors, scheduled jobs and hops from another ssh session pass. The docs call it a deterrent and list the unfixed gaps. Generated-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-opus-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-24T22:54:44Z;subagent=sha-256-64:2b672fff16e1792d;surface=cli Assisted-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-sonnet-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-15T17:46:19Z;surface=cli
Review found the prefix rule refused the owner: env output matched lines inside multi-line values, and tmux cannot drop variables by prefix, so the owner's panes on an agent-started server inherited one. The guard now checks only CLAUDECODE, CODEX_HOME and CODEX_THREAD_ID. A new test starts a throwaway tmux server with those variables and checks the global environment and every later kind of pane. Generated-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-opus-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-24T22:54:44Z;subagent=sha-256-64:2b672fff16e1792d;surface=cli Assisted-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-sonnet-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-15T17:46:19Z;surface=cli
Match user sees a % token before expansion, so a User of %u, %n or %k
skipped the guard. Refuse any user with a % token.
The exec read markers in SHELL after its startup files ran, so an
unexported assignment there refused the owner, and bash with nounset
stopped before the refusal. A /bin/sh -c now reads them with ${VAR-},
and a failed message no longer skips the kill.
Generated-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-opus-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-24T22:54:44Z;subagent=sha-256-64:2b672fff16e1792d;surface=cli
Assisted-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-sonnet-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-15T17:46:19Z;surface=cli
Match final made ssh read the config twice for every host, so a Host block for an alias target applied too, and a RemoteCommand there broke an alias run with a command. The block sits at the end of the file, so a plain Match host still sees every Hostname and User line. Empty host entries rendered Match host "", which broke every ssh. They are now dropped, and a quote or space in the keys stops the render. A failed refusal message no longer skips the kill under errexit. cspell knows the new test words. Generated-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-opus-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-24T22:54:44Z;subagent=sha-256-64:2b672fff16e1792d;surface=cli Assisted-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-sonnet-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-15T17:46:19Z;surface=cli
A quote, backslash, leading # or control character in a host entry still rendered a config that broke every ssh, and a user such as owner,ops or * refused the ops account. The render now stops unless the account is one plain name and each host entry uses only letters, digits, . _ - * and ?. The account is checked even with no host entries. Generated-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-opus-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-24T22:54:44Z;subagent=sha-256-64:2b672fff16e1792d;surface=cli Assisted-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-sonnet-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-15T17:46:19Z;surface=cli
With sshAgentRefuseUser set to ops or agent, the guard refused the account that agents must use. The render now stops on either name. The test skips its ssh cases when it runs as ops or agent, since it guards the local account. Record two data debts: a 1,023-character host pattern and wrong-typed host data. Generated-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-opus-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-24T22:54:44Z;subagent=sha-256-64:2b672fff16e1792d;surface=cli Assisted-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-sonnet-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-15T17:46:19Z;surface=cli
Keeps both new sh_test targets in BUILD.bazel: tmux_agent_markers_test from this branch and dotfiles_drift_test from main. Generated-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-opus-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-24T22:54:44Z;subagent=sha-256-64:2b672fff16e1792d;surface=cli Assisted-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-sonnet-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-15T17:46:19Z;surface=cli
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.
Why
The owner's account on the shared Linux hosts is for the owner only. Agent sessions on a laptop logged in to it with the owner's key about a hundred times in one day. Nothing in
~/.ssh/configstopped them.Behavior change
With two new keys under
[data]in a host's chezmoi config,sshAgentRefuseUserandsshAgentRefuseHosts,~/.ssh/configgets aMatch host ... user ... execblock at its end. When the final user and host name match andsshinherits a nonempty agent variable (CLAUDECODE,CODEX_HOMEorCODEX_THREAD_ID), theexeccommand prints a refusal and kills its parentsshwith SIGKILL whilesshstill reads the config. A:stylesuffix counts, and so does any user with a%token. A variable that a startup file sets withoutexportdoesn't count. Without an agent variable nothing changes, including for editors, scheduled jobs andsshhops.tmuxnow also dropsCODEX_THREAD_IDfrom a server an agent started.Decisions
sshfromMatch execover a refusingProxyCommandbecause-o ProxyCommand=noneand-Jboth skipped theProxyCommand.IdentityFile /dev/nullwas weaker still, as-iadds a key.Matchat the end of the file overMatch final, which forced a second pass on every host and broke an alias whose target has aRemoteCommand. EveryHostnameandUserline before it still counts.sshcan't parse.Needs human judgment
The marker list is
CLAUDECODE,CODEX_HOMEandCODEX_THREAD_ID. Codex setsCODEX_THREAD_IDin tool commands per its upstream source. Antigravity has no verified variable, and the guard leaves it uncovered. This head drops aTBHB_AGENTprefix rule: matching names needsenvonPATH, andtmuxdrops variables only by exact name.Risk
This is only a deterrent. The control is agents not holding the owner's key, which comes with moving sessions into the agent account. Security debt: an unmarked agent, an empty marker,
-F, an IP address, another name throughProxyCommandorCanonicalizeHostname, an oddSHELL, a later systemHostnameorUserrule, case-insensitive lookup, oldtmuxpanes, hosts without the keys, 1Password checks withoutCODEX_THREAD_ID, a 1,023-character pattern and wrong-typed host data. A marker that a startup file exports refuses the owner too.Verification
bazelisk test //...passes locally, apart fromreuse_test, which scans local files that git ignores. The extendedssh_agent_guard_testrenders the template with fake names and runs onlyssh -G, which never connects. Under/bin/sh,/bin/bashand/bin/zshwith an agent variable it refusesuser@host,-l,-o User=, a bare host name for the local user, the short name, a domain, upper case, a trailing dot, a:stylesuffix,-o Hostname=,-o ProxyCommand=none,-J,-iwith-A, and a laterHostnameline. It also refuses a%token user and a marker under abashoption that stops on unset names. Each marker alone refuses. With no marker it allows a scrubbed environment, a hop withSSH_CONNECTIONset, and the terminal profile. ATBHB_AGENT_variable, a multi-line value and a marker set withoutexportin a startup file pass too. Other users on the guarded host pass. The newtmux_agent_markers_teststarts a throwawaytmuxserver with all three variables and checks the global environment and each later pane:new-session,new-window,split-window, and both from an attached client. Without the newtmuxline, all fail.Opened-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-sonnet-5-5;session=sha-256-64:5540cd94a5c67150;surface=cli
Generated-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-opus-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-24T22:54:44Z;subagent=sha-256-64:2b672fff16e1792d;surface=cli
Assisted-by: LLM effort=medium;harness=claude-code:2.1.331;model=anthropic:claude-sonnet-5-5;session=sha-256-64:5540cd94a5c67150;started_at=2026-11-15T17:46:19Z;surface=cli