Issues (78)

Security Analysis    not enabled

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/console/QueueController.php (3 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
namespace hiapi\console;
4
5
use hiapi\exceptions\NotProcessableException;
6
use hiqdev\yii2\autobus\components\AutoBusFactoryInterface;
7
use hiqdev\yii2\autobus\components\AutoBusInterface;
8
use hiqdev\yii2\autobus\exceptions\WrongCommandException;
9
use PhpAmqpLib\Channel\AMQPChannel;
10
use PhpAmqpLib\Connection\AMQPStreamConnection;
11
use PhpAmqpLib\Message\AMQPMessage;
12
use PhpAmqpLib\Wire\AMQPTable;
13
use Psr\Log\LoggerInterface;
14
use yii\base\Module;
15
use yii\console\ExitCode;
16
use yii\helpers\Console;
17
18
/**
19
 * Class QueueController
20
 *
21
 * @author Dmytro Naumenko <[email protected]>
22
 */
23
class QueueController extends \yii\console\Controller
24
{
25
    /**
26
     * @var LoggerInterface
27
     */
28
    protected $logger;
29
    /**
30
     * @var AMQPStreamConnection
31
     */
32
    protected $amqp;
33
    /**
34
     * @var AutoBusFactoryInterface
35
     */
36
    private $busFactory;
37
38
    public function __construct(
39
        $id,
40
        Module $module,
41
        AMQPStreamConnection $amqp,
42
        LoggerInterface $logger,
43
        AutoBusFactoryInterface $busFactory,
44
        array $config = []
45
    ) {
46
        $this->logger = $logger;
47
        $this->amqp = $amqp;
48
        $this->busFactory = $busFactory;
49
50
        parent::__construct($id, $module, $config);
51
    }
52
53
    /**
54
     * @return \PhpAmqpLib\Channel\AMQPChannel
55
     */
56
    protected function createChannel(string $queue): AMQPChannel
57
    {
58
        $channel = $this->amqp->channel();
59
        $channel->queue_declare($queue, false, true, false, false);
60
61
        return $channel;
62
    }
63
64
    /**
65
     * @param string $queueName
66
     * @param int $messagesCount
67
     * @return int
68
     */
69
    public function actionConsume(string $queueName, $messagesCount = 100)
70
    {
71
        $channel = $this->createChannel($queueName);
72
        $bus = $this->busFactory->get($queueName);
73
74
        Console::output(' [*] Waiting for messages. To exit press CTRL+C');
75
76
        $callback = function (AMQPMessage $msg) use (&$messagesCount, $queueName, $channel, $bus) {
77
            Console::output(' [x] Received ' . $msg->body);
78
            $channel->basic_ack($msg->delivery_info['delivery_tag']);
0 ignored issues
show
Deprecated Code introduced by
The property PhpAmqpLib\Message\AMQPMessage::$delivery_info has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
79
            $messagesCount--;
80
81
            try {
82
                $this->handle($bus, $msg);
83
            } catch (NotProcessableException $e) {
84
                $this->requeue($queueName, $msg, $e);
85
            } catch (\Exception $e) {
86
                $this->handleError($queueName, $msg, $e);
87
            }
88
        };
89
90
        $channel->basic_qos(null, 1, null);
0 ignored issues
show
null is of type null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
        $channel->basic_consume($queueName, '', false, false, false, false, $callback);
92
93
        while ($channel->callbacks && $messagesCount > 0) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $channel->callbacks of type callable[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
94
            $channel->wait();
95
        }
96
97
        Console::output(' [x] Reached consumed messages limit. Stopping process.');
98
99
        return ExitCode::OK;
100
    }
101
102
    private function handleError(string $queueName, AMQPMessage $message, \Exception $exception)
103
    {
104
        Console::error(' [E] Error: ' . $exception->getMessage());
105
        $this->logger->warning('Failed to handle message: ' . $exception->getMessage(), ['amqpMessage' => $message, 'exception' => $exception]);
106
        $this->storeRejected($queueName, $message, $exception);
107
    }
108
109
    /**
110
     * Decodes AMQP message and sends it to the handler
111
     * // TODO: move to separate class?
112
     *
113
     * @param AMQPMessage $msg
114
     * @throws WrongCommandException
115
     */
116
    protected function handle(AutoBusInterface $bus, AMQPMessage $msg): void
117
    {
118
        if ($msg->get_properties()['content_type'] !== 'application/json') {
119
            throw new \RuntimeException('Do not know how to decode ' . $msg->getContentEncoding());
120
        }
121
122
        $body = json_decode($msg->getBody(), true);
123
        if (!isset($body['name'])) {
124
            throw new WrongCommandException('Message must have a name');
125
        }
126
        $parts = explode('\\', $body['name']);
127
        $name = array_pop($parts);
128
129
        $bus->runCommand($name, $body);
130
    }
131
132
    /**
133
     * Resends message to queue with a delay
134
     *
135
     * @param string $queueName
136
     * @param AMQPMessage $msg
137
     * @param NotProcessableException $exception
138
     */
139
    private function requeue(string $queueName, AMQPMessage $msg, NotProcessableException $exception): void
140
    {
141
        $tries = 0;
142
        $headers = $msg->get_properties()['application_headers'];
143
        if ($headers instanceof AMQPTable) {
144
            $tries = $headers->getNativeData()['x-number-of-tries'] ?? 0;
145
        }
146
147
        if ($exception->getMaxTries() !== null && $tries >= $exception->getMaxTries()) {
148
            $this->logger->debug('No tries left for message. Marking it as an error', ['amqpMessage' => $msg, 'exception' => $exception]);
149
            $this->handleError($queueName, $msg, $exception);
150
            return;
151
        }
152
153
        // Init delay exchange
154
        $channel = $this->amqp->channel();
155
        $delayExchange = "$queueName.delayed";
156
        $channel->exchange_declare($delayExchange, 'x-delayed-message', false, true, true, false, false, new AMQPTable([
157
            'x-delayed-type' => 'direct',
158
        ]));
159
        $channel->queue_bind($queueName, $delayExchange);
160
161
        // Send message
162
        $delayDuration = 1000 * $exception->getSecondsBeforeRetry() * (int)($exception->getProgressionMultiplier() ** $tries);
163
        $delayMessage = new AMQPMessage($msg->getBody(), array_merge($msg->get_properties(), [
164
            'application_headers' => new AMQPTable([
165
                'x-delay' => $delayDuration,
166
                'x-number-of-tries' => $tries + 1,
167
            ]),
168
        ]));
169
        $channel->basic_publish($delayMessage, $delayExchange, '');
170
        $this->logger->debug('Delayed message for ' . $delayDuration . 'ms', ['amqpMessage' => $msg, 'exception' => $exception]);
171
    }
172
173
    private function storeRejected(string $queueName, AMQPMessage $message, \Exception $exception): void
174
    {
175
        $channel = $this->amqp->channel();
176
        $failedExchange = "$queueName.failed";
177
178
        $channel->exchange_declare($failedExchange, 'fanout', false, true, false);
179
        $channel->basic_publish($message, $failedExchange);
180
    }
181
}
182