Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 59f29c5

Browse filesBrowse files
OskarStarkfabpot
authored andcommitted
[Notifier] [Slack] Validate token syntax
1 parent af43335 commit 59f29c5
Copy full SHA for 59f29c5

File tree

4 files changed

+26
-11
lines changed
Filter options

4 files changed

+26
-11
lines changed

‎src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

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

1011
5.2.0
1112
-----

‎src/Symfony/Component/Notifier/Bridge/Slack/SlackTransport.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/Slack/SlackTransport.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Slack;
1313

14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
1415
use Symfony\Component\Notifier\Exception\LogicException;
1516
use Symfony\Component\Notifier\Exception\TransportException;
1617
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
@@ -35,6 +36,10 @@ final class SlackTransport extends AbstractTransport
3536

3637
public function __construct(string $accessToken, string $channel = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
3738
{
39+
if (!preg_match('/^xox(b-|p-|a-2)/', $accessToken)) {
40+
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.');
41+
}
42+
3843
$this->accessToken = $accessToken;
3944
$this->chatChannel = $channel;
4045
$this->client = $client;

‎src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackTransportFactoryTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackTransportFactoryTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testCreateWithDsn()
2424
{
2525
$factory = $this->createFactory();
2626

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

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

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

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

‎src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackTransportTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/Slack/Tests/SlackTransportTest.php
+18-9Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpClient\MockHttpClient;
1616
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
1717
use Symfony\Component\Notifier\Bridge\Slack\SlackTransport;
18+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
1819
use Symfony\Component\Notifier\Exception\LogicException;
1920
use Symfony\Component\Notifier\Exception\TransportException;
2021
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
@@ -31,23 +32,31 @@ public function testToStringContainsProperties()
3132
{
3233
$channel = 'test Channel'; // invalid channel name to test url encoding of the channel
3334

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

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

41+
public function testInstatiatingWithAnInvalidSlackTokenThrowsInvalidArgumentException()
42+
{
43+
$this->expectException(InvalidArgumentException::class);
44+
$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.');
45+
46+
new SlackTransport('token', 'testChannel', $this->createMock(HttpClientInterface::class));
47+
}
48+
4049
public function testSupportsChatMessage()
4150
{
42-
$transport = new SlackTransport('testToken', 'testChannel', $this->createMock(HttpClientInterface::class));
51+
$transport = new SlackTransport('xoxb-TestToken', 'testChannel', $this->createMock(HttpClientInterface::class));
4352

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

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

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

@@ -70,7 +79,7 @@ public function testSendWithEmptyArrayResponseThrows()
7079
return $response;
7180
});
7281

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

7584
$transport->send(new ChatMessage('testMessage'));
7685
}
@@ -93,14 +102,14 @@ public function testSendWithErrorResponseThrows()
93102
return $response;
94103
});
95104

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

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

101110
public function testSendWithOptions()
102111
{
103-
$token = 'testToken';
112+
$token = 'xoxb-TestToken';
104113
$channel = 'testChannel';
105114
$message = 'testMessage';
106115

@@ -129,7 +138,7 @@ public function testSendWithOptions()
129138

130139
public function testSendWithNotification()
131140
{
132-
$token = 'testToken';
141+
$token = 'xoxb-TestToken';
133142
$channel = 'testChannel';
134143
$message = 'testMessage';
135144

@@ -172,14 +181,14 @@ public function testSendWithInvalidOptions()
172181
return $this->createMock(ResponseInterface::class);
173182
});
174183

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

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

180189
public function testSendWith200ResponseButNotOk()
181190
{
182-
$token = 'testToken';
191+
$token = 'xoxb-TestToken';
183192
$channel = 'testChannel';
184193
$message = 'testMessage';
185194

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.