Issues (14)

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/AppKernel.php (3 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
namespace Nastoletni\Code;
6
7
use Doctrine\DBAL\Configuration;
8
use Doctrine\DBAL\DriverManager;
9
use Monolog\Handler\StreamHandler;
10
use Monolog\Logger;
11
use Nastoletni\Code\Infrastructure\AES256Crypter;
12
use Nastoletni\Code\Infrastructure\Dbal\DbalPasteMapper;
13
use Nastoletni\Code\Infrastructure\Dbal\DbalPasteRepository;
14
use Nastoletni\Code\Infrastructure\HttpsXkcdRepository;
15
use Nastoletni\Code\Slim\DecoratingCallableResolver;
16
use Nastoletni\Code\Slim\Middleware\SymfonySessionMiddleware;
17
use Nastoletni\Code\Twig\SymfonyValidatorExtension;
18
use Nastoletni\Code\Twig\TransExtension;
19
use Nastoletni\Code\UserInterface\Controller\ControllerDecorator;
20
use Nastoletni\Code\UserInterface\Web\Controller\ErrorController;
21
use Nastoletni\Code\UserInterface\Web\Controller\PasteController;
22
use Slim\App;
23
use Slim\Container;
24
use Slim\Handlers\Error;
25
use Slim\Handlers\PhpError;
26
use Slim\Handlers\Strategies\RequestResponseArgs;
27
use Slim\Views\Twig;
28
use Slim\Views\TwigExtension;
29
use Symfony\Component\HttpFoundation\Session\Session;
30
use Symfony\Component\Translation\Loader\PhpFileLoader;
31
use Symfony\Component\Translation\Translator;
32
use Symfony\Component\Yaml\Yaml;
33
34
class AppKernel
35
{
36
    /**
37
     * @var App
38
     */
39
    private $slim;
40
41
    /**
42
     * AppKernel constructor.
43
     */
44
    public function __construct()
45
    {
46
        $this->slim = new App();
47
48
        $this->setupConfig();
49
        $this->setupServices();
0 ignored issues
show
The call to the method Nastoletni\Code\AppKernel::setupServices() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
50
        $this->setupRoutes();
51
52
        // Middlewares
53
        $this->slim->add(new SymfonySessionMiddleware($this->slim->getContainer()['session']));
54
    }
55
56
    /**
57
     * Sets up config in container.
58
     */
59
    private function setupConfig(): void
60
    {
61
        $config = Yaml::parse(file_get_contents(__DIR__.'/config.yml'));
62
63
        $this->slim->getContainer()['config'] = $config;
64
    }
65
66
    /**
67
     * Sets up dependencies in container.
68
     */
69
    private function setupServices(): void
70
    {
71
        $container = $this->slim->getContainer();
72
        $container['settings']['displayErrorDetails'] = $container['config']['debug'];
73
        $container['logger'] = function () {
74
            return new Logger('application', [
75
                new StreamHandler(__DIR__.'/../logs/logs.log'),
76
            ]);
77
        };
78
        $container['foundHandler'] = function () {
79
            return new RequestResponseArgs();
80
        };
81
        $container['notFoundHandler'] = function (Container $container) {
82
            return [$container[ErrorController::class], 'notFound'];
83
        };
84 View Code Duplication
        $container['errorHandler'] = function (Container $container) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
            // Show pretty error page on production and Slim debug info on development.
86
            $next = $container['config']['debug'] ?
87
                new Error($container['config']['debug']) :
88
                [$container[ErrorController::class], 'error'];
89
90
            return new Slim\Handler\LoggingErrorHandler(
91
                $container->get('logger'),
92
                $next
93
            );
94
        };
95 View Code Duplication
        $container['phpErrorHandler'] = function (Container $container) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            // Show pretty error page on production and Slim debug info on development.
97
            $next = $container['config']['debug'] ?
98
                new PhpError($container['config']['debug']) :
99
                [$container[ErrorController::class], 'error'];
100
101
            return new Slim\Handler\LoggingErrorHandler(
102
                $container->get('logger'),
103
                $next
104
            );
105
        };
106
        $container['translator'] = function (Container $container) {
107
            $translator = new Translator($container['config']['locale']);
108
            $translator->setFallbackLocales(['en']);
109
110
            $translator->addLoader('php', new PhpFileLoader());
111
            $translator->addResource('php', __DIR__.'/../resources/translations/messages.php', 'en');
112
            $translator->addResource('php', __DIR__.'/../resources/translations/messages.pl.php', 'pl');
113
114
            return $translator;
115
        };
116
        $container['twig'] = function (Container $container) {
117
            $twig = new Twig(__DIR__.'/../resources/views/', [
118
                'debug' => $container['config']['debug'],
119
            ]);
120
            $twig->addExtension(new TwigExtension($container['router'], $container['config']['base_url']));
121
            $twig->addExtension(new SymfonyValidatorExtension());
122
            $twig->addExtension(new TransExtension($container['translator']));
123
124
            return $twig;
125
        };
126
        $container['session'] = function () {
127
            return new Session();
128
        };
129
        $container['controllerDecorator'] = function (Container $container) {
130
            return new ControllerDecorator(
131
                $container['twig'],
132
                $container['router'],
133
                $container['session']
134
            );
135
        };
136
        $container['callableResolver'] = function (Container $container) {
137
            return new DecoratingCallableResolver(
138
                $container,
139
                $container['controllerDecorator']
140
            );
141
        };
142
        $container['dbal'] = function (Container $container) {
143
            $config = new Configuration();
144
145
            return DriverManager::getConnection([
146
                'driver'   => 'pdo_mysql',
147
                'host'     => $container['config']['database']['host'],
148
                'port'     => $container['config']['database']['port'],
149
                'dbname'   => $container['config']['database']['name'],
150
                'user'     => $container['config']['database']['user'],
151
                'password' => $container['config']['database']['password'],
152
                'charset'  => $container['config']['database']['charset'],
153
            ], $config);
154
        };
155
156
        // Controllers
157
        $container[PasteController::class] = function (Container $container) {
158
            $pasteRepository = new DbalPasteRepository($container['dbal'], new DbalPasteMapper());
159
160
            return new PasteController($pasteRepository, new AES256Crypter());
161
        };
162
        $container[ErrorController::class] = function (Container $container) {
163
            /** @var ControllerDecorator $controllerDecorator */
164
            $controllerDecorator = $container['controllerDecorator'];
165
166
            $errorController = new ErrorController(
167
                new HttpsXkcdRepository()
168
            );
169
            $controllerDecorator->decorate($errorController);
170
171
            return $errorController;
172
        };
173
    }
174
175
    /**
176
     * Sets up routes.
177
     */
178
    private function setupRoutes(): void
179
    {
180
        $routes = Yaml::parse(file_get_contents(__DIR__.'/routes.yml'));
181
182
        foreach ($routes as $routeName => $route) {
183
            $this->slim->map([$route['method']], $route['path'], $route['controller'])->setName($routeName);
184
        }
185
    }
186
187
    /**
188
     * Sends response to the client.
189
     */
190
    public function handle(): void
191
    {
192
        $this->slim->run();
193
    }
194
}
195