Issues (15)

Security Analysis    1 potential vulnerability

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 (1)
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/Routing/Dispatcher.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
declare(strict_types = 1);
3
4
namespace Zortje\MVC\Routing;
5
6
use Monolog\Logger;
7
use Zortje\MVC\Configuration\Configuration;
8
use Zortje\MVC\Controller\ControllerFactory;
9
use Zortje\MVC\Controller\Exception\ControllerActionPrivateInsufficientAuthenticationException;
10
use Zortje\MVC\Controller\Exception\ControllerActionProtectedInsufficientAuthenticationException;
11
use Zortje\MVC\Controller\NotFoundController; // @todo this is a user implemented controller and should be removed after user stuff is cleaned up
12
use Zortje\MVC\Model\Table\Entity\Entity;
13
use Zortje\MVC\Network\Request;
14
use Zortje\MVC\Network\Response;
15
use Zortje\MVC\Routing\Exception\RouteNonexistentException;
16
17
/**
18
 * Class Dispatcher
19
 *
20
 * @package Zortje\MVC\Routing
21
 */
22
class Dispatcher
23
{
24
25
    /**
26
     * @var \PDO PDO
27
     */
28
    protected $pdo;
29
30
    /**
31
     * @var Configuration Configuration
32
     */
33
    protected $configuration;
34
35
    /**
36
     * @var Logger
37
     */
38
    protected $logger;
39
40
    /**
41
     * Dispatcher constructor.
42
     *
43
     * @param \PDO          $pdo
44
     * @param Configuration $configuration
45
     */
46 1
    public function __construct(\PDO $pdo, Configuration $configuration)
47
    {
48 1
        $this->pdo           = $pdo;
49 1
        $this->configuration = $configuration;
50 1
    }
51
52
    /**
53
     * Set logger to be used for any logging that could occure in the dispatching process
54
     *
55
     * @param Logger $logger
56
     */
57 1
    public function setLogger(Logger $logger)
58
    {
59 1
        $this->logger = $logger;
60 1
    }
61
62
    /**
63
     * @param Request     $request Request object
64
     * @param Entity|null $user
65
     *
66
     * @return Response Reponse object
67
     *
68
     * @throws \Exception If unexpected exception is thrown
69
     */
70 1
    public function dispatch(Request $request, Entity $user = null): Response
71
    {
72 1
        $controllerFactory = new ControllerFactory($this->pdo, $this->configuration, $request, $user);
73
74
        /**
75
         * Figure out what controller to use and what action to call
76
         */
77
        try {
78
            /**
79
             * @var Router $router
80
             */
81 1
            $router = $this->configuration->get('Router');
82
83 1
            list($controllerName, $action, $arguments) = array_values($router->route($request->getPath()));
84
85
            /**
86
             * Validate and initialize controller
87
             */
88 1
            $controller = $controllerFactory->create($controllerName);
89
        } catch (RouteNonexistentException $e) {
90
            /**
91
             * Log nonexistent route (404)
92
             */
93
            if ($this->logger) {
94
                $this->logger->addWarning('Route not connected', [
95
                    'path' => $request->getPath()
96
                ]);
97
            }
98
99
            $controller = $controllerFactory->create(NotFoundController::class);
100
            $action     = 'index';
101
            $arguments  = [];
102
        }
103
104
        /**
105
         * Set arguments
106
         */
107 1
        $controller->setArguments($arguments);
108
109
        /**
110
         * Validate and set controller action
111
         */
112
        try {
113 1
            $controller->setAction($action);
114
        } catch (ControllerActionProtectedInsufficientAuthenticationException $e) {
115
            /**
116
             * Log unauthed protected controller action (403)
117
             */
118 View Code Duplication
            if ($this->logger) {
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...
119
                $this->logger->addWarning('Unauthenticated attempt to access protected action', [
120
                    'path'       => $request->getPath(),
121
                    'controller' => $controller->getShortName(),
122
                    'action'     => $action
123
                ]);
124
            }
125
126
            if ($this->configuration->exists('User.SignIn.Controller.Class') && $this->configuration->exists('User.SignIn.Controller.Action')) {
127
                /**
128
                 * Save what controller and action was requested and then redirect to sign in form
129
                 */
130
                // @todo test that this works
131
                $request->getCookie()->set('SignIn.onSuccess.path', $request->getPath());
132
133
                $controller = $controllerFactory->create($this->configuration->get('User.SignIn.Controller.Class'));
134
                $controller->setAction($this->configuration->get('User.SignIn.Controller.Action'));
135
            } else {
136
                $controller = $controllerFactory->create(NotFoundController::class);
137
                $controller->setAction('index');
138
            }
139
        } catch (ControllerActionPrivateInsufficientAuthenticationException $e) {
140
            /**
141
             * Log unauthed private controller action (403)
142
             */
143 View Code Duplication
            if ($this->logger) {
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...
144
                $this->logger->addWarning('Unauthenticated attempt to access private action', [
145
                    'path'       => $request->getPath(),
146
                    'controller' => $controller->getShortName(),
147
                    'action'     => $action
148
                ]);
149
            }
150
        }
151
152
        /**
153
         * Create response from controller action headers and output
154
         */
155 1
        $response = $controller->callAction();
156
157
        /**
158
         * Performance logging
159
         */
160 1
        if ($this->logger) {
161
            $time = number_format((microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000, 2); // @todo $_SERVER usage
162
163
            $this->logger->addDebug("Dispatched request in $time ms", ['path' => $request->getPath()]);
164
        }
165
166 1
        return $response;
167
    }
168
}
169