Issues (1)

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

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 CodeJet\Http;
4
5
use InvalidArgumentException;
6
use Psr\Http\Message\StreamInterface;
7
8
class Stream implements StreamInterface
9
{
10
11
    protected $handle;
12
13
    /**
14
     * Stream constructor.
15
     * @param $stream
16
     */
17 87
    public function __construct($stream)
18
    {
19 87
        if (!$this->isStream($stream)) {
20 3
            throw new InvalidArgumentException('Must be a stream.');
21
        }
22
23 84
        $this->handle = $stream;
24 84
    }
25
26
    /**
27
     * @param $stream
28
     * @return bool
29
     */
30 87
    protected function isStream($stream)
31
    {
32 87
        if (is_resource($stream) && get_resource_type($stream) === 'stream') {
33 84
            return true;
34
        }
35
36 3
        return false;
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42 18
    public function __toString()
43
    {
44 18
        if (!$this->isReadable()) {
45 6
            return '';
46
        }
47
48 12
        if ($this->isSeekable()) {
49 12
            $this->rewind();
50
        }
51
52 12
        return stream_get_contents($this->handle);
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 24
    public function close()
59
    {
60 24
        if (!$this->handle) {
61
            return;
62
        }
63
64 24
        $handle = $this->detach();
65 24
        fclose($handle);
66 24
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71 27
    public function detach()
72
    {
73 27
        $handle = $this->handle;
74 27
        $this->handle = null;
75
76 27
        return $handle;
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82 3
    public function getSize()
83
    {
84 3
        $fstatData = fstat($this->handle);
85
86 3
        if (isset($fstatData['size'])) {
87 3
            return $fstatData['size'];
88
        }
89
90
        return null;
91
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96 6
    public function tell()
97
    {
98 6
        if (!$this->handle) {
99 3
            throw new \RuntimeException('Cannot tell position, stream is closed.');
100
        }
101
102 3
        return ftell($this->handle);
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108 6
    public function eof()
109
    {
110 6
        if (!$this->handle) {
111 3
            return true;
112
        }
113
114 3
        return feof($this->handle);
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120 24
    public function isSeekable()
121
    {
122 24
        if (!$this->handle) {
123 3
            return false;
124
        }
125
126 21
        if ($this->getMetadata('seekable')) {
127 21
            return true;
128
        }
129
130
        return false;
131
    }
132
133
    /**
134
     * @inheritdoc
135
     */
136 18
    public function seek($offset, $whence = SEEK_SET)
137
    {
138 18
        if (!$this->isSeekable()) {
139
            throw new \RuntimeException('Stream is not seekable.');
140
        }
141
142 18
        fseek($this->handle, $offset, $whence);
143 18
    }
144
145
    /**
146
     * @inheritdoc
147
     */
148 18
    public function rewind()
149
    {
150 18
        if (!$this->isSeekable()) {
151 3
            throw new \RuntimeException('Cannot rewind, stream is not seekable.');
152
        }
153
154 15
        $this->seek(0);
155 15
    }
156
157
    /**
158
     * @inheritdoc
159
     */
160 9
    public function isWritable()
161
    {
162 9
        if (!$this->handle) {
163 3
            return false;
164
        }
165
166 6
        $mode = $this->getMetadata('mode');
167
168 6
        if (!$mode) {
169
            return false;
170
        }
171
172
        // Nix 'b'inary and 't'ext only identifiers from the mode.
173 6
        $mode = rtrim(rtrim($mode, 'b'), 't');
174
175 6
        if ($mode === 'r') {
176
            // 'r' is the only read-only mode.
177
            // The rest are read-write or write-only.
178 3
            return false;
179
        }
180
181 6
        return true;
182
    }
183
184
    /**
185
     * @inheritdoc
186
     */
187 6
    public function write($string)
188
    {
189 6
        if (!$this->isWritable($this->handle)) {
0 ignored issues
show
The call to Stream::isWritable() has too many arguments starting with $this->handle.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
190 3
            throw new \RuntimeException('Stream is not writable.');
191
        }
192
193 3
        if (!is_string($string)) {
194
            throw new \RuntimeException('Stream write value must be a string.');
195
        }
196
197 3
        return fwrite($this->handle, $string);
198
    }
199
200
    /**
201
     * @inheritdoc
202
     */
203 33
    public function isReadable()
204
    {
205 33
        if (!$this->handle) {
206 9
            return false;
207
        }
208
209 24
        $mode = $this->getMetadata('mode');
210
211 24
        if (!$mode) {
212
            return false;
213
        }
214
215
        // Nix 'b'inary and 't'ext only identifiers from the mode.
216 24
        $mode = rtrim(rtrim($mode, 'b'), 't');
217
        // Grab the last character of the mode.
218 24
        $readIdentifier = substr($mode, -1, 1);
219
220 24
        if ($readIdentifier === 'r' || $readIdentifier === '+') {
221 21
            return true;
222
        }
223
224 6
        return false;
225
    }
226
227
    /**
228
     * @inheritdoc
229
     */
230 6
    public function read($length)
231
    {
232 6
        if (!$this->isReadable()) {
233 3
            throw new \RuntimeException('Stream is not readable.');
234
        }
235
236 3
        $data = fread($this->handle, $length);
237
238 3
        return $data;
239
    }
240
241
    /**
242
     * @inheritdoc
243
     */
244 3
    public function getContents()
245
    {
246 3
        if (!$this->isReadable()) {
247
            throw new \RuntimeException('Stream is not readable.');
248
        }
249
250 3
        return stream_get_contents($this->handle);
251
    }
252
253
    /**
254
     * @inheritdoc
255
     */
256 48
    public function getMetadata($key = null)
257
    {
258 48
        $metaData = stream_get_meta_data($this->handle);
259
260 48
        if (is_null($key)) {
261 3
            return $metaData;
262
        }
263
264 45
        if (isset($metaData[$key])) {
265 42
            return $metaData[$key];
266
        }
267
268 3
        return null;
269
    }
270
}
271