Skip to content

fix(ssh): deter agent logins to the owner's account - #42

Merged
tbhb-agent merged 11 commits into
mainfrom
fix/ssh-refuse-owner-account-for-agents
Nov 25, 2026
Merged

tbhb-agent merged 11 commits into
mainfrom
fix/ssh-refuse-owner-account-for-agents

Conversation

@tbhb-agent

@tbhb-agent tbhb-agent commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

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/config stopped them.

Behavior change

With two new keys under [data] in a host's chezmoi config, sshAgentRefuseUser and sshAgentRefuseHosts, ~/.ssh/config gets a Match host ... user ... exec block at its end. When the final user and host name match and ssh inherits a nonempty agent variable (CLAUDECODE, CODEX_HOME or CODEX_THREAD_ID), the exec command prints a refusal and kills its parent ssh with SIGKILL while ssh still reads the config. A :style suffix counts, and so does any user with a % token. A variable that a startup file sets without export doesn't count. Without an agent variable nothing changes, including for editors, scheduled jobs and ssh hops. tmux now also drops CODEX_THREAD_ID from a server an agent started.

Decisions

  • Chose a trigger on agent variables only over the terminal-profile allowlist because a refusal of the owner's own use blocks them, while a missed agent is security debt.
  • Chose killing ssh from Match exec over a refusing ProxyCommand because -o ProxyCommand=none and -J both skipped the ProxyCommand. IdentityFile /dev/null was weaker still, as -i adds a key.
  • Chose a plain Match at the end of the file over Match final, which forced a second pass on every host and broke an alias whose target has a RemoteCommand. Every Hostname and User line before it still counts.
  • Chose stopping the render on anything but a plain account name and plain host patterns over a config that ssh can't parse.
  • Chose per-host data over a host pattern in the template, keeping names out of this repo.

Needs human judgment

The marker list is CLAUDECODE, CODEX_HOME and CODEX_THREAD_ID. Codex sets CODEX_THREAD_ID in tool commands per its upstream source. Antigravity has no verified variable, and the guard leaves it uncovered. This head drops a TBHB_AGENT prefix rule: matching names needs env on PATH, and tmux drops 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 through ProxyCommand or CanonicalizeHostname, an odd SHELL, a later system Hostname or User rule, case-insensitive lookup, old tmux panes, hosts without the keys, 1Password checks without CODEX_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 from reuse_test, which scans local files that git ignores. The extended ssh_agent_guard_test renders the template with fake names and runs only ssh -G, which never connects. Under /bin/sh, /bin/bash and /bin/zsh with an agent variable it refuses user@host, -l, -o User=, a bare host name for the local user, the short name, a domain, upper case, a trailing dot, a :style suffix, -o Hostname=, -o ProxyCommand=none, -J, -i with -A, and a later Hostname line. It also refuses a % token user and a marker under a bash option that stops on unset names. Each marker alone refuses. With no marker it allows a scrubbed environment, a hop with SSH_CONNECTION set, and the terminal profile. A TBHB_AGENT_ variable, a multi-line value and a marker set without export in a startup file pass too. Other users on the guarded host pass. The new tmux_agent_markers_test starts a throwaway tmux server 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 new tmux line, 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

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
@tbhb-agent
tbhb-agent requested a review from tbhb as a code owner November 24, 2026 23:13
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
@tbhb-agent tbhb-agent changed the title fix(ssh): stop agent logins to the owner's account fix(ssh): deter agent logins to the owner's account Nov 24, 2026
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
@tbhb-agent
tbhb-agent merged commit fd21d3c into main Nov 25, 2026
5 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