Issues (3099)

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.

AdminBundle/EventListener/ToolbarListener.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
namespace Kunstmaan\AdminBundle\EventListener;
4
5
use Kunstmaan\AdminBundle\Helper\AdminRouteHelper;
6
use Kunstmaan\AdminBundle\Helper\Toolbar\DataCollector;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
12
use Symfony\Component\HttpKernel\Event\ResponseEvent;
13
use Symfony\Component\HttpKernel\HttpKernel;
14
use Symfony\Component\HttpKernel\KernelEvents;
15
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
17
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
18
use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
19
use Twig\Environment;
20
21
class ToolbarListener implements EventSubscriberInterface
22
{
23
    const DISABLED = 1;
24
25
    const ENABLED = 2;
26
27
    /**
28
     * @var Environment
29
     */
30
    protected $twig;
31
32
    /**
33
     * @var UrlGeneratorInterface
34
     */
35
    protected $urlGenerator;
36
37
    /**
38
     * @var DataCollector
39
     */
40
    protected $dataCollector;
41
42
    /**
43
     * @var AuthorizationChecker
44
     */
45
    protected $authorizationChecker;
46
47
    /**
48
     * @var TokenStorageInterface
49
     */
50
    protected $tokenStorage;
51
52
    /**
53
     * @var bool
54
     */
55
    protected $enabled;
56
57
    /**
58
     * @var ContainerInterface
59
     */
60
    private $container;
61
62
    /**
63
     * @var AdminRouteHelper
64
     */
65
    protected $adminRouteHelper;
66
67
    /**
68
     * @var array
69
     */
70
    protected $providerKeys;
71
72
    /**
73
     * @var array
74
     */
75
    protected $adminFirewallName;
76
77
    /**
78
     * ToolbarListener constructor.
79
     *
80
     * @param Environment           $twig
81
     * @param UrlGeneratorInterface $urlGenerator
82
     * @param DataCollector         $dataCollector
83
     * @param AuthorizationChecker  $authorizationChecker
84
     * @param TokenStorageInterface $tokenStorage
85
     * @param bool                  $enabled
86
     * @param ContainerInterface    $container
87
     * @param AdminRouteHelper      $adminRouteHelper
88
     * @param array                 $providerKeys
89
     * @param string                $adminFirewallName
90
     */
91
    public function __construct(
92
        Environment $twig,
93
        UrlGeneratorInterface $urlGenerator,
94
        DataCollector $dataCollector,
95
        AuthorizationChecker $authorizationChecker,
96
        TokenStorageInterface $tokenStorage,
97
        $enabled,
98
        ContainerInterface $container,
99
        AdminRouteHelper $adminRouteHelper,
100
        array $providerKeys,
101
        $adminFirewallName = 'main'
102
    ) {
103
        $this->twig = $twig;
104
        $this->urlGenerator = $urlGenerator;
105
        $this->dataCollector = $dataCollector;
106
        $this->authorizationChecker = $authorizationChecker;
107
        $this->tokenStorage = $tokenStorage;
108
        $this->enabled = $enabled;
109
        $this->container = $container;
110
        $this->adminRouteHelper = $adminRouteHelper;
111
        $this->providerKeys = $providerKeys;
112
        $this->adminFirewallName = $adminFirewallName;
0 ignored issues
show
Documentation Bug introduced by
It seems like $adminFirewallName of type string is incompatible with the declared type array of property $adminFirewallName.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
113
    }
114
115
    /**
116
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<*,array<string|integer>>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
117
     */
118 3
    public static function getSubscribedEvents()
119
    {
120
        return [
121 3
            KernelEvents::RESPONSE => ['onKernelResponse', -125],
122
        ];
123
    }
124
125
    /**
126
     * @return bool
127
     */
128
    public function isEnabled()
129
    {
130
        return !$this->container->has('profiler') && $this->enabled;
131
    }
132
133
    /**
134
     * @param FilterResponseEvent|ResponseEvent $event
135
     */
136
    public function onKernelResponse($event)
137
    {
138 View Code Duplication
        if (!$event instanceof FilterResponseEvent && !$event instanceof ResponseEvent) {
139
            throw new \InvalidArgumentException(\sprintf('Expected instance of type %s, %s given', \class_exists(ResponseEvent::class) ? ResponseEvent::class : FilterResponseEvent::class, \is_object($event) ? \get_class($event) : \gettype($event)));
140
        }
141
142
        if (!$this->isEnabled() || HttpKernel::MASTER_REQUEST !== $event->getRequestType()) {
143
            return;
144
        }
145
146
        $response = $event->getResponse();
147
        $request = $event->getRequest();
148
        $session = $request->getSession();
149
        $url = $event->getRequest()->getRequestUri();
150
        $token = $this->tokenStorage->getToken();
151
152
        if (null !== $token && method_exists($token, 'getProviderKey')) {
153
            $key = $token->getProviderKey();
154
        } else {
155
            $key = $this->adminFirewallName;
156
        }
157
158
        // Only enable toolbar when the kunstmaan_admin.toolbar_firewall_names config value contains the current firewall name.
159
        if (!\in_array($key, $this->providerKeys, false)) {
160
            return false;
161
        }
162
163
        // Only enable toolbar when we can find an authenticated user in the session from the kunstmaan_admin.admin_firewall_name config value.
164
        $authenticated = false;
165
        /* @var PostAuthenticationGuardToken $token */
166
        if ($session->isStarted() && $session->has(sprintf('_security_%s', $this->adminFirewallName))) {
167
            $token = unserialize($session->get(sprintf('_security_%s', $this->adminFirewallName)));
168
            $authenticated = $token->isAuthenticated();
169
        }
170
171
        // Do not capture redirects or modify XML HTTP Requests
172
        if (!$authenticated || !$event->isMasterRequest() || $request->isXmlHttpRequest() || $this->adminRouteHelper->isAdminRoute($url)) {
173
            return;
174
        }
175
176 View Code Duplication
        if ($response->isRedirection() || ($response->headers->has('Content-Type') && false === strpos(
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...
177
                    $response->headers->get('Content-Type'),
178
                    'html'
179
                ))
180
            || 'html' !== $request->getRequestFormat()
181
            || false !== stripos($response->headers->get('Content-Disposition'), 'attachment;')
182
        ) {
183
            return;
184
        }
185
186
        $this->injectToolbar($response, $request);
187
    }
188
189
    /**
190
     * Injects the admin toolbar into the given Response.
191
     *
192
     * @param Response $response A Response instance
193
     */
194
    protected function injectToolbar(Response $response, Request $request)
195
    {
196
        $content = $response->getContent();
197
        $pos = strripos($content, '</body>');
198
199
        if (false !== $pos) {
200
            $toolbar = "\n".str_replace(
201
                    "\n",
202
                    '',
203
                    $this->twig->render(
204
                        '@KunstmaanAdmin/Toolbar/toolbar.html.twig',
205
                        ['collectors' => $this->dataCollector->getDataCollectors()]
206
                    )
207
                )."\n";
208
            $content = substr($content, 0, $pos).$toolbar.substr($content, $pos);
209
            $response->setContent($content);
210
        }
211
    }
212
}
213