Skip to content

feat(relay): authenticate registered alternate poll shims - #3462

Closed
derickdsouza wants to merge 2 commits into
kunchenguid:mainfrom
derickdsouza:fm/harbor-hitl-trust-binding
Closed

derickdsouza wants to merge 2 commits into
kunchenguid:mainfrom
derickdsouza:fm/harbor-hitl-trust-binding

Conversation

@derickdsouza

Copy link
Copy Markdown

Intent

Build a deliberate trust-binding mechanism so firstmate authenticates a REGISTERED alternate relay-poll shim - the harbor telegram-hitl wrapper - as a first-class X-mode poll path, per the captain's ruling of 2026-09-01 (adopt).

state/x-watch.check.sh is generated by fm-bootstrap.sh (x_mode_setup) from fm-x-lib.sh's fmx_poll_shim_content, and the watcher validates it byte-statically, rejecting any other bytes as an unauthenticated state check. Harbor's telegram-hitl extension rewrites the shim to exec its wrapper bin/fm-x-poll-harbor.sh, which itself wraps stock bin/fm-x-poll.sh; firstmate correctly refuses, and harbor's deploy re-arms it periodically. The captain adopted the integration: firstmate grows the authentication path.

Requirements:

  1. One explicit registration command (placement per firstmate-coding-guidelines' one-owner rule) that binds the CURRENT bytes of the alternate x-watch shim PLUS the identity of its exec target: absolute path and content hash of the wrapped script. Binding records live under state/, mode 0600, never auto-created.
  2. Both validators honor a valid registration: the watcher's check dispatch accepts the registered variant, and fm-bootstrap's x_mode_setup regeneration PRESERVES a registered variant whose binding still matches instead of clobbering it back to stock.
  3. Stale registration semantics: if the registered target's content changed, bootstrap regenerates stock, the watcher rejects the stale variant exactly as today, and the rejection message names the re-register command as the remedy.
  4. Security posture preserved: homes without a registration behave exactly as today - stock generation, byte-static validation, rejection of any rewrite. Registration is a root-explicit act; no path auto-trusts new bytes.
  5. Colocated tests: stock validates; registered variant validates through both the watcher path and bootstrap regeneration; unregistered rewrite rejected; tampered target (hash mismatch) rejected and named; bootstrap regen preserves a valid registration and replaces a stale one with stock.
  6. Docs: pointer-style, one-owner - a sentence in docs/configuration.md's Relay section and the owning script's header; no duplication.

Constraints:

  • Shared tracked material follows firstmate-coding-guidelines (one sentence per line, one-owner rule, shellcheck-clean, colocated tests, no agent co-author).
  • Do not change the relay wire protocol, the stock poll script's behavior, or the fmx-respond policy rules - authentication plumbing only.
  • Do not touch anything under /opt/harbor.
  • Accepted review decision: drop the Cursor Co-authored-by trailer; apply the dead-composite-validator cleanup (remove unused fmx_poll_shim_valid).
  • Delivery routing (captain 2026-09-01): land on the fork derickdsouza/firstmate, PR against that fork's own main. Never open a PR on upstream kunchenguid/firstmate. Resume from the cancelled run that already passed review, test, document, lint, and fork push; open/attest the fork-internal PR and wait until CI is green.

Prior run 01M1F4W5VBPZXV5PMAD4TNAQ4T completed through push to the fork then was aborted at PR. Fork PR derickdsouza#1 already exists for this head; attach the no-mistakes signature to that fork-internal PR rather than opening another PR.

What Changed

  • Add bin/fm-x-watch-register.sh, the sole writer of a state/x-watch.poll-trust binding record (mode 0600, never auto-created) that pins the current shim bytes plus the absolute path and sha256 of its exec target.
  • The watcher's check dispatch and fm-bootstrap.sh's x_mode_setup now resolve the poll exec target through the new fmx_poll_exec_target/fmx_poll_shim_registered_valid helpers: a still-matching registration is preserved across bootstrap regeneration and dispatched by the watcher, while stale or tampered bindings are rejected exactly like unregistered rewrites, with the rejection message naming the re-register command as the remedy.
  • Replace the dead composite validator fmx_poll_shim_valid with the split fmx_poll_shim_stock_valid, and document the registration path in the docs/configuration.md Relay section, docs/scripts.md, and AGENTS.md, with colocated tests in tests/fm-x-watch-register.test.sh covering stock, registered, unregistered, and tampered-target variants.

Testing

  • ⏭️ Test - skipped

Pipeline

Updates from git push no-mistakes

⏭️ **intent** - skipped

Step was skipped.

⏭️ **Rebase** - skipped

Step was skipped.

⏭️ **Review** - skipped

Step was skipped.

⏭️ **Test** - skipped

Step was skipped.

⏭️ **Document** - skipped

Step was skipped.

⏭️ **Lint** - skipped

Step was skipped.

⏭️ **Push** - skipped

Step was skipped.

An explicit binding of the current x-watch shim bytes plus exec-target identity lets a harbor-style wrapper stay armed across bootstrap, while homes without a registration keep the stock byte-static path.
@greptile-apps

greptile-apps Bot commented Sep 1, 2026

Copy link
Copy Markdown

Confidence Score: 4/5

The inconsistent shim-to-target binding should be fixed before merging; the path-boundary and execution-race concerns are additional non-blocking hardening work.

Registration authenticates the shim and supplied target independently, allowing watcher dispatch to execute a program that the alternate shim does not identify.

Files Needing Attention: bin/fm-x-watch-register.sh, bin/fm-x-lib.sh, bin/fm-watch.sh

Security Review

Two non-blocking hardening gaps remain: target paths can resolve beneath state/ despite the lexical exclusion, and execution reopens a mutable pathname after hash validation.

Reviews (1): Last reviewed commit: "no-mistakes(document): Align Relay poll-..." | Re-trigger Greptile

Comment on lines +64 to +77
if fmx_poll_shim_stock_valid "$CHECK" "$FM_HOME" "$FM_ROOT"; then
echo "error: shim matches the stock Relay poll identity; an alternate shim is required" >&2
exit 1
fi
fm_pr_regular_destination_on_device_or_absent "$TRUST" "$STATE_DEVICE" \
|| { echo "error: poll trust path is unavailable" >&2; exit 1; }

SHIM_HASH=$(fmx_sha256 "$CHECK") || { echo "error: poll shim hash is unavailable" >&2; exit 1; }
TARGET_HASH=$(fmx_sha256 "$TARGET") || { echo "error: exec target hash is unavailable" >&2; exit 1; }

umask 077
TMP=$(mktemp "$STATE/.fm-x-watch-poll-trust.XXXXXX") || exit 1
trap '[ -z "$TMP" ] || rm -f -- "$TMP"' EXIT HUP INT TERM
printf '%s\n%s\n%s\n%s\n' fm-x-watch-poll-v1 "$SHIM_HASH" "$TARGET" "$TARGET_HASH" > "$TMP" || exit 1

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Shim target binding is inconsistent

When registration receives a target different from the alternate shim's exec target, it authenticates both independently and the watcher executes the supplied target, causing a program not identified by the shim to be silently accepted.

Comment on lines +51 to +56
case "$TARGET" in
"$STATE"|"$STATE"/*)
echo "error: exec target must not live under state/" >&2
exit 1
;;
esac

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 security State exclusion uses lexical paths

A target containing parent traversal or a symlinked parent can resolve beneath state/ while passing this prefix check, allowing mutable runtime state to be registered as executable code. How this was verified: The target validator accepts absolute paths containing .., and neither registration nor validation resolves parent directories before enforcing the boundary.

Comment thread bin/fm-x-lib.sh
Comment on lines +323 to +325
[ -f "$FMX_POLL_TRUST_TARGET" ] && [ ! -L "$FMX_POLL_TRUST_TARGET" ] || return 1
hash=$(fmx_sha256 "$FMX_POLL_TRUST_TARGET") || return 1
[ "$hash" = "$FMX_POLL_TRUST_TARGET_HASH" ]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 security Validated target path remains mutable

If a deployment or other same-user process replaces the wrapper after hashing but before dispatch, the watcher reopens the mutable pathname and executes bytes outside the trust binding. How this was verified: Validation returns only the pathname, and the watcher later passes that path to run_check_capture without snapshotting or revalidation.

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