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

Đang mở Phù hợp với người mới
#59 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
postgresql, redis, typescript

Hướng nghiên cứu

Đọc phần quanh handleWebhook trong apps/api/src/instagram/instagram.controller.ts và phần quanh processEchoMessagingEvent trong apps/api/src/instagram/services/instagram-webhook-router.service.ts. Kiểm tra các mẫu hiện có về xác minh chữ ký webhook và loại bỏ trùng lặp bằng Redis, sau đó chạy các bài kiểm thử API liên quan. Công việc hoàn tất khi các secret đã cấu hình kích hoạt việc xác minh chữ ký và các sự kiện echo trùng lặp được trả về trước khi ghi vào hội thoại hoặc phần theo dõi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.
Ngôn ngữ chính
TypeScript
Star
2
Fork
1
Merge trung bình
6 phút
Pull request đã merge (30 ngày)
27

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của sumamakhan761/tryinstance

Tất cả issue của sumamakhan761/tryinstance

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.