Issues (94)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/BasicBot.php (11 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework;
20
21
use PhpBotFramework\Exceptions\BotException;
22
23
use PhpBotFramework\Entities\Message;
24
use PhpBotFramework\Entities\CallbackQuery;
25
use PhpBotFramework\Entities\ChosenInlineResult;
26
use PhpBotFramework\Entities\InlineQuery;
27
28
/**
29
 * \class Bot Bot
30
 * \brief Bot class to handle updates and commands.
31
 * \details Class Bot to handle task like API request, or more specific API method like sendMessage, editMessageText, etc..
32
 * An example of its usage is available in webhook.php
33
 *
34
 */
35
class BasicBot extends Core\CoreBot
36
{
37
    use \PhpBotFramework\Commands\CommandHandler;
38
39
    /** @internal
40
      * \brief True if the bot is using webhook? */
41
    protected $_is_webhook;
42
43
    /**
44
     * \brief Construct an empty base bot.
45
     * \details Construct a base bot that can handle updates.
46
     */
47 1
    public function __construct(string $token)
48
    {
49 1
        parent::__construct($token);
50
51
        // Add alias for entity classes
52 1
        class_alias('PhpBotFramework\Entities\Message', 'PhpBotFramework\Entities\EditedMessage');
53 1
        class_alias('PhpBotFramework\Entities\Message', 'PhpBotFramework\Entities\ChannelPost');
54 1
        class_alias('PhpBotFramework\Entities\Message', 'PhpBotFramework\Entities\EditedChannelPost');
55 1
    }
56
57
    /** @} */
58
59
    /**
60
     * \addtogroup Bot
61
     * @{
62
     */
63
64
    /**
65
     * \brief Get update and process it.
66
     * \details Call this method if user is using webhook.
67
     * It'll get bot's update from php::\input, check it and then process it using <b>processUpdate</b>.
68
     */
69
    public function processWebhookUpdate()
70
    {
71
        $this->_is_webhook = true;
72
73
        $this->initCommands();
74
        $this->processUpdate(json_decode(file_get_contents('php://input'), true));
75
    }
76
77
    /**
78
     * \brief Get updates received by the bot, and hold the offset in $offset.
79
     * \details Get the <code>update_id</code> of the first update to parse, set it in $offset and
80
     * then it start an infinite loop where it processes updates and keep $offset on the update_id of the last update received.
81
     * Each processUpdate() method call is surrounded by a try/catch.
82
     * @see getUpdates
83
     * @param int $limit <i>Optional</i>. Limits the number of updates to be retrieved. Values between 1—100 are accepted.
84
     * @param int $timeout <i>Optional</i>. Timeout in seconds for long polling.
85
     */
86
    public function getUpdatesLocal(int $limit = 100, int $timeout = 60)
87
    {
88
        $update = [];
0 ignored issues
show
$update is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
90
        // While there aren't updates to process
91
        while (empty($update = $this->getUpdates(0, 1))) {
0 ignored issues
show
This while loop is empty and can be removed.

This check looks for while loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
92
        }
93
94
        $offset = $update[0]['update_id'];
95
        $this->initCommands();
96
97
        // Process all updates
98
        while (true) {
99
            $updates = $this->execRequest("getUpdates?offset=$offset&limit=$limit&timeout=$timeout");
100
101
            foreach ($updates as $key => $update) {
0 ignored issues
show
The expression $updates of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
102
                try {
103
                    $this->processUpdate($update);
104
                } catch (BotException $e) {
105
                    echo $e->getMessage();
106
                }
107
            }
108
109
            $offset += sizeof($updates);
110
        }
111
    }
112
113
    /** @} */
114
115
    /**
116
     * @internal
117
     * \brief Dispatch each update to the right method (processMessage, processCallbackQuery, etc).
118
     * \details Set $chat_id for each update, $text, $data and $query are set for each update that contains them.
119
     * @param array $update Reference to the update received.
120
     * @return int The id of the update processed.
121
     */
122 5
    protected function processUpdate(array $update) : int
123
    {
124 5
        static $updates_type = [
125
            'message' => 'Message',
126
            'callback_query' => 'CallbackQuery',
127
            'inline_query' => 'InlineQuery',
128
            'channel_post' => 'ChannelPost',
129
            'edited_message' => 'EditedMessage',
130
            'edited_channel_post' => 'EditedChannelPost',
131
            'chosen_inline_result' => 'ChosenInlineResult',
132
            'pre_checkout_query' => 'PreCheckoutQuery',
133
            'shipping_query' => 'ShippingQuery'
134
        ];
135
136 5
        if ($this->processCommands($update)) {
137 3
            return $update['update_id'];
138
        }
139
140 2
        foreach ($updates_type as $offset => $class) {
141 2
            if (isset($update[$offset])) {
142 2
                $object_class = "PhpBotFramework\Entities\\$class";
143 2
                $object = new $object_class($update[$offset]);
144
145 2
                $this->_chat_id = $object->getChatID();
146
147 2
                if (method_exists($object, 'getBotParameter')) {
148
                    $var = $object->getBotParameter();
149
                    $this->{$var['var']} = $var['id'];
150
                }
151
152 2
                $this->{"process$class"}($object);
153
154 2
                return $update['update_id'];
155
            }
156
        }
157
    }
158
159
    /**
160
     * \brief Called every message received by the bot.
161
     * \details Override it to script the bot answer for each message.
162
     * <code>$chat_id</code> and <code>$text</code>, if the message contains text(use getMessageText() to access it), set inside of this function.
163
     * @param Message $message Reference to the message received.
164
     */
165
    protected function processMessage(Message $message)
166
    {
167
    }
168
169
    /**
170
     * \brief Process updates which involve PreCheckout queries (part of Payments API).
171
     * \details Ovveride it to script the bot answer for each PreCheckout query.
172
     * @param PreCheckoutQuery $pre_checkout_query Reference to the query received.
173
     */
174
    protected function processPreCheckoutQuery(PreCheckoutQuery $pre_checkout_query)
175
    {
176
    }
177
178
    /**
179
     * \brief Process updates which involve shipping queries (part of Payments API).
180
     * \details Ovveride it to script the bot answer for each shipping query.
181
     * @param ShippingQuery $shipping_query Reference to the query received.
182
     */
183
    protected function processShippingQuery(ShippingQuery $shipping_query)
0 ignored issues
show
The parameter $shipping_query is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
184
    {
185
    }
186
187
188
    /**
189
     * \brief Called every callback query received by the bot.
190
     * \details Override it to script the bot answer for each callback.
191
     * <code>$chat_id</code> and <code>$data</code>, if set in the callback query(use getCallbackData() to access it) set inside of this function.
192
     * @param CallbackQuery $callback_query Reference to the callback query received.
193
     */
194
    protected function processCallbackQuery(CallbackQuery $callback_query)
0 ignored issues
show
The parameter $callback_query is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
195
    {
196
    }
197
198
    /**
199
     * \brief Called every inline query received by the bot.
200
     * \details Override it to script the bot answer for each inline query.
201
     * $chat_id and $query(use getInlineQuery() to access it) set inside of this function.
202
     * @param InlineQuery $inline_query Reference to the inline query received.
203
     */
204
    protected function processInlineQuery(InlineQuery $inline_query)
0 ignored issues
show
The parameter $inline_query is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
205
    {
206
    }
207
208
    /**
209
     * \brief Called every chosen inline result received by the bot.
210
     * \details Override it to script the bot answer for each chosen inline result.
211
     * <code>$chat_id</code> set inside of this function.
212
     * @param ChosenInlineResult $chosen_inline_result Reference to the chosen inline result received.
213
     */
214
    protected function processChosenInlineResult(ChosenInlineResult $chosen_inline_result)
0 ignored issues
show
The parameter $chosen_inline_result is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
215
    {
216
    }
217
218
    /**
219
     * \brief Called every chosen edited message received by the bot.
220
     * \details Override it to script the bot answer for each edited message.
221
     * <code>$chat_id</code> set inside of this function.
222
     * @param Message $edited_message The message edited by the user.
223
     */
224
    protected function processEditedMessage(Message $edited_message)
0 ignored issues
show
The parameter $edited_message is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
225
    {
226
    }
227
228
    /**
229
     * \brief Called every new post in the channel where the bot is in.
230
     * \details Override it to script the bot answer for each post sent in a channel.
231
     * <code>$chat_id</code> set inside of this function.
232
     * @param Message $post The message sent in the channel.
233
     */
234
    protected function processChannelPost(Message $post)
0 ignored issues
show
The parameter $post is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
235
    {
236
    }
237
238
    /**
239
     * \brief Called every time a post get edited in the channel where the bot is in.
240
     * \details Override it to script the bot answer for each post edited  in a channel.
241
     * <code>$chat_id</code> set inside of this function.
242
     * @param Message $post The message edited in the channel.
0 ignored issues
show
There is no parameter named $post. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
243
     */
244
    protected function processEditedChannelPost(Message $edited_post)
0 ignored issues
show
The parameter $edited_post is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
245
    {
246
    }
247
248
    /** @} */
249
}
250