Skip to content

feat(relay): forge-v2 webhooks with chain-encrypted secrets - #27

Merged
PastaPastaPasta merged 5 commits into
masterfrom
feat/v2-relay
Sep 26, 2026
Merged

PastaPastaPasta merged 5 commits into
masterfrom
feat/v2-relay

Conversation

@PastaPastaPasta

Copy link
Copy Markdown
Owner

Summary

This PR rewrites forge-relay for forge-v2: it turns on-chain activity into GitHub-shaped webhooks, signed with an HMAC secret that is stored encrypted on chain. It is part 1 of 3 of PR G. The others are import/migrate, then the GitHub Mirror Action.

  • Webhooks on chain. A repository maintainer runs dg webhook add|list|remove. This writes a webhook document holding the URL, the events, and the HMAC secret. The secret is encrypted to the relay identity's ENCRYPTION key using ecdh-secp256k1-aes256-cbc (forge_core::envelope, via the platform-encryption crate) and recorded with encryptedFor. forge_core::webhooks holds the shared document codec.
  • Relay daemon.
    • Finding repos: it discovers the repositories it serves through the relay index and the webhook documents.
    • What it polls: refUpdate, protectedRefUpdate, config, issues, patches, comments, reviews, events, releases and check runs.
    • Cursors: read-then-commit. A Tail baseline is primed from the same reads that built the relay's state, so nothing written between setup and the first poll is lost.
    • Budgets: per-repo poll budgets resume at the stage where they were cut off. Merge tips are tracked only for the base refs of known PRs.
  • Delivery. GitHub-shaped payloads (push, issues, pull_request, issue_comment, pull_request_review, release, check_run) with X-Hub-Signature-256.
    • SSRF guard: the connection is pinned to the resolved IP, with no redirects and no proxy. Private, loopback and link-local addresses are refused unless --allow-private is passed.
    • Concurrency: a dispatcher per hook, a circuit breaker, per-tenant and per-IP slot limits, and a slot timeout that fails only the current attempt.
    • Failures: a delivery that still fails after its retries is logged once as DEAD-LETTER.
    • Merge events: a merge whose oid is not among the base's recorded tips is sent as merged: false with dash_merge_unverified: true.
  • Secrets. The relay decrypts each hook secret with its own ENCRYPTION key. It needs only a minimal key file (identity id plus ENCRYPTION keys). Secrets are held in Zeroizing buffers and never logged.
  • Packaging.
    • crates/forge-relay/Dockerfile builds from source.
    • Dockerfile.release is used by release.yml, whose new relay-image / relay-image-push jobs publish ghcr.io/pastapastapasta/forge-relay.
    • The push job runs behind the release environment and after publish.
    • docs/INSTALL.md and the relay README cover running it.

Three independent security reviews were run. All High and Medium findings are fixed; the remaining items are listed under Gaps.

