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/event/PublishToExchangeListener.php (4 issues)

a variable is defined before it is read.

Bug Major

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\event;
4
5
use League\Event\AbstractListener;
6
use League\Event\EventInterface;
7
use PhpAmqpLib\Connection\AMQPStreamConnection;
8
use PhpAmqpLib\Channel\AMQPChannel;
9
use PhpAmqpLib\Message\AMQPMessage;
10
use Psr\Log\LoggerInterface;
11
use yii\base\InvalidConfigException;
12
use yii\helpers\Inflector;
13
14
/**
15
 * Class PublishToExchangeListener published events to AMQP exchange.
16
 *
17
 * @author Dmytro Naumenko <[email protected]>
18
 */
19
class PublishToExchangeListener extends AbstractListener
20
{
21
    /**
22
     * @var AMQPStreamConnection
23
     */
24
    protected $amqp;
25
26
    /**
27
     * @var LoggerInterface
28
     */
29
    private $logger;
30
31
    /**
32
     * @var AMQPChannel
33
     */
34
    protected $channel;
35
36
    /**
37
     * @var string the exchange name for the published messages
38
     */
39
    public $exchange;
40
41
    /**
42
     * @var string the exchange type. Defaults to 'direct'
43
     */
44
    public $exchangeType = 'direct';
45
46
    public function __construct(AMQPStreamConnection $amqp, LoggerInterface $logger)
47
    {
48
        $this->amqp = $amqp;
49
        $this->logger = $logger;
50
    }
51
52
    /**
53
     * Handle an event.
54
     * @param EventInterface $event
55
     * @return void
56
     */
57
    public function handle(EventInterface $event): void
58
    {
59
        if ($this->exchange === null) {
60
            throw new \RuntimeException('Property PublishToQueueListener::queue must be set');
61
        }
62
63
        try {
64
            $message = $this->createMessage($event);
65
            $this->getChannel()->basic_publish($message, $this->exchange, $this->buildRoutingKey($event));
66
        } catch (InvalidConfigException $exception) {
67
            $this->logger->critical($exception->getMessage());
68
        }
69
    }
70
71
    protected function getChannel(): AMQPChannel
72
    {
73
        if ($this->channel === null) {
74
            $this->channel = $channel = $this->amqp->channel();
75
            $channel->exchange_declare($this->exchange, $this->exchangeType, false, true, true, false, false);
76
        }
77
78
        return $this->channel;
79
    }
80
81
    private function createMessage($event): AMQPMessage
82
    {
83
        if (!$event instanceof \JsonSerializable) {
84
            throw new InvalidConfigException('Event "' . get_class($event) . '" can not be sent to exchange');
85
        }
86
87
        return new AMQPMessage(json_encode($event->jsonSerialize(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), [
88
            'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
89
            'content_type' => 'application/json',
90
        ]);
91
    }
92
93
    /**
94
     * Builds routing key for $event. Default logic:
95
     *
96
     * For events named with `Was` (or `WillBe`) keyword (e.g. `ObjectWasChanged`) routing key will be `object.was.changed`.
97
     * For other events [[InvalidConfigException]] will be thrown.
98
     *
99
     * You can override this method to implement own routing key generation logic.
100
     *
101
     * @param EventInterface $event
102
     * @return string
103
     * @throws InvalidConfigException when `Was` keyword
104
     */
105
    public function buildRoutingKey(EventInterface $event)
106
    {
107
        $className = (new \ReflectionClass($event))->getShortName();
108
109
        foreach (['Was', 'WillBe', 'Will'] as $keyword) {
110
            if (strpos($className, $keyword) === false) {
111
                continue;
112
            }
113
114
            [$object, $eventName] = explode($keyword, $className);
0 ignored issues
show
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
The variable $eventName seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
115
116
            $object = Inflector::camel2id($object);
0 ignored issues
show
The variable $object seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
117
            $eventName = Inflector::camel2id($eventName);
0 ignored issues
show
The variable $eventName seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
118
            $lowerKeyword = strtolower($keyword);
119
120
            return mb_strtolower("$object.$lowerKeyword.$eventName");
121
        }
122
123
        throw new InvalidConfigException("Event class name \"$className\" does not contain \"Was\" or \"WillBe\" keywords and can not be processed with default logic.");
124
    }
125
}
126