Issues (234)

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/Provider/Pool.php (6 issues)

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Provider;
15
16
use Sonata\Form\Validator\ErrorElement;
17
use Sonata\MediaBundle\Model\MediaInterface;
18
use Sonata\MediaBundle\Security\DownloadStrategyInterface;
19
20
/**
21
 * @final since sonata-project/media-bundle 3.21.0
22
 */
23
class Pool
24
{
25
    /**
26
     * @var array
27
     */
28
    protected $providers = [];
29
30
    /**
31
     * @var array
32
     */
33
    protected $contexts = [];
34
35
    /**
36
     * NEXT_MAJOR: remove this property.
37
     *
38
     * @deprecated since sonata-project/media-bundle 3.1 and will be removed in 4.0. Use $downloadStrategies instead
39
     *
40
     * @var DownloadStrategyInterface[]
41
     */
42
    protected $downloadSecurities = [];
43
44
    /**
45
     * @var DownloadStrategyInterface[]
46
     */
47
    protected $downloadStrategies = [];
48
49
    /**
50
     * @var string
51
     */
52
    protected $defaultContext;
53
54
    /**
55
     * @param string $context
56
     */
57
    public function __construct($context)
58
    {
59
        $this->defaultContext = $context;
60
    }
61
62
    /**
63
     * @param string $name
64
     *
65
     * @throws \RuntimeException
66
     *
67
     * @return MediaProviderInterface
68
     */
69
    public function getProvider($name)
70
    {
71
        if (!$name) {
72
            throw new \InvalidArgumentException('Provider name cannot be empty, did you forget to call setProviderName() in your Media object?');
73
        }
74
        if (empty($this->providers)) {
75
            throw new \RuntimeException(sprintf('Unable to retrieve provider named "%s" since there are no providers configured yet.', $name));
76
        }
77
        if (!isset($this->providers[$name])) {
78
            throw new \InvalidArgumentException(sprintf('Unable to retrieve the provider named "%s". Available providers are %s.', $name, '"'.implode('", "', $this->getProviderList()).'"'));
79
        }
80
81
        return $this->providers[$name];
82
    }
83
84
    /**
85
     * @param string $name
86
     */
87
    public function addProvider($name, MediaProviderInterface $instance): void
88
    {
89
        $this->providers[$name] = $instance;
90
    }
91
92
    /**
93
     * NEXT_MAJOR: remove this method.
94
     *
95
     * @deprecated since sonata-project/media-bundle 3.1, to be removed in 4.0
96
     *
97
     * @param string $name
98
     */
99
    public function addDownloadSecurity($name, DownloadStrategyInterface $security): void
100
    {
101
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
102
            'The '.__METHOD__.' method is deprecated since version 3.1 and will be removed in 4.0.',
103
            E_USER_DEPRECATED
104
        );
105
106
        $this->downloadSecurities[$name] = $security;
0 ignored issues
show
Deprecated Code introduced by
The property Sonata\MediaBundle\Provi...ol::$downloadSecurities has been deprecated with message: since sonata-project/media-bundle 3.1 and will be removed in 4.0. Use $downloadStrategies instead

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
107
108
        $this->addDownloadStrategy($name, $security);
109
    }
110
111
    /**
112
     * @param string $name
113
     */
114
    public function addDownloadStrategy($name, DownloadStrategyInterface $security): void
115
    {
116
        $this->downloadStrategies[$name] = $security;
117
    }
118
119
    /**
120
     * @param array $providers
121
     */
122
    public function setProviders($providers): void
123
    {
124
        $this->providers = $providers;
125
    }
126
127
    /**
128
     * @return MediaProviderInterface[]
129
     */
130
    public function getProviders()
131
    {
132
        return $this->providers;
133
    }
134
135
    /**
136
     * @param string $name
137
     */
138
    public function addContext($name, array $providers = [], array $formats = [], array $download = []): void
139
    {
140
        if (!$this->hasContext($name)) {
141
            $this->contexts[$name] = [
142
                'providers' => [],
143
                'formats' => [],
144
                'download' => [],
145
            ];
146
        }
147
148
        $this->contexts[$name]['providers'] = $providers;
149
        $this->contexts[$name]['formats'] = $formats;
150
        $this->contexts[$name]['download'] = $download;
151
    }
152
153
    /**
154
     * @param string $name
155
     *
156
     * @return bool
157
     */
158
    public function hasContext($name)
