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/Models/Media.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\Models;
4
5
use DateTimeInterface;
6
use Illuminate\Contracts\Support\Htmlable;
7
use Illuminate\Contracts\Support\Responsable;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\MorphTo;
10
use Illuminate\Support\Arr;
11
use Illuminate\Support\Collection;
12
use Illuminate\Support\HtmlString;
13
use Spatie\MediaLibrary\Conversion\Conversion;
14
use Spatie\MediaLibrary\Conversion\ConversionCollection;
15
use Spatie\MediaLibrary\Filesystem\Filesystem;
16
use Spatie\MediaLibrary\HasMedia\HasMedia;
17
use Spatie\MediaLibrary\Helpers\File;
18
use Spatie\MediaLibrary\Helpers\TemporaryDirectory;
19
use Spatie\MediaLibrary\ImageGenerators\FileTypes\Image;
20
use Spatie\MediaLibrary\Models\Concerns\IsSorted;
21
use Spatie\MediaLibrary\Models\Traits\CustomMediaProperties;
22
use Spatie\MediaLibrary\ResponsiveImages\RegisteredResponsiveImages;
23
use Spatie\MediaLibrary\UrlGenerator\UrlGeneratorFactory;
24
25
class Media extends Model implements Responsable, Htmlable
26
{
27
    use IsSorted,
28
        CustomMediaProperties;
29
30
    const TYPE_OTHER = 'other';
31
32
    protected $guarded = [];
33
34
    protected $casts = [
35
        'manipulations' => 'array',
36
        'custom_properties' => 'array',
37
        'responsive_images' => 'array',
38
    ];
39
40
    public function model(): MorphTo
41
    {
42
        return $this->morphTo();
43
    }
44
45
    /*
46
     * Get the full url to a original media file.
47
    */
48
    public function getFullUrl(string $conversionName = ''): string
49
    {
50
        return url($this->getUrl($conversionName));
51
    }
52
53
    /*
54
     * Get the url to a original media file.
55
     */
56
    public function getUrl(string $conversionName = ''): string
57
    {
58
        $urlGenerator = UrlGeneratorFactory::createForMedia($this, $conversionName);
59
60
        return $urlGenerator->getUrl();
61
    }
62
63
    public function getTemporaryUrl(DateTimeInterface $expiration, string $conversionName = '', array $options = []): string
64
    {
65
        $urlGenerator = UrlGeneratorFactory::createForMedia($this, $conversionName);
66
67
        return $urlGenerator->getTemporaryUrl($expiration, $options);
68
    }
69
70
    /*
71
     * Get the path to the original media file.
72
     */
73
    public function getPath(string $conversionName = ''): string
74
    {
75
        $urlGenerator = UrlGeneratorFactory::createForMedia($this, $conversionName);
76
77
        return $urlGenerator->getPath();
78
    }
79
80
    public function getImageGenerators(): Collection
81
    {
82
        return collect(config('medialibrary.image_generators'));
83
    }
84
85
    public function getTypeAttribute(): string
86
    {
87
        $type = $this->getTypeFromExtension();
88
89
        if ($type !== self::TYPE_OTHER) {
90
            return $type;
91
        }
92
93
        return $this->getTypeFromMime();
94
    }
95
96
    public function getTypeFromExtension(): string
97
    {
98
        $imageGenerator = $this->getImageGenerators()
99
            ->map(function (string $className) {
100
                return app($className);
101
            })
102
            ->first->canHandleExtension(strtolower($this->extension));
103
104
        return $imageGenerator
105
            ? $imageGenerator->getType()
106
            : static::TYPE_OTHER;
107
    }
108
109
    public function getTypeFromMime(): string
110
    {
111
        $imageGenerator = $this->getImageGenerators()
112
            ->map(function (string $className) {
113
                return app($className);
114
            })
115
            ->first->canHandleMime($this->mime_type);
116
117
        return $imageGenerator
118
            ? $imageGenerator->getType()
119
            : static::TYPE_OTHER;
120
    }
121
122
    public function getExtensionAttribute(): string
123
    {
124
        return pathinfo($this->file_name, PATHINFO_EXTENSION);
125
    }
126
127
    public function getHumanReadableSizeAttribute(): string
128
    {
129
        return File::getHumanReadableSize($this->size);
130
    }
131
132
    public function getDiskDriverName(): string
133
    {
134
        return strtolower(config("filesystems.disks.{$this->disk}.driver"));
135
    }
136
137
    /*
138
     * Determine if the media item has a custom property with the given name.
139
     */
140
    public function hasCustomProperty(string $propertyName): bool
141
    {
142
        return Arr::has($this->custom_properties, $propertyName);
143
    }
144
145
    /**
146
     * Get the value of custom property with the given name.
147
     *
148
     * @param string $propertyName
149
     * @param mixed $default
150
     *
151
     * @return mixed
152
     */
153
    public function getCustomProperty(string $propertyName, $default = null)
154
    {
155
        return Arr::get($this->custom_properties, $propertyName, $default);
156
    }
157
158
    /**
159
     * @param string $name
160
     * @param mixed $value
161
     *
162
     * @return $this
163
     */
164
    public function setCustomProperty(string $name, $value): self
165
    {
166
        $customProperties = $this->custom_properties;
167
168
        Arr::set($customProperties, $name, $value);
169
170
        $this->custom_properties = $customProperties;
171
172
        return $this;
173
    }
174
175
    public function forgetCustomProperty(string $name): self
176
    {
177
        $customProperties = $this->custom_properties;
178
179
        Arr::forget($customProperties, $name);
180
181
        $this->custom_properties = $customProperties;
182
183
        return $this;
184
    }
185
186
    /*
187
     * Get all the names of the registered media conversions.
188
     */
189
    public function getMediaConversionNames(): array
190
    {
191
        $conversions = ConversionCollection::createForMedia($this);
192
193
        return $conversions->map(function (Conversion $conversion) {
194
            return $conversion->getName();
195
        })->toArray();
196
    }
197
198
    public function hasGeneratedConversion(string $conversionName): bool
199
    {
200
        $generatedConversions = $this->getGeneratedConversions();
201
202
        return $generatedConversions[$conversionName] ?? false;
203
    }
204
205
    public function markAsConversionGenerated(string $conversionName, bool $generated): self
206
    {
207
        $this->setCustomProperty("generated_conversions.{$conversionName}", $generated);
208
209
        $this->save();
210
211
        return $this;
212
    }
213
214
    public function getGeneratedConversions(): Collection
215
    {
216
        return collect($this->getCustomProperty('generated_conversions', []));
217
    }
218
219
    /**
220
     * Create an HTTP response that represents the object.
221
     *
222
     * @param  \Illuminate\Http\Request $request
223
     *
224
     * @return \Illuminate\Http\Response
225
     */
226
    public function toResponse($request)
227
    {
228
        $downloadHeaders = [
229
            'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
230
            'Content-Type' => $this->mime_type,
231
            'Content-Length' => $this->size,
232
            'Content-Disposition' => 'attachment; filename="'.$this->file_name.'"',
233
            'Pragma' => 'public',
234
        ];
235
236
        return response()->stream(function () {
0 ignored issues
show
The method stream does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
237
            $stream = $this->stream();
238
239
            fpassthru($stream);
240
241
            if (is_resource($stream)) {
242
                fclose($stream);
243
            }
244
        }, 200, $downloadHeaders);
245
    }
246
247
    public function getResponsiveImageUrls(string $conversionName = ''): array
248
    {
249
        return $this->responsiveImages($conversionName)->getUrls();
250
    }
251
252
    public function hasResponsiveImages(string $conversionName = ''): bool
253
    {
254
        return count($this->getResponsiveImageUrls($conversionName)) > 0;
255
    }
256
257
    public function getSrcset(string $conversionName = ''): string
258
    {
259
        return $this->responsiveImages($conversionName)->getSrcset();
260
    }
261
262
    public function toHtml()
263
    {
264
        return $this->img();
265
    }
266
267
    /**
268
     * @param string|array $conversion
269
     * @param array $extraAttributes
270
     *
271
     * @return string
272
     */
273
    public function img($conversion = '', array $extraAttributes = []): string
274
    {
275
        if (! (new Image())->canHandleMime($this->mime_type)) {
276
            return '';
277
        }
278
279
        if (is_array($conversion)) {
280
            $attributes = $conversion;
281
282
            $conversion = $attributes['conversion'] ?? '';
283
284
            unset($attributes['conversion']);
285
286
            $extraAttributes = array_merge($attributes, $extraAttributes);
287
        }
288
289
        $attributeString = collect($extraAttributes)
290
            ->map(function ($value, $name) {
291
                return $name.'="'.$value.'"';
292
            })->implode(' ');
293
294
        if (strlen($attributeString)) {
295
            $attributeString = ' '.$attributeString;
296
        }
297
298
        $media = $this;
299
300
        $viewName = 'image';
301
302
        $width = '';
303
304
        if ($this->hasResponsiveImages($conversion)) {
305
            $viewName = config('medialibrary.responsive_images.use_tiny_placeholders')
306
                ? 'responsiveImageWithPlaceholder'
307
                : 'responsiveImage';
308
309
            $width = $this->responsiveImages($conversion)->files->first()->width();
310
        }
311
312
        return view("medialibrary::{$viewName}", compact(
313
            'media',
314
            'conversion',
315
            'attributeString',
316
            'width'
317
        ));
318
    }
319
320
    public function move(HasMedia $model, $collectionName = 'default', string $diskName = ''): self
321
    {
322
        $newMedia = $this->copy($model, $collectionName, $diskName);
323
324
        $this->delete();
325
326
        return $newMedia;
327
    }
328
329
    public function copy(HasMedia $model, $collectionName = 'default', string $diskName = ''): self
330
    {
331
        $temporaryDirectory = TemporaryDirectory::create();
332
333
        $temporaryFile = $temporaryDirectory->path('/').DIRECTORY_SEPARATOR.$this->file_name;
334
335
        app(Filesystem::class)->copyFromMediaLibrary($this, $temporaryFile);
336
337
        $newMedia = $model
338
            ->addMedia($temporaryFile)
339
            ->usingName($this->name)
340
            ->withCustomProperties($this->custom_properties)
341
            ->toMediaCollection($collectionName, $diskName);
342
343
        $temporaryDirectory->delete();
344
345
        return $newMedia;
346
    }
347
348
    public function responsiveImages(string $conversionName = ''): RegisteredResponsiveImages
349
    {
350
        return new RegisteredResponsiveImages($this, $conversionName);
351
    }
352
353
    public function stream()
354
    {
355
        $filesystem = app(Filesystem::class);
356
357
        return $filesystem->getStream($this);
358
    }
359
360
    public function __invoke(...$arguments)
361
    {
362
        return new HtmlString($this->img(...$arguments));
363
    }
364
}
365