Issues (23)

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.

Controller/MediaController.php (4 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
namespace Alpixel\Bundle\MediaBundle\Controller;
4
5
use Alpixel\Bundle\MediaBundle\Entity\Media;
6
use Alpixel\Bundle\MediaBundle\Exception\InvalidMimeTypeException;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\Filesystem\Filesystem;
11
use Symfony\Component\HttpFoundation\File\Exception\UploadException;
12
use Symfony\Component\HttpFoundation\File\UploadedFile;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
class MediaController extends Controller
18
{
19
    /**
20
     * @Route("/media/upload/wysiwyg", name="upload_wysiwyg")
21
     *
22
     * @Method({"POST"})
23
     *
24
     * @param Request $request
25
     *
26
     * @return Response
27
     */
28
    public function uploadFilesWysiwygAction(Request $request)
0 ignored issues
show
uploadFilesWysiwygAction uses the super-global variable $_FILES which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
uploadFilesWysiwygAction uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
uploadFilesWysiwygAction uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
29
    {
30
        $template = 'AlpixelMediaBundle:admin:blocks/upload_wysiwyg.html.twig';
31
32
        try {
33
            $file = $request->files->get("upload");
34
            if ($file !== null) {
35
                $media = $this->get('alpixel_media.manager')->upload($file, $request->get('folder'), null);
36
37
                return $this->render(
38
                    $template,
39
                    [
40
                        'file_uploaded' => $media,
41
                    ]
42
                );
43
            } else {
44
                if (empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && strtolower(
45
                        $_SERVER['REQUEST_METHOD']
46
                    ) == 'post'
47
                ) {
48
                    throw new UploadException(
49
                        sprintf("Votre fichier dépasse la limite maximale de %s", ini_get('post_max_size'))
50
                    );
51
                }
52
            }
53
        } catch (\Exception $e) {
54
            return $this->render(
55
                $template,
56
                [
57
                    'error' => $e->getMessage(),
58
                ]
59
            );
60
        }
61
    }
62
63
    /**
64
     * @Route("/media/upload", name="upload")
65
     * @Route("/media/upload/{uploadConfiguration}", name="upload_configuration")
66
     *
67
     * @Method({"POST"})
68
     *
69
     * @param Request $request
70
     *
71
     * @return JsonResponse
72
     */
73
    public function uploadAction(Request $request, $uploadConfiguration = null)
74
    {
75
        $returnData = [];
76
77
        if ($request->get('lifetime') !== null) {
78
            $lifetime = new \DateTime($request->get('lifetime'));
79
        }
80
81
        if (empty($lifetime) || $lifetime == new \DateTime('now')) {
82
            $lifetime = new \DateTime('+6 hours');
83
        }
84
85
        if ($uploadConfiguration !== null &&
86
            !array_key_exists($uploadConfiguration, $this->getParameter("alpixel_media.upload_configurations"))
87
        ) {
88
            throw new \InvalidArgumentException();
89
        }
90
91
        $mediaPreview = $this->get('twig.extension.media_preview_extension');
92
        foreach ($request->files as $files) {
93
            if (!is_array($files)) {
94
                $files = [$files];
95
            }
96
            foreach ($files as $file) {
97
                /** @var UploadedFile $file */
98
                try {
99
                    $media = $this->get('alpixel_media.manager')->upload(
100
                        $file,
101
                        $request->get('folder'),
102
                        $lifetime,
103
                        $uploadConfiguration
104
                    );
105
106
                    $path = $mediaPreview->generatePathFromSecretKey($media->getSecretKey());
107
                    $returnData[] = [
108
                        'id'    => $media->getSecretKey(),
109
                        'path'  => $path,
110
                        'name'  => $media->getName(),
111
                        'error' => false,
112
                    ];
113
                } catch (InvalidMimeTypeException $e) {
114
                    $returnData[] = [
115
                        'name'         => $file->getClientOriginalName(),
116
                        'error'        => true,
117
                        'errorMessage' => $this->get('translator')->trans(
118
                            "Le format de fichier %format% n'est pas autorisé",
119
                            [
120
                                '%format%' => $file->getClientMimeType(),
121
                            ]
122
                        ),
123
                    ];
124
                }
125
            }
126
        }
127
128
        return new JsonResponse($returnData);
129
    }
130
131
    /**
132
     * @Route("/media/download/{id}-{name}", name="media_download_public")
133
     * @Route("/media/download/{filter}/{id}-{name}", name="media_download_public_filters")
134
     * @Route("/media/download/{secretKey}/{filter}", name="media_download_private")
135
     *
136
     * @Method({"GET"})
137
     * @param Media $media
138
     * @param string $filter
139
     * @return Response
140
     */
141
    public function downloadMediaAction(Media $media, $filter = null)
0 ignored issues
show
The parameter $filter is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
142
    {
143
        $response = new Response();
144
        $response->setContent(file_get_contents($this->get('alpixel_media.manager')->getAbsolutePath($media)));
145
        $response->headers->set('Content-Type', 'application/force-download');
146
        $response->headers->set('Content-disposition', 'filename='.$media->getName());
147
148
        return $response;
149
    }
150
151
    /**
152
     * @Route("/media/{id}-{name}", name="media_show_public")
153
     * @Route("/media/{filter}/{id}-{name}", name="media_show_public_filters")
154
     * @Route("/media/{secretKey}/{filter}", name="media_show_private")
155
     *
156
     * @Method({"GET"})
157
     */
158
    public function showMediaAction(Request $request, Media $media, $filter = null)
159
    {
160
        $response = new Response();
161
        $lastModified = new \DateTime('now');
162
163
        //Checking if it is an image or not
164
        $src = $this->get('alpixel_media.manager')->getAbsolutePath($media);
165
        $isImage = @getimagesize($src);
166
167
        try {
168
            if ($isImage) {
169
                $response->headers->set('Content-disposition', 'inline;filename='.$media->getName());
170
                if (!empty($filter) && $isImage) {
171
                    $src = $this->get('alpixel_media.manager')->getAbsolutePath($media, $filter);
172
                    $dataManager = $this->get('liip_imagine.data.manager'); // the data manager service
173
                    $filterManager = $this->get('liip_imagine.filter.manager'); // the filter manager service
174
                    $uploadDir = $this->get('alpixel_media.manager')->getUploadDir($filter);
175
176
                    if (!is_file($src)) {
177
                        $fs = new Filesystem();
178
                        if (!$fs->exists($uploadDir.$media->getFolder())) {
179
                            $fs->mkdir($uploadDir.$media->getFolder());
180
                        }
181
182
                        $path = 'upload/'.$media->getUri();
183
184
                        // find the image and determine its type
185
                        $image = $dataManager->find($filter, $path);
186
187
                        // run the filter
188
                        $responseData = $filterManager->applyFilter($image, $filter);
189
                        $data = $responseData->getContent();
190
                        file_put_contents($uploadDir.$media->getUri(), $data);
191
                    } else {
192
                        $data = file_get_contents($src);
193
                        $lastModified->setTimestamp(filemtime($src));
194
                    }
195
                } else {
196
                    $src = $this->get('alpixel_media.manager')->getAbsolutePath($media);
197
                    $lastModified->setTimestamp(filemtime($src));
198
                    $data = file_get_contents($src);
199
                }
200
            } else {
201
                $lastModified->setTimestamp(filemtime($src));
202
                $data = file_get_contents($src);
203
                $response->headers->set('Content-disposition', 'attachment;filename='.basename($media->getUri()));
204
            }
205
206
            if ($data === false) {
207
                throw $this->createNotFoundException();
208
            }
209
        } catch (\Exception $e) {
210
            throw $this->createNotFoundException($e->getMessage());
211
        }
212
213
        $response->setLastModified($lastModified);
214
        $response->setPublic();
215
        $response->headers->set('Content-Type', $media->getMime());
216
217
        if ($response->isNotModified($request)) {
218
            return $response;
219
        }
220
221
        $response->setContent($data);
222
223
        return $response;
224
    }
225
}
226