Skip to content

feat(replies): add verified sender context and optional return-route diagnostics - #86

Merged
abruption merged 5 commits into
mainfrom
feat/21-sender-context
Oct 3, 2026
Merged

abruption merged 5 commits into
mainfrom
feat/21-sender-context

Conversation

@abruption

@abruption abruption commented Oct 2, 2026 •

Copy link
Copy Markdown
Owner

Summary

Closes #21.

Retargeted to main after #84 merged (b2575da). The five #21 commits were rebased onto main (b2575da on top of #79 da0e06d); the tree differs from the pre-merge stacked head 63eec74 only by #79's three files. History below records the earlier stacked state.

Review fixes (6689f7b). These address the independent review of f35f1c6:

  • origin-bound return routes (invalid_return_route, return_host_is_receiver, and refusing loopback reply hosts on remote sends);
  • preserving a configured HostKeyAlias, read through the same ssh -G;
  • exact Tailscale Online states, including refusing known-offline peers with MagicDNS off;
  • the portable exit 0 probe command and documented lookup bounds;
  • the preload now also clearing Codex identity variables.

src/ssh.ts gains sshConfig() on this branch only. #84 is unchanged.

Follow-up (63eec74, non-blocking review).

  • Wording: the receiver reason return_host_is_receiver now reads "known self name OR unprovable numeric form". Known names are only an exact match with the OS host name or the Tailscale self names and addresses; there is no claim of verification against other DNS or LAN aliases. The remote reply-host refusal now names non-canonical numerics as well as loopback. Code comments, PARITY and the four guides are updated.
  • Regressions:
    • a private [email protected] return host is an ordinary remote host, forwarded and probed (verified with the fake probe), not refused;
    • with an unusable login name ('Fixture User', injected through a test-only --import preload), --reply-to remote-host stays unqualified, and even --reply-to localhost stays an SSH route because "this user on this machine" cannot be proven.
  • Narrow validation: test/replies.test.ts, test/ssh-hosts.test.ts and test/process.test.ts gave 24 tests, 23 pass, 0 fail, 1 skipped (Windows-only); the repository check is OK.

Review fix (15d10d4, formerly 5a84cf9): non-canonical numerics. The 45b753d helper guessed inet_aton meanings, but resolvers differ: this Mac's getaddrinfo(AI_NUMERICHOST) reads 0177.0.0.1 as 177.0.0.1 and wraps overflow forms such as 6425673729 to loopback. There are now two separate predicates that are never mixed.

  • unsafeReturnHost() is conservative and fails closed. It refuses return/reply hosts that are localhost names, canonical 127.0.0.0/8 or 0.0.0.0/8, ::1, ::, their ::ffff: mapped forms, IPv4-compatible IPv6 (::a.b.c.d, ::7f00:1), or any non-canonical numeric (leading zeros, fewer than four parts, hex/octal parts, single integers, overflow).
  • localName() is strict and decides local delivery. It accepts only exact localhost, canonical 127.x.y.z, ::1 and canonical mapped ::ffff:127.x.y.z; never non-canonical numerics and never IPv4-compatible addresses (RFC 4291 section 2.5.5.1, deprecated).
  • Regressions:
    • (A) self URIs with 0177.0.0.1, 127.1, 2130706433, 6425673729, ::7f00:1 and ::127.0.0.1 stay SSH (one attempt, no ssh_self);
    • (B) 6425673729, 4294967296 and 18446744075840258049, plus [::7f00:1] and [::127.0.0.1], are refused as invalid_return_route with zero SSH, and fail on the receiver as return_host_is_receiver with no probe;
    • unit tables for both predicates and for isSelf.
  • The contract is documented in PARITY and the four guides.

Review fix (81c6353). --ssh-jump now counts as an explicit SSH transport. A same-user self reply URI combined with --ssh-jump stays an SSH route through the hop (one SSH attempt, sshJump reported) instead of being normalized to local with the jump silently ignored. A regression was added to the self-normalization cases.

Review fixes (45b753d, formerly 93bce35).

  • Numeric loopback. Loopback and unspecified addresses are compared by value, without DNS. This covers every inet_aton IPv4 form (127.1, 2130706433, 0, 0x7f.1, 0177.0.0.1, 017700000001) and IPv4-mapped or compatible IPv6 ([::ffff:127.0.0.1], ::ffff:7f00:1, ::7f00:1), alongside ::1, :: and the localhost names. All of these are refused as invalid_return_route with zero SSH, fail on the receiver as return_host_is_receiver with no probe, and are refused as remote-send reply hosts (invalid_reply_host).
  • Receiver names. The new namesThisMachine() decides whether a host names the receiver independently of the login user, so bob@<receiver hostname> fails as return_host_is_receiver even when os.userInfo().username is unusable (unit regression with a mocked 'Fixture User'). isSelf() keeps requiring that user for local normalization.

Rebase notes (--ssh-jump).

Earlier rebase notes.

Adds sender context, generated structured reply routes, Tailscale routing hints, same-user self normalization and an opt-in doctor --check-return-route. All of this is inert metadata: nothing here authenticates a peer, authorizes a send, executes Reply text or infers ACK. Delivery contracts are unchanged: owner verification, ambiguity refusal, pre-submit revalidation, exact-version SSH preflight, stdin JSON request, and no retry after unknown.

Acceptance criteria

Criterion Status Implementation
Detect sender context from bounded evidence; ambiguous/absent evidence omits identity; no Antigravity Done Inbox-socket agent: the inherited *_CODE_MESSAGING_SOCKET must match exactly one live registered session, by PID parsed from …<pid>.sock as in Python, else by exact socket path. Its name is used only if it is printable, not all digits, has no :, and is unique by casefold; otherwise the PID is used. Codex: CODEX_THREAD_ID, else CODEX_SESSION_ID, must be a valid UUID. codexHome is added only for an absolute CODEX_HOME. Ambiguity: two different Codex IDs, or socket-agent evidence and Codex evidence together (a nested agent), give no identity. Python instead prefers the socket agent; the issue asks for ambiguous evidence to be omitted.
--reply-to and env precedence; keep --reply-address, --no-from, --no-reply-to; only validated structured URIs Done Host precedence: --reply-to, then SESSION_PEER_REPLY_HOST, then CC_PEER_REPLY_HOST, then the tailnet address (MagicDNS name, else 100.64.0.0/10 IPv4 from tailscale status). The current user is added when missing. --reply-to, --reply-address and --no-reply-to are mutually exclusive (conflicting_reply_options). An invalid configured host is invalid_reply_host. A generated URI is accepted only if the existing reply() parser parses it back. replyRoute (local verified/same_machine_route; SSH unverified/reverse_ssh_not_checked) and addressResolution (uri, transport, normalizedFrom) use Python's names. There is no executable Reply: line.
Tailscale only as a routing hint Done tailscale status --json: 3 s bound, queried at most once per command, and only when a route needs it. Exact states: only Online === true (boolean) with MagicDNS on is resolved: the alias stays the SSH destination and -o HostName=<MagicDNS> is added after the fixed hardening options. -o HostKeyAlias=<original> is added only when the same ssh -G used for sshUser read the config and found no hostkeyalias. A configured alias is kept, and an unreadable config is never overridden. Online === false (boolean) is refused before any SSH (tailscale_peer_offline) even with MagicDNS off; an ambiguous name gives tailscale_destination_ambiguous. A missing, null, string or numeric Online, MagicDNS off, an unknown name, a stopped/failing/missing CLI, or SESSION_PEER_TAILSCALE=off means ordinary SSH with no canonicalization. Metadata follows Python: host is the MagicDNS name and sshHost is the alias.
Same-machine normalization only when host and user match; doctor --check-return-route [--reply-to USER@HOST]; forward never implies reverse Done A reply URI is normalized to local (normalizedFrom: "ssh_self") only when its host has an explicit user equal to the current OS user, names this machine, and the route came from the URI alone. Python also normalizes a host without a user; TS requires the user. Origin binding: with --host, a loopback return host (localhost, *.localhost, 127.x, ::1) is refused before any SSH (invalid_return_route). On the receiver, a forwarded return host naming the receiver itself (loopback, its hostname or its tailnet self, for any user) fails as return_host_is_receiver and is never reported as a verified local route. A remote send refuses a loopback reply host (invalid_reply_host). The probe runs ssh -T -o BatchMode=yes -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no -o NumberOfPasswordPrompts=0 -o StrictHostKeyChecking=yes -o UpdateHostKeys=no -o ConnectTimeout=5 -o ConnectionAttempts=1 -o ControlMaster=no -o ControlPath=none -- USER@HOST 'exit 0'. The 8 s deadline covers that final ssh only; the Tailscale (3 s) and ssh -G (5 s) lookups are bounded separately. exit 0 is a no-op in POSIX shells, cmd.exe and PowerShell (Python uses true, which cmd/PowerShell lack); only POSIX return hosts have fixture coverage. There is no stderr in output and no retry. It never changes ok/ready.
Tests: malformed/conflicting URIs, wrong-user self routes, absent/ambiguous senders, offline/unknown peers, reverse auth/host-key/timeout; never execute Reply text or infer ACK Done New test/replies.test.ts (10 tests).

