Skip to content

[Notifier] [Slack] Validate token syntax #39606

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
Dec 28, 2020
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Check for maximum number of buttons in Slack action block
* Add HeaderBlock
* Slack access tokens needs to start with "xox" (see https://api.slack.com/authentication/token-types)

5.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Notifier\Bridge\Slack;

use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
Expand All @@ -35,6 +36,10 @@ final class SlackTransport extends AbstractTransport

public function __construct(string $accessToken, string $channel = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
if (!preg_match('/^xox(b-|p-|a-2)/', $accessToken)) {
throw new InvalidArgumentException('A valid Slack token needs to start with "xoxb-", "xoxp-" or "xoxa-2". See https://api.slack.com/authentication/token-types for further information.');
}

$this->accessToken = $accessToken;
$this->chatChannel = $channel;
$this->client = $client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testCreateWithDsn()
{
$factory = $this->createFactory();

$transport = $factory->create(Dsn::fromString('slack://testUser@host.test/?channel=testChannel'));
$transport = $factory->create(Dsn::fromString('slack://xoxb-TestUser@host.test/?channel=testChannel'));

$this->assertSame('slack://host.test?channel=testChannel', (string) $transport);
}
Expand All @@ -33,7 +33,7 @@ public function testCreateWithDsnWithoutPath()
{
$factory = $this->createFactory();

$transport = $factory->create(Dsn::fromString('slack://testUser@host.test?channel=testChannel'));
$transport = $factory->create(Dsn::fromString('slack://xoxb-TestUser@host.test?channel=testChannel'));

$this->assertSame('slack://host.test?channel=testChannel', (string) $transport);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransport;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
Expand All @@ -31,23 +32,31 @@ public function testToStringContainsProperties()
{
$channel = 'test Channel'; // invalid channel name to test url encoding of the channel

$transport = new SlackTransport('testToken', $channel, $this->createMock(HttpClientInterface::class));
$transport = new SlackTransport('xoxb-TestToken', $channel, $this->createMock(HttpClientInterface::class));
$transport->setHost('host.test');

$this->assertSame('slack://host.test?channel=test+Channel', (string) $transport);
}

public function testInstatiatingWithAnInvalidSlackTokenThrowsInvalidArgumentException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('A valid Slack token needs to start with "xoxb-", "xoxp-" or "xoxa-2". See https://api.slack.com/authentication/token-types for further information.');

new SlackTransport('token', 'testChannel', $this->createMock(HttpClientInterface::class));
}

public function testSupportsChatMessage()
{
$transport = new SlackTransport('testToken', 'testChannel', $this->createMock(HttpClientInterface::class));
$transport = new SlackTransport('xoxb-TestToken', 'testChannel', $this->createMock(HttpClientInterface::class));

$this->assertTrue($transport->supports(new ChatMessage('testChatMessage')));
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
}

public function testSendNonChatMessageThrowsLogicException()
{
$transport = new SlackTransport('testToken', 'testChannel', $this->createMock(HttpClientInterface::class));
$transport = new SlackTransport('xoxb-TestToken', 'testChannel', $this->createMock(HttpClientInterface::class));

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

Expand All @@ -70,7 +79,7 @@ public function testSendWithEmptyArrayResponseThrows()
return $response;
});

$transport = new SlackTransport('testToken', 'testChannel', $client);
$transport = new SlackTransport('xoxb-TestToken', 'testChannel', $client);

$transport->send(new ChatMessage('testMessage'));
}
Expand All @@ -93,14 +102,14 @@ public function testSendWithErrorResponseThrows()
return $response;
});

$transport = new SlackTransport('testToken', 'testChannel', $client);
$transport = new SlackTransport('xoxb-TestToken', 'testChannel', $client);

$transport->send(new ChatMessage('testMessage'));
}

public function testSendWithOptions()
{
$token = 'testToken';
$token = 'xoxb-TestToken';
$channel = 'testChannel';
$message = 'testMessage';

Expand Down Expand Up @@ -129,7 +138,7 @@ public function testSendWithOptions()

public function testSendWithNotification()
{
$token = 'testToken';
$token = 'xoxb-TestToken';
$channel = 'testChannel';
$message = 'testMessage';

Expand Down Expand Up @@ -172,14 +181,14 @@ public function testSendWithInvalidOptions()
return $this->createMock(ResponseInterface::class);
});

$transport = new SlackTransport('testToken', 'testChannel', $client);
$transport = new SlackTransport('xoxb-TestToken', 'testChannel', $client);

$transport->send(new ChatMessage('testMessage', $this->createMock(MessageOptionsInterface::class)));
}

public function testSendWith200ResponseButNotOk()
{
$token = 'testToken';
$token = 'xoxb-TestToken';
$channel = 'testChannel';
$message = 'testMessage';

Expand Down