Skip to content

[Notifier][Twilio] WhatsApp number validation fails due to missing whatsapp: prefix in regex #61776

@OvidiuGi

Description

@OvidiuGi

Symfony version(s) affected

6.4

Description

The current implementation of TwilioTransport.php fails to validate numbers for use with Twilio's WhatsApp API. The regex used to validate the From number does not account for the required whatsapp: prefix, causing a InvalidArgumentException to be thrown for all valid WhatsApp sender IDs.

This prevents the library from being used to send messages via Twilio's WhatsApp service.

How to reproduce

  1. Attempt to send a message using a From number formatted for WhatsApp (e.g., whatsapp:+14155238886).

  2. The application will throw an InvalidArgumentException.

<?php
require_once __DIR__.'/.vendor/autoload.php';
use Symfony\Component\Notifier\Exception\InvalidArgumentException;

$from = 'whatsapp:+14155238886';

if (!preg_match('/^[a-zA-Z0-9\s]{2,11}$/', $from) && !preg_match('/^\+[1-9]\d{1,14}$/', $from)) {
    throw new InvalidArgumentException(\sprintf('The "From" number "%s" is not a valid phone number, shortcode, or alphanumeric sender ID.', $from));
}

Possible Solution

Modify the regex in TwilioTransport.php to allow for the optional whatsapp: prefix.

@@  -61,67 +61,67 @@
$from = $message->getFrom() ?: $this->from;

- -> if (!preg_match('/^[a-zA-Z0-9\s]{2,11}$/', $from) && !preg_match('/^\+[1-9]\d{1,14}$/', $from)) {
+ -> if (!preg_match('/^[a-zA-Z0-9\s]{2,11}$/', $from) && !preg_match('/^(?:whatsapp:)?\+[1-9]\d{1,14}$/', $from)) {
            throw new InvalidArgumentException(\sprintf('The "From" number "%s" is not a valid phone number, shortcode, or alphanumeric sender ID.', $from));
}

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions