Issues (6)

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/ParseMultiPartBodyModifier.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\HttpModifiers;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use Thruster\Component\HttpMessage\UploadedFile;
7
use Thruster\Component\HttpModifier\ServerRequestModifierInterface;
8
9
/**
10
 * Class ParseMultiPartBodyModifier
11
 *
12
 * @package Thruster\Component\HttpModifiers
13
 * @author  Aurimas Niekis <[email protected]>
14
 */
15
class ParseMultiPartBodyModifier implements ServerRequestModifierInterface
16
{
17
    public function modify(ServerRequestInterface $request) : ServerRequestInterface
18
    {
19
        $headerLine = $request->getHeaderLine('Content-Type');
20
21
        if (false === strpos($headerLine, 'multipart/')) {
22
            return $request;
23
        }
24
25
        $boundary = null;
26
        if (preg_match("/boundary=\"?(.*)\"?$/", $headerLine, $matches)) {
27
            $boundary = $matches[1];
28
        }
29
30
        $variables = [];
31
        $files     = [];
32
33
        $body     = $request->getBody()->detach();
34
        $partInfo = null;
35
36
        while (false !== ($lineN = fgets($body))) {
37
            if (0 === strpos($lineN, '--')) {
38
                if (null === $boundary) {
39
                    $boundary = rtrim($lineN);
40
                }
41
42
                continue;
43
            }
44
45
            $line = rtrim($lineN);
46
47
            if ('' === $line) {
48
                if (isset($partInfo['Content-Disposition']) &&
49
                    true !== empty($partInfo['Content-Disposition']['filename'])
50
                ) {
51
                    $this->parseFile($body, $boundary, $partInfo, $files);
52
                } elseif ($partInfo !== null) {
53
                    $this->parseVariable($body, $boundary, $partInfo['Content-Disposition']['name'], $variables);
54
                }
55
56
                $partInfo = null;
57
                continue;
58
            }
59
60
            list($key, $value) = explode(':', $line, 2);
61
62
            $partInfo[$key] = $this->parseHeader($value, $key);
63
        }
64
65
        return $request
66
            ->withParsedBody($variables)
67
            ->withUploadedFiles($files);
68
    }
69
70
    private function parseFile($body, $boundary, $partInfo, &$files)
71
    {
72
        $tempDir = sys_get_temp_dir();
73
74
        $name        = $partInfo['Content-Disposition']['name'];
75
        $fileName    = $partInfo['Content-Disposition']['filename'] ?? null;
76
        $contentType = $partInfo['Content-Type']['value'] ?? null;
77
78
        if (empty($tempDir)) {
79
            $errorCode = UPLOAD_ERR_NO_TMP_DIR;
80
81
            $files[$name] = new UploadedFile(null, 0, $errorCode, $fileName, $contentType);
82
83
            return;
84
        }
85
86
        $tempName = tempnam($tempDir, 'thruster_file_upload');
87
        $stream   = fopen($tempName, 'wb');
88
89
        if (false === $stream) {
90
            $errorCode = UPLOAD_ERR_CANT_WRITE;
91
92
            $files[$name] = new UploadedFile(null, 0, $errorCode, $fileName, $contentType);
93
94
            return;
95
        }
96
97
        $lastLine = null;
98
        while (false !== ($lineN = fgets($body, 4096))) {
99
            if ($lastLine !== null) {
100
                if (false !== strpos($lineN, $boundary)) {
101
                    break;
102
                }
103
104 View Code Duplication
                if (false === fwrite($stream, $lastLine)) {
105
                    $errorCode = UPLOAD_ERR_CANT_WRITE;
106
107
                    $files[$name] = new UploadedFile(null, 0, $errorCode, $fileName, $contentType);
108
                    fclose($stream);
109
                    unlink($tempName);
110
111
                    return;
112
                }
113
114
            }
115
116
            $lastLine = $lineN;
117
        }
118
119
        if (null !== $lastLine) {
120 View Code Duplication
            if (false === fwrite($stream, rtrim($lastLine, "\r\n"))) {
121
                $errorCode = UPLOAD_ERR_CANT_WRITE;
122
123
                $files[$name] = new UploadedFile(null, 0, $errorCode, $fileName, $contentType);
124
                fclose($stream);
125
                unlink($tempName);
126
127
                return;
128
            }
129
        }
130
131
        fclose($stream);
132
133
        $files[$name] = new UploadedFile(
134
            $tempName,
135
            filesize($tempName),
136
            UPLOAD_ERR_OK,
137
            $fileName,
138
            $contentType
139
        );
140
    }
141
142
    private function parseVariable($body, $boundary, $name, &$variables)
143
    {
144
        $fullValue = '';
145
        $lastLine = null;
146
147
        while (false !== ($lineN = fgets($body)) && false === strpos($lineN, $boundary)) {
148
            if (null !== $lastLine) {
149
                $fullValue .= $lastLine;
150
            }
151
152
            $lastLine = $lineN;
153
        }
154
155
        if (null !== $lastLine) {
156
            $fullValue .= rtrim($lastLine, "\r\n");
157
        }
158
159
        $variables[$name] = $fullValue;
160
    }
161
162
    private function parseHeader($value, $key = '')
163
    {
164
        $result = [];
165
166
        $regex = '/(^|;)\s*(?P<name>[^=:,;\s"]*):?(=("(?P<quotedValue>' .
167
            '[^"]*(\\.[^"]*)*)")|(\s*(?P<value>[^=,;\s"]*)))?/mx';
168
169
        preg_match_all($regex, $value, $matches, PREG_SET_ORDER);
170
171
        foreach ($matches as $index => $match) {
0 ignored issues
show
The expression $matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
172
            $name        = $match['name'];
173
            $quotedValue = $match['quotedValue'];
174
175
            if (empty($quotedValue)) {
176
                $value = $match['value'];
177
            } else {
178
                $value = stripcslashes($quotedValue);
179
            }
180
181
            if ($name == $key && 0 === $index) {
182
                $name = 'value';
183
            }
184
185
            $result[$name] = $value;
186
        }
187
188
        return $result;
189
    }
190
}
191