This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
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
|
|||
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 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
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:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.