Files

  • src/replies.ts (new): sender(), tailnet(), route(), isSelf(), detectedHost(), configuredHost(), returnHost(), replyUri(), probeReturnRoute().
  • src/cli.ts: new --reply-to and --check-return-route flags and the wire-only --return-route-host; sender and route generation; reply-URI self normalization; per-host Tailscale resolution in remotes(); doctor probe and remote returnRoute validation; replyRoute/addressResolution on send results.
  • src/protocol.ts: envelope() takes an optional 4th sender ({agent,id} or null). Existing three-argument calls behave as before. docs/api.md is updated.
  • src/output.ts: doctor text Return route: line. src/help.ts: send/doctor/transport usage.
  • Docs: Replies section of the four guides (EN/KO/JA/zh-CN, real translations), plus the new PARITY section "Sender context and return routes (unreleased source)" and updated env/JSON/SSH rows.
  • Tests: test/replies.test.ts, and the test/fixtures/isolate-env.mjs preload, added to npm test and npm run test:windows. The preload sets SESSION_PEER_TAILSCALE=off and clears the inbox-socket variable, CODEX_THREAD_ID, CODEX_SESSION_ID, SESSION_PEER_REPLY_HOST and CC_PEER_REPLY_HOST; fixtures that need an identity set it explicitly. A guard test asserts this.
  • Packaging lists: scripts/release.mjs, test/package-smoke.ts include dist/replies.*.

Type

  • feat
  • fix
  • docs
  • chore / refactor / test / perf

Validation

  • npm ci --ignore-scripts and npm run build
  • SESSION_PEER_PYTHON_ROOT=/path/to/pinned/reference npm test (on main after retargeting): 101 tests, 96 pass, 0 fail, 5 skipped (Windows-only).
  • npm run test:package: packageSmoke: pass. npm audit: 0 vulnerabilities.
  • node scripts/check-repository.mjs: OK. node scripts/release.mjs pack: includes dist/replies.*.
  • Latest CI release gate and CodeQL run on the main-based head after retargeting (see the latest checks on this PR). Earlier stacked runs (e.g. 36970305866 on 63eec74) are history.
  • Behavior changes include relevant tests; refused/unknown/queued/ACK remain distinct
  • README changes are synchronized in EN/KO/JA/zh-CN (README entry pages unchanged; the four guides are updated together)
  • No credentials, real messages, session IDs, agent homes or local evidence in the diff

Test coverage (test/replies.test.ts, fake ssh and fake tailscale on a temp PATH):

  • Sender detection: absent, valid socket agent, nested socket and Codex (no identity), unmatched socket, Codex thread vs session (same, different), invalid UUID, relative and absolute CODEX_HOME. Name fallback to PID for casefold collisions, all-digit names, : and newlines. Exact-path matching for pipe-style sockets.
  • Routing unit tests: online, offline, ambiguous, unknown, MagicDNS-off and no-status routing. Self detection for correct user versus missing or wrong user and non-local hosts. Detected host (MagicDNS, IPv4 fallback, non-tailnet IPs ignored). URI round trip and invalid URIs.
  • Local send: exact delivered envelope for From + local Reply-To, --no-from, and --no-reply-to. Absent and ambiguous senders add nothing. Precedence order across --reply-to, SESSION_PEER_REPLY_HOST and CC_PEER_REPLY_HOST. A self --reply-to gives a local route. Conflicts and invalid hosts are refused before delivery. A body containing $(touch pwned) is delivered as text and never executed. Zero ssh and zero tailscale calls.
  • Tailscale list: three hosts (online, offline, unknown), exit 1, one status query. Exact argv (HostName/HostKeyAlias after the fixed options and before -p). The offline host gets zero SSH calls. Missing, stopped and off Tailscale give ordinary SSH.
  • Remote send: a Codex sender gets an SSH Reply-To with the tailnet host (unverified). The body and Reply-To are never in argv.
  • Reply-URI self normalization: local delivery with zero SSH. Wrong user, missing user or explicit --host stays SSH (exactly one attempt). Malformed and conflicting URIs give zero SSH and zero deliveries.
  • Review regressions: loopback --reply-to with --host gives invalid_return_route and zero SSH (me@localhost, 127.0.0.1, [::1]). A receiver's own hostname gives return_host_is_receiver with no probe. A direct wire request with --return-route-host bob@localhost gives return_host_is_receiver. A remote send with a loopback --reply-to or SESSION_PEER_REPLY_HOST gives invalid_reply_host and zero SSH. For HostKeyAlias: a configured alias is kept, -G failure means no injection, and no config alias means ours is injected; each is checked for both worker and audit@worker. Online values undefined, null, 'true', 'false', 1 and 0 all give ordinary SSH. With MagicDNS off, an offline peer is refused.
  • Doctor: --reply-to without --check-return-route, list --check-return-route and --return-route-host outside the wire are refused. Return-route outcomes: unavailable host, self as local without SSH, and verified/auth/host-key/timeout-stderr/other with no stderr leak. A hung probe is killed at 8 s and reported as timeout, with no retry. Exact probe argv is checked. Over SSH, two hosts each probe back with the forwarded --return-route-host. A missing return host is not forwarded. Text output is checked.