159
    {
160
        return isset($this->contexts[$name]);
161
    }
162
163
    /**
164
     * @param string $name
165
     *
166
     * @return array|null
167
     */
168
    public function getContext($name)
169
    {
170
        if (!$this->hasContext($name)) {
171
            return null;
172
        }
173
174
        return $this->contexts[$name];
175
    }
176
177
    /**
178
     * Returns the context list.
179
     *
180
     * @return array
181
     */
182
    public function getContexts()
183
    {
184
        return $this->contexts;
185
    }
186
187
    /**
188
     * @param string $name
189
     *
190
     * @return array|null
191
     */
192
    public function getProviderNamesByContext($name)
193
    {
194
        $context = $this->getContext($name);
195
196
        if (!$context) {
197
            return null;
198
        }
199
200
        return $context['providers'];
201
    }
202
203
    /**
204
     * @param string $name
205
     *
206
     * @return array|null
207
     */
208
    public function getFormatNamesByContext($name)
209
    {
210
        $context = $this->getContext($name);
211
212
        if (!$context) {
213
            return null;
214
        }
215
216
        return $context['formats'];
217
    }
218
219
    /**
220
     * @param string $name
221
     *
222
     * @return array
223
     */
224
    public function getProvidersByContext($name)
225
    {
226
        $providers = [];
227
228
        if (!$this->hasContext($name)) {
229
            return $providers;
230
        }
231
232
        foreach ($this->getProviderNamesByContext($name) as $name) {
0 ignored issues
show
The expression $this->getProviderNamesByContext($name) of type array|null 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...
233
            $providers[] = $this->getProvider($name);
234
        }
235
236
        return $providers;
237
    }
238
239
    /**
240
     * @return array
241
     */
242
    public function getProviderList()
243
    {
244
        $choices = [];
245
        foreach (array_keys($this->providers) as $name) {
246
            $choices[$name] = $name;
247
        }
248
249
        return $choices;
250
    }
251
252
    /**
253
     * NEXT_MAJOR: remove this method.
254
     *
255
     * @deprecated since sonata-project/media-bundle 3.1, to be removed in 4.0
256
     *
257
     * @throws \RuntimeException
258
     *
259
     * @return DownloadStrategyInterface
260
     */
261
    public function getDownloadSecurity(MediaInterface $media)
262
    {
263
        @trigger_error('The '.__METHOD__.' method is deprecated since version 3.1 and will be removed in 4.0.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
264
265
        return $this->getDownloadStrategy($media);
266
    }
267
268
    /**
269
     * @throws \RuntimeException
270
     *
271
     * @return DownloadStrategyInterface
272
     */
273
    public function getDownloadStrategy(MediaInterface $media)
274
    {
275
        $context = $this->getContext($media->getContext());
276
277
        $id = $context['download']['strategy'];
278
279
        // NEXT_MAJOR: remove this line with the next major release.
280
        if (isset($this->downloadSecurities[$id])) {
0 ignored issues
show
Deprecated Code introduced by
The property Sonata\MediaBundle\Provi...ol::$downloadSecurities has been deprecated with message: since sonata-project/media-bundle 3.1 and will be removed in 4.0. Use $downloadStrategies instead

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
281
            return $this->downloadSecurities[$id];
0 ignored issues
show
Deprecated Code introduced by
The property Sonata\MediaBundle\Provi...ol::$downloadSecurities has been deprecated with message: since sonata-project/media-bundle 3.1 and will be removed in 4.0. Use $downloadStrategies instead

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
282
        }
283
284
        if (!isset($this->downloadStrategies[$id])) {
285
            throw new \RuntimeException('Unable to retrieve the download security : '.$id);
286
        }
287
288
        return $this->downloadStrategies[$id];
289
    }
290
291
    /**
292
     * @return string
293
     */
294
    public function getDownloadMode(MediaInterface $media)
295
    {
296
        $context = $this->getContext($media->getContext());
297
298
        return $context['download']['mode'];
299
    }
300
301
    /**
302
     * @return string
303
     */
304
    public function getDefaultContext()
305
    {
306
        return $this->defaultContext;
307
    }
308
309
    public function validate(ErrorElement $errorElement, MediaInterface $media): void
310
    {
311
        if (!$media->getProviderName()) {
312
            return;
313
        }
314
315
        $provider = $this->getProvider($media->getProviderName());
316
317
        $provider->validate($errorElement, $media);
318
    }
319
}
320