diff --git a/pm-stack/aggregator/scripts/laptop_known_hosts b/pm-stack/aggregator/scripts/laptop_known_hosts new file mode 100644 index 0000000000..215cd989bc --- /dev/null +++ b/pm-stack/aggregator/scripts/laptop_known_hosts @@ -0,0 +1,8 @@ +# Pinned SSH host key for the laptop aggregator (user@100.99.113.103). +# Captured 2026-10-26 via ssh-keyscan over the Tailscale route (tailnet +# WireGuard peer authentication) for issue #948; the laptop sshd offers +# only this ed25519 key. push-fleet-feed.sh verifies against this file +# with StrictHostKeyChecking=yes. If the laptop's host key is ever +# regenerated, re-capture over the tailnet and update this file in a +# reviewed commit — never bypass the check instead. +100.99.113.103 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHmqYb8kYdZwwdd8n7vkW9XenQc2ymIZRoeOSj0lt5ja diff --git a/pm-stack/aggregator/scripts/push-fleet-feed.guard.sh b/pm-stack/aggregator/scripts/push-fleet-feed.guard.sh new file mode 100755 index 0000000000..8f7628e35e --- /dev/null +++ b/pm-stack/aggregator/scripts/push-fleet-feed.guard.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +# Grep-level guard for push-fleet-feed.sh (issue #948). +# Fails if the proxy credential is ever handed to a process as an +# argument again, or if SSH host-key verification is disabled again. +# Forbidden patterns are assembled from fragments so this guard file +# itself does not contain them literally. +set -euo pipefail +cd "$(dirname "$0")" +TARGET="push-fleet-feed.sh" +fail=0 + +check_absent() { + local pat="$1" why="$2" + if grep -nE "$pat" "$TARGET" >/dev/null; then + echo "guard: FORBIDDEN pattern present ($why): $pat" >&2 + grep -nE "$pat" "$TARGET" >&2 || true + fail=1 + fi +} + +check_present() { + local pat="$1" why="$2" + if ! grep -qE "$pat" "$TARGET"; then + echo "guard: REQUIRED pattern missing ($why): $pat" >&2 + fail=1 + fi +} + +check_absent "proxy""auth" "proxy credential must never be an argv token (e.g. to socat)" +check_absent "Proxy""Command" "no proxy helper subprocess; the tunnel is in-process" +check_absent "StrictHostKeyChecking=no" "host-key verification must stay on" +check_absent "UserKnownHostsFile=/dev/null" "host keys must come from the pinned file" +check_absent "so""cat" "socat carried the credential in argv; do not reintroduce it" + +check_present "StrictHostKeyChecking=yes" "host-key verification is required" +check_present "laptop_known_hosts" "the pinned known_hosts file must be used" +check_present "Proxy-Authorization" "credential goes on the proxy wire only" + +if [ ! -s "laptop_known_hosts" ]; then + echo "guard: laptop_known_hosts is missing or empty" >&2 + fail=1 +fi + +if [ "$fail" -ne 0 ]; then + exit 1 +fi +echo "guard: push-fleet-feed.sh OK (no argv credential, host key pinned)" diff --git a/pm-stack/aggregator/scripts/push-fleet-feed.sh b/pm-stack/aggregator/scripts/push-fleet-feed.sh index 9b3904d871..ec4c97c8e5 100755 --- a/pm-stack/aggregator/scripts/push-fleet-feed.sh +++ b/pm-stack/aggregator/scripts/push-fleet-feed.sh @@ -1,10 +1,22 @@ #!/usr/bin/env bash # Collect the fleet feed on this (sandbox) host and push it to the -# laptop aggregator, over the same tailnet proxy route laptop-ssh.sh -# uses (socat PROXY via hatch-egress-proxy:3130). The proxy credential -# comes from this process's HTTPS_PROXY when present (agent runs), or -# from ~/tools/secrets.d/mcpx-daemon.env (systemd timer runs) — it is -# read at runtime and never stored here. +# laptop aggregator over the tailnet proxy route (hatch-egress-proxy:3130). +# +# Security (issue #948): +# - The proxy credential is NEVER passed as a process argument. The +# Python process below reads it from HTTPS_PROXY (agent runs) or +# ~/tools/secrets.d/mcpx-daemon.env (systemd timer runs), opens an +# HTTP CONNECT tunnel to the laptop itself, and sends the credential +# only on the wire, in the Proxy-Authorization header. ssh/scp talk +# to a throwaway 127.0.0.1 forwarder owned by that same process, so +# no spawned process (ssh, scp, or any tunnel helper) carries the +# credential in its argv. +# - The laptop host key is pinned: scripts/laptop_known_hosts is +# checked in, and ssh verifies against it with +# StrictHostKeyChecking=yes + HostKeyAlias=. +# A mismatched key fails the push. +# Guard: scripts/push-fleet-feed.guard.sh fails if either pattern +# regresses. # # Cadence: ~5 min via fleet-feed-collector.timer (systemd, sandbox). set -euo pipefail @@ -23,13 +35,23 @@ PKG="$PWD" FEED="$PKG/fleet-feed.json" LAPTOP=user@100.99.113.103 DEST_DIR="~/pm-stack/aggregator" +KNOWN_HOSTS="${PUSH_FLEET_FEED_KNOWN_HOSTS:-$PKG/scripts/laptop_known_hosts}" bun run scripts/collect-fleet-feed.ts --out "$FEED" -exec python3 - "$FEED" "$LAPTOP" "$DEST_DIR" <<'PYEOF' -import os, re, subprocess, sys, urllib.parse +exec python3 - "$FEED" "$LAPTOP" "$DEST_DIR" "$KNOWN_HOSTS" <<'PYEOF' +import base64, os, re, socket, subprocess, sys, threading, urllib.parse + +feed, laptop, dest_dir, known_hosts = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4] +target_host = laptop.split("@", 1)[-1] +target_port = 22 +# Tailnet tunnel port on the egress proxy (same route the old external +# proxy-helper command used; the HTTPS_PROXY URL port is the general +# egress port). +PROXY_PORT = 3130 -feed, laptop, dest_dir = sys.argv[1], sys.argv[2], sys.argv[3] +if not os.path.isfile(known_hosts) or os.path.getsize(known_hosts) == 0: + sys.exit(f"push-fleet-feed: pinned known_hosts missing/empty: {known_hosts}") proxy = os.environ.get("HTTPS_PROXY") or os.environ.get("https_proxy") if not proxy: @@ -45,25 +67,111 @@ if not proxy: u = urllib.parse.urlparse(proxy) user = urllib.parse.unquote(u.username or "") pw = urllib.parse.unquote(u.password or "") -proxycmd = f"socat - PROXY:{u.hostname}:100.99.113.103:22,proxyport=3130,proxyauth={user}:{pw}" +# Credential exists only in this process's memory and on the proxy wire. +auth = base64.b64encode(f"{user}:{pw}".encode()).decode() + + +def open_tunnel(): + """CONNECT through the egress proxy to the laptop sshd. + Returns (socket, leftover_bytes_received_after_the_CONNECT_reply).""" + s = socket.create_connection((u.hostname, PROXY_PORT), timeout=25) + req = ( + f"CONNECT {target_host}:{target_port} HTTP/1.1\r\n" + f"Host: {target_host}:{target_port}\r\n" + f"Proxy-Authorization: Basic {auth}\r\n" + "Proxy-Connection: keep-alive\r\n\r\n" + ) + s.sendall(req.encode()) + buf = b"" + while b"\r\n\r\n" not in buf: + chunk = s.recv(4096) + if not chunk: + s.close() + raise OSError("proxy closed during CONNECT") + buf += chunk + head, leftover = buf.split(b"\r\n\r\n", 1) + status = head.split(b"\r\n", 1)[0].decode("latin1") + if " 200" not in status: + s.close() + raise OSError(f"proxy CONNECT failed: {status}") + return s, leftover + + +def relay(src, dst, initial=b""): + try: + if initial: + dst.sendall(initial) + while True: + data = src.recv(65536) + if not data: + break + dst.sendall(data) + except OSError: + pass + finally: + try: + dst.shutdown(socket.SHUT_WR) + except OSError: + pass + + +def handle(local): + try: + remote, leftover = open_tunnel() + except OSError as e: + print(f"push-fleet-feed: tunnel failed: {e}", file=sys.stderr) + local.close() + return + t = threading.Thread(target=relay, args=(remote, local, leftover), daemon=True) + t.start() + relay(local, remote) + t.join(timeout=5) + local.close() + remote.close() + + +listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) +listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) +listener.bind(("127.0.0.1", 0)) +listener.listen(5) +local_port = listener.getsockname()[1] + + +def accept_loop(): + while True: + try: + conn, _ = listener.accept() + except OSError: + return + threading.Thread(target=handle, args=(conn,), daemon=True).start() + + +threading.Thread(target=accept_loop, daemon=True).start() + ssh_opts = [ - "-o", "StrictHostKeyChecking=no", - "-o", "UserKnownHostsFile=/dev/null", + "-o", "StrictHostKeyChecking=yes", + "-o", f"UserKnownHostsFile={known_hosts}", + "-o", f"HostKeyAlias={target_host}", "-o", "IdentitiesOnly=yes", "-i", os.path.expanduser("~/.ssh/id_ed25519"), - "-o", f"ProxyCommand={proxycmd}", "-o", "ConnectTimeout=25", - "-o", "ControlMaster=auto", - "-o", "ControlPath=" + os.path.expanduser("~/.ssh/cm-%r@%h:%p"), - "-o", "ControlPersist=120", + # No ControlMaster/ControlPersist: the tunnel lives in this process, + # so a persisted master connection would outlive its forwarder. + "-o", "ControlMaster=no", ] -subprocess.run( - ["scp", *ssh_opts, feed, f"{laptop}:{dest_dir}/fleet-feed.json.tmp"], - check=True, -) -subprocess.run( - ["ssh", *ssh_opts, laptop, f"mv {dest_dir}/fleet-feed.json.tmp {dest_dir}/fleet-feed.json"], - check=True, -) +dest = f"{laptop.split('@', 1)[0]}@127.0.0.1" +try: + subprocess.run( + ["scp", "-P", str(local_port), *ssh_opts, feed, + f"{dest}:{dest_dir}/fleet-feed.json.tmp"], + check=True, + ) + subprocess.run( + ["ssh", "-p", str(local_port), *ssh_opts, dest, + f"mv {dest_dir}/fleet-feed.json.tmp {dest_dir}/fleet-feed.json"], + check=True, + ) +finally: + listener.close() print("push-fleet-feed: feed pushed to laptop") PYEOF