feat(automations): make the webhook trigger fire — inbound bearer route, token generation, row UI

Merged
SA
3mo ago
+514-8

Summary

The webhook trigger kind existed on main as a row that could never fire: no inbound route, no token, no UI. This PR adds all three.

  • API POST /api/automations/webhook/{automationId} — verifies Authorization: Bearer sset_wh_… against every enabled webhook trigger on the automation before reading the body (SHA-256 hash in automation_triggers.secret_hash, constant-time compare; 401 on miss; sset_wh_ prefix precheck and a 300/min per-automation rate limit sit before the trigger query), records the JSON body verbatim in automation_events (provider=webhook, event_type=webhook.received, fresh uuid external_event_id), then hands off to the shared dispatchMatchingTriggers({…, automationId}). No dedupe — measured against the reference: one authenticated POST is one event and one run, Idempotency-Key is not honored. Body must be a JSON object/array (empty → {}), 400 otherwise; 413 over 1 MiB; paused automation → 400 Automation <id> is disabled; disabled trigger row → its token stops authenticating. If the QStash publish fails the event row is deleted and 500 returned so a sender retry re-enters cleanly.
  • Shared WebhookMatchableEvent + case "webhook" in triggerMatches → {matches:true} (automation-matching/webhook.ts).
  • tRPC automation.rotateWebhookSecret({triggerId}) returns the token once (hash stored); automation.setTriggerSecret({triggerId, secret}) stores a provider-issued signing secret verbatim for HMAC kinds (Circleback); automation.get returns secretPrefix/secretRotatedAt per trigger (never the column).
  • Desktop row: Webhook triggered · <URL chip, click copies> · Generate auth header → Copy auth header (copies the literal Authorization: Bearer … line) → after reload Regenerate auth header (confirm dialog naming the old prefix). EndpointChip is a shared TriggerSentence/components chip (Circleback reuses it byte-identical). Uses main's SentenceContext.triggerId.

No migration: secret_hash/secret_prefix/secret_rotated_at and the webhook enum value were already on main. Schema comment on secret_hash updated (bearer kinds hash, HMAC kinds raw; never returned by the API).

Verified

  • Desktop over CDP (pre-seam-consolidation build; the rebase was mechanical): add "Webhook triggered" → Save → Generate auth header → Copy auth header (clipboard = Authorization: Bearer sset_wh_…); reload → Regenerate auth header; regenerate → old token 401, new token accepted.
  • curl against the local API on the final head: no auth 401 · bad token 401 · null/scalar body 400 · 1.1 MB 413 · paused automation 400 · two identical bodies → two distinct events · same Idempotency-Key twice → two distinct events · 320-request bad-token burst → 293×401 + 27×429 · good token reaches the shared dispatcher (QStash rejects loopback destinations locally, so the publish itself isn't provable here — same path as the GitHub route).
  • turbo typecheck (desktop, api, trpc, shared, db) exit 0; root bun run lint exit 0.

Not in scope: injecting the event payload into the run prompt (dispatch.ts doesn't read automation_events for GitHub either).

Summary by CodeRabbit

  • New Features
    • Added webhook trigger support for receiving authenticated JSON payloads and dispatching matching automations.
    • Added webhook endpoint display with copy-to-clipboard support, HTTP method details, and helpful status feedback.
    • Added secure webhook token generation, rotation, and one-time token display.
    • Added support for storing provider signing secrets and viewing secret metadata.
  • Security & Reliability
    • Added authorization checks, rate limiting, payload size limits, and validation for webhook requests.
    • Webhook events are cleaned up if automation dispatch fails.