Issues (10)

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/Util/Helper.php (3 issues)

Labels

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
declare(strict_types=1);
3
4
namespace Sirius\Upload\Util;
5
6
class Helper
7
{
8
    const PSR7_UPLOADED_FILE_CLASS = '\Psr\Http\Message\UploadedFileInterface';
9
    const SYMFONY_UPLOADED_FILE_CLASS = 'Symfony\Component\HttpFoundation\File\UploadedFile';
10
11
    /**
12
     * We do not type-hint or import the class since it may not be used
13
     *
14
     * @param \Psr\Http\Message\UploadedFileInterface $file
15
     *
16
     * @return array
17
     */
18
    public static function extractFromUploadedFileInterface($file)
19
    {
20
        /** @var \Psr\Http\Message\UploadedFileInterface $file */
21
        $tempName = tempnam(sys_get_temp_dir(), 'srsupld_');
22
        $file->moveTo($tempName);
23
        $result = [
24
            'name'     => $file->getClientFilename(),
25
            'tmp_name' => $tempName,
26
            'type'     => $file->getClientMediaType(),
27
            'error'    => $file->getError(),
28
            'size'     => $file->getSize()
29
        ];
30
31
        return $result;
32
    }
33
34
    public static function extractFromSymfonyFile($file)
35
    {
36
        /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
37
38
        $result = [
39
            'name'     => $file->getClientOriginalName(),
40
            'tmp_name' => $file->getPathname(),
41
            'type'     => $file->getMimeType(),
42
            'error'    => $file->getError(),
43
            'size'     => $file->getSize()
44
        ];
45
46
        return $result;
47
    }
48
49
    public static function remapFilesArray(array $files): array
50
    {
51
        $result = [];
52
        foreach (array_keys($files['name']) as $k) {
53
            $result[$k] = [
54
                'name'     => $files['name'][$k],
55
                'type'     => @$files['type'][$k],
56
                'size'     => @$files['size'][$k],
57
                'error'    => @$files['error'][$k],
58
                'tmp_name' => $files['tmp_name'][$k]
59
            ];
60
        }
61
62
        return $result;
63
    }
64
65
    /**
66
     * Fixes the $_FILES array problem and ensures the result is an array of files
67
     *
68
     * PHP's $_FILES variable is not properly formated for iteration when
69
     * multiple files are uploaded under the same name
70
     * @see http://www.php.net/manual/en/features.file-upload.php
71
     *
72
     * @param array|Symfony\Component\HttpFoundation\File\UploadedFile|\Psr\Http\Message\UploadedFileInterface $files
73
     *
74
     * @return array
75
     */
76
    public static function normalizeFiles($files): array
77
    {
78
        if (empty($files)) {
79
            return [];
80
        }
81
82
        if (is_object($files)) {
83
            if (is_subclass_of($files, self::PSR7_UPLOADED_FILE_CLASS)) {
0 ignored issues
show
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if self::PSR7_UPLOADED_FILE_CLASS can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
84
                return [self::extractFromUploadedFileInterface($files)];
85
            }
86
            if (get_class($files) == self::SYMFONY_UPLOADED_FILE_CLASS) {
87
                return [self::extractFromSymfonyFile($files)];
88
            }
89
        }
90
91
        // If caller passed in an array of objects (Either PSR7 or Symfony)
92
        if (is_array($files) && is_object(reset($files))) {
93
            if (is_subclass_of(reset($files), self::PSR7_UPLOADED_FILE_CLASS)) {
0 ignored issues
show
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if self::PSR7_UPLOADED_FILE_CLASS can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
94
                $result = [];
95
                foreach ($files as $file) {
96
                    $result[] = self::extractFromUploadedFileInterface($file);
97
                }
98
99
                return $result;
100
            }
101
102
            if (get_class(reset($files)) == self::SYMFONY_UPLOADED_FILE_CLASS) {
103
                $result = [];
104
                foreach ($files as $file) {
105
                    $result[] = self::extractFromSymfonyFile($file);
106
                }
107
108
                return $result;
109
            }
110
        }
111
112
        // The caller passed $_FILES['some_field_name']
113
        if (isset($files['name'])) {
114
            // we have a single file
115
            if (! is_array($files['name'])) {
116
                return [$files];
117
            } else {
118
                // we have list of files, which PHP messes up
119
                return Helper::remapFilesArray($files);
0 ignored issues
show
It seems like $files defined by parameter $files on line 76 can also be of type object<Psr\Http\Message\UploadedFileInterface>; however, Sirius\Upload\Util\Helper::remapFilesArray() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
120
            }
121
        } else {
122
            // The caller passed $_FILES
123
            $keys = array_keys($files);
124
            if (isset($keys[0]) && isset($files[$keys[0]]['name'])) {
125
                if (! is_array($files[$keys[0]]['name'])) {
126
                    // $files is in the correct format already, even in the
127
                    // case it contains a single element.
128
                    return $files;
129
                } else {
130
                    // we have list of files, which PHP messes up
131
                    return Helper::remapFilesArray($files[$keys[0]]);
132
                }
133
            }
134
        }
135
136
        // If we got here, the $file argument is wrong
137
        return [];
138
    }
139
}
140