Skip to content

Commit b6fdd6d

Browse files
committed
feature #39606 [Notifier] [Slack] Validate token syntax (OskarStark)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [Notifier] [Slack] Validate token syntax | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | --- | License | MIT | Doc PR | - This PR follows #39560 @odolbeau @malteschlueter @norkunas @fabpot can you confirm all your tokens start with `xox`? _From the Slack documentation:_ * Bot user token strings begin with `xoxb-` * User token strings begin with `xoxp-` * Workspace access token strings begin with `xoxa-2` Commits ------- 59f29c5 [Notifier] [Slack] Validate token syntax
2 parents 4a053e5 + 59f29c5 commit b6fdd6d

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* The bridge is not marked as `@experimental` anymore
88
* Check for maximum number of buttons in Slack action block
99
* Add HeaderBlock
10+
* Slack access tokens needs to start with "xox" (see https://api.slack.com/authentication/token-types)
1011

1112
5.2.0
1213
-----

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

Lines 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;
@@ -33,6 +34,10 @@ final class SlackTransport extends AbstractTransport
3334

3435
public function __construct(string $accessToken, string $channel = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
3536
{
37+
if (!preg_match('/^xox(b-|p-|a-2)/', $accessToken)) {
38+
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.');
39+
}
40+
3641
$this->accessToken = $accessToken;
3742
$this->chatChannel = $channel;
3843
$this->client = $client;

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

Lines 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

Lines 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy