Issues (74)

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/Filesystem/Filesystem.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 Spatie\MediaLibrary\Filesystem;
4
5
use Illuminate\Contracts\Filesystem\Factory;
6
use Illuminate\Support\Facades\Storage;
7
use Illuminate\Support\Str;
8
use Spatie\MediaLibrary\Conversion\ConversionCollection;
9
use Spatie\MediaLibrary\Events\MediaHasBeenAdded;
10
use Spatie\MediaLibrary\FileManipulator;
11
use Spatie\MediaLibrary\Helpers\File;
12
use Spatie\MediaLibrary\Helpers\RemoteFile;
13
use Spatie\MediaLibrary\Models\Media;
14
use Spatie\MediaLibrary\PathGenerator\PathGeneratorFactory;
15
16
class Filesystem
17
{
18
    /** @var \Illuminate\Contracts\Filesystem\Factory */
19
    protected $filesystem;
20
21
    /** @var array */
22
    protected $customRemoteHeaders = [];
23
24
    public function __construct(Factory $filesystem)
25
    {
26
        $this->filesystem = $filesystem;
27
    }
28
29
    public function add(string $file, Media $media, ?string $targetFileName = null)
30
    {
31
        $this->copyToMediaLibrary($file, $media, null, $targetFileName);
32
33
        event(new MediaHasBeenAdded($media));
34
35
        app(FileManipulator::class)->createDerivedFiles($media);
36
    }
37
38
    public function addRemote(RemoteFile $file, Media $media, ?string $targetFileName = null)
39
    {
40
        $this->copyToMediaLibraryFromRemote($file, $media, null, $targetFileName);
41
42
        event(new MediaHasBeenAdded($media));
43
44
        app(FileManipulator::class)->createDerivedFiles($media);
45
    }
46
47
    public function copyToMediaLibraryFromRemote(RemoteFile $file, Media $media, ?string $type = null, ?string $targetFileName = null)
48
    {
49
        $storage = Storage::disk($file->getDisk());
50
51
        $destinationFileName = $targetFileName ?: $file->getFilename();
52
53
        $destination = $this->getMediaDirectory($media, $type).$destinationFileName;
54
55
        $this->filesystem->disk($media->disk)
56
            ->getDriver()->writeStream(
57
                $destination,
58
                $storage->getDriver()->readStream($file->getKey()),
59
                $media->getDiskDriverName() === 'local'
60
                    ? [] : $this->getRemoteHeadersForFile(
61
                        $file->getKey(),
62
                        $media->getCustomHeaders(),
63
                        $storage->mimeType($file->getKey())
64
                    )
65
            );
66
    }
67
68
    public function copyToMediaLibrary(string $pathToFile, Media $media, ?string $type = null, ?string $targetFileName = null)
69
    {
70
        $destinationFileName = $targetFileName ?: pathinfo($pathToFile, PATHINFO_BASENAME);
71
72
        $destination = $this->getMediaDirectory($media, $type).$destinationFileName;
73
74
        $file = fopen($pathToFile, 'r');
75
76
        if ($media->getDiskDriverName() === 'local') {
77
            $this->filesystem
78
                ->disk($media->disk)
79
                ->put($destination, $file);
80
81
            fclose($file);
82
83
            return;
84
        }
85
86
        $this->filesystem
87
            ->disk($media->disk)
88
            ->put($destination, $file, $this->getRemoteHeadersForFile($pathToFile, $media->getCustomHeaders()));
89
90
        if (is_resource($file)) {
91
            fclose($file);
92
        }
93
    }
94
95
    public function addCustomRemoteHeaders(array $customRemoteHeaders)
96
    {
97
        $this->customRemoteHeaders = $customRemoteHeaders;
98
    }
99
100
    public function getRemoteHeadersForFile(string $file, array $mediaCustomHeaders = [], string $mimeType = null): array
101
    {
102
        $mimeTypeHeader = ['ContentType' => $mimeType ?: File::getMimeType($file)];
103
104
        $extraHeaders = config('medialibrary.remote.extra_headers');
105
106
        return array_merge($mimeTypeHeader, $extraHeaders, $this->customRemoteHeaders, $mediaCustomHeaders);
107
    }
108
109
    public function getStream(Media $media)
110
    {
111
        $sourceFile = $this->getMediaDirectory($media).'/'.$media->file_name;
112
113
        return $this->filesystem->disk($media->disk)->readStream($sourceFile);
114
    }
115
116
    public function copyFromMediaLibrary(Media $media, string $targetFile): string
117
    {
118
        touch($targetFile);
119
120
        $stream = $this->getStream($media);
121
122
        $targetFileStream = fopen($targetFile, 'a');
123
124
        while (! feof($stream)) {
125
            $chunk = fread($stream, 1024);
126
            fwrite($targetFileStream, $chunk);
127
        }
128
129
        fclose($stream);
130
131
        fclose($targetFileStream);
132
133
        return $targetFile;
134
    }
135
136
    public function removeAllFiles(Media $media)
137
    {
138
        $mediaDirectory = $this->getMediaDirectory($media);
139
140
        $conversionsDirectory = $this->getMediaDirectory($media, 'conversions');
141
142
        $responsiveImagesDirectory = $this->getMediaDirectory($media, 'responsiveImages');
143
144
        collect([$mediaDirectory, $conversionsDirectory, $responsiveImagesDirectory])
145
146
            ->each(function ($directory) use ($media) {
147
                $this->filesystem->disk($media->disk)->deleteDirectory($directory);
148
            });
149
    }
150
151
    public function removeFile(Media $media, string $path)
152
    {
153
        $this->filesystem->disk($media->disk)->delete($path);
154
    }
155
156
    public function removeResponsiveImages(Media $media, string $conversionName = 'medialibrary_original')
157
    {
158
        $responsiveImagesDirectory = $this->getResponsiveImagesDirectory($media);
159
160
        $allFilePaths = $this->filesystem->disk($media->disk)->allFiles($responsiveImagesDirectory);
161
162
        $responsiveImagePaths = array_filter(
163
            $allFilePaths,
164
            function (string $path) use ($conversionName) {
165
                return Str::contains($path, $conversionName);
166
            }
167
        );
168
169
        $this->filesystem->disk($media->disk)->delete($responsiveImagePaths);
170
    }
171
172
    public function syncFileNames(Media $media)
173
    {
174
        $this->renameMediaFile($media);
175
176
        $this->renameConversionFiles($media);
177
    }
178
179
    protected function renameMediaFile(Media $media)
180
    {
181
        $newFileName = $media->file_name;
182
        $oldFileName = $media->getOriginal('file_name');
183
184
        $mediaDirectory = $this->getMediaDirectory($media);
185
186
        $oldFile = $mediaDirectory.'/'.$oldFileName;
187
        $newFile = $mediaDirectory.'/'.$newFileName;
188
189
        $this->filesystem->disk($media->disk)->move($oldFile, $newFile);
190
    }
191
192
    protected function renameConversionFiles(Media $media)
193
    {
194
        $newFileName = $media->file_name;
195
        $oldFileName = $media->getOriginal('file_name');
196
197
        $conversionDirectory = $this->getConversionDirectory($media);
198
199
        $conversionCollection = ConversionCollection::createForMedia($media);
200
201
        foreach ($media->getMediaConversionNames() as $conversionName) {
202
            $conversion = $conversionCollection->getByName($conversionName);
203
204
            $oldFile = $conversionDirectory.$conversion->getConversionFile($oldFileName);
205
            $newFile = $conversionDirectory.$conversion->getConversionFile($newFileName);
206
207
            $disk = $this->filesystem->disk($media->disk);
208
209
            // A media conversion file might be missing, waiting to be generated, failed etc.
210
            if (! $disk->exists($oldFile)) {
211
                continue;
212
            }
213
214
            $disk->move($oldFile, $newFile);
215
        }
216
    }
217
218
    public function getMediaDirectory(Media $media, ?string $type = null): string
219
    {
220
        $pathGenerator = PathGeneratorFactory::create();
221
222
        if (! $type) {
223
            $directory = $pathGenerator->getPath($media);
224
        }
225
226
        if ($type === 'conversions') {
227
            $directory = $pathGenerator->getPathForConversions($media);
228
        }
229
230
        if ($type === 'responsiveImages') {
231
            $directory = $pathGenerator->getPathForResponsiveImages($media);
232
        }
233
234
        if (! in_array($media->getDiskDriverName(), ['s3'], true)) {
235
            $this->filesystem->disk($media->disk)->makeDirectory($directory);
0 ignored issues
show
The variable $directory does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
236
        }
237
238
        return $directory;
239
    }
240
241
    public function getConversionDirectory(Media $media): string
242
    {
243
        return $this->getMediaDirectory($media, 'conversions');
244
    }
245
246
    public function getResponsiveImagesDirectory(Media $media): string
247
    {
248
        return $this->getMediaDirectory($media, 'responsiveImages');
249
    }
250
}
251