Repository navigation
security(aggregator): keep fleet-feed proxy credential off argv, pin laptop host key #970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Pinned SSH host key for the laptop aggregator ([email protected]). | ||
| # 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 |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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=<laptop tailnet IP>. | ||
| # 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" | ||
| [email protected] | ||
| 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 | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the laptop or network pauses for more than 25 seconds after CONNECT succeeds, the socket still carries the timeout set by
socket.create_connection. Consequently,relay(remote, local)catches the timeout as anOSErrorand shuts down the local SSH read side, aborting an otherwise valid upload or rename and leaving the aggregator's previous feed in place. Clear the timeout after the CONNECT handshake so the connection timeout does not also become an SSH inactivity timeout.Useful? React with 👍 / 👎.