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/Twig/Extension/MediaExtension.php (1 issue)

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\Twig\Extension;
15
16
use Sonata\Doctrine\Model\ManagerInterface;
17
use Sonata\MediaBundle\Model\MediaInterface;
18
use Sonata\MediaBundle\Provider\Pool;
19
use Sonata\MediaBundle\Twig\TokenParser\MediaTokenParser;
20
use Sonata\MediaBundle\Twig\TokenParser\PathTokenParser;
21
use Sonata\MediaBundle\Twig\TokenParser\ThumbnailTokenParser;
22
use Twig\Environment;
23
use Twig\Extension\AbstractExtension;
24
use Twig\Extension\InitRuntimeInterface;
25
26
/**
27
 * @final since sonata-project/media-bundle 3.21.0
28
 */
29
class MediaExtension extends AbstractExtension implements InitRuntimeInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Twig\Extension\InitRuntimeInterface has been deprecated with message: since Twig 2.7, to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
30
{
31
    /**
32
     * @var Pool
33
     */
34
    protected $mediaService;
35
36
    /**
37
     * @var array
38
     */
39
    protected $resources = [];
40
41
    /**
42
     * @var ManagerInterface
43
     */
44
    protected $mediaManager;
45
46
    /**
47
     * @var Environment
48
     */
49
    protected $environment;
50
51
    public function __construct(Pool $mediaService, ManagerInterface $mediaManager)
52
    {
53
        $this->mediaService = $mediaService;
54
        $this->mediaManager = $mediaManager;
55
    }
56
57
    public function getTokenParsers()
58
    {
59
        return [
60
            new MediaTokenParser(static::class),
61
            new ThumbnailTokenParser(static::class),
62
            new PathTokenParser(static::class),
63
        ];
64
    }
65
66
    public function initRuntime(Environment $environment): void
67
    {
68
        $this->environment = $environment;
69
    }
70
71
    /**
72
     * @param MediaInterface $media
73
     * @param string         $format
74
     * @param array          $options
75
     *
76
     * @return string
77
     */
78
    public function media($media, $format, $options = [])
79
    {
80
        $media = $this->getMedia($media);
81
82
        if (null === $media) {
83
            return '';
84
        }
85
86
        $provider = $this
87
            ->getMediaService()
88
            ->getProvider($media->getProviderName());
89
90
        $format = $provider->getFormatName($media, $format);
91
92
        $options = $provider->getHelperProperties($media, $format, $options);
93
94
        return $this->render($provider->getTemplate('helper_view'), [
95
            'media' => $media,
96
            'format' => $format,
97
            'options' => $options,
98
        ]);
99
    }
100
101
    /**
102
     * Returns the thumbnail for the provided media.
103
     *
104
     * @param MediaInterface $media
105
     * @param string         $format
106
     * @param array          $options
107
     *
108
     * @return string
109
     */
110
    public function thumbnail($media, $format, $options = [])
111
    {
112
        $media = $this->getMedia($media);
113
114
        if (null === $media) {
115
            return '';
116
        }
117
118
        $provider = $this->getMediaService()
119
           ->getProvider($media->getProviderName());
120
121
        $format = $provider->getFormatName($media, $format);
122
        $format_definition = $provider->getFormat($format);
123
124
        // build option
125
        $defaultOptions = [
126
            'title' => $media->getName(),
127
            'alt' => $media->getName(),
128
        ];
129
130
        if (\is_array($format_definition) && $format_definition['width']) {
131
            $defaultOptions['width'] = $format_definition['width'];
132
        }
133
        if (\is_array($format_definition) && $format_definition['height']) {
134
            $defaultOptions['height'] = $format_definition['height'];
135
        }
136
137
        $options = array_merge($defaultOptions, $options);
138
139
        $options['src'] = $provider->generatePublicUrl($media, $format);
140
141
        return $this->render($provider->getTemplate('helper_thumbnail'), [
142
            'media' => $media,
143
            'options' => $options,
144
        ]);
145
    }
146
147
    /**
148
     * @param string $template
149
     *
150
     * @return mixed
151
     */
152
    public function render($template, array $parameters = [])
153
    {
154
        if (!isset($this->resources[$template])) {
155
            $this->resources[$template] = $this->environment->loadTemplate($template);
156
        }
157
158
        return $this->resources[$template]->render($parameters);
159
    }
160
161
    /**
162
     * @param MediaInterface $media
163
     * @param string         $format
164
     *
165
     * @return string
166
     */
167
    public function path($media, $format)
168
    {
169
        $media = $this->getMedia($media);
170
171
        if (!$media) {
172
            return '';
173
        }
174
175
        $provider = $this->getMediaService()
176
           ->getProvider($media->getProviderName());
177
178
        $format = $provider->getFormatName($media, $format);
179
180
        return $provider->generatePublicUrl($media, $format);
181
    }
182
183
    /**
184
     * @return Pool
185
     */
186
    public function getMediaService()
187
    {
188
        return $this->mediaService;
189
    }
190
191
    /**
192
     * @param mixed $media
193
     */
194
    private function getMedia($media): ?MediaInterface
195
    {
196
        if (!$media instanceof MediaInterface && \strlen((string) $media) > 0) {
197
            $media = $this->mediaManager->findOneBy([
198
                'id' => $media,
199
            ]);
200
        }
201
202
        if (!$media instanceof MediaInterface) {
203
            return null;
204
        }
205
206
        if (MediaInterface::STATUS_OK !== $media->getProviderStatus()) {
207
            return null;
208
        }
209
210
        return $media;
211
    }
212
}
213