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/MultipartStream.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 Thruster\Component\HttpMessage;
4
5
use Psr\Http\Message\StreamInterface;
6
7
/**
8
 * Class MultipartStream
9
 *
10
 * @package Thruster\Component\HttpMessage
11
 * @author  Aurimas Niekis <[email protected]>
12
 */
13
class MultipartStream implements StreamInterface
14
{
15
    use StreamDecoratorTrait;
16
17
    private $boundary;
18
19
    /**
20
     * @param array  $elements Array of associative arrays, each containing a
21
     *                         required "name" key mapping to the form field,
22
     *                         name, a required "contents" key mapping to a
23
     *                         StreamInterface/resource/string, an optional
24
     *                         "headers" associative array of custom headers,
25
     *                         and an optional "filename" key mapping to a
26
     *                         string to send as the filename in the part.
27
     * @param string $boundary You can optionally provide a specific boundary
28
     *
29
     * @throws \InvalidArgumentException
30
     */
31 10
    public function __construct(array $elements = [], $boundary = null)
32
    {
33 10
        $this->boundary = $boundary ?? uniqid();
34 10
        $this->stream = $this->createStream($elements);
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...
35 8
    }
36
37
    /**
38
     * Get the boundary
39
     *
40
     * @return string
41
     */
42 3
    public function getBoundary()
43
    {
44 3
        return $this->boundary;
45
    }
46
47 1
    public function isWritable() : bool
48
    {
49 1
        return false;
50
    }
51
52
    /**
53
     * Get the headers needed before transferring the content of a POST file
54
     */
55 4
    private function getHeaders(array $headers)
56
    {
57 4
        $str = '';
58 4
        foreach ($headers as $key => $value) {
59 4
            $str .= "{$key}: {$value}\r\n";
60
        }
61
62 4
        return "--{$this->boundary}\r\n" . trim($str) . "\r\n\r\n";
63
    }
64
65
    /**
66
     * Create the aggregate stream that will be used to upload the POST data
67
     */
68 10
    protected function createStream(array $elements)
69
    {
70 10
        $stream = new AppendStream();
71
72 10
        foreach ($elements as $element) {
73 6
            $this->addElement($stream, $element);
74
        }
75
76
        // Add the trailing boundary with CRLF
77 8
        $stream->addStream(stream_for("--{$this->boundary}--\r\n"));
78
79 8
        return $stream;
80
    }
81
82 6
    private function addElement(AppendStream $stream, array $element)
83
    {
84 6
        foreach (['contents', 'name'] as $key) {
85 6
            if (false === array_key_exists($key, $element)) {
86 6
                throw new \InvalidArgumentException("A '{$key}' key is required");
87
            }
88
        }
89
90 4
        $element['contents'] = stream_for($element['contents']);
91
92 4
        if (empty($element['filename'])) {
93 4
            $uri = $element['contents']->getMetadata('uri');
94 4
            if ('php://' !== substr($uri, 0, 6)) {
95 3
                $element['filename'] = $uri;
96
            }
97
        }
98
99 4
        list($body, $headers) = $this->createElement(
100 4
            $element['name'],
101 4
            $element['contents'],
102 4
            $element['filename'] ?? null,
103 4
            $element['headers'] ?? []
104
        );
105
106 4
        $stream->addStream(stream_for($this->getHeaders($headers)));
107 4
        $stream->addStream($body);
108 4
        $stream->addStream(stream_for("\r\n"));
109 4
    }
110
111
    /**
112
     * @return array
113
     */
114 4
    private function createElement($name, $stream, $filename, array $headers)
115
    {
116
        // Set a default content-disposition header if one was no provided
117 4
        $disposition = $this->getHeader($headers, 'content-disposition');
118 4
        if (!$disposition) {
119 3
            $headers['Content-Disposition'] = $filename
120 2
                ? sprintf('form-data; name="%s"; filename="%s"',
121
                    $name,
122
                    basename($filename))
123 1
                : "form-data; name=\"{$name}\"";
124
        }
125
126
        // Set a default content-length header if one was no provided
127 4
        $length = $this->getHeader($headers, 'content-length');
128 4
        if (!$length) {
129 4
            if ($length = $stream->getSize()) {
130 4
                $headers['Content-Length'] = (string) $length;
131
            }
132
        }
133
134
        // Set a default Content-Type if one was not supplied
135 4
        $type = $this->getHeader($headers, 'content-type');
136 4
        if (!$type && $filename) {
137 3
            if ($type = mimetype_from_filename($filename)) {
138 3
                $headers['Content-Type'] = $type;
139
            }
140
        }
141
142 4
        return [$stream, $headers];
143
    }
144
145 4
    private function getHeader(array $headers, $key)
146
    {
147 4
        $lowercaseHeader = strtolower($key);
148 4
        foreach ($headers as $k => $v) {
149 4
            if (strtolower($k) === $lowercaseHeader) {
150 4
                return $v;
151
            }
152
        }
153
154 4
        return null;
155
    }
156
}
157