Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Notifier] [Twilio] Ensure from/sender is valid via regex #43497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,51 @@

namespace Symfony\Component\Notifier\Bridge\Clickatell\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransport;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Test\TransportTestCase;
use Symfony\Component\Notifier\Transport\TransportInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

class ClickatellTransportTest extends TestCase
final class ClickatellTransportTest extends TransportTestCase
{
public function testToString()
/**
* @return ClickatellTransport
*/
public function createTransport(HttpClientInterface $client = null, string $from = null): TransportInterface
{
$transport = new ClickatellTransport('authToken', 'fromValue', $this->createMock(HttpClientInterface::class));
$transport->setHost('clickatellHost');
return new ClickatellTransport('authToken', $from, $client ?? $this->createMock(HttpClientInterface::class));
}

$this->assertSame('clickatell://clickatellHost?from=fromValue', (string) $transport);
public function toStringProvider(): iterable
{
yield ['clickatell://api.clickatell.com', $this->createTransport()];
yield ['clickatell://api.clickatell.com?from=TEST', $this->createTransport(null, 'TEST')];
}

public function testSupports()
public function supportedMessagesProvider(): iterable
{
$transport = new ClickatellTransport('authToken', 'fromValue', $this->createMock(HttpClientInterface::class));
yield [new SmsMessage('+33612345678', 'Hello!')];
}

$this->assertTrue($transport->supports(new SmsMessage('+33612345678', 'testSmsMessage')));
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
public function unsupportedMessagesProvider(): iterable
{
yield [new ChatMessage('Hello!')];
yield [$this->createMock(MessageInterface::class)];
}

public function testExceptionIsThrownWhenNonMessageIsSend()
{
$transport = new ClickatellTransport('authToken', 'fromValue', $this->createMock(HttpClientInterface::class));
$transport = $this->createTransport();

$this->expectException(LogicException::class);

$transport->send($this->createMock(MessageInterface::class));
}

Expand All @@ -56,10 +68,11 @@ public function testExceptionIsThrownWhenHttpSendFailed()

$client = new MockHttpClient($response);

$transport = new ClickatellTransport('authToken', 'fromValue', $client);
$transport = $this->createTransport($client);

$this->expectException(TransportException::class);
$this->expectExceptionMessage('Unable to send SMS with Clickatell: Error code 105 with message "Invalid Account Reference EX0000000" (https://documentation-page).');

$transport->send(new SmsMessage('+33612345678', 'testSmsMessage'));
$transport->send(new SmsMessage('+33612345678', 'Hello!'));
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Twilio/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.4
---

* Ensure sender/from is valid via regex

5.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,25 @@

namespace Symfony\Component\Notifier\Bridge\Twilio\Tests;

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransport;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Test\TransportTestCase;
use Symfony\Component\Notifier\Transport\TransportInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

final class TwilioTransportTest extends TransportTestCase
{
/**
* @return TwilioTransport
*/
public function createTransport(HttpClientInterface $client = null): TransportInterface
public function createTransport(HttpClientInterface $client = null, string $from = 'from'): TransportInterface
{
return new TwilioTransport('accountSid', 'authToken', 'from', $client ?? $this->createMock(HttpClientInterface::class));
return new TwilioTransport('accountSid', 'authToken', $from, $client ?? $this->createMock(HttpClientInterface::class));
}

public function toStringProvider(): iterable
Expand All @@ -44,4 +47,95 @@ public function unsupportedMessagesProvider(): iterable
yield [new ChatMessage('Hello!')];
yield [$this->createMock(MessageInterface::class)];
}

/**
* @dataProvider invalidFromProvider
*/
public function testInvalidArgumentExceptionIsThrownIfFromIsInvalid(string $from)
{
$transport = $this->createTransport(null, $from);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage(sprintf('The "From" number "%s" is not a valid phone number, shortcode, or alphanumeric sender ID.', $from));

$transport->send(new SmsMessage('+33612345678', 'Hello!'));
}

public function invalidFromProvider(): iterable
{
// alphanumeric sender ids
yield 'too short' => ['a'];
yield 'too long' => ['abcdefghijkl'];

// phone numbers
yield 'no zero at start if phone number' => ['+0'];
yield 'phone number to short' => ['+1'];
}

/**
* @dataProvider validFromProvider
*/
public function testNoInvalidArgumentExceptionIsThrownIfFromIsValid(string $from)
{
$message = new SmsMessage('+33612345678', 'Hello!');

$response = $this->createMock(ResponseInterface::class);
$response->expects($this->exactly(2))
->method('getStatusCode')
->willReturn(201);
$response->expects($this->once())
->method('getContent')
->willReturn(json_encode([
'sid' => '123',
'message' => 'foo',
'more_info' => 'bar',
]));

$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://api.twilio.com/2010-04-01/Accounts/accountSid/Messages.json', $url);

return $response;
});

$transport = $this->createTransport($client, $from);

$sentMessage = $transport->send($message);

$this->assertSame('123', $sentMessage->getMessageId());
}

public function validFromProvider(): iterable
{
// alphanumeric sender ids
yield ['ab'];
yield ['abc'];
yield ['abcd'];
yield ['abcde'];
yield ['abcdef'];
yield ['abcdefg'];
yield ['abcdefgh'];
yield ['abcdefghi'];
yield ['abcdefghij'];
yield ['abcdefghijk'];
yield ['abcdef ghij'];
yield [' abcdefghij'];
yield ['abcdefghij '];

// phone numbers
yield ['+11'];
yield ['+112'];
yield ['+1123'];
yield ['+11234'];
yield ['+112345'];
yield ['+1123456'];
yield ['+11234567'];
yield ['+112345678'];
yield ['+1123456789'];
yield ['+11234567891'];
yield ['+112345678912'];
yield ['+1123456789123'];
yield ['+11234567891234'];
yield ['+112345678912345'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Notifier\Bridge\Twilio;

use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\MessageInterface;
Expand Down Expand Up @@ -57,6 +58,10 @@ protected function doSend(MessageInterface $message): SentMessage
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
}

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

$endpoint = sprintf('https://%s/2010-04-01/Accounts/%s/Messages.json', $this->getEndpoint(), $this->accountSid);
$response = $this->client->request('POST', $endpoint, [
'auth_basic' => $this->accountSid.':'.$this->authToken,
Expand Down