fix(instagram): enforce webhook signature verification outside production and deduplicate echo events before DB writes

Aperta Adatta ai principianti
#59 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
postgresql, redis, typescript

Direzione di ricerca

Leggi la sezione intorno a handleWebhook in apps/api/src/instagram/instagram.controller.ts e quella intorno a processEchoMessagingEvent in apps/api/src/instagram/services/instagram-webhook-router.service.ts. Controlla i modelli esistenti per la verifica della firma dei webhook e la deduplicazione con Redis, quindi esegui i test API pertinenti. Il lavoro è completato quando i segreti configurati attivano la verifica della firma e gli eventi echo duplicati ritornano prima di qualsiasi scrittura di conversazioni o follow-up.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

backend bug good first issue
Problem Description
  1. Webhook signature verification is skipped when NODE_ENV !== 'production':
    In apps/api/src/instagram/instagram.controller.ts (handleWebhook, lines 258-276), Meta's X-Hub-Signature-256 verification is gated behind process.env.NODE_ENV === 'production'.
    Any staging deployment or misconfigured server where NODE_ENV is unset or not 'production' accepts forged, unauthenticated POST /webhooks/instagram payloads, allowing attackers to inject fake DMs/comments and trigger automations or AI replies.

  2. Echo webhook mutates state before deduplication:
    In apps/api/src/instagram/services/instagram-webhook-router.service.ts (processEchoMessagingEvent, lines 475-518), conversation.update (deactivating AI and setting automationPausedUntil = now + 30m) and pendingFollowUp.updateMany (cancelling pending follow-ups) execute before redisService.reserveWebhookEvent('echo:' + messageMid) (lines 512-518).
    If Meta retries an echo webhook, the conversation's pause window can be re-extended before deduplication runs.


Expected Behavior
  1. POST /webhooks/instagram verifies X-Hub-Signature-256 whenever INSTAGRAM_APP_SECRET or META_APP_SECRET is configured (skipping only if an explicit local dev bypass flag such as ALLOW_UNSIGNED_WEBHOOKS=true is set in non-production).
  2. processEchoMessagingEvent checks redisService.reserveWebhookEvent('echo:' + messageMid) first, returning early on duplicates before mutating conversation or pendingFollowUp.

Where to Look
  • apps/api/src/instagram/instagram.controller.ts (handleWebhook, lines 258-276)
  • apps/api/src/instagram/services/instagram-webhook-router.service.ts (processEchoMessagingEvent, lines 475-518)

Solution / Implementation Guide
  1. In instagram.controller.ts (handleWebhook):
    • Enforce X-Hub-Signature-256 verification whenever an app secret is configured (instead of gating on NODE_ENV === 'production'), and allow unsigned requests only when NODE_ENV !== 'production' && process.env.ALLOW_UNSIGNED_WEBHOOKS === 'true' (so local webhook testing tools still work when explicitly enabled).
  2. In instagram-webhook-router.service.ts (processEchoMessagingEvent):
    • Extract messageMid at the top of processEchoMessagingEvent and call redisService.reserveWebhookEvent('echo:' + messageMid) before updating conversation or cancelling pendingFollowUp records.
Lingua principale
TypeScript
Stelle
2
Fork
1
Merge medio
6m
PR unite (30g)
27

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di sumamakhan761/tryinstance

Tutte le issue di sumamakhan761/tryinstance

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.