Completed
Push — master ( 463038...064ba2 )
by Igor
02:36
created

DefaultRoute::nonsense()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @license MIT
4
 * @author Igor Sorokin <[email protected]>
5
 */
6
namespace Dspbee\Core;
7
8
/**
9
 * Base routing.
10
 *
11
 * Class DefaultRoute
12
 * @package Dspbee\Core
13
 */
14
class DefaultRoute implements IRoute
15
{
16
    /**
17
     * Get object of Response.
18
     *
19
     * @param string $packageRoot
20
     * @param Request $request
21
     *
22
     * @return Response|null
23
     */
24
    public function getResponse($packageRoot, Request $request)
25
    {
26
        $packageRoot = rtrim($packageRoot, '/');
27
        $path = $packageRoot . '/Route/' . $request->route() . '/' . $request->method() . '.php';
28
        if (file_exists($path)) {
29
            require $path;
30
            $controllerClass = $this->nonsense($request->package() . '\\Route_' . str_replace('/', '_', $request->route()) . '\\' . $request->method());
31
            /**
32
             * @var BaseController $controller
33
             */
34
            $controller = new $controllerClass($packageRoot, $request);
0 ignored issues
show
Security Code Execution introduced by
$controllerClass can contain request data and is used in code execution context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Fetching key REQUEST_URI from $_SERVER, and $url is assigned
    in src/Core/Request.php on line 26
  2. $url is passed through explode(), and $url is assigned
    in src/Core/Request.php on line 34
  3. $url is assigned
    in src/Core/Request.php on line 35
  4. $url is passed through trim(), and trim($url) is passed through trim(), and $url is assigned
    in src/Core/Request.php on line 36
  5. $url is passed through explode(), and $partList is assigned
    in src/Core/Request.php on line 38
  6. $partList is passed through implode(), and Request::$route is assigned
    in src/Core/Request.php on line 67
  7. Tainted property Request::$route is read
    in src/Core/Request.php on line 146
  8. Request::route() returns tainted data, and $request->route() is passed through str_replace(), and $controllerClass is assigned
    in src/Core/DefaultRoute.php on line 30

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
35
36
            /**
37
             * Call handler.
38
             */
39
            $handler = $_POST['handler'] ?? $_GET['handler'] ?? 'index';
40
            if (preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $handler) && method_exists($controllerClass, $handler)) {
41
                $handler = $this->nonsense($handler);
42
                $controller->$handler();
0 ignored issues
show
Security Code Execution introduced by
$handler can contain request data and is used in code execution context(s) leading to a potential security vulnerability.

1 path for user data to reach this point

  1. Read from $_POST, and $handler is assigned
    in src/Core/DefaultRoute.php on line 39
  2. $handler is assigned
    in src/Core/DefaultRoute.php on line 41

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
43
                return $controller->getResponse();
44
            }
45
        }
46
47
        return null;
48
    }
49
50
    private function nonsense($val)
51
    {
52
        return $val;
53
    }
54
}