Issues (57)

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/LimitStream.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 Thruster\Component\HttpMessage;
4
5
use Psr\Http\Message\StreamInterface;
6
7
/**
8
 * Class LimitStream
9
 *
10
 * @package Thruster\Component\HttpMessage
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class LimitStream implements StreamInterface
14
{
15
    use StreamDecoratorTrait;
16
17
    /**
18
     * @var int Offset to start reading from
19
     */
20
    private $offset;
21
22
    /**
23
     * @var int Limit the number of bytes that can be read
24
     */
25
    private $limit;
26
27
    /**
28
     * @param StreamInterface $stream Stream to wrap
29
     * @param int             $limit  Total number of bytes to allow to be read
30
     *                                from the stream. Pass -1 for no limit.
31
     * @param int|null        $offset Position to seek to before reading (only
32
     *                                works on seekable streams).
33
     */
34 17
    public function __construct(
35
        StreamInterface $stream,
36
        $limit = -1,
37
        $offset = 0
38
    ) {
39 17
        $this->stream = $stream;
0 ignored issues
show
The property stream does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40 17
        $this->setLimit($limit);
41 17
        $this->setOffset($offset);
42 17
    }
43
44 9
    public function eof()
45
    {
46
        // Always return true if the underlying stream is EOF
47 9
        if ($this->stream->eof()) {
48 2
            return true;
49
        }
50
51
        // No limit and the underlying stream is not at EOF
52 9
        if ($this->limit == -1) {
53 3
            return false;
54
        }
55
56 6
        return $this->stream->tell() >= $this->offset + $this->limit;
57
    }
58
59
    /**
60
     * Returns the size of the limited subset of data
61
     * {@inheritdoc}
62
     */
63 3
    public function getSize()
64
    {
65 3
        if (null === ($length = $this->stream->getSize())) {
66 1
            return null;
67 2
        } elseif (-1 == $this->limit) {
68 1
            return $length - $this->offset;
69
        } else {
70 1
            return min($this->limit, $length - $this->offset);
71
        }
72
    }
73
74
    /**
75
     * Allow for a bounded seek on the read limited stream
76
     * {@inheritdoc}
77
     */
78 6
    public function seek($offset, $whence = SEEK_SET)
79
    {
80 6
        if (SEEK_SET !== $whence || 0 > $offset) {
81 1
            throw new \RuntimeException(sprintf(
82 1
                'Cannot seek to offset % with whence %s',
83
                $offset,
84
                $whence
85
            ));
86
        }
87
88 6
        $offset += $this->offset;
89
90 6
        if (-1 !== $this->limit) {
91 4
            if ($offset > $this->offset + $this->limit) {
92 1
                $offset = $this->offset + $this->limit;
93
            }
94
        }
95
96 6
        $this->stream->seek($offset);
97 6
    }
98
99
    /**
100
     * Give a relative tell()
101
     * {@inheritdoc}
102
     */
103 3
    public function tell()
104
    {
105 3
        return $this->stream->tell() - $this->offset;
106
    }
107
108
    /**
109
     * Set the offset to start limiting from
110
     *
111
     * @param int $offset Offset to seek to and begin byte limiting from
112
     *
113
     * @throws \RuntimeException if the stream cannot be seeked.
114
     */
115 17
    public function setOffset($offset)
116
    {
117 17
        $current = $this->stream->tell();
118
119 17
        if ($current !== $offset) {
120
            // If the stream cannot seek to the offset position, then read to it
121 16
            if ($this->stream->isSeekable()) {
122 16
                $this->stream->seek($offset);
123 1
            } elseif ($current > $offset) {
124 1
                throw new \RuntimeException("Could not seek to stream offset $offset");
125
            } else {
126
                $this->stream->read($offset - $current);
127
            }
128
        }
129
130 17
        $this->offset = $offset;
131 17
    }
132
133
    /**
134
     * Set the limit of bytes that the decorator allows to be read from the
135
     * stream.
136
     *
137
     * @param int $limit Number of bytes to allow to be read from the stream.
138
     *                   Use -1 for no limit.
139
     */
140 17
    public function setLimit($limit)
141
    {
142 17
        $this->limit = $limit;
143 17
    }
144
145 9
    public function read($length)
146
    {
147 9
        if ($this->limit == -1) {
148 3
            return $this->stream->read($length);
149
        }
150
151
        // Check if the current position is less than the total allowed
152
        // bytes + original offset
153 6
        $remaining = ($this->offset + $this->limit) - $this->stream->tell();
154 6
        if ($remaining > 0) {
155
            // Only return the amount of requested data, ensuring that the byte
156
            // limit is not exceeded
157 6
            return $this->stream->read(min($remaining, $length));
158
        }
159
160 1
        return '';
161
    }
162
}
163