Skip to content

Commit 2e57ef0

Browse files
authoredJun 15, 2019
Merge pull request #970 from noplanman/772-simple_prevent_system_command_calls
Prevent system commands from being called by the user directly
2 parents 0e5821b + c1b3a82 commit 2e57ef0

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed
 

‎CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
1414
- `TelegramLog` now adheres to [PSR-3] `LoggerInterface` and allows custom logger implementations.
1515
### Deprecated
1616
- Old logging that uses Monolog still works but will be removed in the near future. Use `TelegramLog::initialize($logger, $update_logger);` from now on.
17+
- [:exclamation:][unreleased-bc-startcommand-is-now-a-usercommand] `StartCommand` is now a `UserCommand` (not `SystemCommand` any more).
1718
### Removed
1819
- Botan.io integration completely removed.
1920
### Fixed
2021
- `forward_date` is now correctly saved to the DB.
2122
- Broken `StickerSet::getStickers()` method.
2223
### Security
2324
- Security disclosure managed by Tidelift.
25+
- Don't allow a user to call system commands directly.
2426

2527
## [0.57.0] - 2019-06-01
2628
:exclamation: After updating to this version, you will need to execute the [SQL migration script][0.57.0-sql-migration] on your database.
@@ -273,7 +275,11 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
273275
- Move `hideKeyboard` to `removeKeyboard`.
274276

275277
[unreleased-sql-migration]: https://github.com/php-telegram-bot/core/tree/develop/utils/db-schema-update/unreleased.sql
278+
<<<<<<< HEAD
279+
[unreleased-bc-startcommand-is-now-a-usercommand]: https://github.com/php-telegram-bot/core/wiki/Breaking-backwards-compatibility#startcommand-is-now-a-usercommand
280+
=======
276281
[unreleased-bc]: https://github.com/php-telegram-bot/core/wiki/Breaking-backwards-compatibility#unreleased
282+
>>>>>>> upstream/develop
277283
[0.57.0-sql-migration]: https://github.com/php-telegram-bot/core/tree/master/utils/db-schema-update/0.56.0-0.57.0.sql
278284
[0.55.0-sql-migration]: https://github.com/php-telegram-bot/core/tree/master/utils/db-schema-update/0.54.1-0.55.0.sql
279285
[0.55.0-bc-move-animation-out-of-games-namespace]: https://github.com/php-telegram-bot/core/wiki/Breaking-backwards-compatibility#move-animation-out-of-games-namespace

‎src/Commands/SystemCommand.php

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515

1616
abstract class SystemCommand extends Command
1717
{
18+
/**
19+
* @{inheritdoc}
20+
*
21+
* Set to empty string to disallow users calling system commands.
22+
*/
23+
protected $usage = '';
24+
1825
/**
1926
* A system command just executes
2027
*

‎src/Commands/SystemCommands/StartCommand.php renamed to ‎src/Commands/UserCommands/StartCommand.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
* file that was distributed with this source code.
99
*/
1010

11-
namespace Longman\TelegramBot\Commands\SystemCommands;
11+
namespace Longman\TelegramBot\Commands\UserCommands;
1212

13-
use Longman\TelegramBot\Commands\SystemCommand;
13+
use Longman\TelegramBot\Commands\UserCommand;
14+
use Longman\TelegramBot\Entities\ServerResponse;
15+
use Longman\TelegramBot\Exception\TelegramException;
1416

1517
/**
1618
* Start command
17-
*
18-
* @todo Remove due to deprecation!
1919
*/
20-
class StartCommand extends SystemCommand
20+
class StartCommand extends UserCommand
2121
{
2222
/**
2323
* @var string
@@ -37,21 +37,20 @@ class StartCommand extends SystemCommand
3737
/**
3838
* @var string
3939
*/
40-
protected $version = '1.0.0';
40+
protected $version = '1.1.0';
4141

4242
/**
4343
* Command execute method
4444
*
45-
* @return mixed
45+
* @return ServerResponse
46+
* @throws TelegramException
4647
*/
4748
public function execute()
4849
{
4950
//$message = $this->getMessage();
5051
//$chat_id = $message->getChat()->getId();
5152
//$user_id = $message->getFrom()->getId();
5253

53-
trigger_error(__CLASS__ . ' is deprecated and will be removed and handled by ' . GenericmessageCommand::class . ' by default in a future release.', E_USER_DEPRECATED);
54-
5554
return parent::execute();
5655
}
5756
}

‎src/Telegram.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -460,15 +460,16 @@ public function processUpdate(Update $update)
460460
if ($update_type === 'message') {
461461
$message = $this->update->getMessage();
462462
$type = $message->getType();
463-
if ($type === 'command') {
464-
$command = $message->getCommand();
465-
} else {
466-
// Let's check if the message object has the type field we're looking for
467-
// and if a fitting command class is available.
468-
$command_tmp = $this->getCommandFromType($type);
469-
if ($this->getCommandObject($command_tmp) !== null) {
470-
$command = $command_tmp;
471-
}
463+
464+
// Let's check if the message object has the type field we're looking for...
465+
$command_tmp = $type === 'command' ? $message->getCommand() : $this->getCommandFromType($type);
466+
// ...and if a fitting command class is available.
467+
$command_obj = $this->getCommandObject($command_tmp);
468+
469+
// Empty usage string denotes a non-executable command.
470+
// @see https://github.com/php-telegram-bot/core/issues/772#issuecomment-388616072
471+
if ($command_obj !== null && $command_obj->getUsage() !== '') {
472+
$command = $command_tmp;
472473
}
473474
} else {
474475
$command = $this->getCommandFromType($update_type);

0 commit comments

Comments
 (0)