Issues (10)

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/AnnotationService.php (1 issue)

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
 * This file is part of the silex-annotation-provider package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @license       MIT License
8
 * @copyright (c) 2018, Dana Desrosiers <[email protected]>
9
 */
10
11
declare(strict_types=1);
12
13
namespace DDesrosiers\SilexAnnotations;
14
15
use DDesrosiers\SilexAnnotations\AnnotationReader\AnnotationReader;
16
use DDesrosiers\SilexAnnotations\Annotations\Controller;
17
use DDesrosiers\SilexAnnotations\Cache\AnnotationCache;
18
use Psr\SimpleCache\InvalidArgumentException;
19
use Silex\Application;
20
use Silex\ControllerCollection;
21
22
/**
23
 * Class AnnotationService parses annotations on classes and converts them to
24
 * Silex routes.
25
 *
26
 * @author Dana Desrosiers <[email protected]>
27
 */
28
class AnnotationService
29
{
30
    /** @var Application */
31
    private $app;
32
33
    /** @var ControllerFinder */
34
    private $controllerFinder;
35
36
    /** @var AnnotationReader */
37
    private $reader;
38
39
    /** @var AnnotationCache */
40
    private $cache;
41
42
    const CONTROLLER_CACHE_INDEX = 'annot.controllerFiles';
43
44
    /**
45
     * @param Application      $app
46
     * @param ControllerFinder $finder
47
     * @param AnnotationReader $reader
48
     * @param AnnotationCache  $cache
49
     */
50
    public function __construct(
51
        Application $app,
52
        ControllerFinder $finder,
53
        AnnotationReader $reader,
54
        AnnotationCache $cache
55
    ) {
56
        $this->app = $app;
57
        $this->controllerFinder = $finder;
58
        $this->cache = $cache;
59
        $this->reader = $reader;
60
    }
61
62
    /**
63
     * @throws InvalidArgumentException
64
     */
65
    public function registerControllers()
66
    {
67
        $controllers = $this->cache->fetch(self::CONTROLLER_CACHE_INDEX, function () {
68
            $controllers = [];
69
            foreach ($this->controllerFinder->getControllerClasses() as $className) {
70
                $controller = $this->getControllerAnnotation($className);
71
                if ($controller instanceof Controller) {
72
                    $controllers[$controller->getPrefix()][] = $className;
73
                }
74
            }
75
            return $controllers;
76
        });
77
78
        foreach ($controllers as $prefix => $controllerGroup) {
79
            // register the controller only if the prefix matches the request uri
80
            if (strpos($_SERVER['REQUEST_URI'], $this->app['annot.base_uri'].$prefix) === 0) {
81
                foreach ($controllerGroup as $controllerClassName) {
82
                    $this->registerController($controllerClassName);
83
                }
84
            }
85
        }
86
    }
87
88
    /**
89
     * @param string $controllerClassName
90
     * @return Controller|null
91
     * @throws InvalidArgumentException
92
     */
93
    private function getControllerAnnotation(string $controllerClassName): ?Controller
94
    {
95
        return $this->cache->fetch($controllerClassName, function () use ($controllerClassName) {
96
            return $this->reader->getControllerAnnotation($controllerClassName);
97
        });
98
    }
99
100
    /**
101
     * Register the controller using the controller definition parsed from annotation.
102
     *
103
     * @param string $controllerClassName
104
     * @throws InvalidArgumentException
105
     */
106
    private function registerController(string $controllerClassName)
107
    {
108
        $controllerAnnotation = $this->getControllerAnnotation($controllerClassName);
109
110
        if ($controllerAnnotation instanceof Controller) {
111
            $controllerName = trim($controllerClassName, "\\");
112
            $this->app["$controllerName"] = function (Application $app) use ($controllerName) {
113
                return new $controllerName($app);
114
            };
115
116
            $this->processClassAnnotation($controllerAnnotation);
117
        }
118
    }
119
120
    /**
121
     * @param Controller $controllerAnnotation
122
     */
123
    private function processClassAnnotation(Controller $controllerAnnotation)
124
    {
125
        /** @var ControllerCollection $controllerCollection */
126
        $controllerCollection = $this->app['controllers_factory'];
127
128
        foreach ($controllerAnnotation->getModifiers() as $name => $values) {
129
            foreach ($values as $value) {
130
                $controllerCollection->$name(...$value);
131
            }
132
        }
133
134
        foreach ($controllerAnnotation->getRoutes() as $route) {
135
            $controller = $controllerCollection->match($route->getUri(), $route->getControllerName());
136
            if (($method = strtoupper($route->getMethod())) != 'MATCH') {
137
                $controller->method($method);
138
            }
139
            foreach ($route->getModifiers() as $name => $values) {
140
                foreach ($values as $value) {
141
                    $controller->$name(...$value);
142
                }
143
            }
144
        }
145
146
        $this->app->mount($controllerAnnotation->getPrefix(), $controllerCollection);
0 ignored issues
show
$controllerCollection is of type object<Silex\ControllerCollection>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
147
    }
148
}
149