Skip to content

Commit 94cd5fc

Browse files
committed
Fix weird stuff regarding safe mode and replace with actual safe mode, a.k.a. use webhook secret
1 parent 127c5e5 commit 94cd5fc

File tree

6 files changed

+17
-9
lines changed

6 files changed

+17
-9
lines changed

config/nutgram.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ nutgram:
22
# The Telegram bot token
33
token: '%env(string:TELEGRAM_TOKEN)%'
44

5-
# If true, the webhook mode validates the incoming IP range is from a Telegram server
6-
safeMode: false
5+
# Specify webhook secret for increased security
6+
#webhook_secret: '%env(string:TELEGRAM_TOKEN)%'
77

88
# If the nutgram bundle should automatically load the routes from config/telegram.php
99
routes: true

src/Console/WebhookSetCommand.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\Console\Input\InputOption;
1111
use Symfony\Component\Console\Output\OutputInterface;
1212
use Symfony\Component\Console\Style\SymfonyStyle;
13+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
1314

1415
#[AsCommand(
1516
name: 'nutgram:hook:set',
@@ -19,10 +20,13 @@ class WebhookSetCommand extends Command
1920
{
2021
private Nutgram $bot;
2122

22-
public function __construct(Nutgram $bot, string $name = null)
23+
private ParameterBagInterface $parameters;
24+
25+
public function __construct(Nutgram $bot, ParameterBagInterface $parameters, string $name = null)
2326
{
2427
parent::__construct($name);
2528
$this->bot = $bot;
29+
$this->parameters = $parameters;
2630
}
2731

2832
protected function configure(): void
@@ -50,7 +54,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5054
$max_connections = (int)$max_connections;
5155
}
5256

53-
$this->bot->setWebhook($url, ip_address: $ip_address, max_connections: $max_connections);
57+
$secret = $this->parameters->get('nutgram.config')['webhook_secret'];
58+
$this->bot->setWebhook($url, ip_address: $ip_address, max_connections: $max_connections, secret_token: $secret);
5459

5560
$io->info("Bot webhook set with url: $url");
5661

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function getConfigTreeBuilder(): TreeBuilder
1414
$treeBuilder->getRootNode()
1515
->children()
1616
->scalarNode('token')->end()
17-
->booleanNode('safeMode')->end()
17+
->scalarNode('webhook_secret')->defaultNull()->end()
1818
->booleanNode('routes')->end()
1919
->arrayNode('config')
2020
->children()

src/DependencyInjection/Factory/NutgramFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ public function createNutgram(
5353
} else {
5454
$webhook = Webhook::class;
5555

56-
if ($config['safe_mode'] ?? false) {
57-
$webhook = new $webhook(fn() => $requestStack->getCurrentRequest()?->getClientIp());
56+
if ($config['webhook_secret']) {
57+
$webhook = new Webhook(secretToken: $config['webhook_secret']);
58+
$webhook->setSafeMode(true);
5859
}
5960

6061
$bot->setRunningMode($webhook);

tests/Fixtures/test_config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ framework:
44

55
nutgram:
66
token: '%env(string:TELEGRAM_TOKEN)%'
7-
safeMode: false
87
routes: true
8+
webhook_secret: 'VerySecret'
99
config:
1010
botId: 123
1111
apiUrl: 'BlaBla'

tests/Functional/CommandTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use SergiX44\NutgramBundle\Console\WebhookRemoveCommand;
99
use SergiX44\NutgramBundle\Console\WebhookSetCommand;
1010
use Symfony\Component\Console\Tester\CommandTester;
11+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
1112

1213
it('call the logout command', function () {
1314
/** @var \SergiX44\Nutgram\Testing\FakeNutgram $instance */
@@ -72,8 +73,9 @@
7273
it('calls the set webhook', function () {
7374
/** @var \SergiX44\Nutgram\Testing\FakeNutgram $instance */
7475
$instance = static::getContainer()->get(Nutgram::class);
76+
$parameters = static::getContainer()->get(ParameterBagInterface::class);
7577

76-
$commandTester = new CommandTester(new WebhookSetCommand($instance));
78+
$commandTester = new CommandTester(new WebhookSetCommand($instance, $parameters));
7779
$commandTester->execute(['url' => 'http://foo.bar']);
7880
$commandTester->assertCommandIsSuccessful();
7981

0 commit comments

Comments
 (0)