Issues (1507)

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.

PHPDaemon/Clients/Gearman/Connection.php (8 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 PHPDaemon\Clients\GearmanClient;
4
5
use PHPDaemon\Network\ClientConnection;
6
use PHPDaemon\Utils\Crypt;
7
8
/**
9
 * Class Connections
10
 */
11
class Connection extends ClientConnection
12
{
13
    /**
14
     * Magic code for request
15
     */
16
    const MAGIC_REQUEST = "\0REQ";
17
18
    /**
19
     * Magic code for response
20
     */
21
    const MAGIC_RESPONSE = "\0RES";
22
23
    /*
24
     * Byte length of header
25
     */
26
    const HEADER_LENGTH = 12;
27
28
    /**
29
     * Header binary format
30
     */
31
    const HEADER_WRITE_FORMAT = "a4NN";
32
33
    /**
34
     * Header read format
35
     */
36
    const HEADER_READ_FORMAT = "a4magic/Ntype/Nsize";
37
38
    /**
39
     * Delimeter for function arguments
40
     */
41
    const ARGS_DELIMITER = "\0";
42
43
44
    /**
45
     * Request codes
46
     *
47
     * @var array
48
     */
49
    protected static $requestCommandList = [
50
        'CAN_DO' => 1,
51
        'CANT_DO' => 2,
52
        'RESET_ABILITIES' => 3,
53
        'PRE_SLEEP' => 4,
54
        'SUBMIT_JOB' => 7,
55
        'GRAB_JOB' => 9,
56
        'WORK_STATUS' => 12,
57
        'WORK_COMPLETE' => 13,
58
        'WORK_FAIL' => 14,
59
        'GET_STATUS' => 15,
60
        'ECHO_REQ' => 16,
61
        'SUBMIT_JOB_BG' => 18,
62
        'SUBMIT_JOB_HIGH' => 21,
63
        'SET_CLIENT_ID' => 22,
64
        'CAN_DO_TIMEOUT' => 23,
65
        'ALL_YOURS' => 24,
66
        'WORK_EXCEPTION' => 25,
67
        'OPTION_REQ' => 26,
68
        'OPTION_RES' => 27,
69
        'WORK_DATA' => 28,
70
        'WORK_WARNING' => 29,
71
        'GRAB_JOB_UNIQ' => 30,
72
        'SUBMIT_JOB_HIGH_BG' => 32,
73
        'SUBMIT_JOB_LOW' => 33,
74
        'SUBMIT_JOB_LOW_BG' => 34,
75
        'SUBMIT_JOB_SCHED' => 35,
76
        'SUBMIT_JOB_EPOCH' => 36,
77
    ];
78
    protected static $requestCommandListFlipped;
79
80
    /**
81
     * Response codes
82
     *
83
     * @var array
84
     */
85
    protected static $responseCommandList = [
86
        'NOOP' => 6,
87
        'JOB_CREATED' => 8,
88
        'NO_JOB' => 10,
89
        'JOB_ASSIGN' => 11,
90
        'WORK_STATUS' => 12,
91
        'WORK_COMPLETE' => 13,
92
        'WORK_FAIL' => 14,
93
        'ECHO_RES' => 17,
94
        'ERROR' => 19,
95
        'STATUS_RES' => 20,
96
        'WORK_EXCEPTION' => 25,
97
        'OPTION_RES' => 27,
98
        'WORK_WARNING' => 29,
99
        'JOB_ASSIGN_UNIQ' => 31,
100
    ];
101
102
    protected static $responseCommandListFlipped;
103
104
    /**
105
     * @var mixed
106
     */
107
    public $response;
108
109
    /**
110
     * @var string
111
     */
112
    public $responseType;
113
114
    /**
115
     * @var string
116
     */
117
    public $responseCommand;
118
119
    /**
120
     * Called when new data received
121
     *
122
     * @return void
123
     */
124
    public function onRead()
125
    {
126
        if (($head = $this->lookExact(static::HEADER_LENGTH)) === false) {
127
            return;
128
        }
129
130
        list($magic, $typeInt, $size) = unpack(static::HEADER_READ_FORMAT, $head);
131
132
        if ($this->getInputLength() < static::HEADER_LENGTH + $size) {
133
            return;
134
        }
135
136
        $this->drain(static::HEADER_LENGTH);
137
        $pct = $this->read($size);
138
139
        if ($magic === static::MAGIC_RESPONSE) {
140
            $this->responseType = static::$responseCommandListFlipped[$typeInt];
141
            $this->response = explode(static::ARGS_DELIMITER, $pct);
142
            $this->onResponse->executeOne($this);
143
            $this->responseType = null;
144
            $this->responseCommand = null;
145
            $this->responseType = null;
146
            $this->checkFree();
147
            return;
148
        } else {
149
            $type = static::$requestCommandListFlipped[$typeInt];
0 ignored issues
show
$type 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...
150
            // @TODO
151
        }
152
    }
153
154
    /**
155
     * Called when the connection is handshaked (at low-level), and peer is ready to recv. data
156
     * @return void
157
     */
158
    public function onReady()
159
    {
160
        if (static::$requestCommandListFlipped === null) {
161
            static::$requestCommandListFlipped = array_flip(static::$requestCommandList);
162
        }
163
        if (static::$responseCommandListFlipped === null) {
164
            static::$responseCommandListFlipped = array_flip(static::$responseCommandList);
165
        }
166
        parent::onReady();
167
    }
168
169
170
    /**
171
     * Function send ECHO
172
     *
173
     * @param $payload
174
     * @param callable|null $cb
175
     */
176
    public function sendEcho($payload, $cb = null)
177
    {
178
        $this->sendCommand('ECHO_REQ', $payload, $cb);
179
    }
180
181
    /**
182
     * Send a command
183
     *
184
     * @param $commandName
185
     * @param $payload
186
     * @param callable $cb = null
0 ignored issues
show
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
187
     */
188
    public function sendCommand($commandName, $payload, $cb = null)
189
    {
190
191
        $pct = implode(
192
            static::ARGS_DELIMITER,
193
            array_map(function ($item) {
194
                return !is_scalar($item) ? serialize($item) : $item;
195
            }, (array)$payload)
196
        );
197
        $this->onResponse->push($cb);
0 ignored issues
show
It seems like $cb defined by parameter $cb on line 188 can also be of type null; however, PHPDaemon\Structures\StackCallbacks::push() does only seem to accept callable, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
198
        $this->write(
199
            pack(
200
                static::HEADER_WRITE_FORMAT,
201
                static::MAGIC_REQUEST,
202
                $this->requestCommandList[$commandName],
203
                mb_orig_strlen($pct)
204
            )
205
        );
206
        $this->write($pct);
207
    }
208
209
    /**
210
     * Function run task and wait result in callback
211
     *
212
     * @param $params
213
     * @param callable $cb = null
0 ignored issues
show
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
214
     * @param boolean $unique
0 ignored issues
show
There is no parameter named $unique. 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...
215
     */
216
    public function submitJob($params, $cb = null)
217
    {
218
        $closure = function () use (&$params, $cb) {
219
            $this->sendCommand(
220
                'SUBMIT_JOB'
221
                . (isset($params['pri']) ? '_ ' . strtoupper($params['pri']) : '')
222
                . (isset($params['bg']) && $params['bg'] ? '_BG' : ''),
223
                [$params['function'], $params['unique'], $params['payload']],
224
                $cb
225
            );
226
        };
227
        if (isset($params['unique'])) {
228
            $closure();
229
        } else {
230
            Crypt::randomString(
231
                10,
232
                '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
233
                function ($random) use ($closure) {
234
                    $params['unique'] = $random;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = 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...
235
                    $closure();
236
                }
237
            );
238
        }
239
    }
240
241
    /**
242
     * Get job status
243
     *
244
     * @param mixed $jobHandle Job handle that was given in JOB_CREATED packet.
245
     * @param callable $cb = null
0 ignored issues
show
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
246
     *
247
     */
248
    public function getStatus($jobHandle, $cb = null)
249
    {
250
        $this->sendCommand('GET_STATUS', [$jobHandle], $cb);
251
    }
252
253
    /**
254
     * Function set settings for current connection
255
     * Available settings
256
     * 'exceptions' - Forward WORK_EXCEPTION packets to the client.
257
     *
258
     * @url http://gearman.org/protocol/
259
     *
260
     *
261
     * @param int $optionName
262
     * @param callable $cb = null
0 ignored issues
show
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
263
     */
264
    public function setConnectionOption($optionName, $cb = null)
265
    {
266
        $this->sendCommand('OPTION_RES', [$optionName], $cb);
267
    }
268
}
269