Skip to content

fix(cloud-api): guard contacts[0].profile.name to stop status webhooks from crashing#2609

Closed
iscarelli wants to merge 1 commit into
evolution-foundation:mainfrom
iscarelli:fix/cloud-api-status-contacts-profile
Closed

fix(cloud-api): guard contacts[0].profile.name to stop status webhooks from crashing#2609
iscarelli wants to merge 1 commit into
evolution-foundation:mainfrom
iscarelli:fix/cloud-api-status-contacts-profile

Conversation

@iscarelli

@iscarelli iscarelli commented Jun 26, 2026

Copy link
Copy Markdown

Problem

WhatsApp Cloud API status webhooks (delivered / read receipts) carry a contacts array that does not include a profile object — only message webhooks do. In BusinessStartupService.messageHandle, the code unconditionally reads received.contacts[0].profile.name:

if (received.contacts) pushName = received.contacts[0].profile.name;

For a status payload, received.contacts[0].profile is undefined, so this throws:

TypeError: Cannot read properties of undefined (reading 'name')

The throw happens before the handler can persist/forward the update, so MESSAGES_UPDATE events (delivered/read) are never processed on the WHATSAPP-BUSINESS channel and delivery/read ticks stay broken.

Repro (abridged)

A Cloud API status webhook looks like:

{
  "statuses": [{ "id": "wamid...", "status": "delivered", "recipient_id": "55..." }],
  "contacts": [{ "wa_id": "55..." }]
}

contacts[0].profile is absent → received.contacts[0].profile.name throws TypeError: Cannot read properties of undefined (reading 'name').

Fix

One-line guard with optional chaining so pushName is simply undefined for status webhooks (which don't need a pushName) and the handler continues to process the update:

if (received.contacts) pushName = received.contacts[0]?.profile?.name;

@sourcery-ai

sourcery-ai Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds a null-safe guard when reading WhatsApp Cloud API contact profile names so that status webhooks without a profile object no longer crash the handler and MESSAGES_UPDATE events can be processed correctly.

Sequence diagram for WhatsApp Cloud API status webhook handling with guarded contact profile access

sequenceDiagram
  participant WhatsAppCloudAPI
  participant BusinessStartupService

  WhatsAppCloudAPI->>BusinessStartupService: messageHandle(received)
  alt [received.contacts[0].profile exists]
    BusinessStartupService->>BusinessStartupService: pushName = received.contacts[0].profile.name
  else [received.contacts[0].profile missing]
    BusinessStartupService->>BusinessStartupService: pushName = received.contacts[0]?.profile?.name
  end
  BusinessStartupService->>BusinessStartupService: handle MESSAGES_UPDATE
Loading

File-Level Changes

Change Details Files
Make reading the WhatsApp contact profile name null-safe so status webhooks without profile data no longer throw.
  • Replace direct access to contacts[0].profile.name with optional chaining on both the contact entry and its profile object.
  • Preserve existing behavior for message webhooks that include profile.name while allowing pushName to be undefined for status webhooks.
src/api/integrations/channel/meta/whatsapp.business.service.ts

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai 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.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@NeritonDias

Copy link
Copy Markdown

🔴 👎

O guard em contacts[0].profile.name é uma correção válida e pontual, mas o PR aponta para main. O fluxo de contribuição aqui é via develop — reabre contra develop que a gente reavalia.

@dpaes

@iscarelli

Copy link
Copy Markdown
Author

Thanks for the review!

Looking at develop, this is already guarded there — messageHandle now does:

const incomingContact = received?.contacts?.[0];
if (incomingContact) {
  pushName = incomingContact?.profile?.name ?? incomingContact?.name ?? incomingContact?.wa_id ?? undefined;
}

(src/api/integrations/channel/meta/whatsapp.business.service.ts, ~L487–491)

So a PR against develop would be a no-op — there's nothing to change. The crash only affects the released line (2.4.0-rc2 / main), where the code still does the unguarded received.contacts[0].profile.name.

Closing this, since the develop fix just needs to ship in a release. Thanks again! 🙏

@iscarelli iscarelli closed this Jun 26, 2026
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.

2 participants