Issues (9)

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/ConnectionFactory.php (2 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 Drupal\rabbitmq;
4
5
use Drupal\Core\Site\Settings;
6
use PhpAmqpLib\Connection\AMQPStreamConnection;
7
use PhpAmqpLib\Connection\AMQPSSLConnection;
8
9
/**
10
 * RabbitMQ connection factory class.
11
 */
12
class ConnectionFactory {
13
  const DEFAULT_CREDENTIALS_KEY = 'default';
14
  const DEFAULT_SERVER_ALIAS = 'localhost';
15
  const DEFAULT_HOST = self::DEFAULT_SERVER_ALIAS;
16
  const DEFAULT_PORT = 5672;
17
  const DEFAULT_USER = 'guest';
18
  const DEFAULT_PASS = 'guest';
19
20
  const CREDENTIALS = 'rabbitmq_credentials';
21
22
  /**
23
   * The RabbitMQ connection.
24
   *
25
   * @var \PhpAmqpLib\Connection\AMQPStreamConnection
26
   */
27
  protected $connection;
28
29
  /**
30
   * The settings service.
31
   *
32
   * @var \Drupal\Core\Site\Settings
33
   */
34
  protected $settings;
35
36
  /**
37
   * The name of the server to connect to.
38
   *
39
   * @var string
40
   */
41 5
  protected $credentialsKey;
42
43 5
  /**
44 5
   * Constructor.
45
   *
46 5
   * @param \Drupal\Core\Site\Settings $settings
47 5
   *   The settings service.
48
   * @param string $credentialsKey
49
   *   The key of the credentials to use for the connection.
50
   */
51
  public function __construct(Settings $settings, $credentialsKey = 'default') {
52
    // Cannot continue if the library wasn't loaded.
53
    assert(class_exists('\PhpAmqpLib\Connection\AMQPStreamConnection'),
54
      'Could not find php-amqplib. See the rabbitmq/README.md file for details.'
55 5
    );
56 5
    $this->settings = $settings;
57
    $this->credentialsKey = $credentialsKey;
58 5
  }
59 5
60 5
  /**
61 5
   * Get a configured connection to RabbitMQ.
62 5
   *
63
   * @return \PhpAmqpLib\Connection\AMQPSSLConnection|\PhpAmqpLib\Connection\AMQPStreamConnection
64
   *   The AMQP or SSL connection.
65 5
   */
66
  public function getConnection() {
67 5
    if (empty($this->connection)) {
68
      if (!empty($credentials['ssl'])) {
0 ignored issues
show
The variable $credentials seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
69
        $connection = $this->getSecureConnection();
70
      }
71
      else {
72
        $connection = $this->getStandardConnection();
73
      }
74
75
      $this->connection = $connection;
76
    }
77
78
    return $this->connection;
79 5
  }
80 5
81 5
  /**
82 5
   * Get the credentials defined in the settings.php file.
83 5
   *
84 5
   * @return array
85
   *   Array of credentials.
86
   */
87 5
  protected function getCredentials() {
88
    $defaultCredentials['default'] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$defaultCredentials was never initialized. Although not strictly required by PHP, it is generally a good practice to add $defaultCredentials = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
89 5
      'host' => static::DEFAULT_SERVER_ALIAS,
90
      'port' => static::DEFAULT_PORT,
91
      'username' => static::DEFAULT_USER,
92
      'password' => static::DEFAULT_PASS,
93
      'vhost' => '/',
94
    ];
95
96
    $credentials = $this->settings->get(self::CREDENTIALS, $defaultCredentials);
97
98
    if (!array_key_exists($this->credentialsKey, $credentials)) {
99
      $this->credentialsKey = static::DEFAULT_CREDENTIALS_KEY;
100
    }
101
102
    return $credentials[$this->credentialsKey];
103
  }
104
105
  /**
106
   * Return SSL connection object.
107
   *
108
   * @return \PhpAmqpLib\Connection\AMQPSSLConnection
109
   *   SSL connection object.
110
   */
111
  protected function getSecureConnection() {
112
    $credentials = $this->getCredentials();
113
    return new AMQPSSLConnection(
114
      $credentials['host'],
115
      $credentials['port'],
116
      $credentials['username'],
117
      $credentials['password'],
118
      $credentials['vhost'],
119
      $credentials['ssl'],
120
      $credentials['options']
121
    );
122
  }
123
124
  /**
125
   * Return standard connection object.
126
   *
127
   * @return \PhpAmqpLib\Connection\AMQPStreamConnection
128
   *   Standard connection object.
129
   */
130
  protected function getStandardConnection() {
131
    $credentials = $this->getCredentials();
132
    $defaultOptions = [
133
      'insist' => FALSE,
134
      'login_method' => 'AMQPLAIN',
135
      'login_response' => NULL,
136
      'locale' => 'en_US',
137
      'connection_timeout' => 3.0,
138
      'read_write_timeout' => 3.0,
139
      'context' => NULL,
140
      'keepalive' => FALSE,
141
      'heartbeat' => 0,
142
    ];
143
144
    if (empty($credentials['options'])) {
145
      $credentials['options'] = [];
146
    }
147
148
    $credentials['options'] = array_merge($defaultOptions, $credentials['options']);
149
150
    return new AMQPStreamConnection(
151
      $credentials['host'],
152
      $credentials['port'],
153
      $credentials['username'],
154
      $credentials['password'],
155
      $credentials['vhost'],
156
      $credentials['options']['insist'],
157
      $credentials['options']['login_method'],
158
      $credentials['options']['login_response'],
159
      $credentials['options']['locale'],
160
      $credentials['options']['connection_timeout'],
161
      $credentials['options']['read_write_timeout'],
162
      $credentials['options']['context'],
163
      $credentials['options']['keepalive'],
164
      $credentials['options']['heartbeat']
165
    );
166
  }
167
168
}
169