Live verification (devnet moutai, re-registered contracts from #25)

  1. Recreated relay-e2e (3HKxeeGrJjfVP6msx7yEKUPp9ghev2sAPFHEFLQ1c1x1) under the test OWNER identity.
  2. Ran dg webhook add --events push --secret-env. The webhook document was accepted with the secret encrypted to RELAY key 4.
  3. Ran forge-relay run --allow-private with an ENCRYPTION-only key file against a local receiver that checks the HMAC.
  4. Pushed one commit. The receiver got event=push, hmac_valid=true, and responded 200 on the first attempt. That was 2.9 s after the refUpdate's $createdAt.
  5. Ran dg webhook remove. The relay dropped the repo on its next refresh, and a second push produced no delivery.
  6. The secret never appears in the relay log.

Gates

cargo fmt --check, cargo clippy --workspace --all-targets -D warnings, cargo test --workspace, and actionlint on release.yml all pass.

Gaps

  • Delivery is best-effort. Retries happen only within one 30 s window, and there is no durable retry queue, so a receiver that is down longer loses the event (a DEAD-LETTER log line).
  • The dg webhook remove path that writes a tombstone over another maintainer's still-current hook was not tested live.
  • Merge verification checks the merge oid against the base ref's recorded tips, not full commit reachability.
  • A check_run updated in place (queued → completed) is not observed; only creations are.
  • Tenants behind one CDN address share that address's delivery slots (documented in the README).
  • At about 5.6k lines this PR is over the ~3000-line split guideline. It is one component (the relay rewrite plus its shared codec), so it is kept whole.

🤖 Generated with Claude Code

PastaPastaPasta and others added 5 commits September 26, 2026 13:41
forge-core gains envelope (Platform's encryptedFor ecdh-secp256k1-aes256-cbc via platform-encryption, known-answer tested), an SDK-free identity key view, and webhooks (the forge-collab webhook document: newest per (repoId, hookId), maintainer-signed create/remove, relay-side secret decryption with key and length checks). dg webhook add/list/remove writes them.

forge-relay is rewritten for forge-v2: it discovers hooks addressed to its identity through the relay index, requires the writer to still be a maintainer, decrypts secrets in memory, and polls each repo's refUpdate/protectedRefUpdate, release, issue, patch, event/authorEvent, comment, review and checkRun streams. New release and pull_request_review payloads. The SSRF guard now uses reqwest's own URL parser, refuses userinfo and NAT64/multicast/reserved ranges; delivery is bounded per host with a body cap. v1 registry lookups and the plaintext secrets map are gone. Dockerfiles plus a release job that publishes ghcr.io forge-relay images.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Delivery no longer blocks polling: per-hook workers with bounded queues, a circuit breaker with exponential cool-down, per-destination-IP pools and a per-repo time budget per cycle. Stream cursors are (createdAt, seen ids) instead of a start_after id, so a deleted comment or review no longer kills its stream. Pushes are reported only when the update moves its ref by the protected-ref rule (rules::is_update_valid, now public); the CI example verifies the folded ref tip (repo::read_ref_state). Late-appearing repos start no earlier than the relay's start and resume where they stopped.

Secrets must be 32..=96 printable ASCII bytes, checked on add and on decrypt (the real wrong-key check). dg webhook add refuses URLs with a query or userinfo unless --force; logs show scheme and host only. The relay loads a minimal ENCRYPTION-key file. Merges are merged:true only when the oid was a valid base-ref tip. Open PR heads seed and bound check_run streams; thread streams are limited to open or recently active targets. webhook remove writes its tombstone first, addressed to the remover. The image push job needs publish and the release environment; archives are checksummed before imaging; base images are pinned by digest and protoc is sha256-checked. Signing moved to forge_core::webhooks.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Polls are never cancelled mid-stream: the hard per-repo timeout is gone, the deadline is checked between streams, and a stream's cursor is saved only after its events are enqueued. Merges are checked against a per-repo set of valid tips per base ref (backfilled once at init, fed by the ref streams) and require baseRefNameHash to match baseRefName. Discovery and repo setup run in their own task with timeouts; repos are polled concurrently (8 at once), skipping one whose last poll is still running.

Check-run heads carry their own baseline, so a restart or a re-pushed oid never replays old runs; only recently opened PRs are seeded. A repo coming back resumes no earlier than its hook, and streams only some hooks want start no earlier than those hooks. Config is read after the ref streams and a failed config read defers judging. Threads beyond the priority set are read in rotation.

Delivery takes a slot per attempt (2 per address and host under 8 per address), releases it during backoff, and a slot wait does not trip the breaker; removed hooks drop their queues. Secrets are wiped on drop and the relay warns about key files holding more than ENCRYPTION keys. The CI example checks that a valid update set the ref to the pushed oid. INSTALL.md documents the two release approvals.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A new repo is published to the poller only after the dispatcher knows its hooks and its wants are set, under one lock, and enqueue logs once per repo if a served repo has no hook. The event feed is skipped in a cycle whose ref streams or config could not be read, so merges are never judged against stale tips. With a Tail baseline, init_repo primes the config, issue and PR cursors from the reads that built its state (and the ref cursors from their newest page), so nothing written between setup and the first poll is lost.

A poll resumes at the stage (repo streams, threads, check runs) where its deadline stopped it, and the thread rotation advances only by the threads read. Merge tips are tracked only for PR base refs, backfilled per refNameHash. Pruned heads drop their checkRun cursors. A slot timeout now fails only that attempt; the shared-CDN-address limit is documented.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Sep 26, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 6 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: fef53559-d244-45a1-9040-1d935e9ebf5e

📥 Commits

Reviewing files that changed from the base of the PR and between ba5e390 and 6cf29ac.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (31)
  • .github/workflows/release.yml
  • Cargo.toml
  • crates/dg/src/errors.rs
  • crates/dg/src/main.rs
  • crates/dg/src/webhook.rs
  • crates/forge-core/Cargo.toml
  • crates/forge-core/src/envelope.rs
  • crates/forge-core/src/keystore.rs
  • crates/forge-core/src/lib.rs
  • crates/forge-core/src/platform.rs
  • crates/forge-core/src/refs.rs
  • crates/forge-core/src/repo.rs
  • crates/forge-core/src/rules.rs
  • crates/forge-core/src/webhooks.rs
  • crates/forge-relay/Cargo.toml
  • crates/forge-relay/Dockerfile
  • crates/forge-relay/Dockerfile.release
  • crates/forge-relay/README.md
  • crates/forge-relay/examples/ci_consumer.rs
  • crates/forge-relay/src/config.rs
  • crates/forge-relay/src/daemon.rs
  • crates/forge-relay/src/deliver.rs
  • crates/forge-relay/src/error.rs
  • crates/forge-relay/src/ingest.rs
  • crates/forge-relay/src/main.rs
  • crates/forge-relay/src/payload.rs
  • crates/forge-relay/src/ssrf.rs
  • crates/forge-relay/src/subscriptions.rs
  • docs/INSTALL.md
  • docs/prd/05-forge-relay.md
  • e2e/README.md

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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