Issues (15)

Security Analysis    no request data  

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/Controller/ImageController.php (2 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
namespace SvImages\Controller;
3
4
use SvImages\Exception\SvImagesException;
5
use SvImages\Options\ModuleOptions;
6
use SvImages\Parser\Result;
7
use SvImages\Router\RouteMatch;
8
use SvImages\Service\CacheManager;
9
use SvImages\Service\ImageService;
10
use Zend\Mvc\Controller\AbstractActionController;
11
use Zend\Http\Response;
12
13
/**
14
 * @author Vytautas Stankus <[email protected]>
15
 * @license MIT
16
 */
17
class ImageController extends AbstractActionController
18
{
19
    /**
20
     * @var ImageService
21
     */
22
    protected $imageService;
23
24
    /**
25
     * @var CacheManager
26
     */
27
    protected $cacheManager;
28
29
    /**
30
     * @var ModuleOptions
31
     */
32
    private $options;
33
34
    /**
35
     * @param ImageService  $imageService
36
     * @param CacheManager  $cacheManager
37
     * @param ModuleOptions $options
38
     */
39
    public function __construct(
40
        ImageService $imageService,
41
        CacheManager $cacheManager,
42
        ModuleOptions $options
43
    ) {
44
        $this->imageService = $imageService;
45
        $this->cacheManager = $cacheManager;
46
        $this->options = $options;
47
    }
48
49
    public function imageAction()
50
    {
51
        $routeMatch = $this->getEvent()->getRouteMatch();
52
53
        if (!$routeMatch instanceof RouteMatch) {
54
            $this->notFoundAction(); // todo
55
        }
56
57
        $result = $routeMatch->getParserResult();
58
        $uriPath = $result->getUriPath();
59
60
        if ($this->options->isCacheEnabled() && $contents = $this->cacheManager->get($uriPath)) {
61
            return $this->prepareResponse($contents, $result);
62
        }
63
64
        // fixme: this makes debugging very difficult by hiding all exceptions
65
        try {
66
            $contents = $this->imageService->generateImage($result);
67
        } catch (SvImagesException $exception) {
68
            // TODO: maybe implement some kind of failure handling strategy?
69
            // error image or 1px image would be better?
70
            $this->notFoundAction();
71
        }
72
73
        if ($this->options->isCacheEnabled() && $contents) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $contents of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
74
            $this->cacheManager->save($uriPath, $contents);
75
        }
76
77
        return $this->prepareResponse($contents, $result);
0 ignored issues
show
It seems like $contents defined by $this->imageService->generateImage($result) on line 66 can also be of type false; however, SvImages\Controller\Imag...ller::prepareResponse() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
78
    }
79
80
    /**
81
     * Creates response object
82
     *
83
     * @param string $content
84
     * @param Result $result
85
     *
86
     * @return Response
87
     */
88
    protected function prepareResponse($content, Result $result)
89
    {
90
        $mimeType = $this->guessMimeType($result->getFilePath());
91
92
        if (function_exists('mb_strlen')) {
93
            $contentLength = mb_strlen($content, '8bit');
94
        } else {
95
            $contentLength = strlen($content);
96
        }
97
98
        /* @var $response Response */
99
        $response = $this->getResponse();
100
        $response->getHeaders()
101
            ->addHeaderLine('Content-Transfer-Encoding', 'binary')
102
            ->addHeaderLine('Content-Type', $mimeType)
103
            ->addHeaderLine('Content-Length', $contentLength)
104
            // fixme: maybe add cache options to config
105
            ->addHeaderLine('Cache-Control', 'max-age=31536000, public')
106
            ->addHeaderLine('Expires', date_create('+1 years')->format('D, d M Y H:i:s').' GMT');
107
108
        $response->setContent($content);
109
110
        return $response;
111
    }
112
113
    /**
114
     * Guess MimeType by extension
115
     *
116
     * @param $filename
117
     *
118
     * @return string
119
     * @throws SvImagesException
120
     *
121
     * TODO: create MimeType resolver. Current implementation has a lot of problems
122
     */
123
    protected function guessMimeType($filename)
124
    {
125
        $mimeTypes = [
126
            'jpeg' => 'image/jpeg',
127
            'jpg'  => 'image/jpeg',
128
            'gif'  => 'image/gif',
129
            'png'  => 'image/png',
130
            'wbmp' => 'image/vnd.wap.wbmp',
131
            'xbm'  => 'image/xbm',
132
        ];
133
134
        $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
135
        if (!isset($mimeTypes[$extension])) {
136
            throw new SvImagesException('Invalid format');
137
        }
138
139
        return $mimeTypes[$extension];
140
    }
141
}
142