Skip to content

feat(alerts): outbound HTTPS webhooks + delivery log (#225) - #509

Merged
unjica merged 14 commits into
developfrom
feature/225-alert-webhooks
Jul 17, 2026
Merged

unjica merged 14 commits into
developfrom
feature/225-alert-webhooks

Conversation

@unjica

@unjica unjica commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Implements project-scoped HTTPS alert webhooks (Integration: outbound HTTPS alert webhooks #225): CRUD/enable/test on Alerts → Delivery, signed alert.fired JSON from fireProjectAlert, retry + AlertWebhookDelivery persistence, SSRF-safe URL checks (no redirect follow).
  • Adds the missing operator-facing delivery log: GET /api/project/webhook-deliveries (optional webhookId filter) and a Recent webhook deliveries list in Alerts → Delivery (status, timestamps, HTTP/error summary, which webhook).
  • Parent notifications vision remains #492 (vision only); this PR does not start Notification Center #508 or other channel children.

Test plan

  • Apply migration 20260717120000_project_alert_webhooks and confirm ProjectWebhook / AlertWebhookDelivery exist
  • As editor/owner: add an HTTPS webhook, copy signing secret once, enable/disable, remove
  • Reject http://, localhost, private IPs, and IPv4 shorthand like 127.1 on create
  • Click Test and confirm SUCCESS (or FAILED/DEAD) appears under Recent webhook deliveries
  • Fire a real alert (spike/quota) and confirm delivery rows + alert still persists if the endpoint fails
  • As viewer: can read webhooks/deliveries but cannot mutate
  • pnpm lint && pnpm test && pnpm build (already green locally)

Made with Cursor

Add project webhook CRUD, signed alert.fired dispatch with retry/dead-letter, and an operator-facing delivery history on Alerts → Delivery so MVP #225 is closable after merge.

Co-authored-by: Cursor <[email protected]>
@unjica unjica mentioned this pull request Jul 17, 2026
8 of 10 tasks
…as FAILED

DNS-rebinding helper hostnames passed create-time string checks; re-check resolved IPs on each delivery. Single-shot test deliveries never retry, so record FAILED instead of DEAD.

Co-authored-by: Cursor <[email protected]>
Comment thread apps/api/src/lib/alert-dispatch.ts Fixed
unjica and others added 2 commits July 17, 2026 11:23
Use the resolved, validated address for direct alert deliveries and record retry outcomes without relying on unsupported queue states.

Co-authored-by: Cursor <[email protected]>
Enqueue PENDING AlertWebhookDelivery rows before fireProjectAlert returns,
process them via a claim/lease worker (brief-worker pattern), and pin HTTPS
connects to validated resolved IPs to close DNS-rebinding TOCTOU.

Co-authored-by: Cursor <[email protected]>
@unjica

unjica commented Jul 17, 2026

Copy link
Copy Markdown
Collaborator Author

Merge blockers addressed

1. DNS-based SSRF (pin-to-IP)

  • Delivery-time resolveWebhookHostForDelivery() resolves A/AAAA and rejects private/loopback/link-local/ULA/metadata ranges (IPv4 + IPv6).
  • postWebhookOnce() connects via Node https.request with a custom lookup that returns only the validated address, while keeping TLS SNI + Host as the original hostname (no separate DNS-then-fetch(url) TOCTOU).
  • Redirects are not followed (https.request does not auto-follow).

2. Durable delivery + retries

  • fireProjectAlert now awaits enqueueAlertWebhookDeliveries() so PENDING AlertWebhookDelivery rows exist before return.
  • New alert-webhook-worker reuses the brief-worker claim/lease pattern (FOR UPDATE SKIP LOCKED, lease_owner / lease_expires_at, next_attempt_at).
  • Flow: claim → DNS/IP validate + pin → POST → SUCCESS, or FAILED + backoff, or terminal DEAD after 2 attempts.
  • Run: pnpm --filter api alert-webhook-worker (see docs/ALERT-WEBHOOKS.md).

Schema fields on AlertWebhookDelivery: PENDING/PROCESSING, lease_owner, lease_expires_at, next_attempt_at, updated_at.

Commit: 3e44631 (plus prior SSRF groundwork on this branch).

Comment thread apps/api/src/lib/alert-dispatch.ts Resolved
Keep AlertEvent insert and PENDING enqueue in one transaction so a failed createMany cannot orphan a dedupe key, and reuse the test delivery UUID in the log row.

Co-authored-by: Cursor <[email protected]>
Normalize IPv6 hextets so IPv4-mapped and IPv4-compatible forms
(compressed, expanded, or hex-mapped) cannot bypass the delivery blocklist.

Co-authored-by: Cursor <[email protected]>
Check completeAlertWebhookDelivery after successful POST so a lost lease
is not reported as success, renew the lease before POST, and create
webhooks under a serializable count+insert to honor MAX_PROJECT_WEBHOOKS.

Co-authored-by: Cursor <[email protected]>
When HTTP delivery succeeds but the lease-scoped complete misses, still
mark the row SUCCESS so it cannot stay PROCESSING and be POSTed again.

Co-authored-by: Cursor <[email protected]>
Prevent short lease overrides from expiring mid-HTTPS POST, and persist
test delivery rows before sending so Recent deliveries cannot miss a hit.

Co-authored-by: Cursor <[email protected]>
Exclude webhook:test: rows from worker claim and fail null alert_event_id
deliveries instead of inventing a generic payload that could double-POST.

Co-authored-by: Cursor <[email protected]>
Retry the final SUCCESS/FAILED update and fall back to FAILED so
claim-excluded webhook:test rows cannot stay PROCESSING forever.

Co-authored-by: Cursor <[email protected]>
Clear PROCESSING immediately without burning the attempt toward DEAD, so a still-owned row is not stuck until lease expiry.

Co-authored-by: Cursor <[email protected]>

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit c5ec495. Configure here.

Reclaiming an expired lease no longer increments attempt, so a crash before
POST still gets the full WEBHOOK_MAX_ATTEMPTS of real deliveries.

Co-authored-by: Cursor <[email protected]>
@unjica
unjica merged commit fc8bff5 into develop Jul 17, 2026
6 of 8 checks passed
@unjica
unjica deleted the feature/225-alert-webhooks branch July 17, 2026 10:46
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