Files
conversation-history/SquareMCP/2026-06-10-twilio-whatsapp-webhook-deployment.md

6.0 KiB

Deployment: Twilio WhatsApp Inbound Webhook + Meta Hardening

Date: 2026-06-10 Branch: main Commit: e5152ee feat(webhook): Twilio WhatsApp inbound route + Meta webhook hardening Service: hermes-mcp (K8s deployment hermes-mcp in namespace fetcherpay) Status: ✅ DEPLOYED Related: 2026-06-10-twilio-whatsapp-inbound-webhook-eng-review

Security note: Meta webhook was fail-open (accepting unsigned requests → 200). Deployed fix now returns 503 when WHATSAPP_APP_SECRET is unset. Fail-closed activated.


Hotfix: Raw Body Preservation (f74f90a)

Bug found during deploy: Setting caused the Meta webhook to crash with .

Root cause: The global middleware (line 77) parses the body into a JavaScript object before the route-level runs. When a signature is present, receives an Object instead of a Buffer.

Fix: Added before to preserve the raw Buffer for HMAC validation.

Verification after hotfix:

Test Result
Meta webhook, no signature
Meta webhook, malformed signature
Meta webhook, wrong-length signature
Meta webhook, valid-length wrong signature
Twilio route, no signature
Twilio route, bad signature
Health endpoint
Pod restarts

What Was Built

  1. POST /webhook/twilio/whatsapp — inbound WhatsApp messages from Twilio land here. The pilot approval loop ("Alex replies 1/2/3 → draft approved → tracking link created → Facebook post published") is now wired.

  2. Meta webhook hardening — two security bugs fixed:

    • WHATSAPP_APP_SECRET unset previously skipped validation entirely (fail-open). Now returns 503.
    • timingSafeEqual on a malformed x-hub-signature-256 header previously threw RangeError → 500. Length guard added → 403.

Files Changed

File Change
src/webhooks/twilio-whatsapp.ts New. HMAC-SHA1 validation, Redis dedup, approval handler, Redis seed
src/webhooks/twilio-whatsapp.test.ts New. 18 tests, all passing
src/index.ts Modified. Import + initTwilioWebhookMapping() in main() + app.post('/webhook/twilio/whatsapp') + Meta fail-open fix + timingSafeEqual length guard

Env Vars Required in hermes-k8s.yaml

All three vars must be set before deploying. TWILIO_AUTH_TOKEN is already at line 146 — do NOT add a duplicate.

- name: PUBLIC_WEBHOOK_BASE_URL
  value: "https://hermes.squaremcp.com"

- name: PILOT_CUSTOMER_ID
  value: "9a3f1a23-3080-4f9f-932c-02dae813ee96"

- name: WHATSAPP_APP_SECRET
  value: "04d52724efa5f3ac5eb3f2b673c3d70a"

PILOT_CUSTOMER_ID: run SELECT id FROM customers WHERE pilot_active = 1 LIMIT 1; against the production MySQL.

WHATSAPP_APP_SECRET: Value confirmed and filled in above. Setting it activates HMAC-SHA256 signature validation on the Meta webhook (was fail-open without it).


Twilio Console Configuration

After deploying, configure the WhatsApp inbound webhook on the Twilio number (+19547385805):

  • Console URL: Twilio Console → Phone Numbers → +19547385805 → Messaging → WhatsApp Sandbox (or registered number)
  • Webhook URL: https://hermes.squaremcp.com/webhook/twilio/whatsapp
  • HTTP Method: POST
  • The voice webhook on the same number stays as-is.

Pre-Deploy Verification (already confirmed by QA)

Check Result
npm run build ✅ clean
vitest run src/webhooks/twilio-whatsapp.test.ts ✅ 18/18
vitest run src/tracking-links.test.ts ✅ 20/20

Post-Deploy Verification

Run these after kubectl rollout status confirms the new pod is up:

# 1. Health
curl https://hermes.squaremcp.com/health
# Expect: {"status":"ok",...}

# 2. Twilio route exists — no signature → 403 (not 404)
curl -s -o /dev/null -w "%{http_code}" -X POST https://hermes.squaremcp.com/webhook/twilio/whatsapp \
  -d "Body=1&From=whatsapp%3A%2B15551234567&MessageSid=SM_test&NumMedia=0&To=whatsapp%3A%2B19547385805"
# Expect: 403

# 3. Twilio route — bad signature → 403
curl -s -o /dev/null -w "%{http_code}" -X POST https://hermes.squaremcp.com/webhook/twilio/whatsapp \
  -H "X-Twilio-Signature: bad" \
  -d "Body=1&From=whatsapp%3A%2B15551234567&MessageSid=SM_test&NumMedia=0&To=whatsapp%3A%2B19547385805"
# Expect: 403

# 4. Meta webhook — no signature → 503 (fail-closed, not 200)
curl -s -w "\nHTTP %{http_code}" -X POST https://hermes.squaremcp.com/webhook/whatsapp \
  -H "Content-Type: application/json" \
  -d '{"object":"whatsapp_business_account","entry":[]}'
# Expect: HTTP 503  (was: HTTP 200 before this fix — confirms WHATSAPP_APP_SECRET is now set)

# 5. Meta webhook — malformed x-hub-signature-256 → 403 (not 500)
curl -s -o /dev/null -w "%{http_code}" -X POST https://hermes.squaremcp.com/webhook/whatsapp \
  -H "Content-Type: application/json" \
  -H "x-hub-signature-256: short" \
  -d '{"object":"whatsapp_business_account","entry":[]}'
# Expect: 403

Approval Flow End-to-End (after deploy)

  1. Weekly cron (not yet built) generates post_drafts rows for PILOT_CUSTOMER_ID with status='pending'
  2. Drafts sent to Alex via Twilio WhatsApp
  3. Alex replies "1" to +19547385805
  4. Twilio POSTs to /webhook/twilio/whatsapp (HMAC-SHA1 validated)
  5. Redis dedup check — skip if already processed
  6. Allowlist check — From must match customers.pilot_owner_phone
  7. post_drafts queried for pending batch
  8. Tracking link created via createTrackingLink()
  9. post_drafts.status → 'approved'
  10. Confirmation sent to Alex via Twilio: "Draft 1 approved and live! Track clicks: https://hermes.squaremcp.com/t/..."

What This Does NOT Include

  • Pilot draft cron (src/pilot/cron.ts) — weekly Claude API draft generation. The approval handler is ready and waiting; it just needs drafts to exist in post_drafts.
  • Actual Facebook publish — the handler marks the draft approved and creates a tracking link. Calling the Facebook API to publish the post is the next wire-up step.
  • GET /dashboard — lead dashboard for Lodge Brothers.