Issues (10)

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/Reactor.php (1 issue)

Labels
Severity

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 Helix\Socket;
4
5
use Countable;
6
use Helix\Socket\WebSocket\WebSocketClient;
7
use Helix\Socket\WebSocket\WebSocketError;
8
use Throwable;
9
10
/**
11
 * Selects and calls reactive sockets when they are readable.
12
 */
13
class Reactor implements Countable {
14
15
    /**
16
     * All sockets in the reactor, keyed by ID.
17
     *
18
     * @var ReactiveInterface[]
19
     */
20
    protected $sockets = [];
21
22
    /**
23
     * Selects instances. Can be used to select non-reactive sockets.
24
     *
25
     * @see https://php.net/socket_select
26
     *
27
     * @param SocketInterface[] $read
28
     * @param SocketInterface[] $write
29
     * @param SocketInterface[] $except
30
     * @param float|null $timeout Maximum seconds to block. `NULL` blocks forever.
31
     * @return int
32
     * @throws SocketError
33
     */
34
    public static function select (array &$read, array &$write, array &$except, ?float $timeout = null): int {
35
        $rwe = [$read, $write, $except];
36
        array_walk_recursive($rwe, function(SocketInterface &$each) {
37
            $each = $each->getResource();
38
        });
39
        $uSec = (int)(fmod($timeout, 1) * 1000000); // ignored if timeout is null
40
        $count = @socket_select($rwe[0], $rwe[1], $rwe[2], $timeout, $uSec); // keys are preserved
41
        if ($count === false) {
42
            $read = $write = $except = [];
43
            throw new SocketError;
44
        }
45
        $read = array_intersect_key($read, $rwe[0]);
46
        $write = array_intersect_key($write, $rwe[1]);
47
        $except = array_intersect_key($except, $rwe[2]);
48
        return $count;
49
    }
50
51
    /**
52
     * Adds a reactive socket for selection.
53
     *
54
     * @param ReactiveInterface $socket
55
     * @return $this
56
     */
57
    public function add (ReactiveInterface $socket) {
58
        $this->sockets[$socket->getId()] = $socket;
59
        return $this;
60
    }
61
62
    /**
63
     * The number of reactive sockets in the reactor.
64
     *
65
     * @return int
66
     */
67
    public function count (): int {
68
        return count($this->sockets);
69
    }
70
71
    /**
72
     * @return ReactiveInterface[]
73
     */
74
    public function getSockets () {
75
        return $this->sockets;
76
    }
77
78
    /**
79
     * Whether a socket is in the reactor.
80
     *
81
     * @param ReactiveInterface $socket
82
     * @return bool
83
     */
84
    public function has (ReactiveInterface $socket): bool {
85
        return isset($sockets[$socket->getId()]);
0 ignored issues
show
The variable $sockets does not exist. Did you mean $socket?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
86
    }
87
88
    /**
89
     * @param int $channel
90
     * @param ReactiveInterface $socket
91
     * @param Throwable $error
92
     */
93
    protected function onError (int $channel, $socket, Throwable $error): void {
94
        unset($channel);
95
        if ($socket instanceof WebSocketClient and $error instanceof WebSocketError) {
96
            if ($socket->isOpen()) {
97
                $socket->close($error->getCode(), $error->getMessage());
98
            }
99
        }
100
        else {
101
            if ($socket->isOpen()) {
102
                $socket->close();
103
            }
104
        }
105
    }
106
107
    /**
108
     * Selects the reactor's sockets and calls their reactive methods.
109
     *
110
     * Invoke this in a loop that checks {@link Reactor::count()} a condition.
111
     *
112
     * Closed sockets are automatically removed from the reactor.
113
     *
114
     * @param float|null $timeout Maximum seconds to block. `NULL` blocks forever.
115
     * @return int Number of sockets selected.
116
     */
117
    public function react (?float $timeout = null): int {
118
        /** @var ReactiveInterface[][] $rwe */
119
        $rwe = [$this->sockets, [], $this->sockets];
120
        $count = static::select($rwe[0], $rwe[1], $rwe[2], $timeout);
121
        foreach ([2 => 'onOutOfBand', 0 => 'onReadable'] as $channel => $method) {
122
            foreach ($rwe[$channel] as $id => $socket) {
123
                try {
124
                    $socket->{$method}();
125
                }
126
                catch (Throwable $error) {
127
                    unset($rwe[0][$id]); // prevent onReadable() if this is an OOB error.
128
                    $this->onError($channel, $socket, $error);
129
                }
130
                finally {
131
                    if (!$socket->isOpen() and $this->has($socket)) {
132
                        $this->remove($socket);
133
                    }
134
                }
135
            }
136
        }
137
        return $count;
138
    }
139
140
    /**
141
     * Removes a socket from the reactor.
142
     *
143
     * @param ReactiveInterface $socket
144
     * @return $this
145
     */
146
    public function remove (ReactiveInterface $socket) {
147
        unset($this->sockets[$socket->getId()]);
148
        return $this;
149
    }
150
}
151