Issues (21)

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/Routing/Router.php (2 issues)

Severity

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 JetFire\Routing;
4
5
/**
6
 * Class Router
7
 * @package JetFire\Routing
8
 */
9
class Router
10
{
11
12
    /**
13
     * @var Route
14
     */
15
    public $route;
16
    /**
17
     * @var RouteCollection
18
     */
19
    public $collection;
20
    /**
21
     * @var ResponseInterface
22
     */
23
    public $response;
24
    /**
25
     * @var array
26
     */
27
    public $middlewareCollection = [];
28
    /**
29
     * @var array
30
     */
31
    public $matcher = [];
32
    /**
33
     * @var
34
     */
35
    public $dispatcher;
36
    /**
37
     * @var array
38
     */
39
    private $config = [
40
        'templateExtension' => ['.html', '.php', '.json', '.xml'],
41
        'templateCallback' => [],
42
        'di' => '',
43
        'generateRoutesPath' => false,
44
    ];
45
46
    /**
47
     * @param RouteCollection $collection
48
     * @param ResponseInterface $response
49
     * @param Route $route
50
     */
51
    public function __construct(RouteCollection $collection, ResponseInterface $response = null, Route $route = null)
52
    {
53
        $this->collection = $collection;
54
        $this->response = is_null($response) ? new Response() : $response;
55
        $this->route = is_null($route) ? new Route() : $route;
56
        $this->config['di'] = function ($class) {
57
            return new $class;
58
        };
59
    }
60
61
    /**
62
     * @param array $config
63
     */
64
    public function setConfig($config)
65
    {
66
        $this->config = array_merge($this->config, $config);
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function getConfig()
73
    {
74
        return $this->config;
75
    }
76
77
    /**
78
     * @param object|array $middleware
79
     */
80
    public function setMiddleware($middleware)
81
    {
82
        $this->middlewareCollection = is_array($middleware)
83
            ? $middleware
84
            : [$middleware];
85
    }
86
87
    /**
88
     * @param MiddlewareInterface $middleware
89
     */
90
    public function addMiddleware(MiddlewareInterface $middleware)
91
    {
92
        $this->middlewareCollection[] = $middleware;
93
    }
94
95
    /**
96
     * @param object|array $matcher
97
     */
98
    public function setMatcher($matcher)
99
    {
100
        $this->matcher = is_array($matcher)
101
            ? $matcher
102
            : [$matcher];
103
    }
104
105
    /**
106
     * @param string $matcher
107
     */
108
    public function addMatcher($matcher)
109
    {
110
        $this->matcher[] = $matcher;
111
    }
112
113
    /**
114
     * @description main function
115
     */
116
    public function run()
117
    {
118
        $this->setUrl();
119
        if ($this->config['generateRoutesPath']) $this->collection->generateRoutesPath();
120
        if ($this->match() === true) {
121
            $this->callMiddleware('before');
122
            if (!in_array(substr($this->response->getStatusCode(), 0, 1), [3,4,5])) {
123
                $this->callTarget();
124
            }
125
        }else{
126
            $this->response->setStatusCode(404);
127
        }
128
        $this->callMiddleware('after');
129
        return $this->response->send();
130
    }
131
132
    /**
133
     * @description call the middleware before and after the target
134
     * @param $action
135
     */
136
    public function callMiddleware($action)
137
    {
138
        foreach ($this->middlewareCollection as $middleware) {
139
            if ($middleware instanceof MiddlewareInterface) {
140
                foreach ($middleware->getCallbacks($action) as $callback) {
141
                    if (method_exists($middleware, $callback)) {
142
                        call_user_func_array([$middleware, $callback], [$action]);
143
                    }
144
                }
145
            }
146
        }
147
    }
148
149
    /**
150
     * @param null $url
151
     */
152
    public function setUrl($url = null)
0 ignored issues
show
setUrl uses the super-global variable $_GET 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...
setUrl 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...
153
    {
154
        if (is_null($url))
155
            $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1);
156
        $this->route->setUrl('/' . trim(explode('?', $url)[0], '/'));
157
    }
158
159
    /**
160
     * @return bool
161
     */
162
    public function match()
163
    {
164
        foreach ($this->matcher as $key => $matcher) {
165
            if (call_user_func([$this->matcher[$key], 'match'])) return true;
166
        }
167
        return false;
168
    }
169
170
    /**
171
     * @description call the target for the request uri
172
     */
173
    public function callTarget()
174
    {
175
        $target = is_array($this->route->getTarget('dispatcher')) ? $this->route->getTarget('dispatcher') : [$this->route->getTarget('dispatcher')];
176
        if (!empty($target)) {
177
            foreach ($target as $call) {
178
                $this->dispatcher = new $call($this);
179
                call_user_func([$this->dispatcher, 'call']);
180
            }
181
        }
182
    }
183
}
184