Tested environments / limitations

  • Local: macOS (darwin arm64), Node 24.16.0. All SSH and Tailscale interactions use fake executables. No real tailnet query, SSH destination or agent session was used.
  • Windows: CI runs only the platform-neutral unit tests in test/replies.test.ts. The CLI fixtures are POSIX-only. Windows pipe-path sender matching is unit-tested on POSIX with a pipe-style string; there is no Windows-native evidence.
  • No live evidence of the socket-agent environment variable format or the Tailscale JSON shape beyond Python's reference and fixtures.

Release impact

No release or deployment. Behavior changes, all additive or opt-in:

  • Sends from inside a detected agent session now append a structured Reply-To by default (Python parity). --no-reply-to disables it.
  • Commands with --host may query tailscale status --json (≤3 s) and may refuse a known offline peer.
  • New JSON fields: replyRoute, addressResolution, returnRoute. host becomes the MagicDNS name for resolved peers.
  • envelope() gains an optional 4th parameter.
  • New env: SESSION_PEER_REPLY_HOST, CC_PEER_REPLY_HOST, SESSION_PEER_TAILSCALE.

Both SSH endpoints still require the same version. No native dependency changes.

@abruption abruption added the enhancement New feature or request label Oct 2, 2026
@abruption
abruption force-pushed the feat/21-sender-context branch 5 times, most recently from 5a84cf9 to 63eec74 Compare October 2, 2026 05:44
@abruption
abruption force-pushed the feat/20-multi-host-ssh branch from 8cbad61 to 05f1234 Compare October 3, 2026 02:35
…diagnostics

Detect the sender from bounded evidence: the inherited inbox socket variable
must match exactly one live registered session, and the Codex thread/session
variable must be a valid UUID. Nested, conflicting or absent evidence gives no
identity. Generate inert session-peer://v1/reply routes with host precedence
--reply-to, SESSION_PEER_REPLY_HOST, CC_PEER_REPLY_HOST, then the tailnet
address, and report replyRoute/addressResolution like the Python reference.

Use tailscale status only as a routing hint with exact Online states: keep
the SSH alias, add HostName for known online peers and HostKeyAlias only when
ssh_config sets none, refuse known offline or ambiguous peers before SSH, and
leave everything else as ordinary SSH. Normalize a reply URI to local only for
this user on this machine. Add doctor --check-return-route as an explicit,
bounded, noninteractive reverse SSH probe that stays bound to the origin and
is never inferred from forward reachability.

Closes #21
…eiver names from the login user

Treat every numeric IPv4 form accepted by inet_aton (127.1, 2130706433,
0x7f.1, 0177.0.0.1, 0) and IPv4-mapped or compatible IPv6 as loopback or
unspecified, without DNS. Decide whether a host names the receiver with a
helper that ignores the login user, so an unusable local username cannot
turn the receiver's own host name into a verified remote route.
A jump host implies SSH, so a reply URI naming this user on this machine is
normalized to a local route only when neither --host, an SSH option nor
--ssh-jump was given.
Split the host predicates. Return and reply hosts that might name a machine
are refused without guessing resolver behavior: localhost names, canonical
loopback and unspecified addresses, their mapped forms, IPv4-compatible IPv6
and every non-canonical numeric form. Local normalization accepts only exact
localhost, canonical 127.x.y.z, ::1 and canonical mapped loopback, never a
non-canonical numeric or an IPv4-compatible address.
@abruption
abruption force-pushed the feat/21-sender-context branch from 63eec74 to bfd0e8f Compare October 3, 2026 02:39
@abruption
abruption changed the base branch from feat/20-multi-host-ssh to main October 3, 2026 02:39
State that the receiver refuses a known self name or an unprovable numeric
form, that known names are only the exact OS host name and Tailscale self
names and addresses, and that remote sends refuse loopback and non-canonical
numeric reply hosts. Add regressions for a private return host and for an
unusable login name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(replies): add verified sender context and optional return-route diagnostics

1 participant