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

Open Beginner friendly
#59 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
postgresql, redis, typescript

Research direction

Read apps/api/src/instagram/instagram.controller.ts around handleWebhook and apps/api/src/instagram/services/instagram-webhook-router.service.ts around processEchoMessagingEvent. Check existing webhook signature and Redis deduplication patterns, then run the relevant API tests. Done means configured secrets trigger signature verification and duplicate echo events return before conversation or follow-up writes.

Written by the indexing model from the issue text.

Description

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.
Dominant language
TypeScript
Stars
2
Forks
1
Avg merge
6m
Merged PRs (30d)
27

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sumamakhan761/tryinstance

All issues in sumamakhan761/tryinstance

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.